对imdb使用词嵌入
from keras.layers import Dense,Embedding,Flatten
import numpy as np
import keras.preprocessing as preprocessing
from keras.datasets import imdb
from keras.models import Sequential
max_features = 1000 #取常用的1000个单词数
maxlen = 20
(x_train,y_train),(x_test,y_test) = imdb.load_data(num_words=max_features)
# print(x_train) #一维列表有25000个元素 包含常用的1000个单词数
x_train = preprocessing.sequence.pad_sequences(x_train,maxlen=maxlen) #每20个元素截断一次
# print(x_train)
x_test = preprocessing.sequence.pad_sequences(x_test,maxlen=maxlen)
model = Sequential()
model.add(Embedding(max_features,8,input_length=maxlen))
model.add(Flatten())#维度变化为 (samples,maxlen*8)的二维张量
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['acc'])
model.summary()
histoty = model.fit(x_train,y_train,batch_size=32,epochs=10,validation_split=0.2)
print(histoty.history['acc'])