我正在尝试解决分类问题,但代码中有些东西对我不起作用。 我有一个接收 30 个输入并输出 4 个动作的网络。 每个动作可以接收0到4之间的整数值(即接收0到4之间的数字,总共5个选项\5类)
我尝试实现此代码,但有些东西无法正常工作。我确定尺寸有问题 将不胜感激帮助! :)
这是我的代码:
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
# Load data from CSV file
file_path = '/content/30_1_output.csv'
df = pd.read_csv(file_path)
# Extract features and target outputs
X = df.iloc[:, :-4].values # Assuming the last 4 columns are the target outputs
Y = df.iloc[:, -4:].values # Extracting the last 4 columns as target outputs
# Map the values according to the specified mapping
Y = np.where(Y == -2, 0, # If value is -2, map it to 1
np.where(Y == -1, 1, # If value is -1, map it to 2
np.where(Y == 0, 2, # If value is 0, map it to 3
np.where(Y == 1, 3, # If value is 1, map it to 4
np.where(Y == 2, 4, Y) # If value is 2, map it to 5
)
)
)
)
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=42)
# Convert to PyTorch tensors
X_train_tensor = torch.tensor(X_train, dtype=torch.float32)
y_train_tensor = torch.tensor(y_train, dtype=torch.long)
X_test_tensor = torch.tensor(X_test, dtype=torch.float32)
y_test_tensor = torch.tensor(y_test, dtype=torch.long)
# Create datasets and dataloaders
train_dataset = TensorDataset(X_train_tensor, y_train_tensor)
test_dataset = TensorDataset(X_test_tensor, y_test_tensor)
# Adjust the batch sizes in DataLoader to match the batch size of the model outputs
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
# Define the neural network model
class NeuralNetwork(nn.Module):
def __init__(self, input_size, num_actions):
super(NeuralNetwork, self).__init__()
self.layer1 = nn.Linear(input_size, 64*2)
self.relu = nn.ReLU()
self.layer2 = nn.Linear(64*2, 32*2)
self.output_layer = nn.Linear(32*2, num_actions * 5) # 5 probabilities for each action
def forward(self, x):
out = self.layer1(x)
out = self.relu(out)
out = self.layer2(out)
out = self.relu(out)
out = self.output_layer(out)
out = torch.softmax(out.view(-1, 5), dim=1) # Reshape output and apply softmax
return out
# Initialize the model
input_size = X.shape[1] # Number of input features
num_actions = Y.shape[1] # Number of actions
model = NeuralNetwork(input_size, num_actions)
# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Training loop
num_epochs = 30
for epoch in range(num_epochs):
total_loss = 0
for inputs, labels in train_loader:
# Forward pass
outputs = model(inputs)
# Reshape outputs back to [batch_size, num_actions, 5]
outputs = outputs.view(-1, num_actions, 5)
# Take argmax along the last dimension to get the index of the class with the highest probability
#outputs2 = torch.argmax(outputs, dim=2)
#labels = labels.view(-1)
loss = criterion(outputs, labels)
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
total_loss += loss.item()
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {total_loss / len(train_loader):.4f}')
print("Training complete")
非常感谢!!
这是我得到的错误:
RuntimeError: Expected target size [64, 5], got [64, 4]
标签数据
Y
有4列:
Y = df.iloc[:, -4:].values # Extracting the last 4 columns as target outputs
而模型输出 5 列:
out = torch.softmax(out.view(-1, 5), dim=1) # Reshape output and apply softmax
Y
和模型的预测之间需要有匹配的列数,否则无法直接比较,无法计算损失。
从模型中删除冗余列以使其与
Y
完全对齐,或者将所需列添加到 Y
以匹配模型的输出。这些列的大小和含义需要匹配,因此您需要确定缺失的列代表什么以及它应该去哪里。