Arjun Bhandari
Arjun Bhandari

Reputation: 299

TensorFlow custom loss ValueError: No gradients provided for any variable:

I am implementing a custom loss function as in the code below for a simple classification. However, when I run the code I get the error ValueError: No gradients provided for any variable:

import os 

os.environ['KERAS_BACKEND'] = "tensorflow"

import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt 
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
import statistics as st 
import tensorflow as tf
from keras.utils import np_utils

# if the probability is greater than 0.75 then set the value to 1 for buy or sell else set it to None
# convert the y_pred to 0 and 1 using argmax function
# add the two matrices y_pred and y_true
# if value is 2 then set that to 0
# multiply by misclassification matrix
# add the losses to give a unique number
def custom_loss(y_true, y_pred):
    y_pred = y_pred.numpy()
    y_pred_dummy = np.zeros_like(y_pred)
    y_pred_dummy[np.arange(len(y_pred)), y_pred.argmax(1)] = 1
    y_pred = y_pred_dummy
    y_true = y_true.numpy()
    y_final = y_pred + y_true
    y_final[y_final == 2] = 0
    w_array = [[1,1,5],[1,1,1],[5,1,1]]
    return tf.convert_to_tensor(np.sum(np.dot(y_final, w_array)))
     

model = keras.Sequential()
model.add(layers.Dense(32, input_dim=4, activation='relu'))
model.add(layers.Dense(16, input_dim=4, activation='relu'))
model.add(layers.Dense(8, input_dim=4, activation='relu'))
model.add(layers.Dense(3, activation='softmax'))

model.compile(loss=custom_loss, optimizer='adam', run_eagerly=True)

I do not understand what I am doing incorrectly over here. I read through the issues on tensorflow and one of the reasons is that the link between the loss function and input variables is broken. But I am using y_true in the loss function

Thanks

Upvotes: 0

Views: 185

Answers (1)

Andrey
Andrey

Reputation: 6367

You can not use numpy within custom loss function. this function is a part of graph and should deal with tensors, not arrays. Numpy doesn't support backpropagation of gradients.

Upvotes: 1

Related Questions