【问题标题】:Tensorflow 2.2.0 error: [Predictions must be > 0] [Condition x >= y did not hold element-wise:] while using Bidirectional LSTM layerTensorflow 2.2.0 错误:[Predictions must be > 0] [Condition x >= y did not hold element-wise:] while using Bidirectional LSTM layer
【发布时间】:2020-10-17 17:46:07
【问题描述】:

我在处理命名实体识别任务时收到以下错误消息:

tensorflow.python.framework.errors_impl.InvalidArgumentError:  assertion failed: [predictions must be >= 0] [Condition x >= y did not hold element-wise:] [x (bidirectional_lstm_model/time_distributed/Reshape_1:0) = ] [[[-0.100267865 -0.104010895 0.04090859...]]...] [y (Cast_2/x:0) = ] [0]
     [[{{node assert_greater_equal/Assert/AssertGuard/else/_1/Assert}}]] [Op:__inference_train_function_6216]
Function call stack:
train_function

我该如何解决这个问题?我检查了我的输入 train_xtrain_y 张量,它们看起来很好(最后提供了一些示例)。

我最初使用的是条件随机场解码器。我将其替换为 Dense 层,以查看是否会更改错误消息。但错误仍然相同,并且在某种程度上与模型的 RNN 组件有关。

一般来说,您使用什么策略从 TF 内部深处解决此类错误?我试图在 PyCharm 上设置一个调试会话并跳过一堆 TF 文件,但没有学到任何关于如何解决我的问题的有用信息。

以下是我的网络架构:

Model: "bidirectional_lstm_model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
encoder_input (InputLayer)   [(None, None)]            0         
_________________________________________________________________
encoder_embedding (Embedding (None, None, 300)         2013300   
_________________________________________________________________
encoder_bidirectional_rnn (B (None, None, 32)          40576     
_________________________________________________________________
time_distributed (TimeDistri (None, None, 25)          825       
=================================================================
Total params: 2,054,701
Trainable params: 41,401
Non-trainable params: 2,013,300
_________________________________________________________________

以上 + 更多细节(损失、优化器等):

# Create model
encoder_input = keras.Input(shape=(None,), name='encoder_input')
encoder_embedding = layers.Embedding(input_dim=input_vocabulary,
                                     output_dim=embedding_vector_len,
                                     embeddings_initializer=tf.keras.initializers.Constant(embedding_matrix),
                                     trainable=False, name='encoder_embedding')(encoder_input)
encoder_rnn = layers.LSTM(16, return_sequences=True, name='encoder_rnn')
encoder_bidirectional_rnn = layers.Bidirectional(encoder_rnn, name='encoder_bidirectional_rnn')(encoder_embedding)
decoder_dense = layers.TimeDistributed(layers.Dense(number_of_tags, name='decoder_dense'))(encoder_bidirectional_rnn)
model = keras.Model(inputs=encoder_input, outputs=decoder_dense, name='bidirectional_lstm_model')
model.summary()

metrics_precision = tf.keras.metrics.Precision()
metrics_recall = tf.keras.metrics.Recall()
model.compile(
    loss=tf.keras.losses.categorical_crossentropy,
    optimizer='adam',
    metrics=[metrics_precision, metrics_recall]
)

这是我的 train_xtrain_y 数组的样子:

# Shapes
train_x.shape  # (9775, 47)  (np.ndarray type)
train_y.shape  # TensorShape([9775, 47, 25])  (Obtained from tf.one_hot)

# Sample (Zero-padded from the right)
train_x[0, :]

# array([4917, 2806, 6357, 2287, 6059,    0,    0,    0,    0,    0,    0,
#      0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
#      0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
#      0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
#      0,    0,    0])

train_y[0, :, :]

# array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],  # Non "O" tag
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],  # Non "O" tag
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
#   [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]], dtype=float32)

【问题讨论】:

    标签: python tensorflow lstm


    【解决方案1】:

    您缺少最后一层激活:

    decoder_dense = layers.TimeDistributed(layers.Dense(number_of_tags, name='decoder_dense'))(encoder_bidirectional_rnn)
    

    你应该指定你想要一个softmax,将激活保留为默认值实际上是一个线性激活,这意味着你可以有任何值,因此是负值。您应该按如下方式创建最后一个 Dense 层:

    decoder_dense = layers.TimeDistributed(layers.Dense(number_of_tags, activation='softmax', name='decoder_dense'))(encoder_bidirectional_rnn)
    
    

    【讨论】:

    • 谢谢!我猜这个错误信息让我在 RNN 层上看得太仔细了。
    • 你是对的,TensorFlow 的错误信息通常很神秘。
    猜你喜欢
    • 2021-01-11
    • 1970-01-01
    • 2021-01-06
    • 2019-07-15
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    相关资源
    最近更新 更多