Skip to content

Lab1 Part1: TypeError: Layer.add_weight() got multiple values for argument 'shape' while running cell no. 22 #163

@amin2783

Description

@amin2783

The error comes up when running locally, but not in colab.

The problematic cell is the no.22 in the solution notebook. and for reference, here's the code

### Defining a network Layer ###

# n_output_nodes: number of output nodes
# input_shape: shape of the input
# x: input to the layer

class OurDenseLayer(tf.keras.layers.Layer):
  def __init__(self, n_output_nodes):
    super(OurDenseLayer, self).__init__()
    self.n_output_nodes = n_output_nodes

  def build(self, input_shape):
    d = int(input_shape[-1])
    # Define and initialize parameters: a weight matrix W and bias b
    # Note that parameter initialization is random!
    self.W = self.add_weight("weight", shape=[d, self.n_output_nodes]) # note the dimensionality
    self.b = self.add_weight("bias", shape=[1, self.n_output_nodes]) # note the dimensionality

  def call(self, x):
    '''TODO: define the operation for z (hint: use tf.matmul)'''
    z = tf.matmul(x, self.W) + self.b # TODO
    # z = # TODO

    '''TODO: define the operation for out (hint: use tf.sigmoid)'''
    y = tf.sigmoid(z) # TODO
    # y = # TODO
    return y

# Since layer parameters are initialized randomly, we will set a random seed for reproducibility
tf.keras.utils.set_random_seed(1)
layer = OurDenseLayer(3)
layer.build((1,2))
x_input = tf.constant([[1,2.]], shape=(1,2))
y = layer.call(x_input)

# test the output!
print(y.numpy())
mdl.lab1.test_custom_dense_layer_output(y)

Output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[e:\Programming\Github](file:///E:/Programming/Github) Repos\MIT-Intro-to-Deep-Learning\lab1\solutions\Part1_TensorFlow_Solution.ipynb Cell 22 line 3
     [30](vscode-notebook-cell:/e%3A/Programming/Github%20Repos/MIT-Intro-to-Deep-Learning/lab1/solutions/Part1_TensorFlow_Solution.ipynb#X30sZmlsZQ%3D%3D?line=29) tf.keras.utils.set_random_seed(1)
     [31](vscode-notebook-cell:/e%3A/Programming/Github%20Repos/MIT-Intro-to-Deep-Learning/lab1/solutions/Part1_TensorFlow_Solution.ipynb#X30sZmlsZQ%3D%3D?line=30) layer = OurDenseLayer(3)
---> [32](vscode-notebook-cell:/e%3A/Programming/Github%20Repos/MIT-Intro-to-Deep-Learning/lab1/solutions/Part1_TensorFlow_Solution.ipynb#X30sZmlsZQ%3D%3D?line=31) layer.build((1,2))
     [33](vscode-notebook-cell:/e%3A/Programming/Github%20Repos/MIT-Intro-to-Deep-Learning/lab1/solutions/Part1_TensorFlow_Solution.ipynb#X30sZmlsZQ%3D%3D?line=32) x_input = tf.constant([[1,2.]], shape=(1,2))
     [34](vscode-notebook-cell:/e%3A/Programming/Github%20Repos/MIT-Intro-to-Deep-Learning/lab1/solutions/Part1_TensorFlow_Solution.ipynb#X30sZmlsZQ%3D%3D?line=33) y = layer.call(x_input)

File [c:\Users\USER\AppData\Local\Programs\Python\Python311\Lib\site-packages\keras\src\layers\layer.py:223](file:///C:/Users/USER/AppData/Local/Programs/Python/Python311/Lib/site-packages/keras/src/layers/layer.py:223), in Layer.__new__.<locals>.build_wrapper(*args, **kwargs)
    [220](file:///C:/Users/USER/AppData/Local/Programs/Python/Python311/Lib/site-packages/keras/src/layers/layer.py:220) @wraps(original_build_method)
    [221](file:///C:/Users/USER/AppData/Local/Programs/Python/Python311/Lib/site-packages/keras/src/layers/layer.py:221) def build_wrapper(*args, **kwargs):
    [222](file:///C:/Users/USER/AppData/Local/Programs/Python/Python311/Lib/site-packages/keras/src/layers/layer.py:222)     with obj._open_name_scope():
--> [223](file:///C:/Users/USER/AppData/Local/Programs/Python/Python311/Lib/site-packages/keras/src/layers/layer.py:223)         original_build_method(*args, **kwargs)
    [224](file:///C:/Users/USER/AppData/Local/Programs/Python/Python311/Lib/site-packages/keras/src/layers/layer.py:224)     # Record build config.
    [225](file:///C:/Users/USER/AppData/Local/Programs/Python/Python311/Lib/site-packages/keras/src/layers/layer.py:225)     signature = inspect.signature(original_build_method)

[e:\Programming\Github](file:///E:/Programming/Github) Repos\MIT-Intro-to-Deep-Learning\lab1\solutions\Part1_TensorFlow_Solution.ipynb Cell 22 line 1
     [13](vscode-notebook-cell:/e%3A/Programming/Github%20Repos/MIT-Intro-to-Deep-Learning/lab1/solutions/Part1_TensorFlow_Solution.ipynb#X30sZmlsZQ%3D%3D?line=12) d = int(input_shape[-1])
     [14](vscode-notebook-cell:/e%3A/Programming/Github%20Repos/MIT-Intro-to-Deep-Learning/lab1/solutions/Part1_TensorFlow_Solution.ipynb#X30sZmlsZQ%3D%3D?line=13) # Define and initialize parameters: a weight matrix W and bias b
     [15](vscode-notebook-cell:/e%3A/Programming/Github%20Repos/MIT-Intro-to-Deep-Learning/lab1/solutions/Part1_TensorFlow_Solution.ipynb#X30sZmlsZQ%3D%3D?line=14) # Note that parameter initialization is random!
---> [16](vscode-notebook-cell:/e%3A/Programming/Github%20Repos/MIT-Intro-to-Deep-Learning/lab1/solutions/Part1_TensorFlow_Solution.ipynb#X30sZmlsZQ%3D%3D?line=15) self.W = self.add_weight("weight", shape=[d, self.n_output_nodes]) # note the dimensionality
     [17](vscode-notebook-cell:/e%3A/Programming/Github%20Repos/MIT-Intro-to-Deep-Learning/lab1/solutions/Part1_TensorFlow_Solution.ipynb#X30sZmlsZQ%3D%3D?line=16) self.b = self.add_weight("bias", shape=[1, self.n_output_nodes])

TypeError: Layer.add_weight() got multiple values for argument 'shape'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions