我正在Google Colab中构建一个Tensorflow模型。我对嵌入层的行为感到困惑。它将输入层的大小不断减小一半。
def build_model(vocab_size, embedding_dim, rnn_units, batch_size):
model = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, embedding_dim,
batch_input_shape=[batch_size, 100]),
tf.keras.layers.GRU(rnn_units,
return_sequences=True,
stateful=True,
recurrent_initializer='glorot_uniform'),
tf.keras.layers.Dense(vocab_size)
])
return model
model = build_model(
vocab_size = len(vocab),
embedding_dim=embedding_dim,
rnn_units=rnn_units,
batch_size=BATCH_SIZE)
model.compile(optimizer='adam', loss=loss)
这是输入。
dataset = helperDf(df, 64,100)
数据集是批处理帮助程序类。每次调用它时,它都会返回带有两个张量的数组,它们的大小分别为[(64,100),(64,100)],用于训练和标注。
通话中
example_batch_predictions = model(dataset.batch())
print(example_batch_predictions.shape, "# (batch_size, sequence_length, vocab_size)")
还可以。与(64, 100, 48) # (batch_size, sequence_length, vocab_size)
但是我打给您时:
history = model.fit(dataset.batch(), epochs=EPOCHS, callbacks=[checkpoint_callback])
返回:
WARNING:tensorflow:Model was constructed with shape (64, 100) for input Tensor("embedding_4_input:0", shape=(64, 100), dtype=float32), but it was called on an input with incompatible shape (32, 100).
为什么将输入检测为32而不是64?
问候
[您的整个数据集可能没有64个整数倍的样本。最后一批可能较小。
如果是这种情况,很明显您的数据集是32的倍数,您可以尝试使用32的批处理大小(在数据集迭代器和模型中都可以)来检查是否是问题所在。
由于您使用的是stateful=True
,并且需要批处理大小,因此您需要处理数据集。如果确实需要64个,则可能需要创建32个以上示例(也许您复制了一些代表性不足的类的示例),或者删除了32个样本(最好是从代表性过多的类中删除)。