# Ch1 build first neutral network ###### tags: `Python`` tensorflow` ## Sample Code ```python= from tensorflow.keras.datasets import mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data() print("train_image[0] : ") print(train_images[0]) print("\n") print("train_labels[0] : ") print(train_labels[0]) print("shape of train_images") # train_image is an array, sized 60000, shaped 28*28 print(train_images.shape) print("shape of train_labels") # train_label is an array, sized 60000, train_label[0] is 5 print(train_labels.shape) print("\n") print("\n") import matplotlib.pyplot as plt plt.gcf().set_size_inches(15, 4) # get the current figure for i in range(5): ax = plt.subplot(1, 5, 1+i) # set subplot sized 1 * 5 ax.imshow(train_images[i], cmap= 'gray') # set output picture gray scale(็ฐ้šŽ) ax.set_title('label = '+str(train_labels[i]), fontsize=18) # set title plt.show() # show picture x_train = train_images.reshape((60000, 28 * 28)) # transform shape of data from (60000, 28, 28) to (60000, 784) x_train = x_train.astype('float32') / 255 # transform value of pixel from 0-255 to float number between 0-1 x_test = test_images.reshape((10000, 28 * 28)) x_test = x_test.astype('float32') / 255 from tensorflow.keras.utils import to_categorical y_train = to_categorical(train_labels) y_test = to_categorical(test_labels) print("before One-hot coding, label: ") print(train_labels[0]) print("after One-hot coding, label: ") print(y_train[0]) print("\n") print('shape of train_labels :') print(train_labels.shape) print("\n") print('shape of test_labels :') print(test_labels.shape) from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense model = Sequential() model.add(Dense(512, activation='relu', input_dim= 784)) # relu layer of CNN model.add(Dense(10, activation='softmax')) # fully connected layers of CNN model.compile(optimizer='rmsprop', # optimizer loss='categorical_crossentropy', # loss function metrics=['acc']) # benchmark standard ('acc' is abbreviation of 'accuracy') model.summary() history = model.fit(x_train, y_train, epochs=5, batch_size=128) # x_train => training sample, y_train => training label(correct answer), epoch => training 5 times, batch_size => how many samples to train in one time test_loss, test_acc = model.evaluate(x_test, y_test) print('accuracy to testing dataset : ', test_acc) predict = model.predict(x_test) # predict() will return predicting result predict.round(1) # decimal point 1 predict = model.predict_classes(x_test) print(predict) print(test_labels) predict = model.predict_classes(x_test) plt.gcf().set_size_inches(15, 4) for i in range(5): ax = plt.subplot(1, 5, 1+i) ax.imshow(test_images[i], cmap='binary') ax.set_title('label = '+str(test_labels[i]) + '\npredi = '+str(predict[i]), fontsize=18) ax.set_xticks([]); ax.set_yticks([]) plt.show() model.save('MnistModel.h5') #save model to file ``` ## Description of Sample Code * line.1: mnist(Mixed National Institute of Standards and Technology database => we download handwritten dataset from mnist database * line.24: matplotlib is a plotting(็นชๅœ–) library for python programming and NumPy of python * line.43 & line.45: why do we need to transform value of pixel from 0~255 to 0~1(float), check relu in line.79 and softmax in line.80 * line.50~line.53: take number as example (0-9), train_label[0] is 5; after One-hot coding, result will become[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.] * line.59: Sequential groups a linear stack of layers into a tf.keras.Model, and it provides training and inference features on this model **(???)** * line.60: class Dense => densely-connected NN(neutral-network) layer. * line.62~line.64: * input layers => let you input values of features (a little bit like linear regression) * hidden layers => used to do calculating, and they're made by neutrons, and they get data by 'forward propagation' * output layers => can get result by input_data and calculating by hidden layers * line.63: * 512 means number of neutral networks * relu means Rectified Linear Unit, a kind of activation function in AI * 784 means amount of dimension, and we only have to type dimensions one time, the following layer would follow dimensions declared in first layer * line.64: * 10 means number of neutral networks, it also means output dimensions(0~9) * softmax is a kind of activation function in AI * line.64(about softmax): * three key point about softmax => * 1. output value muse between 0 and 1 * 2. total of output value equals to 1 * 3. negative number(-) will become to positive number(+) by itself take arabic numerals as example there are ten options: 0 1 2 3 4 5 6 7 8 9 there's a picture with arabic numerals result of softmax may become 0.0 0.0 12.0 0.0 0.0 88.0 0.0 0.0 0.0 0.0 so the result of that picture may be 5 * line.70(about model.summary()): ![](https://lh3.googleusercontent.com/d/1mrcd4YQFZWY2PyxOhTFdW_XuFuQnuRO5) ## Sample Code ```python= from tensorflow.keras.datasets import mnist from tensorflow.keras.utils import to_categorical from tensorflow.keras.models import load_model # load load_model library model = load_model('MnistModel.h5') # load model from file (_,_), (test_images, test_labels) = mnist.load_data() x_test = test_images.reshape((10000, 28 * 28)) x_test = x_test.astype('float32') / 255 y_test = to_categorical(test_labels) test_loss, test_acc = model.evaluate(x_test, y_test) print('accuracy of test data : ', test_acc) ``` ## source 1. https://kknews.cc/zh-tw/code/3342l3o.html (mnist) 2. https://waternotetw.blogspot.com/2018/03/keras-mnist.html (mnist) 3. https://zh.wikipedia.org/wiki/Matplotlib (matplotlib) 4. https://www.itread01.com/content/1545049123.html (to_categorical) (one-hot coding) 5. https://keras.io/api/models/sequential/ (Sequential) 6. https://keras.io/api/layers/core_layers/dense/ (Dense) 7. https://vimsky.com/zh-tw/examples/detail/python-method-keras.layers.Dense.html (Dense) 8. https://zh.wikipedia.org/wiki/%E7%BA%BF%E6%80%A7%E6%95%B4%E6%B5%81%E5%87%BD%E6%95%B0 (ReLU) 9. https://zh.wikipedia.org/wiki/Softmax%E5%87%BD%E6%95%B0 (softmax) 10. https://blog.csdn.net/bitcarmanlee/article/details/82320853 (softmax) > Written with [StackEdit](https://stackedit.io/).