2017-10-21 4 views
1

PyTorch에서 아래의 코드가 주어지면 Keras에 해당하는 코드는 무엇입니까?Pytorch to Keras 코드 동등한

class Network(nn.Module): 

    def __init__(self, state_size, action_size): 
     super(Network, self).__init__() 

     # Inputs = 5, Outputs = 3, Hidden = 30 
     self.fc1 = nn.Linear(5, 30) 
     self.fc2 = nn.Linear(30, 3) 

    def forward(self, state): 
     x = F.relu(self.fc1(state)) 
     outputs = self.fc2(x) 
     return outputs 

이게 이거 야?

model = Sequential() 
model.add(Dense(units=30, input_dim=5, activation='relu')) 
model.add(Dense(units=30, activation='relu')) 
model.add(Dense(units=3, activation='linear')) 

또는이입니까?

model = Sequential() 
model.add(Dense(units=30, input_dim=5, activation='linear')) 
model.add(Dense(units=30, activation='relu')) 
model.add(Dense(units=3, activation='linear')) 

또는?

model = Sequential() 
model.add(Dense(units=30, input_dim=5, activation='relu')) 
model.add(Dense(units=30, activation='linear')) 
model.add(Dense(units=3, activation='linear')) 

덕분에 그들의

답변

4

아무도 내 지식에 따라 제대로 표시되지 않습니다. 원래 올바른 Keras 동등한 코드가 될 것이다 :

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

model.add (밀도 (30 input_shape = (5) 활성화 = 'relu'))를 입력으로서 취한다

모델 모양의 배열 (*, 5)과 모양의 출력 배열 (*, 30). input_shape 대신 input_dim을 사용할 수도 있습니다. input_dim=5input_shape=(5,)과 같습니다.

model.add (고밀도 (3))

첫 번째 레이어 후, 더 이상 입력의 크기를 지정할 필요가 없습니다. 또한 활성화를 위해 아무 것도 지정하지 않으면 활성화가 적용되지 않습니다 (선형 활성화와 동일).


또 다른 대안은 다음과 같습니다

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

는 희망이 말이

!

+0

답변에 따라 네트워크에 몇 개의 레이어가 있습니까? –

+0

2 개의 조밀 한 층, 하나는 활성화 "relu"이고 다른 하나는 "선형"입니다. –

+0

이 경우 나타나지 않는 "숨겨진 입력 레이어"도 있습니다. 이것을 계산하면 3 개의 레이어가됩니다. –

1

는 Keras 모델에 Pytorch 모델을 변환하려는 경우, 당신은 또한 Pytorch2Keras 변환을 시도 할 수있는

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

처럼 보인다.

Conv2d, 선형, 활성화, 요소 별 작업과 같은 기본 레이어를 지원합니다. 그래서 ResNet50을 1e-6 오류로 변환했습니다.