<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Step 1
Simply define your PyTorch model like usual, and create an instance of it.
Step1:  Step 2
Determine the names of the layers.
For the above model example it is very straightforward, but if you use param groups it may be a little more involved. To determine the names of the layers the next commands are useful
Step2:  Step 3
Define an equivalent Keras network. Use the built-in name keyword argument for each layer with params.
Step3:  Step 4
Now simply convert!
Step4:  Done!
Now let's check whether it was succesful. If it was, both networks should have the same output.
<ASSISTANT_TASK:>
Python Code:
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1   = nn.Linear(16*5*5, 120)
        self.fc2   = nn.Linear(120, 84)
        self.fc3   = nn.Linear(84, 10)
    def forward(self, x):
        out = F.relu(self.conv1(x))
        out = F.max_pool2d(out, 2)
        out = F.relu(self.conv2(out))
        out = F.max_pool2d(out, 2)
        out = out.view(out.size(0), -1)
        out = F.relu(self.fc1(out))
        out = F.relu(self.fc2(out))
        out = self.fc3(out)
        return out
    
pytorch_network = LeNet()
Explanation: Step 1
Simply define your PyTorch model like usual, and create an instance of it.
End of explanation
# The most useful, just print the network
print(pytorch_network)
# Also useful: will only print those layers with params
state_dict = pytorch_network.state_dict()
print(util.state_dict_layer_names(state_dict))
Explanation: Step 2
Determine the names of the layers.
For the above model example it is very straightforward, but if you use param groups it may be a little more involved. To determine the names of the layers the next commands are useful:
End of explanation
import keras
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
K.set_image_data_format('channels_first')
def lenet_keras():
    model = Sequential()
    model.add(Conv2D(6, kernel_size=(5, 5),
                     activation='relu',
                     input_shape=(1,32,32),
                     name='conv1'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Conv2D(16, (5, 5), activation='relu', name='conv2'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dense(120, activation='relu', name='fc1'))
    model.add(Dense(84, activation='relu', name='fc2'))
    model.add(Dense(10, activation=None, name='fc3'))
    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=keras.optimizers.Adadelta())
    
    return model
    
keras_network = lenet_keras()
Explanation: Step 3
Define an equivalent Keras network. Use the built-in name keyword argument for each layer with params.
End of explanation
transfer.keras_to_pytorch(keras_network, pytorch_network)
Explanation: Step 4
Now simply convert!
End of explanation
# Create dummy data
data = torch.rand(6,1,32,32)
data_keras = data.numpy()
data_pytorch = Variable(data, requires_grad=False)
# Do a forward pass in both frameworks
keras_pred = keras_network.predict(data_keras)
pytorch_pred = pytorch_network(data_pytorch).data.numpy()
assert keras_pred.shape == pytorch_pred.shape
plt.axis('Off')
plt.imshow(keras_pred)
plt.show()
plt.axis('Off')
plt.imshow(pytorch_pred)
plt.show()
Explanation: Done!
Now let's check whether it was succesful. If it was, both networks should have the same output.
End of explanation
<END_TASK>