import keras
import tensorflow as tf
from keras.datasets import imdb
from keras.preprocessing import sequence
max_features = 2000 # number of words to consider as features
max_len = 500 # cut texts after this number of words (among top max_features most common words)
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=max_len)
x_test = sequence.pad_sequences(x_test, maxlen=max_len)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)
from keras.models import Sequential
from keras import layers
from keras.optimizers import RMSprop
model = Sequential()
model.add(layers.Embedding(max_features,
128,
input_length=max_len
))
model.add(layers.Conv1D(32, 7, activation='relu', name="first_layer"))
model.add(layers.MaxPooling1D(5))
with tf.name_scope("second_layer"):
model.add(layers.Conv1D(32, 7, activation='relu',name="hogehoge"))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Dense(1))
model.summary()
model.compile(optimizer=RMSprop(lr=1e-4),
loss='binary_crossentropy',
metrics=['acc'])
callbacks = [
keras.callbacks.TensorBoard(
log_dir = 'my_log_dir',
histogram_freq = 1
)
]
history = model.fit(x_train, y_train,
epochs=5,
batch_size=128,
validation_split=0.2,
callbacks=callbacks)