Skip to content

Instantly share code, notes, and snippets.

@nesterione
Created July 7, 2017 20:34
Show Gist options
  • Select an option

  • Save nesterione/a7ea65dfb0ad2e904b99ba6ac8bb115f to your computer and use it in GitHub Desktop.

Select an option

Save nesterione/a7ea65dfb0ad2e904b99ba6ac8bb115f to your computer and use it in GitHub Desktop.
# https://keras.io/getting-started/sequential-model-guide/
from __future__ import print_function
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
import pandas as pd
data = pd.read_csv("./data/train.csv")
data_set = data.as_matrix()
x_data = data_set[:,1:]
y_data = data_set[:,0]
num_classes = 10
b = int(x_data.shape[0]*0.8)
x_train, x_test = x_data[:b], x_data[b:]
y_train, y_test = y_data[:b], y_data[b:]
print(x_train.shape)
print(x_test.shape)
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
# now the model will take as input arrays of shape (*, 16)
# and output arrays of shape (*, 32)
# after the first layer, you don't need to specify
# the size of the input anymore:
model.add(Dense(500, input_shape=(784,)))
model.add(Activation('sigmoid'))
model.add(Dense(300))
model.add(Activation('sigmoid'))
model.add(Dense(120))
model.add(Activation('sigmoid'))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
# mse
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
x_train = x_train.astype('float32')
x_train /= 255
x_test = x_test.astype('float32')
x_test /= 255
model.fit(x_train, y_train, epochs=20, batch_size=32, validation_data=(x_test, y_test), shuffle=True)
print('saving model')
model.save('nn_dence.h5')
model.save_weights('nn_dence_weights.h5')
print('model saved')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment