我在嵌入层的输出形状上遇到了一些问题,根据keras文档,嵌入层应具有3D张量的输出形状,但是我的嵌入层仅输出2D张量。
class MyModel(Model):
def __init__(self, vocab_size, embedding_matrix, max_length):
super(MyModel, self).__init__()
self.embedding_l1 = tf.keras.layers.Embedding(input_dim=vocab_size,
output_dim=max_length,
input_length=max_length,
weights=[embedding_matrix],
trainable=False)
self.bidirectional_l1 = Bidirectional(
tf.compat.v1.keras.layers.CuDNNLSTM(32,
return_sequences=False))
self.dense_l1 = Dense(units=256, activation='relu')
self.dropout_l1 = Dropout(rate=2e-5)
self.dense_l2 = Dense(units=1, activation='sigmoid')
def call(self, x):
embedding_out = self.embedding_l1(x)
print("SHAPE:",embedding_out.shape)
bid_out1 = self.bidirectional_l1(self.reshape_l1(embedding_out))
dense_out1 = self.dense_l1(bid_out1)
drop_out1 = self.dropout_l2(dense_out1)
dense_out2 = self.dense_l2(drop_out2)
return dense_out2
将嵌入层的形状输出为2D(300,300)张量。这会导致双向lstm错误:
ValueError: Input 0 of layer bidirectional is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [300, 300]
答案 0 :(得分:0)
找出它是嵌入层上的输入。我没有提供批处理大小,因此嵌入的输入看起来是[batch_size,300],而不是[300]。