【发布时间】:2019-12-05 00:59:18
【问题描述】:
我创建了一个线性 ReLu 网络,它应该过度拟合我的数据。我使用 BCEWithLogisticLoss 作为损失函数。我用它来分类 3d 点。由于数据足够小,我不在乎批量生产。它工作得很好。然而,现在我已经在其中实现了批次,似乎预测值不是我所期望的(即 0 或 1),而是它给了我像 -25.4562 这样的数字,我没有从网络中改变任何其他东西,只有批次。
我尝试了二进制损失函数 BSELoss 但是它似乎是 pytorch 版本中的一个错误,所以我不能使用它。您可以在下面查看我的代码:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# We load the training data
Samples, Ocupancy = common.load_samples()
for i in range(0,Ocupancy.shape[0]):
if Ocupancy[i] > 1 or Ocupancy[i] < 0:
print("upsie")
max = np.amax(Samples)
min = np.amin(Samples)
x_test = torch.from_numpy(Samples.astype(np.float32)).to(device)
y_test = torch.from_numpy(Ocupancy.astype(np.float32)).to(device)
train_data = CustomDataset(x_test, y_test)
train_loader = DataLoader(dataset=train_data, batch_size= 22500, shuffle=False) # Batches_size equal to the number of points in each slice
phi = common.MLP(3, 1).to(device)
criterion = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(phi.parameters(), lr = 0.01)
epoch = 10
fit_start_time = time.time()
for epoch in range(epoch):
for x_batch, y_batch in train_loader:
#optimizer.zero_grad()
x_train = x_batch.to(device)
y_train = y_batch.to(device)
y_pred = phi(x_batch)
print(y_pred)
# Compute Loss
loss = criterion(y_pred.squeeze(), y_batch.squeeze())
print('Epoch {}: train loss: {}'.format(epoch, loss.item())) # Backward pass
loss.backward()
optimizer.step()
fit_end_time = time.time()
print("Total time = %f" % (fit_end_time - fit_start_time))
min = -2
max = 2
resolution = 0.05
X,Y,Z = np.mgrid[min:max:resolution,min:max:resolution,min:max:resolution] # sample way more
xyz = torch.from_numpy(np.vstack([X.ravel(), Y.ravel(),Z.ravel()]).transpose().astype(np.float32)).to(device)
eval = LabelData(xyz)
eval_loader = DataLoader(dataset=eval, batch_size= 22500, shuffle=False) # Make bigger batches
# feed the network bit by bit?
i = 0
for x_batch in eval_loader:
phi.eval()
labels = phi(x_batch).to(device)
print(labels)
visualization_iso(X,Y,Z,labels)
我希望预测值是 0 或 1,或者至少是概率,但是它给了我不理解的大数字。喜欢:19.5953 请查看我的代码,如果您发现任何重大错误,请告诉我。我真的很困惑,因为在我扩展我使用的数据的大小之前它工作得很好。
问候
【问题讨论】:
标签: python neural-network pytorch