簡體   English   中英

我想將下面的代碼(神經網絡)從keras轉換為pytorch

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

我想用pytorch代替keras,但我自己做不到

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

由於__init__(self, input_dims)需要一個參數__init__(self, input_dims)在調用Model()時需要提供一個參數。

應該是這樣的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM