Keras TensorFlow自定义损失值错误:没有为任何变量提供梯度:

Keras TensorFlow自定义损失值错误:没有为任何变量提供梯度:,keras,tensorflow2.0,Keras,Tensorflow2.0,我正在实现一个自定义损失函数,如下代码所示,用于简单分类。但是,当我运行代码时,会得到错误值error:没有为任何变量提供渐变: 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.k

我正在实现一个自定义损失函数,如下代码所示,用于简单分类。但是,当我运行代码时,会得到错误值error:没有为任何变量提供渐变:

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)
我不明白我在这里做错了什么。我通读了有关tensorflow的问题,其中一个原因是损失函数和输入变量之间的联系被破坏了。但我在损失函数中使用了y_true


谢谢

您不能在自定义丢失功能中使用numpy。这个函数是图形的一部分,应该处理张量,而不是数组。Numpy不支持梯度的反向传播。

我修改了代码以使用张量def custom_loss(y_true,y_pred):y_pred=tf.where(tf.equal(tf.reduce_max(y_pred,axis=1,keepdims=true),y_pred.constant(1,shape=y_pred.shape),tf.constant(0,shape=y_pred.shape))y_pred=tf.cast(y_pred,dtype=int32)y_pred=tf.cast(y_真,dtype=int32)y_final=y_pred+y_真y_final=tf.其中(tf.equal(y_final,2),tf.zeros_like(y_final,y_final),y_final)w_array=tf.常量([[1,1,5],[1,1,1]])返回tf math.求和(tf.tensordot(y_final,w_array,axes=1,keepdims=False)),但得到相同的错误请创建一个单独的问题。现在可能很难重现您的错误