【问题标题】:keras argmax has none for gradients. How to define gradient for argmax?keras argmax 没有渐变。如何定义 argmax 的梯度?
【发布时间】:2018-09-16 20:46:24
【问题描述】:

我在伽马层中使用Keras.Backend.armax()。模型编译正常,但在 fit() 期间抛出错误。

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.

我的模特:

latent_dim = 512
encoder_inputs = Input(shape=(train_data.shape[1],))
encoder_dense = Dense(vocabulary, activation='softmax')
encoder_outputs = Embedding(vocabulary, latent_dim)(encoder_inputs)
encoder_outputs = LSTM(latent_dim, return_sequences=True)(encoder_outputs)
encoder_outputs = Dropout(0.5)(encoder_outputs)
encoder_outputs = encoder_dense(encoder_outputs)
encoder_outputs = Lambda(K.argmax, arguments={'axis':-1})(encoder_outputs)
encoder_outputs = Lambda(K.cast, arguments={'dtype':'float32'})(encoder_outputs)

encoder_dense1 = Dense(train_label.shape[1], activation='softmax')
decoder_embedding = Embedding(vocabulary, latent_dim)
decoder_lstm1 = LSTM(latent_dim, return_sequences=True)
decoder_lstm2 = LSTM(latent_dim, return_sequences=True)
decoder_dense2 = Dense(vocabulary, activation='softmax')

decoder_outputs = encoder_dense1(encoder_outputs)
decoder_outputs = decoder_embedding(decoder_outputs)
decoder_outputs = decoder_lstm1(decoder_outputs)
decoder_outputs = decoder_lstm2(decoder_outputs)
decoder_outputs = Dropout(0.5)(decoder_outputs)
decoder_outputs = decoder_dense2(decoder_outputs)
model = Model(encoder_inputs, decoder_outputs)
model.summary()

便于可视化的模型摘要:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_7 (InputLayer)         (None, 32)                0         
_________________________________________________________________
embedding_13 (Embedding)     (None, 32, 512)           2018816   
_________________________________________________________________
lstm_19 (LSTM)               (None, 32, 512)           2099200   
_________________________________________________________________
dropout_10 (Dropout)         (None, 32, 512)           0         
_________________________________________________________________
dense_19 (Dense)             (None, 32, 3943)          2022759   
_________________________________________________________________
lambda_5 (Lambda)            (None, 32)                0         
_________________________________________________________________
lambda_6 (Lambda)            (None, 32)                0         
_________________________________________________________________
dense_20 (Dense)             (None, 501)               16533     
_________________________________________________________________
embedding_14 (Embedding)     (None, 501, 512)          2018816   
_________________________________________________________________
lstm_20 (LSTM)               (None, 501, 512)          2099200   
_________________________________________________________________
lstm_21 (LSTM)               (None, 501, 512)          2099200   
_________________________________________________________________
dropout_11 (Dropout)         (None, 501, 512)          0         
_________________________________________________________________
dense_21 (Dense)             (None, 501, 3943)         2022759   
=================================================================
Total params: 14,397,283
Trainable params: 14,397,283
Non-trainable params: 0
_________________________________________________________________

我在谷歌上搜索了解决方案,但几乎所有都是关于一个有缺陷的模型。有些人建议不要使用导致问题的函数。但是,如您所见,如果没有 K.argmax,我无法创建此模型(如果您知道任何其他方式,请告诉我)。我该如何解决这个问题,从而训练我的模型?

【问题讨论】:

  • 你有一个巨大的概念问题,argmax 没有梯度,它不可微,所以你不能将它用于你的模型。
  • 是的,我知道 argmax 没有渐变,并希望找到一种方法来定义它,用一些东西(如 0)来修复错误。我需要一个像 argmax 这样的函数才能让我的模型工作,你知道我可以使用的其他函数吗?
  • 又是一个概念问题,不能定义argmax的梯度,如果定义了,总是错的,然后模型就无法训练,因为梯度中的信息会完全错了。
  • 所以您是说没有解决此错误的方法?因此,我必须使用不同的模型(因为如果没有 argmax,这显然行不通)?
  • 是的,这行不通。不要使用没有梯度的操作。

标签: python tensorflow keras deep-learning nlp


【解决方案1】:

出于显而易见的原因,Argmax 函数没有梯度;那怎么定义呢?为了使您的模型正常工作,您需要使该层不可训练。根据this question(或the documentation),您需要将trainable = False 传递给您的图层。至于层权重(如果适用),您可能希望将其设置为单位矩阵。

【讨论】:

  • 使 trainable = False 没有帮助。我仍然得到同样的错误。我认为在没有参数的图层上设置 trainable = False 没有效果。
猜你喜欢
  • 2019-06-07
  • 1970-01-01
  • 2022-07-25
  • 2022-06-12
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多