import torch import os import torch.nn as nn import torch.optim as optim import numpy as np import random class Net(nn.Module): def init(self): super(Net, self).init() self.conv1 = nn.Conv2d(1, 16, kernel_size=3,stride=1) self.pool = nn.MaxPool2d(kernel_size=2,stride=2) self.conv2 = nn.Conv2d(16, 32, kernel_size=3,stride=1) self.fc1 = nn.Linear(32 * 9 * 9, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 2) def forward(self, x): x = self.pool(nn.functional.relu(self.conv1(x))) x = self.pool(nn.functional.relu(self.conv2(x))) x = x.view(-1, 32 * 9 * 9) x = nn.functional.relu(self.fc1(x)) x = nn.functional.relu(self.fc2(x)) x = self.fc3(x) return x net = Net() criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) folder_path = 'random_matrices2' # 创建空的tensor x = torch.empty((40, 1, 42, 42)) # 遍历文件夹内的文件,将每个矩阵转化为tensor并存储 for j in range(40): for j in range(40): file_name = 'matrix_{}.npy'.format(j) file_path = os.path.join(folder_path, file_name) matrix = np.load(file_path) x[j] = torch.from_numpy(matrix).unsqueeze(0) #y = torch.cat((torch.zeros(20), torch.ones(20))) #y = torch.cat((torch.zeros(20, dtype=torch.long), torch.ones(20, dtype=torch.long))) y = torch.cat((torch.zeros(20, dtype=torch.long), torch.ones(20, dtype=torch.long)), dim=0) for epoch in range(10): running_loss = 0.0 for i in range(40): inputs = x[i] labels = y[i].unsqueeze(0) labels = labels.long() optimizer.zero_grad() outputs = net(inputs) #loss = criterion(outputs, labels) loss = criterion(outputs.unsqueeze(0), labels.float()) loss.backward() optimizer.step() running_loss += loss.item() print('[%d] loss: %.3f' % (epoch + 1, running_loss / 40)) print('Finished Training') 报错:RuntimeError: Expected target size [1, 2], got [1],怎么修改?
时间: 2023-11-29 08:08:00 浏览: 64
torch-1.2.0-cp36-cp36m-win-amd64.whl
5星 · 资源好评率100%
这个错误是因为标签labels的形状不正确。在你的代码中,你使用了`labels = y[i].unsqueeze(0)`来定义标签,但这会将标签的形状从`(20,)`转换为`(1,20)`。因此,你需要使用`labels = y[i]`来定义标签,这样标签的形状将是`(1,)`,然后在计算损失时,使用`loss = criterion(outputs.unsqueeze(0), labels)`即可。修改后的代码如下:
```
import torch
import os
import torch.nn as nn
import torch.optim as optim
import numpy as np
import random
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3,stride=1)
self.pool = nn.MaxPool2d(kernel_size=2,stride=2)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3,stride=1)
self.fc1 = nn.Linear(32 * 9 * 9, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 2)
def forward(self, x):
x = self.pool(nn.functional.relu(self.conv1(x)))
x = self.pool(nn.functional.relu(self.conv2(x)))
x = x.view(-1, 32 * 9 * 9)
x = nn.functional.relu(self.fc1(x))
x = nn.functional.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
folder_path = 'random_matrices2'
# 创建空的tensor
x = torch.empty((40, 1, 42, 42))
# 遍历文件夹内的文件,将每个矩阵转化为tensor并存储
for j in range(40):
file_name = 'matrix_{}.npy'.format(j)
file_path = os.path.join(folder_path, file_name)
matrix = np.load(file_path)
x[j] = torch.from_numpy(matrix).unsqueeze(0)
y = torch.cat((torch.zeros(20, dtype=torch.long), torch.ones(20, dtype=torch.long)), dim=0)
for epoch in range(10):
running_loss = 0.0
for i in range(40):
inputs = x[i]
labels = y[i]
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs.unsqueeze(0), labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print('[%d] loss: %.3f' % (epoch + 1, running_loss / 40))
print('Finished Training')
```
希望能够帮助到你。
阅读全文