Проблема с DataLoader или моделью? ⇐ Python

Программы на Python
Anonymous
Проблема с DataLoader или моделью?

Сообщение Anonymous »

Проблема связана с DataLoader или с моделью? Обычно, если входные размеры различаются, как их можно преобразовать к фиксированному размеру? Если мы хотим передать результаты GAT в CNN, какой подход лучше всего подойдет?

Буду очень признателен за вашу помощь.

Полный код очень длинный. Я постарался включить важные части, чтобы упростить задачу.
Модель:

Код: Выделить всё

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import GATConv
from torch_geometric.data import Dataset,Data
from torch_geometric.loader import DataLoader

class GAT(nn.Module):
def __init__(self, in_channels, hidden_channels, out_channels, heads):
super(GAT, self).__init__()
self.conv1 = GATConv(in_channels, hidden_channels, heads=heads)
# ... code truncated for brevity : conv2,3,4,5,6 ...
self.conv7 = GATConv(hidden_channels * heads, out_channels, heads=1, concat=False)
self.bn = nn.BatchNorm1d(hidden_channels * heads)
self.dropout = nn.Dropout(0.1)
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = self.bn(x)
x = F.relu(x)
x = self.dropout(x)
# ... code truncated for brevity : conv2,3,4,5,6 ...
x = self.conv7(x, edge_index)
return x

class MODELGAT(nn.Module):
def __init__(self):
super(DTI, self).__init__()
self.gat = GAT(5, 16, 32, heads=16)
def forward(self, D1, D2, Distances1, Distances2, Angles1, Angles2):
# Distances1, Distances2, Angles1, Angles2: I will use these later in the model
print(D1) # DataBatch(x=[214, 5], edge_index=[2, 228], batch=[214], ptr=[6])
print(D2) # DataBatch(x=[110, 5], edge_index=[2, 115], batch=[110], ptr=[6])
x1 = self.gat(D1)
x2 = self.gat(D2)
print(x1.shape) # torch.Size([214, 32])
print(x2.shape) # torch.Size([110, 32])

x1 = x1.unsqueeze(0).unsqueeze(0)
x1 = F.interpolate(x1, size=(x2.size(0), 32), mode='bicubic', align_corners=False)
x1 = x1.squeeze(0)
x2 = x2.unsqueeze(0)
combined = x1 + x2
combined = combined.unsqueeze(0)

x = self.cnn(combined)
x = torch.sigmoid(x)
x = x.squeeze(0).squeeze(0)

return x # Problem
Разве мы не считали batch_size равным 5! Почему входные данные такие?

Код: Выделить всё

DataBatch(x=[214, 5], edge_index=[2, 228], batch=[214], ptr=[6])
В конечном итоге он должен выдать 5 выходных данных для тестирования с помощью L. Но почему выходные данные torch.Size([214, 32])?
Обычно, когда входные данные имеют разные размеры, результатом являются выходные данные типа torch.Size([214 , 32]) и torch.Size([110, 32]), как мне преобразовать их до одинакового размера перед подачей в CNN? Правилен ли метод, который я использую?
DataLoader:

Код: Выделить всё

def DATA(D):
# ...  code truncated for brevity : OUT DD1 & DD2 & L...
L = torch.tensor(L, dtype=torch.float).squeeze(0) # 0 OR 1
D1 = DD1[0] # graph_data -> Data(x=[59, 5], edge_index=[2, 64])
Distances1 = torch.tensor(DD1[1], dtype=torch.float32) # torch.Size([57])
Angles1 = torch.tensor(DD1[2], dtype=torch.float32) # torch.Size([57])
D2 = DD2[0] # graph_data -> Data(x=[22, 5], edge_index=[2, 23])
Distances2 = torch.tensor(DD2[1], dtype=torch.float32) # torch.Size([20])
Angles2 = torch.tensor(DD2[2], dtype=torch.float32) # torch.Size([20])
data = Data(
Distances1=Distances1,
Distances2=Distances2,
Angles1=Angles1,
Angles2=Angles2,
)
return D1, D2, data, L

def TRAIN(D):
inputs = []
targets = []
for train_list in D:
D1, D2, data, l = DATA(train_list)
inputs.append((D1, D2, data))
targets.append(l)
return inputs, targets

class GATDataset(Dataset):
def __init__(self, input, target):
self.input = input
self.target = target
def __len__(self):
return len(self.input)
def __getitem__(self, idx):
return self.input[idx], self.target[idx]

input, target = TRAIN(LIST) # List of data
dataset = GATDataset(input, target)
Train_Loader = DataLoader(dataset, batch_size=5)
Правильно ли написан код DataLoader? В итоге D1, D2, Расстояния1, Расстояния2, Углы1, < strong>Углы2 должны быть переданы в сеть.
Обычно в случаях, когда данные имеют разные размеры, как их следует преобразовать к одному и тому же размеру без потери важные функции или удаление частей данных?
Обучить:

Код: Выделить всё

torch.manual_seed(123)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = MODELGAT().to(device)
criterion = nn.BCEWithLogitsLoss()
optimizer = torch.optim.AdamW(model.parameters(), lr=0.00005, weight_decay=0.0001)

for Batch, L in Train_Loader:
model.train()
optimizer.zero_grad()
Distances1 = Batch[2].Distances1.to(device)
Distances2 = Batch[2].Distances2.to(device)
Angles1 = Batch[2].Angles1.to(device)
Angles2 = Batch[2].Angles2.to(device)
output = model(Batch[0], Batch[1], Distances1, Distances2, Angles1, Angles2)
output = output.float()
target = L.float()
train_loss = criterion(output, target)
train_loss.backward()
optimizer.step()
print(train_loss)
Почему результат вывода = model(Batch[0], Batch[1], Distances1, Distances2, Angles1, Angles2) представляет собой одно число?
output.shape -> torch.Size([]),
target.shape -> torch.Size ([5])

Я ценю ваши идеи и советы.


Подробнее здесь: https://stackoverflow.com/questions/790 ... -the-model

Вернуться в «Python»