简体   繁体   中英

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)

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>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM