Skip to content

Chapter 13 Part 2 MyModel Fix #179

@natelg1998

Description

@natelg1998

Hello,

This has been a tremendous book so far in my understanding of ML and Neural Network Architecture. I believe there was an update to the Tensorflow library that affected the section "Building a NN Model In Tensorflow". When I used this code:

class MyModel(tf.keras.Model):
  def init(self):
    super(MyModel, self).init()
    self.w = tf.Variable(0.0, name='weight')
    self.b = tf.Variable(0.0, name='bias')
  def call(self, x):
    return self.w*x + self.b

When I got to the _Model training via the .compile() and .fit() method_s portion of the sample code, I was receiving a
UserWarning: The model does not have any trainable
weights.
warnings.warn("The model does not have any trainable weights."

which caused the model object to not learn from the toy dataset and fit the regression line properly. I found a fix that appears to work and it is quick. Rather define each variable as tf.Variable, I used tf.keras.Variable and it appears to work. I am guessing the mapping of classes are more explicit to go back to the keras backend. I hope this helps anyone that was having this issue:

FIXED CODE

class MyModel(tf.keras.Model):
  def init(self):
    super(MyModel, self).init()
    self.w = tf.keras.Variable(0.0, name='weight')
    self.b = tf.keras.Variable(0.0, name='bias')
  def call(self, x):
    return self.w*x + self.b

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions