Skip to content

Instantly share code, notes, and snippets.

@KentaKudo
Created January 28, 2018 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KentaKudo/529291633463e26638226e3e7ad6613c to your computer and use it in GitHub Desktop.
Save KentaKudo/529291633463e26638226e3e7ad6613c to your computer and use it in GitHub Desktop.
from keras.datasets import reuters
from keras.models import Sequential
from keras.layers import Dense
from keras.preprocessing.text import Tokenizer
(x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=1000,
test_split=0.2)
tokenizer = Tokenizer(num_words=1000)
x_train = tokenizer.sequences_to_matrix(x_train, mode='binary')
x_test = tokenizer.sequences_to_matrix(x_test, mode='binary')
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Dense(512, input_shape=(1000,), activation='relu'))
model.add(Dense(256, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(46, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=32, epochs=5, verbose=2, validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Loss:', score[0])
print('Accuracy:', score[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment