InvalidArgumentError:不兼容的形状:[10,2856,2856]与[10,2856]

时间:2020-04-06 03:17:36

标签: python tensorflow keras nlp lstm

更多的是概念上的误解,但是当我通过嵌入层时,我的2-D x_train变成了3-D矩阵。但是,如何使用仍为二维的y_train将其拟合到模型中呢?我尝试将flatten()转换为二维矩阵,但是没有用。

这是我的代码:

def biDirectRNN (vocab_size, embedding_dim, batch_size, subcat, file):
    x_train, x_test, y_train, y_test = preprocess (subcat,file)
    x,y = y_train.shape
    model = tf.keras.Sequential()
    model.add(keras.layers.Embedding(input_dim = vocab_size, output_dim = embedding_dim, batch_input_shape=[batch_size, None]))
    forward_layer = keras.layers.LSTM(64, return_sequences = True)
    backward_layer = keras.layers.LSTM(32, activation='relu', return_sequences=True,
                       go_backwards=True)
    model.add(tf.keras.layers.Bidirectional(forward_layer, backward_layer = backward_layer))

    #model.add(keras.layers.Flatten())
    model.add(keras.layers.Dense(y, activation = tf.nn.softmax))
    model.compile(optimizer = 'adam',
          loss='mean_squared_error',
        metrics=['accuracy'])
    model.fit(x_train, y_train, batch_size=batch_size, epochs = 1)
    test_loss, test_acc = model.evaluate(x_test, y_test, epochs=10, batch_size=32)
    return test_loss, test_acc

函数调用:

biDirectRNN (2856, 100, 10, 'Crime', 'Crime14' )

我的错误是: InvalidArgumentError:不兼容的形状:[10,2856,2856]与[10,2856]

1 个答案:

答案 0 :(得分:0)

您需要提供 input_shape 而不是 batch_input_shape ,因为您已经在model.fit()中提到了批量大小。在“密集”层之前添加展平。

尝试以下代码。

checkArrowDisable

输出:

x,y = y_train.shape
model = tf.keras.Sequential()
model.add(keras.layers.Embedding(input_dim = 100, output_dim = 20, input_shape=(50,)))
forward_layer = keras.layers.LSTM(64, return_sequences = True)
backward_layer = keras.layers.LSTM(64, activation='relu', return_sequences=True,
                    go_backwards=True)
model.add(tf.keras.layers.Bidirectional(forward_layer, backward_layer = backward_layer))

model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(y, activation = tf.nn.softmax))
model.compile(optimizer = 'adam',
      loss='mean_squared_error',
    metrics=['accuracy'])
model.summary()
model.fit(x_train, y_train, batch_size=12, epochs = 1)
test_loss, test_acc = model.evaluate(x_test, y_test,  batch_size=32)