Witam ! Mam problem z poniższym kodem:
import numpy as np
import torch
import pandas as pd
import torch.nn as nn
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
class ConNet(nn.Module):
def __init__(self):
super(ConNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=12, kernel_size=5, stride=1, padding=0)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(kernel_size=2)
self.conv2 = nn.Conv2d(in_channels=12, out_channels=20, kernel_size=3, stride=1, padding=1)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(kernel_size=2)
self.fc = nn.Linear(1999 * 97 * 8 * 5, 1)
# Feed forwad function
def forward(self, input):
output = self.conv1(input)
output = self.relu1(output)
output = self.pool1(output)
output = self.conv2(output)
output = self.relu2(output)
output = self.pool2(output)
output = output.view(output.size(0), -1)
output = self.fc(output)
return output
if __name__ == "__main__":
# Read data from csv using pandas
data = pd.read_csv("numbers.csv")
new_data = data.iloc[:, 1:784]
expectation = data['label']
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Train data and create Tensors
X_train, X_test, Y_train, Y_test = train_test_split(new_data, expectation, test_size=0.2, random_state=0)
Scaler = StandardScaler()
X_train = [[Scaler.fit_transform(X_train)]]
X_test = Scaler.fit_transform(X_test)
X_train = torch.FloatTensor(np.asarray(X_train)).to(device)
X_test = torch.FloatTensor(np.asarray(X_test)).to(device)
Y_train = torch.FloatTensor(np.asarray(Y_train)).to(device)
Y_test = torch.FloatTensor(np.asarray(Y_test)).to(device)
# New Network
Neural_Network = ConNet()
Neural_Network.to(device)
LossFunction = nn.L1Loss().to(device)
opt = torch.optim.SGD(Neural_Network.parameters(), lr=0.01) # lr = lear rate
# Create List of errors and define number of iterations
ListLoss = []
iterations = 100
# Training Network
for iterator in range(0, iterations):
y_pred = Neural_Network.forward(X_train)
loss = LossFunction(y_pred, Y_train)
ListLoss.append(loss.item())
print(f" Epoch: {iterator} Loss:{loss.item()}")
opt.zero_grad()
loss.backward()
opt.step()
Po opaleniu następuje wyświetlenie:" UserWarning: Using a target size (torch.Size([8000])) that is different to the input size (torch.Size([1, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
return F.l1_loss(input, target, reduction=self.reduction)". Z własnych obserwacji i testów rozumiem w czym tkwi problem. Czy mógłby ktoś spróbować nakierować mnie jak rozwiązać mój problem? Dziękuję za odpowiedzi !