一維卷積神經網路處理序列模型
阿新 • • 發佈:2018-12-31
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Embedding, Conv1D, MaxPooling1D, GlobalMaxPooling1D, Dense
from keras.optimizers import RMSprop
max_features = 10000
max_len = 500
(input_train, y_train), (input_test, y_test) = imdb.load_data(num_words = max_features)
print(len(input_train), 'train sequences')
print(input_train[0])
model = Sequential()
model.add(Embedding(max_features, 128, input_length = max_len))
model.add(Conv1D(32, 7, activation = 'relu'))
model.add(MaxPooling1D(5))
model.add(Conv1D(32, 7, activation = 'relu'))
model.add(GlobalMaxPooling1D() )
model.add(Dense(1))
model.summary()
model.compile(optimizer = RMSprop(lr = 1e-4),
loss = 'binary_crossentropy',
metrics = ['acc'])
history = model.fit(x_train, y_train,
epochs = 10,
batch_size = 128,
validation_split = 0.2)