NO IMAGE

簡單的程式碼,後註上解析

from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense,Embedding
from keras.layers import LSTM
from keras.datasets import imdb
max_features = 20000
maxlen = 80
batch_size = 32
print('Loading data...')
(x_train,y_train),(x_test,y_test) = imdb.load_data(num_words= max_features )
print(len(x_train),'train sequences')
print(len(x_test),'test sequences')
print('Pad sequences(samples x time)')
x_train = sequence .pad_sequences(x_train ,maxlen= maxlen )
x_test = sequence .pad_sequences(x_test ,maxlen= maxlen )
print('x_train shape:',x_train .shape )
print('x_test shape:',x_test .shape )
print('Build model...')
model = Sequential()
model.add(Embedding (max_features ,128))#嵌入層將正整數下標轉換為固定大小的向量。只能作為模型的第一層
model.add(LSTM (128,dropout= 0.2,recurrent_dropout= 0.2))
model.add(Dense(1,activation= 'sigmoid'))
model.compile(loss= 'binary_crossentropy',optimizer= 'adam',metrics= ['accuracy'])
print('Train...')
model.fit(x_train ,y_train ,batch_size= batch_size ,epochs= 5,validation_data= (x_test ,y_test ))
score,acc = model.evaluate(x_test ,y_test ,batch_size= batch_size )
print('Test score:',score)
print('Test accuracy:', acc)

嵌入層Embedding
嵌入層是將正整數的下標轉換為就有固定大小的向量,而且只能作為模型的第一層。
其中、常用的引數:
input_dim:字典長度,即輸入資料最大下標 1。
output_dim : 全連線嵌入的維度。
input_length:當輸入序列的長度固定時,該值為其長度。如果要在該層後接Flatten層,然後接Dense層,則必須指定該引數,否則Dense層的輸出維度無法自動推斷。