【发布时间】:2020-04-07 16:14:34
【问题描述】:
我正在 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)
dataset 是一个批处理助手类。每次调用时,它都会返回包含两个张量的数组,其大小为 [(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?
问候
【问题讨论】:
标签: python tensorflow keras embedding