I’m trying to convert CNN model code from Keras to Pytorch.
here is the original keras model:
input_shape = (28, 28, 1)
model = Sequential()
model.add(Conv2D(28, kernel_size=(3,3), input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # Flattening the 2D arrays for fully connected layers
model.add(Dense(128, activation=tf.nn.relu))
model.add(Dropout(0.2))
model.add(Dense(10,activation=tf.nn.softmax))
And this is my Pytorch model, and I am not sure am I doing right or not as I am new in CNN and Pytorch. I couldn’t find out what should be output channel in Pytorch corespond with this keras mode. Any comment would appreciate.
class NeuralNet(nn.Module):
def __int__(self):
super(NeuralNet, self).__init__()
self.hidden1 = nn.conv2d(28, 28, kernel_size=(3, 3))
self.hidden2 = nn.maxpoo2d(2, 2)
self.hidden3 = nn.Linear(128, 10) # equivalent to Dense in keras
self.hidden4 = nn.Dropout(0.2)
self.hidden5 = nn.linear(10)
def forward(self, x):
x = self.hidden2(self.hidden1(x))
x = self.hidden2(F.relu(self.hidden3(x)))
x = self.hidden2(F.relu(self.hidden5(x)))
x = x.view(-1, 128)
return x