Skip to content

Instantly share code, notes, and snippets.

@darden1
Last active April 16, 2018 10:53
Show Gist options
  • Save darden1/bb7f75994fe21b71d8ff47d791294def to your computer and use it in GitHub Desktop.
Save darden1/bb7f75994fe21b71d8ff47d791294def to your computer and use it in GitHub Desktop.
train_with_keras_mlp.py
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from keras.losses import categorical_crossentropy
from keras.initializers import he_normal
n_features = X.shape[1]
n_classes = Y.shape[1]
batch_size = int(len(X_train)*0.2) # ミニバッチサイズ
epochs = 100 # エポック数
mu = 0.05 # 学習率
random_state = 1
np.random.seed(random_state)
model = Sequential()
model.add(Dense(10, activation='relu', kernel_initializer=he_normal(seed=random_state), bias_initializer='zeros', input_shape=(n_features,)))
model.add(Dense(10, activation='relu', kernel_initializer=he_normal(seed=random_state), bias_initializer='zeros'))
model.add(Dense(10, activation='relu', kernel_initializer=he_normal(seed=random_state), bias_initializer='zeros'))
model.add(Dense(n_classes, activation='softmax', kernel_initializer=he_normal(seed=random_state), bias_initializer='zeros'))
#model.summary()
model.compile(loss=categorical_crossentropy, optimizer=SGD(lr=mu), metrics=['accuracy'])
history = model.fit(X_train, Y_train, # トレーニングデータ
batch_size=batch_size, # バッチサイズ
epochs=epochs, # 総エポック数
validation_data=(X_test, Y_test), # テストデータ
shuffle=False, # エポック毎にデータをシャッフルしない
verbose=0) # 学習履歴をprintしない
print("acc_train: "+ str(history.history['acc'][-1]) + " acc_test: "+ str(history.history['val_acc'][-1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment