<jupyter_start><jupyter_text>Multilayer Percepton https://zhuanlan.zhihu.com/p/33669143<jupyter_code>import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import tensorflow as tf

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.utils import np_utils
import pandas as pd

#load the data
train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")<jupyter_output>Using TensorFlow backend.<jupyter_text>data preparation<jupyter_code>from sklearn.model_selection import train_test_split
Y_train = train["label"]
# Drop 'label' column
X_train = train.drop(labels = ["label"],axis = 1) 

random_seed = 2
#split the data into train set and validation set
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.3, random_state=random_seed)
#data normalization from [0..255] to [0..1]
X_train = X_train / 255.0
X_val = X_val / 255.0
test = test / 255.0
#decode the lable to one-hot vectors
Y_train = np_utils.to_categorical(Y_train)
Y_val = np_utils.to_categorical(Y_val)
num_classes = Y_val.shape[1]<jupyter_output><empty_output><jupyter_text>Model Architecture<jupyter_code>#model architecture
model = Sequential()
#hidden layer use relu activation function
model.add(Dense(784, input_dim=784, kernel_initializer="normal", activation='relu'))
#output use softmax to classifier 
model.add(Dense(num_classes, kernel_initializer="normal", activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_3 (Dense)              (None, 784)               615440    
_________________________________________________________________
dense_4 (Dense)              (None, 10)                7850      
=================================================================
Total params: 623,290
Trainable params: 623,290
Non-trainable params: 0
_________________________________________________________________<jupyter_text>Evaluate the Modelepechs=10 accuracy is 0.9789<jupyter_code>model.fit(X_train, Y_train, validation_data=(X_val, Y_val), epochs=10, batch_size=200, verbose=2)<jupyter_output>Train on 29400 samples, validate on 12600 samples
Epoch 1/10
 - 2s - loss: 5.8998e-07 - acc: 1.0000 - val_loss: 0.1363 - val_acc: 0.9791
Epoch 2/10
 - 2s - loss: 5.5708e-07 - acc: 1.0000 - val_loss: 0.1368 - val_acc: 0.9789
Epoch 3/10
 - 2s - loss: 5.2520e-07 - acc: 1.0000 - val_loss: 0.1371 - val_acc: 0.9789
Epoch 4/10
 - 2s - loss: 5.0092e-07 - acc: 1.0000 - val_loss: 0.1372 - val_acc: 0.9789
Epoch 5/10
 - 2s - loss: 4.7155e-07 - acc: 1.0000 - val_loss: 0.1373 - val_acc: 0.9789
Epoch 6/10
 - 2s - loss: 4.4646e-07 - acc: 1.0000 - val_loss: 0.1380 - val_acc: 0.9790
Epoch 7/10
 - 2s - loss: 4.2467e-07 - acc: 1.0000 - val_loss: 0.1389 - val_acc: 0.9790
Epoch 8/10
 - 2s - loss: 4.0605e-07 - acc: 1.0000 - val_loss: 0.1385 - val_acc: 0.9791
Epoch 9/10
 - 2s - loss: 3.8464e-07 - acc: 1.0000 - val_loss: 0.1393 - val_acc: 0.9784
Epoch 10/10
 - 2s - loss: 3.6825e-07 - acc: 1.0000 - val_loss: 0.1390 - val_acc: 0.9789<jupyter_text>change the Epoches 100the accuracy does not improve too muchso we change another moethod<jupyter_code># change the epoches 
model.fit(X_train, Y_train, validation_data=(X_val, Y_val), nb_epoch=100, batch_size=200, verbose=2)<jupyter_output>/Users/yangjing/anaconda3/envs/py36/lib/python3.6/site-packages/keras/models.py:942: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
  warnings.warn('The `nb_epoch` argument in `fit` '
StarcoderdataJupyterStructuredCleanDedup