我制作了一个模型并出现了这个错误(它是一个 kaggle notebook),这是错误:
ValueError: Input 0 of layer bidirectional_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (13, 64)
<ipython-input-25-f223adf5c5a7> in <module>
12 for i in range(len(scores_transformed_train)):
13 # Optimize the model
---> 14 loss_value, grads = grad(temp_model, title_train[i],
scores_transformed_train[i])
15 optimizer.apply_gradients(zip(grads, temp_model.trainable_variables))
16
模型:
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization()
encoder.adapt(title_train)
model = tf.keras.Sequential([
encoder,
tf.keras.layers.Embedding(
input_dim=len(encoder.get_vocabulary()),
output_dim=64,
mask_zero=True),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(1, 'sigmoid')
])
我的功能
loss_object = tf.keras.losses.MeanSquaredError()
def loss(model, x, y, training):
y_ = model(x, training=training)
return loss_object(y_true=y, y_pred=y_)
def grad(model, inputs, targets):
with tf.GradientTape() as tape:
loss_value = loss(model, inputs, targets, training=True)
return loss_value, tape.gradient(loss_value, model.trainable_variables)
optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
请帮我修复它。如果需要,我很乐意回答任何其他问题...
答案 0 :(得分:0)
通常这种模型中的预期维度是 3。 (时间 x 批次 x 功能)。您提供了 2d。