Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Keras损失函数值错误:ValueError:梯度的操作为“无”。关于LSTM网络_Python_Tensorflow_Machine Learning_Keras - Fatal编程技术网

Python Keras损失函数值错误:ValueError:梯度的操作为“无”。关于LSTM网络

Python Keras损失函数值错误:ValueError:梯度的操作为“无”。关于LSTM网络,python,tensorflow,machine-learning,keras,Python,Tensorflow,Machine Learning,Keras,因此,我尝试训练我的LSTM网络语言模型,并使用困惑函数作为损失函数,但我得到以下错误: ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval. 我的损失函数如下所

因此,我尝试训练我的LSTM网络语言模型,并使用困惑函数作为损失函数,但我得到以下错误:

ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.
我的损失函数如下所示:

from keras import backend as K
def perplexity_raw(y_true, y_pred):
    """
    The perplexity metric. Why isn't this part of Keras yet?!
    https://stackoverflow.com/questions/41881308/how-to-calculate-perplexity-of-rnn-in-tensorflow
    https://github.com/keras-team/keras/issues/8267
    """
#     cross_entropy = K.sparse_categorical_crossentropy(y_true, y_pred)
    cross_entropy = K.cast(K.equal(K.max(y_true, axis=-1),
                          K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
                  K.floatx())
    perplexity = K.exp(cross_entropy)
    return perplexity
# define model
model = Sequential()
model.add(Embedding(vocab_size, 500, input_length=max_length-1))
model.add(LSTM(750))
model.add(Dense(vocab_size, activation='softmax'))
print(model.summary())
# compile network
model.compile(loss=perplexity_raw, optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(X, y, epochs=150, verbose=2)

我创建的模型如下所示:

from keras import backend as K
def perplexity_raw(y_true, y_pred):
    """
    The perplexity metric. Why isn't this part of Keras yet?!
    https://stackoverflow.com/questions/41881308/how-to-calculate-perplexity-of-rnn-in-tensorflow
    https://github.com/keras-team/keras/issues/8267
    """
#     cross_entropy = K.sparse_categorical_crossentropy(y_true, y_pred)
    cross_entropy = K.cast(K.equal(K.max(y_true, axis=-1),
                          K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
                  K.floatx())
    perplexity = K.exp(cross_entropy)
    return perplexity
# define model
model = Sequential()
model.add(Embedding(vocab_size, 500, input_length=max_length-1))
model.add(LSTM(750))
model.add(Dense(vocab_size, activation='softmax'))
print(model.summary())
# compile network
model.compile(loss=perplexity_raw, optimizer='adam', metrics=['accuracy'])
# fit network
model.fit(X, y, epochs=150, verbose=2)


当我尝试拟合模型时会发生错误。有人知道错误的原因和修复方法吗?

这些是罪魁祸首:
K.argmax
K.max
。它们没有梯度。我还认为,你只是直截了当地说,在你的损失指标中不需要它们!这是因为
max
ing和
argmax
ing某物会删除有关预测错误程度的信息


我不知道你想测量什么样的损失,但我认为你在寻找类似
tf.exp(tf.nn.sigmoid\u cross\u entropy\u with_logits(y\u true,y\u pred))
tf.exp(tf.softmax\u cross\u entopy\u with_logits(y\u true,y\u pred))
。您可能需要使用
tf.one\u hot

将您的登录名转换为一个热编码。谢谢您的帮助,但是我该如何调用我的登录名对其进行编码呢?