簡體   English   中英

在創建 pytorch NN 模塊時使用列表

[英]using list in creating pytorch NN module

這段代碼運行良好,可以創建一個簡單的前饋神經網絡。 使用self將層 ( torch.nn.Linear ) 分配給 class 變量。

class MultipleRegression3L(torch.nn.Module):
  def __init__(self, num_features):
    super(MultipleRegression3L, self).__init__()

    self.layer_1 = torch.nn.Linear(num_features, 16)
    ## more layers

    self.relu = torch.nn.ReLU()

  def forward(self, inputs):
    x = self.relu(self.layer_1(inputs))
    x = self.relu(self.layer_2(x))
    x = self.relu(self.layer_3(x))
    x = self.layer_out(x)
    return (x)

  def predict(self, test_inputs):
    return self.forward(test_inputs)

但是,當我嘗試使用列表存儲圖層時:

class MultipleRegression(torch.nn.Module):
  def __init__(self, num_features, params):
    super(MultipleRegression, self).__init__()

    number_of_layers = 3 if not 'number_of_layers' in params else params['number_of_layers']
    number_of_neurons_in_each_layer = [16, 32, 16] if not 'number_of_neurons_in_each_layer' in params else params['number_of_neurons_in_each_layer']
    activation_function = "relu" if not 'activation_function' in params else params['activation_function']

    self.layers = []
    v1 = num_features
    for i in range(0, number_of_layers):
        v2 = number_of_neurons_in_each_layer[i]
        self.layers.append(torch.nn.Linear(v1, v2))
        v1 = v2

    self.layer_out = torch.nn.Linear(v2, 1)

    if activation_function == "relu":
        self.act_func = torch.nn.ReLU()
    else:
        raise Exception("Activation function %s is not supported" % (activation_function))

  def forward(self, inputs):
    x = self.act_func(self.layers[0](inputs))

    for i in range(1, len(self.layers)):
        x = self.act_func(self.layers[i](x))

    x = self.layer_out(x)

    return (x)

這兩個模型的行為方式不同。 這里有什么問題?

Pytorch 需要保留 model 中的模塊圖,所以使用list不起作用。 使用self.layers = torch.nn.ModuleList()解決了這個問題。

暫無
暫無

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

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