【问题标题】:TypeError: __init__() missing 2 required positional arguments: 'dimensions' and 'activations'类型错误:__init__() 缺少 2 个必需的位置参数:“维度”和“激活”
【发布时间】:2022-07-13 23:38:15
【问题描述】:

我试图从类中的函数返回一些值,但它给了我这个错误。

TypeError: __init__() missing 2 required positional arguments: 'dimensions' and 'activations'

通过使用:

a = SET_MLP()
print(a._update_w_b())

所以我试图从下面的函数中获取权重值以供以后使用,但它让我缺少“维度”和“激活”。 这些行是我正在处理的代码部分:

class SET_MLP:
def __init__(self, dimensions, activations,epsilon=20):
    """
    :param dimensions: (tpl/ list) Dimensions of the neural net. (input, hidden layer, output)
    :param activations: (tpl/ list) Activations functions.
    Example of three hidden layer with
    - 3312 input features
    - 3000 hidden neurons
    - 3000 hidden neurons
    - 3000 hidden neurons
    - 5 output classes
    layers -->    [1,        2,     3,     4,     5]
    ----------------------------------------
    dimensions =  (3312,     3000,  3000,  3000,  5)
    activations = (          Relu,  Relu,  Relu,  Sigmoid)
    """
    self.num_layers = len(dimensions)
    self.loss = None
    self.learning_rate = None
    self.momentum=None
    self.weight_decay = None
    self.epsilon = epsilon  # control the sparsity level as discussed in the paper
    self.zeta = None  # the fraction of the weights removed
    self.dimensions=dimensions


    # Weights and biases are initiated by index. For a one hidden layer net you will have a w[1] and w[2]
    self.w = {}
    self.b = {}
    self.pdw={}
    self.pdd={}

    # Activations are also initiated by index. For the example we will have activations[2] and activations[3]
    self.activations = {}
    for i in range(len(dimensions) - 1):
        self.w[i + 1] = createSparseWeights(self.epsilon, dimensions[i], dimensions[i + 1])#create sparse weight matrices
        self.b[i + 1] = np.zeros(dimensions[i + 1])
        self.activations[i + 2] = activations[i]


def _update_w_b(self, index, dw, delta):
    """
    Update weights and biases.
    :param index: (int) Number of the layer
    :param dw: (array) Partial derivatives
    :param delta: (array) Delta error.
    """

    #perform the update with momentum
    if (index not in self.pdw):
        self.pdw[index]=-self.learning_rate * dw
        self.pdd[index] =  - self.learning_rate * np.mean(delta, 0)
    else:
        self.pdw[index]= self.momentum*self.pdw[index]-self.learning_rate * dw
        self.pdd[index] =  self.momentum * self.pdd[index] - self.learning_rate * np.mean(delta, 0)

    self.w[index] += self.pdw[index]-self.weight_decay*self.w[index]
    self.b[index] += self.pdd[index]-self.weight_decay*self.b[index]

【问题讨论】:

    标签: python function


    【解决方案1】:

    您必须在创建对象时指定激活次数和尺寸:

    a = SET_MLP(activations = x, dimensions = y)
    

    xy 是您的 NN 的值。

    这是因为__init__ 是类SET_MLP 的构造函数。

    或者,您可以像在此处所做的那样应用默认值 (epsilon=20):

    def __init__(self, dimensions=x, activations=y, epsilon=20):
    

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 1970-01-01
      • 2022-01-11
      • 2021-07-06
      • 2023-03-19
      • 2020-01-29
      • 2017-10-30
      • 2021-09-29
      相关资源
      最近更新 更多