bayan elshiwy
bayan elshiwy

Reputation: 44

i want to convert the code below(neural network) from keras to pytorch

i want to use pytorch instead of keras but i failed to do it my self

Keras

def _model(self):
    model = Sequential()
    model.add(Dense(units=64, input_dim=self.state_size, 
    activation="relu"))
    model.add(Dense(units=32, activation="relu"))
    model.add(Dense(units=8, activation="relu"))
    model.add(Dense(self.action_size, activation="linear"))
    model.compile(loss="mse", optimizer=Adam(lr=0.001))

    return model

Pytorch

class Model(nn.Module):
    def __init__(self, input_dim):
        super(Model, self).__init__()
        self.fc1 = nn.ReLU(input_dim, 64)
        self.fc2 = nn.ReLU(64,32)
        self.fc3 = nn.Relu(32, 8)
        self.fc4 = nn.Linear(8, 3)
model = Model()
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

Upvotes: 0

Views: 47

Answers (1)

Manvir
Manvir

Reputation: 779

model = Model()

You need to provide an argument when you call Model() since your __init__(self, input_dims) requires an argument.

It should be this:

model = Model(<integer dimensions for your network>)

Upvotes: 1

Related Questions