Pytorch深度学习



我正在尝试用大小为:torch的数据训练一个模型。尺寸([280652,87])和目标:火炬。大小([280652,64]),训练数据占80%,测试数据占20%。我的代码:

#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}%")

错误发生在correct += (predict == labels).sum().item()这一行,错误如下:张量a(19)的大小必须与张量b(64)在非单维1上的大小匹配我不知道19来自哪里,我认为批量大小应该不重要。我忘记了什么,还是有一个管理错误在我的代码?

我试着调整批量大小和图层大小,并尝试了一些不同的方法,但似乎没有什么是正确的。

您已经两次应用了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
#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}%")

相关内容

  • 没有找到相关文章

最新更新