gpt4 book ai didi

python - Pytorch 训练和评估不同的样本量

转载 作者:行者123 更新时间:2023-11-28 18:56:37 26 4
gpt4 key购买 nike

我正在学习 pytorch,并且有以下(缩写的)代码来设置建模:

# define the model class for a neural net with 1 hidden layer
class myNN(nn.Module):
def __init__(self, D_in, H, D_out):
super(myNN, self).__init__()
self.lin1 = nn.Linear(D_in,H)
self.lin2 = nn.Linear(H,D_out)
def forward(self,X):
return torch.sigmoid(self.lin2(torch.sigmoid(self.lin1(x))))

# now make the datasets & dataloaders
batchSize = 5
# Create the data class
class Data(Dataset):
def __init__(self, x, y):
self.x = torch.FloatTensor(x)
self.y = torch.Tensor(y.astype(int))
self.len = self.x.shape[0]
self.p = self.x.shape[1]
def __getitem__(self, index):
return self.x[index], self.y[index]
def __len__(self):
return self.len
trainData = Data(trnX, trnY)
trainLoad = DataLoader(dataset = trainData, batch_size = batchSize)
testData = Data(tstX, tstY)
testLoad = DataLoader(dataset = testData, batch_size = len(testData))

# define the modeling objects
hiddenLayers = 30
learningRate = 0.1
model = myNN(p,hiddenLayers,1)
print(model)
optimizer = torch.optim.SGD(model.parameters(), lr = learningRate)
loss = nn.BCELoss()

trnX.shape=(70, 2), trnY.shape=(70,), tstX.shape=(30,2)tstY.shape=(30,)。训练代码为:

# train!
epochs = 1000
talkFreq = 0.2
trnLoss = [np.inf]*epochs
tstLoss = [np.inf]*epochs
for i in range(epochs):
# train with minibatch gradient descent
for x, y in trainLoad:
# forward step
yhat = model(x)
# compute loss (not storing for now, will do after minibatching)
l = loss(yhat, y)
# backward step
optimizer.zero_grad()
l.backward()
optimizer.step()
# evaluate loss on training set
yhat = model(trainData.x)
trnLoss[i] = loss(yhat, trainData.y)
# evaluate loss on testing set
yhat = model(testData.x)
tstLoss[i] = loss(yhat, testData.y)

数据集 trainDatatestData 分别有 70 和 30 个观察值。这可能只是一个新手问题,但是当我运行训练单元时,它在 trnLoss[i] = loss(yhat, trainData.y) 行出错

ValueError: Target and input must have the same number of elements. target nelement (70) != input nelement (5)

当我检查 yhat=model(trainData.x) 行的输出时,我看到 yhat 是一个 batchSize 的张量元素,尽管 trainData.x.shape = torch.Size([70, 2])

如何使用小批量梯度下降迭代训练模型,然后使用该模型计算完整训练集和测试集的损失和准确度?我尝试在小批量迭代之前设置 model.train(),然后在评估代码之前设置 model.eval(),但无济于事。

最佳答案

myNN.forward() 中,您将小写字母 x 作为输入传递给 self.lin1,而函数的输入参数是命名为大写字母 X。小写 x 是一种在 for 循环中为 trainload 定义的全局变量,因此您不会收到任何语法错误,但您打算传递的值不会传递给self.lin1.

我可能还建议您考虑使用 model.eval()with torch.no_grad() 作为您的测试代码。它在这里不是绝对必要的,但它会更有意义。

关于python - Pytorch 训练和评估不同的样本量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57490945/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com