tensorflow InvalidArgumentError:图形执行错误:接收到的标签值为83193,超出了有效范围[0,128)

j8ag8udp  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(91)

我试图建立一个模型(简单的聊天机器人),但我坚持嵌入层。我有X_train形状=(100082,1307)-->问题y_train形状=(100082,1307)-->答案其中1307是我的最大长度的填充序列
下面是我的代码:

model = Sequential([
    Embedding(total_tokens, 128, input_length=max_seq_length),
    # Bidirectional(LSTM(150)),
    # Dense(total_tokens, activation='softmax')
])
model.summary()

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(X_train, y_train, epochs=50)

字符串
但是,当我训练模型时,出现了一个错误,如下所示:接收到的标签值为83193,超出了有效范围[0,128)。
我该如何解决这个问题?

nwlls2ji

nwlls2ji1#

这个问题是因为标签y_train包含的整数大于指定的间隔[0, 128),因为Embedding层的输出大小设置为128
让我们用一个简化的例子来说明这一点。在这里,我将形状从(100082, 1307)减少到(1000, 1307)
我们使用随机生成器创建y_trainTensor,如下所示:

y_train = tf.random.uniform(shape=([1000, 1307]), minval=0, maxval=127)

字符串
这里我们测试10个epoch:

model = Sequential([
    Embedding(total_tokens, 128, input_length=max_seq_length),
])
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10)


下面是输出:

Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 embedding_1 (Embedding)     (None, 1307, 128)         128000    
                                                                 
=================================================================
Total params: 128000 (500.00 KB)
Trainable params: 128000 (500.00 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/10
32/32 [==============================] - 4s 124ms/step - loss: 10.5185 - accuracy: 0.0000e+00
Epoch 2/10
32/32 [==============================] - 5s 152ms/step - loss: 10.4612 - accuracy: 0.0000e+00
Epoch 3/10
32/32 [==============================] - 4s 116ms/step - loss: 10.4123 - accuracy: 0.0000e+00
Epoch 4/10
32/32 [==============================] - 4s 116ms/step - loss: 10.3636 - accuracy: 0.0000e+00
Epoch 5/10
32/32 [==============================] - 6s 175ms/step - loss: 10.3126 - accuracy: 0.0000e+00
Epoch 6/10
32/32 [==============================] - 4s 120ms/step - loss: 10.2577 - accuracy: 0.0000e+00
Epoch 7/10
32/32 [==============================] - 4s 119ms/step - loss: 10.1967 - accuracy: 0.0000e+00
Epoch 8/10
32/32 [==============================] - 5s 152ms/step - loss: 10.1267 - accuracy: 0.0000e+00
Epoch 9/10
32/32 [==============================] - 4s 119ms/step - loss: 10.0431 - accuracy: 0.0000e+00
Epoch 10/10
32/32 [==============================] - 4s 119ms/step - loss: 9.9369 - accuracy: 7.6511e-07
<keras.src.callbacks.History at 0x7de0b88f0ee0>


在此示例中,模型运行时没有错误。
但是,如果生成器的上限设置为200(大于128):

y_train = tf.random.uniform(shape=([1000, 1307]), minval=0, maxval=200)


出现错误:

Received a label value of 199 which is outside the valid range of [0, 128).


因此,您应该确保y_train中的标签在Embedding图层的输出大小指定的有效范围内。

相关问题