我正在尝试使用Keras实现将n嵌入1个序列的word2vec到序列模型。
我的代码
创建输入数据:
vec_x=[]
for sent in tok_x:
sentvec = [ft_sg_model[w] for w in sent if w in ft_sg_model.wv.vocab]
vec_x.append(sentvec)
vec_y=[]
for sent in tok_y:
sentvec = [ft_sg_model[w] for w in sent if w in ft_sg_model.wv.vocab]
#print(sentvec)
vec_y.append(sentvec)
for tok_sent in vec_x:
tok_sent[maxlen-1:]=[]
tok_sent.append(ft_sg_model['_E_'])
for tok_sent in vec_x:
if len(tok_sent) < maxlen:
for i in range(maxlen -len(tok_sent)):
tok_sent.append(ft_sg_model['_E_'])
定义模型
import os
import pickle
import numpy as np
from keras.models import Sequential
import gensim
from keras.layers.recurrent import LSTM,SimpleRNN
from sklearn.model_selection import train_test_split
import theano
theano.config.optimizer="None"
vec_x=np.array(vec_x,dtype=np.float64)
vec_y=np.array(vec_y,dtype=np.float64)
x_train,x_test, y_train,y_test = train_test_split(vec_x, vec_y, test_size=0.2, random_state=1)
model=Sequential()
print(y_train.shape[1:])
print(x_train.shape)
#model.add(LSTM(output_dim=300,input_shape=x_train.shape[1:],return_sequences=False, init='glorot_normal', inner_init='glorot_normal', activation='sigmoid',batch_size=1))
model.add(LSTM(output_dim=300,input_shape=x_train.shape[1:], activation='sigmoid'))
model.compile(loss='cosine_proximity', optimizer='adam', metrics=['accuracy'])
model.summary()
训练模型
model.fit(x_train, y_train, nb_epoch=500,validation_data=(x_test, y_test))
# This gives error
model.save('LSTM500.h5');
但是出现以下错误:
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
"""Entry point for launching an IPython kernel.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-e9018df8d708> in <module>()
----> 1 model.fit(x_train, y_train, nb_epoch=500,validation_data=(x_test, y_test))
2 model.save('LSTM500.h5');
3 model.fit(x_train, y_train, nb_epoch=500,validation_data=(x_test, y_test))
4 model.save('LSTM1000.h5');
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
787 feed_output_shapes,
788 check_batch_axis=False, # Don't enforce the batch size.
--> 789 exception_prefix='target')
790
791 # Generate sample-wise weight values given the `sample_weight` and
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
126 ': expected ' + names[i] + ' to have ' +
127 str(len(shape)) + ' dimensions, but got array '
--> 128 'with shape ' + str(data_shape))
129 if not check_batch_axis:
130 data_shape = data_shape[1:]
ValueError: Error when checking target: expected lstm_4 to have 2 dimensions, but got array with shape (525, 1, 300)
我是NLP和深度学习的新手,我对如何更改编码和目标感到困惑。请提供一些有关如何在代码中定义正确和最佳超参数的建议。
先谢谢您。