英文:
Pytorch deeplearning
问题
在代码的以下部分出现了错误:
correct += (predicted == labels).sum().item()
错误信息提示了问题:张量 a 的大小(19)必须与张量 b 的大小(64)在非单例维度 1 处匹配。这意味着你的模型输出(predicted)的大小在维度 1 上是 19,而标签(labels)的大小在相同的维度上是 64。这是导致错误的原因。
问题可能出现在以下几个地方:
-
模型的输出层大小:检查你的模型输出层(
self.fc2
)的大小是否正确。根据你的目标数据,输出层应该有 64 个神经元。 -
损失函数:你在使用损失函数时,
nn.CrossEntropyLoss()
应该用于多类别分类问题,而不是回归问题。确保你的问题是分类问题,如果是回归问题,使用适当的损失函数。 -
标签的格式:确保标签(labels)的格式正确。标签应该是整数形式,而不是浮点数。你可以使用
labels.long()
来将标签转换为整数类型。 -
输出的处理:你在处理模型的输出时,可以尝试添加 softmax 操作,以确保输出是类别概率。例如,可以使用
outputs = F.softmax(model(inputs), dim=1)
来获得概率分布。
检查并确保上述问题没有出现,应该能够解决这个错误。如果问题仍然存在,可以提供更多关于数据和模型结构的信息,以便更详细地排查问题。
英文:
I am trying to train a model with the data of the size: torch.Size([280652, 87]) and the target: torch.Size([280652, 64]) with 80% in the training data and 20 in the test data.
My code:
#split the data in train and test
X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.2)
# convert to torch tensors
train = torch.tensor(X_train.values, dtype=torch.float32)
test = torch.tensor(X_test.values, dtype=torch.float32)
train_target = torch.tensor(y_train.values, dtype=torch.float32)
test_target = torch.tensor(y_test.values, dtype=torch.float32)
# inizializing and forward propagation
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(87, 50)# layer 1
self.fc2 = nn.Linear(50, 64)# layer 2
self.relu = nn.ReLU()# aktivation method
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x
#print(shapes)
print(train.shape)
print(train_target.shape)
print(test.shape)
print(test_target.shape)
#trainings datasets
train_dataset = TensorDataset(train, train_target)
train_dataloader = DataLoader(train_dataset, batch_size=64, shuffle=True)
#train_dataloader = DataLoader(train_dataset, batch_size=len(train_dataset), shuffle=False)
test_dataset = TensorDataset(test, test_target)
test_dataloader = DataLoader(test_dataset, batch_size=64, shuffle=False)
#test_dataloader = DataLoader(test_dataset, batch_size=len(test_dataset), shuffle=False)
model = MyModel()
#opimizer (ajust weights) for large amounts
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
print(train.shape)
print(train_target.shape)
print(test.shape)
print(test_target.shape)
#train = F.one_hot(train_target.to(torch.int64))
# Train the model
for epoch in range(10):
for i, (inputs, labels) in enumerate(train_dataloader):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels.float())
loss.backward()
optimizer.step()
# Print the loss every 1000 iterations
if i % 1000 == 0:
print(train.shape)
print(train_target.shape)
print(test.shape)
print(test_target.shape)
print(f"Epoch {epoch+1}, Iteration {i+1}, Loss {loss.item():.4f}")
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in test_dataloader:
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
predicted = torch.argmax(outputs, dim=1)
#correct += (torch.argmax(predicted, dim=1) == labels).sum().item()
#print(len(labels))
#print(len(predicted))
#print(predicted)
#print(labels)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
print(f"Test Accuracy: {accuracy:.2f}%")
the error accures in the line correct += (predicted == labels).sum().item() with the error: The size of tensor a (19) must match the size of tensor b (64) at non-singleton dimension 1
I have no idea where the 19 comes from and i thought that the batch size shouldnt matter.
Did i forget something or is there a mager error in my code?
I tryed adapting the batch size and the layer sizes and tried some diffrent methods but nothing seems to work right.
答案1
得分: 0
你已经两次应用了argmax
函数。
请记住,因为我认为它们是独热编码的,所以你还必须对标签应用argmax
。
我尝试过这个,它有效。我没有你的数据,所以我创建了一些虚拟数据:
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.model_selection import train_test_split
import numpy as np
from torch.utils.data import TensorDataset, DataLoader
from sklearn.preprocessing import OneHotEncoder
#虚拟数据
data = np.random.randn(10000, 87)
target = np.random.randint(0, 64, (10000, 1))
encoder = OneHotEncoder(sparse=False)
target = encoder.fit_transform(target)
#这部分基本上与你的代码相同
#将数据拆分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.2)
# 转换为torch张量
train = torch.tensor(X_train, dtype=torch.float32)
test = torch.tensor(X_test, dtype=torch.float32)
train_target = torch.tensor(y_train, dtype=torch.float32)
test_target = torch.tensor(y_test, dtype=torch.float32)
#初始化和前向传播
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(87, 50) # 第一层
self.fc2 = nn.Linear(50, 64) # 第二层
self.relu = nn.ReLU() # 激活函数
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x
#打印形状
print(train.shape)
print(train_target.shape)
print(test.shape)
print(test_target.shape)
#训练数据集
train_dataset = TensorDataset(train, train_target)
train_dataloader = DataLoader(train_dataset, batch_size=64, shuffle=True)
#测试数据集
test_dataset = TensorDataset(test, test_target)
test_dataloader = DataLoader(test_dataset, batch_size=64, shuffle=False)
#创建模型
model = MyModel()
#优化器(调整权重)用于大规模数据
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
#打印形状
print(train.shape)
print(train_target.shape)
print(test.shape)
print(test_target.shape)
#训练模型
for epoch in range(10):
for i, (inputs, labels) in enumerate(train_dataloader):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels.float())
loss.backward()
optimizer.step()
#每1000次迭代打印损失
if i % 1000 == 0:
print(train.shape)
print(train_target.shape)
print(test.shape)
print(test_target.shape)
print(f"Epoch {epoch + 1}, Iteration {i + 1}, Loss {loss.item():.4f}")
#现在是更改的部分
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in test_dataloader:
outputs = model(inputs)
#print(outputs.shape)
_, predicted = torch.max(outputs.data, 1)
print(predicted.shape)
total += labels.size(0)
labels = torch.argmax(labels, dim=1)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
print(f"Test Accuracy: {accuracy:.2f}%")
英文:
You have applied the argmax
function twice.
Keep in mind, you also have to do apply an argmax
for labels because I believe they are one-hot encoded.
I tried this and worked. I don't have your data so I created some dummy data:
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.model_selection import train_test_split
import numpy as np
from torch.utils.data import TensorDataset, DataLoader
from sklearn.preprocessing import OneHotEncoder
#dummy data
data = np.random.randn(10000,87)
target = np.random.randint(0, 64, (10000,1))
encoder = OneHotEncoder(sparse=False)
target = encoder.fit_transform(target)
#this part is basically identical to yours
#split the data in train and test
X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.2)
# convert to torch tensors
train = torch.tensor(X_train, dtype=torch.float32)
test = torch.tensor(X_test, dtype=torch.float32)
train_target = torch.tensor(y_train, dtype=torch.float32)
test_target = torch.tensor(y_test, dtype=torch.float32)
# inizializing and forward propagation
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc1 = nn.Linear(87, 50)# layer 1
self.fc2 = nn.Linear(50, 64)# layer 2
self.relu = nn.ReLU()# aktivation method
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.fc2(x)
return x
#print(shapes)
print(train.shape)
print(train_target.shape)
print(test.shape)
print(test_target.shape)
#trainings datasets
train_dataset = TensorDataset(train, train_target)
train_dataloader = DataLoader(train_dataset, batch_size=64, shuffle=True)
#train_dataloader = DataLoader(train_dataset, batch_size=len(train_dataset), shuffle=False)
test_dataset = TensorDataset(test, test_target)
test_dataloader = DataLoader(test_dataset, batch_size=64, shuffle=False)
#test_dataloader = DataLoader(test_dataset, batch_size=len(test_dataset), shuffle=False)
model = MyModel()
#opimizer (ajust weights) for large amounts
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
print(train.shape)
print(train_target.shape)
print(test.shape)
print(test_target.shape)
# Train the model
for epoch in range(10):
for i, (inputs, labels) in enumerate(train_dataloader):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels.float())
loss.backward()
optimizer.step()
# Print the loss every 1000 iterations
if i % 1000 == 0:
print(train.shape)
print(train_target.shape)
print(test.shape)
print(test_target.shape)
print(f"Epoch {epoch+1}, Iteration {i+1}, Loss {loss.item():.4f}")
#now here is the change
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in test_dataloader:
outputs = model(inputs)
#print(outputs.shape)
_, predicted = torch.max(outputs.data, 1)
print(predicted.shape)
total += labels.size(0)
#predicted = torch.argmax(outputs, dim=1)
#correct += (torch.argmax(predicted, dim=1) == labels).sum().item()
#print(len(labels))
#print(len(predicted))
#print(predicted)
#print(labels)
labels = torch.argmax(labels, dim=1)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
print(f"Test Accuracy: {accuracy:.2f}%")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论