Pytorch to Keras code equivalence

Looks like a

model = Sequential()
model.add(InputLayer(input_shape=input_shape(5,)) 
model.add(Dense(30, activation='relu')
model.add(Dense(3))

If you are trying to convert Pytorch model to Keras model, you can also try a Pytorch2Keras converter.

It supports base layers like Conv2d, Linear, Activations, Element-wise operations. So, I've converted ResNet50 with error 1e-6.


None of them looks correct according to my knowledge. A correct Keras equivalent code would be:

model = Sequential()
model.add(Dense(30, input_shape=(5,), activation='relu')) 
model.add(Dense(3)) 

model.add(Dense(30, input_shape=(5,), activation='relu'))

Model will take as input arrays of shape (*, 5) and output arrays of shape (*, 30). Instead of input_shape, you can use input_dim also. input_dim=5 is equivalent to input_shape=(5,).

model.add(Dense(3))

After the first layer, you don't need to specify the size of the input anymore. Moreover, if you don't specify anything for activation, no activation will be applied (equivalent to linear activation).


Another alternative would be:

model = Sequential()
model.add(Dense(30, input_dim=5)) 
model.add(Activation('relu'))
model.add(Dense(3)) 

Hopefully this makes sense!

Tags:

Keras

Pytorch