2

I created a class to implement Logistic Regression using Sigmoid function. find below the definition for reference.

class model():
def __init__(self):
    self.loss = 0.0
    self.accuracy = 0.0

@tf.function
def Model(self,X):
    return tf.sigmoid(tf.multiply(X,self.weights[0])+self.weights[1])

def fit(self,x_train,y_train,learning_rate=0.001, epochs=10):
    self.x_train = x_train
    self.y_train = y_train
    self.weights = tf.Variable([0., 0.], dtype=x_train.dtype)
    loss = lambda: tf.reduce_mean((tf.abs(tf.subtract(y_train,self.Model(self.x_train)))))
    for epoch in range(epochs):
        _ = tf.keras.optimizers.SGD(learning_rate=learning_rate).minimize(loss, var_list=[self.weights])
        print(self.weights)
    self.loss = loss().numpy()

def predict(self, x_test):
    self.y_pred = self.Model(x_test)
    return np.round(self.y_pred.numpy())

def evaluate(self,x_test,y_test):
    return (tf.constant(1, dtype=tf.float64) - tf.reduce_mean(tf.abs(tf.subtract(y_test,self.Model(x_test))))).numpy()

Then I'm creating object of this class for training and prediction as follows:

m = model()
m.fit(x_train,y_train,epochs=100,learning_rate=0.1)
m.evaluate(x_test,y_test)

This runs fine in the first iteration. But When I run m.fit for the second time without reinitializing the object, it throws below mentioned exception:

ValueError                                Traceback (most recent call last)
<ipython-input-268-39014f3ec7dd> in <module>
----> 1 m.fit(x_train,y_train,epochs=100,learning_rate=0.1)

<ipython-input-265-4d5e4e8ae4e1> in fit(self, x_train, y_train, learning_rate, epochs)
     14         loss = lambda: tf.reduce_mean((tf.abs(tf.subtract(y_train,self.Model(self.x_train)))))
     15         for epoch in range(epochs):
---> 16             _ = tf.keras.optimizers.SGD(learning_rate=learning_rate).minimize(loss, var_list=[self.weights])
     17             print(self.weights)
     18         self.loss = loss().numpy()

/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/optimizer_v2/optimizer_v2.py in minimize(self, loss, var_list, grad_loss, name)
    317         loss, var_list=var_list, grad_loss=grad_loss)
    318 
--> 319     return self.apply_gradients(grads_and_vars, name=name)
    320 
    321   def _compute_gradients(self, loss, var_list, grad_loss=None):

/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/optimizer_v2/optimizer_v2.py in apply_gradients(self, grads_and_vars, name)
    425       ValueError: If none of the variables have gradients.
    426     """
--> 427     grads_and_vars = _filter_grads(grads_and_vars)
    428     var_list = [v for (_, v) in grads_and_vars]
    429 

/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/optimizer_v2/optimizer_v2.py in _filter_grads(grads_and_vars)
   1023   if not filtered:
   1024     raise ValueError("No gradients provided for any variable: %s." %
-> 1025                      ([v.name for _, v in grads_and_vars],))
   1026   if vars_with_empty_grads:
   1027     logging.warning(

ValueError: No gradients provided for any variable: ['Variable:0'].```
Darshan Parab
  • 75
  • 2
  • 6
  • 1
    `tf.abs` in your loss function is probably the issue. It's not differentiable, resulting in `None` gradients. Get rid of that and use `tf.math.sqrt` and `**2` to compute root mean square (if that's the loss you're looking for), if not, use a differentiable loss. – thushv89 Dec 19 '19 at 11:35
  • I think you can get idea from here -> https://stackoverflow.com/questions/49289930/tensorflow-no-gradients-provided-for-any-variable – Kalana Dec 19 '19 at 11:45
  • @thushv89 I changed loss function as mentioned below ```loss = lambda: tf.losses.mean_squared_error(y_true=self.y_train,y_pred=self.Model(self.x_train))``` Still getting the error. I want to highlight the fact that the code runs fine on 1st iteration and I can see weights getting optimized, but on calling ```m.fit(x_train,y_train,epochs=10,learning_rate=0.1)``` 2nd time without calling ```m = model()``` throws an exception. – Darshan Parab Dec 20 '19 at 05:29
  • @DarshanParab, That's strange. I'll have a look at this over the weekend :) – thushv89 Dec 20 '19 at 05:34
  • facing this issue any solution? – Crazy-Kaleidoscope-7 Apr 17 '20 at 19:10

0 Answers0