in Education by
I am trying to implement the tutorial given on Tensorflow tutorial on custom training. Due to some reason dW and DB is None. I am not getting why t.gradient() is returning None. import tensorflow as tf tf.enable_eager_execution() class Model(object): def __init__(self): self.W = tf.Variable(5.0) self.b = tf.Variable(0.0) def __call__(self,x): return self.W*x+self.b def loss_function(self, y_true, y_predicted): return tf.reduce_mean(tf.square(y_predicted-y_true)) def train(self, inputs, outputs, learning_rate): with tf.GradientTape() as t: current_loss = self.loss_function(inputs,outputs) dW,db = t.gradient(current_loss,[self.W, self.b]) ## dW and db returns None self.W.assign_sub(learning_rate*dW) self.b.assign_sub(learning_rate*db) But the following code works fine when train is not a method of model. Any reason why? import tensorflow as tf tf.enable_eager_execution() class Model(object): def __init__(self): self.W = tf.Variable(5.0) self.b = tf.Variable(0.0) def __call__(self,x): return self.W*x+self.b def loss_function(self, y_true, y_predicted): return tf.reduce_mean(tf.square(y_predicted-y_true)) def train(model, inputs, outputs, learning_rate): with tf.GradientTape() as t: current_loss = model.loss_function(model(inputs),outputs) dW,db = t.gradient(current_loss,[model.W, model.b]) ## dW and db returns None model.W.assign_sub(learning_rate*dW) model.b.assign_sub(learning_rate*db) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
For gradient to work, the whole graph needs to be crafted within the scope of GradientTape. For example, in the code provided in Tensorflow tutorial on custom training: with tf.GradientTape() as t: current_loss = model.loss_function(model(inputs),outputs) The graph where current_loss is connected to the model variables (model.W and model.B) are constructed within the scope of t. If you change the codes provided by the tutorial as follows: logits = model(inputs) with tf.GradientTape() as t: current_loss = model.loss_function(logits, outputs) You will get None for dW and db.

Related questions

0 votes
    In the tensorflow documentation at the autograph section we have the following code snippet @tf.function def train ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I created an LSTM with Keras API. Now I am facing an issue while I try to test different values ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I am having problem in understanding the difference between three optimizers for loss.I went through some documents ... differences? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    In my unit test I am calling the following: every { connectivityManager.activeNetworkInfo } returns null but ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    In the code below, in "parse" function I am trying to get substring from the string "line". I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    In the code below, in "parse" function I am trying to get substring from the string "line". I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    I try to scrape some search results from https://www.companiesintheuk.co.uk/Company/Find?q=a With the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Details: We are using Keycloak authentication server with Asp.Net WebAPI. Now I need to get the Keycloak ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I try to scrape some search results from https://www.companiesintheuk.co.uk/Company/Find?q=a With the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    When I first did npm install I got bunch of warnings, below is just one example @angular/[email protected]. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I inherited code that makes the call to Dns.GetHostEntry("10.1.12.180") (or by using the dns of ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    After I click in the menu navigation (for exemple href='home') when i am in the view .../public ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I'm using EF code first. By convention, I have added a foreign key and a reference to the foreign ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I'm currently developing my first python program, a booking system using tkinter. I have a customer account ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Generic iterator (3 answers) Closed 7 years ago. Suppose I want to ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
...