[PYTHON] Summary of Tensorflow / Keras

Summary of Tensorflow / Keras

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.

usage environment

Python environment in VScode using Anaconda's base. I would like to talk about this in another article.

Main usage of Keras

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

Summary of Tensorflow / Keras
Summary of various operations in Tensorflow
Keras as wrapper of Theano & TensorFlow
Numerical summary of data
Summary of pyenv usage
Summary of string operations
Summary of Python arguments
Summary of logrotate software logrotate
Summary of test method
Summary of error handling methods when installing TensorFlow (2)
Summary of python file operations
Summary of Python3 list operations
2017.3.6 ~ 3.12 Summary of what we did
Convenient library of Tensorflow TF-Slim
Tuning experiment of Tensorflow data
Convenient usage summary of Flask
Summary of Linux distribution types
Basic usage of Pandas Summary
A brief summary of Linux
I touched Tensorflow and keras
Summary of Proxy connection settings
[Keras] batch inference of arcface
MNIST (DCNN) with Keras (TensorFlow backend)
Summary of how to use pandas.DataFrame.loc
Summary of basic knowledge of PyPy Part 1
Summary of basic implementation by PyTorch
Install an older version of Tensorflow
Summary of scraping relations (selenium, pyautogui)
A brief summary of Python collections
H29.2.27 ~ 3.5 Summary of what I did
Summary of Stack Overflow Developer Survey 2020
Summary of how to use pyenv-virtualenv
Machine learning ③ Summary of decision tree
[TensorFlow] [Keras] Neural network construction with Keras
A rough summary of OS history
Diversion of layers of trained keras model
Prediction of sine wave with keras
A brief summary of qubits (beginners)
Summary of go json conversion behavior
An implementation of ArcFace for TensorFlow
TensorFlow Tutorial-Vector Representation of Words (Translation)
A Tour of Go Learning Summary
Implementation of Light CNN (Python Keras)
Summary of "nl command Advent Calendar 2020"
[Anaconda3] Summary of frequently used commands
Summary of how to use csvkit
[For competition professionals] Summary of doubling
Summary of Python indexes and slices
Summary of multi-process processing of script language
Summary of restrictions by file system
[OpenCV; Python] Summary of findcontours function
4/22 prediction of sine wave with keras
June 2017 version to build Tensorflow / Keras environment on GPU instance of AWS
[Python] Summary of how to use pandas
2014/02/28 Summary of contents demoed at #ssmjp, part 1
Summary of frequently used commands of django (beginner)
Summary of methods often used in pandas
Summary of methods for automatically determining thresholds
Guarantee of reproducibility with keras (as of September 22, 2020)
Summary of gamma distribution parameter specification method
Summary of frequently used commands in matplotlib