import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from torch.autograd import Variable
from sklearn import preprocessing
batch_size = 32
num_classes = 8
epochs = 10
img_rows, img_cols = 256, 256
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
print('x_train shape:', x_train.shape)
print("Training samples: {}".format(x_train.shape[0]))
print("Test samples: {}".format
(x_test.shape[0]))
x_train = torch.Tensor(x_train).float()
x_test = torch.Tensor(x_test).float()
y_train = torch.LongTensor(y_train)
y_test = torch.LongTensor(y_test)
#Define model
model = nn.Sequential(
nn.Conv2d(256,80,1, stride=1),
nn.ReLU(),
nn.Conv2d(80,40,1, stride=1),
nn.ReLU(),
nn.MaxPool2d(4,stride=1),
nn.Conv2d(40,30,1, stride=1),
nn.ReLU(),
nn.Conv2d(30,15,1, stride=1),
nn.ReLU(),
nn.Dropout(0.2),
nn.Flatten(),
nn.Linear(32, 32),
nn.Dropout(0.1),
nn.Linear(num_classes, 256),
nn.ReLU()
)
criterion = nn.CrossEntropyLoss()# cross entropy loss
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
for epoch in range(100):
optimizer.zero_grad()
out = model(x_train)
loss = criterion(out, y_train)
loss.backward()
optimizer.step()
print(f"Epoch {epoch+1}, loss: {loss.item()}")
However when I run this code it produces the error at this specifc line
RuntimeError Traceback (most recent call last)
<ipython-input-10-96fa8b09f1ec> in <module>
63 for epoch in range(100):
64 optimizer.zero_grad()
---> 65 out = model(x_train)
66 loss = criterion(out, y_train)
67 loss.backward()
with the following error message
RuntimeError: Given input size: (40x256x1). Calculated output size: (40x253x-2). Output size is too small
I am unsure on how to fix this as I am new to pytorch and the original model did work in tensorflow. Any help would be much appreciated