Tensorflow ValueError: No gradients provided for any variable:

Viewed 19

I was trying to do a custom loss function but I get the error. This is supposed to be cyclegan for text This is the loss function:

def sloss(ytrue, ypred):
  nump = 0
  print(nump)
  for i in range(len(ypred[0])):
    if int(round(ypred[0][i])) != int(ytrue[0][i]):
      nump+=1
  return(nump)

Model:

models = tf.keras.models.Sequential()
models.add(Dense(100, activation='relu'))
models.add(Dense(100, activation='linear'))
models.add(Dense(100, activation='sigmoid'))

models.compile(loss=sloss, optimizer='adam')

models.fit(sx(1,100 np arr), randes(1,100 np arr), epochs=1)
1 Answers

The loss function needs to be calculated based on ypred, otherwise the gradient cannot be calculated. Your loss function is just a sum of multiple 1's:

nump += 1
nump += 1
...
nump += 1

No ypred here. That's why the gradient cannot be calculated and the error message. Another issue is that your loss function is constant on intervals, that is, the derivative is zero everywhere. The optimizer cannot work with a zero gradient.

Related