**來自
imdb 的資料集介紹見:
from __future__ import print_function
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 #最大單詞量
# cut texts after this number of words (among top max_features most common words)
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) #這裡做乙個padding,大於maxlen的部分直接截掉、小於長度的用0填充
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)) #先做乙個embedding,類似於做乙個word2vec,因為輸入資料中的數字是單詞所在字典的位置
model.add(lstm(128, dropout=0.2, recurrent_dropout=0.2)) #加乙個 lstm 層,設定 dropout
model.add(dense(1, activation='sigmoid')) #全連線成,輸出一維的資料,啟用函式是sigmoid
# try using different optimizers and different optimizer configs
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy']) #優化器的定義,使用 binary_crossentropy 損失,adam 優化器,評價指標是 accuracy
print('train...')
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=15,
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)
keras例程學習與神經網路理解(一)
keras的example資料夾中有很多例程,學習mnist mlp.py例程 該例程實現功能為,從mnist資料集獲取60000個訓練樣本和10000個測試樣本,樣本為0 9十個數字,用全連線神經網路進行訓練,並測試結果。例程採用60000個資料樣本進行訓練,對於一般的電腦來說,這樣訓練會比較費時...
OpenCV例程學習
1,影象感興趣區域的顯示 include include int main 這裡定義了乙個imageroi region of interest 即感興趣區域,rect 為取乙個矩形區域,它的前兩個引數是矩形左上角座標,後兩個引數依次是寬度和高度。也可以這樣 include include incl...
keras初步學習
import os os.environ keras backend theano import keras 這樣就可以將keras依賴於theano using theano backend.2.曲線擬合 importos os.environ keras backend theano impor...