下面是我的测试代码,其中包含随机生成的数据。
import numpy as np
import pandas as pd
import keras
from keras_contrib.layers import CRF
from keras.models import Model, Input
from keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional
import keras as k
x_train = np.random.rand(500,20,10)
y_train = np.random.rand(500,6)
print(x_train.shape, y_train.shape)# (500, 20, 10) (500, 6)
n_tags = 6
inputs = Input(shape=(20,10))
word_embedding_size = 300
n_words = 1234
model = Embedding(input_dim=n_words, output_dim=word_embedding_size, input_length=20*10)(inputs)
model = Bidirectional(LSTM(units=word_embedding_size,
return_sequences=True,
dropout=0.5,
recurrent_dropout=0.5,
kernel_initializer=k.initializers.he_normal()))(model)
model = LSTM(units=word_embedding_size * 2,
return_sequences=True,
dropout=0.5,
recurrent_dropout=0.5,
kernel_initializer=k.initializers.he_normal())(model)
model = TimeDistributed(Dense(n_tags, activation="relu"))(model) # previously softmax output layer
crf = CRF(n_tags)
out = crf(model)
model = Model(inputs, out)
adam = k.optimizers.Adam(lr=0.0005, beta_1=0.9, beta_2=0.999)
model.compile(optimizer=adam, loss=crf.loss_function, metrics=[crf.accuracy, 'accuracy'])
history = model.fit(x_train,y_train, batch_size=16, epochs=20, validation_split=0.2, verbose=1)
但出现错误
InvalidArgumentError: Shape must be rank 3 but is rank 2 for 'bidirectional_1/Tile' (op: 'Tile') with input shapes: [?,300,1], [2].
During handling of the above exception, another exception occurred:
似乎Embedding
个参数不适合LSTM
吗?