Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
amin2783 opened this issue Jun 13, 2024 · 2 comments

Comments

@amin2783
Copy link

amin2783 commented Jun 13, 2024

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'
@amin2783 amin2783 changed the title Lab1 Part1: TypeError: Layer.add_weight() got multiple values for argument 'shape' Lab1 Part1: TypeError: Layer.add_weight() got multiple values for argument 'shape' while running cell no. 22 Jun 13, 2024
@intval
Copy link

intval commented Jun 26, 2024

the solution is to add the name= in the add weight function:

    self.W = self.add_weight(name="weight", shape=[d, self.n_output_nodes]) # note the dimensionality
    self.b = self.add_weight(name="bias", shape=[1, self.n_output_nodes]) # note the dimensionality

@amin2783
Copy link
Author

Thanks a lot. I later found out that the issue is with keras versions. I initially installed tensorflow 2.16.1, with keras3. Whereas this code is works with keras2, included with tensorflow 2.15.0 (the version used in colab).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants