2017-12-27 14 views
0

3 차원 신경망 (입력, 숨김, 출력)에 숨겨진 유닛을 동적으로 추가하려고합니다. 나는 새로운 숨겨진 units.This를 추가pytorch에 숨겨진 유닛을 동적으로 추가했습니다.

class my_network(torch.nn.Module): 
    def __init__(self,input_dim,hidden_dim,output_dim): 
     super(my_network,self).__init__() 
     self.I = input_dim 
     self.H = hidden_dim 
     self.O = output_dim 
     self.layer1 = torch.nn.Linear(input_dim,hidden_dim) 
     self.layer2 = torch.nn.Linear(hidden_dim,output_dim) 

    def add_neurons(self,no_of_neurons,flag): 
     if flag == 1: 
      weights = [self.layer1.weight.data,self.layer2.weight.data] 
      self.layer1 = torch.nn.Linear(self.I,self.H+no_of_neurons) 
      self.layer2 = torch.nn.Linear(self.H+no_of_neurons,self.O) 
      self.layer1.weight.data[0:-no_of_neurons,:] = weights[0] 
      self.layer2.weight.data[:,0:-no_of_neurons] = weights[1] 
      self.H = self.H + no_of_neurons 
     return self.layer1.weight.shape[0] 

    def forward(self,x): 
     temp = self.layer1(x) 
     out = self.layer2(temp) 
     return out 

내가 그라디언트 동안 (내가 "add_neurons"메소드를 호출하면, 가중치가 업데이트 중지 것으로 나타났습니다, 내 코드는 네트워크의 훈련 된 부분의 무게를 유지하려면 생성됨). 어떤 도움이라도 대단히 감사 할 것입니다.

답변

0

옵티마이 저가 모델에 추가 한 새 매개 변수에 대해 알리지 않을 수도 있습니다. 가장 쉬운 방법은 업데이트 된 모델 매개 변수 목록을 사용하여 옵티 마이저 개체를 다시 만드는 것입니다.