What is neural network learning? The training data is used to learn the combination of questions (input / data) and answers (output / label). And if you use test data as input to that trained network, By analyzing the obtained output, the performance of the neural network can be measured.
Python environment in VScode using Anaconda's base. I would like to talk about this in another article.
It will be easier to understand if you study while giving usage examples. We would appreciate it if you could take a closer look at the program comments.
・ How to learn MNIST by general machine learning (not CNN, RNN, LSTM)
mnist.py
#Import the MNIST library.
from keras.datasets import mnist
from keras import models
from keras import layers
from keras import optimizers
from keras import regularizers
from keras import utils
#Training data(50,000 pieces)And test data(10000 pieces)Prepare.
#This time, the holdout method is used as an evaluation,
#If the data is small, it is necessary to consider k-validation and random k-validation.
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
print(train_images.shape)
print(len(train_labels))
print(train_labels)
print(test_images.shape)
print(len(test_labels))
print(test_labels)
#Create a model of a linear stack.
network = models.Sequential()
#Dense refers to a tightly coupled layer.
# add()You can add layers with.
#units is the number of cells.
# kernel_The regularizer prevents overfitting by regularizing the network weight values.
# kernel_Both L1 regularization and L2 regularization are applied to the regularizer, but either one may or may not be used.
#activitation is a type of activation function
# input_shape is arrays of shape(*, 28*28).. Formally difficult to understand.
#Dropout is to forcibly set a part of the data propagating from the immediately preceding layer to the immediately following layer to 0.
#Overfitting can be prevented by intentionally adding noise. Basically, it is put in the middle layer, but it is not necessary.
network.add(layers.Dense(units=512,
kernel_regularizer=regularizers.l1_l2(l1=0.001, l2=0.001),
activation='relu',
input_shape=(28 * 28, ))) #Fully connected layer
network.add(layers.Dropout(0.25)) #Dropout layer
network.add(layers.Dense(units=10,
activation='softmax')) #Softmax layer
#You need to compile to get ready to train your network.
#The objective function can be set with loss, this time the cross entropy error.
#You can set the learning method with optimizer, such as SGD, momentum SGD, AdaGrad, RMSprop.
# metrics=['accuracy']Means to calculate the correct answer rate.
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
#Preprocess MNIST data before training.
#The value of the tensor is 0 because it requires a lot of calculation.~It is necessary to devise such as putting it between 1.
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255
print(train_images.shape)
print(train_images)
print(test_images.shape)
print(test_images)
#output(label)Is the number "0",1,2,3,~,Since it is divided into "9", it is necessary to encode it with a category value.
# [1, 0, 0, 0, 0, 0, 0, 0, 0]…0
# [0, 0, 1, 0, 0, 0, 0, 0, 0]…2
# [0, 0, 0, 0, 0, 1, 0, 0, 0]…6
#Like 0,1 represents a number.
train_labels = utils.to_categorical(train_labels)
test_labels = utils.to_categorical(test_labels)
#Now that we are ready to train the network, we call the fit method to fit the model to the training data.
#Enter the training data input and output data as arguments.
#epochs is the number of times the entire data is used. It shows how many times the same all data is used, and overfitting occurs if it is used too much.
# batch_size indicates how many divisions the input data used for training should be divided.
#This time it is 50,000 data, so 50,000 ÷ 100=At 500,"1"Learn with 500 data 100 palindromes per epoch. I don't know the detailed reason.
network.fit(train_images, train_labels, epochs=10, batch_size=100)
#Immediately trained network(network)Using,
#Loss value in test data(loss)And correct answer rate(accuracy)To ask.
#Enter the input and output data of the test data in the argument.
test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)
#Result is,"# test_acc: 0.9795 ", but the value varies depending on the environment.
#The reason is that Keras randomly chooses a seed value for network initialization.
・ How to train MNIST with LSTM
mnist_1.py
Recommended Posts