【问题标题】:TensorFlow custom loss ValueError: No gradients provided for any variable:TensorFlow 自定义损失 ValueError:没有为任何变量提供梯度:
【发布时间】:2021-05-08 09:07:23
【问题描述】:

我正在实现一个自定义损失函数,如下面的代码所示,用于简单分类。但是,当我运行代码时,我收到错误 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)

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

谢谢

【问题讨论】:

    标签: keras tensorflow2.0


    【解决方案1】:

    您不能在自定义损失函数中使用 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) , tf.constant(1, shape=y_pred.shape), tf.constant(0, shape=y_pred.shape) ) y_pred = tf.cast(y_pred, dtype=int32) y_true = tf.cast(y_true, dtype=int32 ) y_final = y_pred + y_true y_final = tf.where(tf.equal(y_final, 2), tf.zeros_like(y_final), y_final) w_array = tf.constant([[1,1,5], [1,1, 1], [5,1,1]]) return tf.math.reduce_sum(tf.tensordot(y_final, w_array, axes=1, keepdims= False)),但得到同样的错误
    • 请创建一个单独的问题。现在可能很难重现您的错误
    猜你喜欢
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 2021-01-08
    • 1970-01-01
    • 2020-10-31
    • 2020-09-30
    相关资源
    最近更新 更多