[PYTHON] Machine learning memo of a fledgling engineer Part 2

Introduction

This is part 2 of the learning memo for "Deep-Learning from scratch".

2-layer neural network (Chapter 4)

train_neuralnet


#Data reading
(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True,flatten=True, one_hot_label=True)

train_neuralnet


network = TwoLayerNet(input_size=784, hidden_size=50, output_size=10)

train_neuralnet


iters_num = 10000  #Set the number of repetitions as appropriate
train_size = x_train.shape[0] # 60000
batch_size = 100
learning_rate = 0.1

train_loss_list = []
train_acc_list = []
test_acc_list = []
#Iterative processing per epoch 60000/ 100
iter_per_epoch = max(train_size / batch_size, 1)

train_neuralnet


for i in range(iters_num): #10000
    #Get a mini batch
    batch_mask = np.random.choice(train_size, batch_size) # (100,)Form of
    x_batch = x_train[batch_mask] # (100,784)Form of
    t_batch = t_train[batch_mask] # (100,784)Form of
    
    #Gradient calculation
    #grad = network.numerical_gradient(x_batch, t_batch)
    grad = network.gradient(x_batch, t_batch)
    
    #Parameter update
    for key in ('W1', 'b1', 'W2', 'b2'):
        network.params[key] -= learning_rate * grad[key]
    
    loss = network.loss(x_batch, t_batch)
    train_loss_list.append(loss)

    #Save data when the conditions are met for 1 epoch at 600
    if i % iter_per_epoch == 0:
        train_acc = network.accuracy(x_train, t_train)
        test_acc = network.accuracy(x_test, t_test)
        train_acc_list.append(train_acc)
        test_acc_list.append(test_acc)
        print("train acc, test acc | " + str(train_acc) + ", " + str(test_acc))

python


#Drawing a graph
x = np.arange(len(train_acc_list))
plt.plot(x, train_acc_list,'o', label='train acc')
plt.plot(x, test_acc_list, label='test acc', linestyle='--')
plt.xlabel("epochs")
plt.ylabel("accuracy")
plt.ylim(0, 1.0)
plt.legend(loc='lower right')
plt.show()

reference

Deep Learning from scratch

Recommended Posts

Machine learning memo of a fledgling engineer Part 1
Machine learning memo of a fledgling engineer Part 2
[Memo] Machine learning
Classification of guitar images by machine learning Part 1
Python & Machine Learning Study Memo ⑤: Classification of irises
Machine learning course memo
Classification of guitar images by machine learning Part 2
Get a glimpse of machine learning in Python
Since the fledgling engineer passed the G test, make a note of the learning content
Impressions of taking the Udacity Machine Learning Engineer Nano-degree
Memo for building a machine learning environment using Python
Basics of Machine Learning (Notes)
Practical machine learning system memo
Build a machine learning environment
Importance of machine learning datasets
Performance verification of data preprocessing for machine learning (numerical data) (Part 2)
A beginner's summary of Python machine learning is super concise.
Performance verification of data preprocessing for machine learning (numerical data) (Part 1)
Significance of machine learning and mini-batch learning
Machine learning ③ Summary of decision tree
Inversely analyze a machine learning model
A Tour of Go Learning Summary
"Scraping & machine learning with Python" Learning memo
A story stuck with the installation of the machine learning library JAX
A memorandum of scraping & machine learning [development technique] by Python (Chapter 4)
A memorandum of scraping & machine learning [development technique] by Python (Chapter 5)
Python & Machine Learning Study Memo: Environment Preparation
Machine learning algorithm (generalization of linear regression)
I changed my job to a machine learning engineer at AtCoder Jobs
Predict power demand with machine learning Part 2
[Learning memo] Basics of class by python
A rough understanding of python-fire and a memo
Aiming to become a machine learning engineer from sales positions using MOOCs
Latin learning for the purpose of writing a Latin sentence analysis program (Part 1)
A story about machine learning with Kyasuket
Become an AI engineer soon! Comprehensive learning of Python / AI / machine learning / deep learning / statistical analysis in a few days!
2020 Recommended 20 selections of introductory machine learning books
Machine learning
Machine learning algorithm (implementation of multi-class classification)
A beginner of machine learning tried to predict Arima Kinen with python
Python learning memo for machine learning by Chainer Chapter 13 Basics of neural networks
A memo explaining the axis specification of axis
[Machine learning] List of frequently used packages
Python & Machine Learning Study Memo ③: Neural Network
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Memo about Sphinx Part 1 (Creating a project)
[Machine learning pictorial book] A memo when performing the Python exercise at the end of the book while checking the data
Python learning memo for machine learning by Chainer until the end of Chapter 2
Creating a development environment for machine learning
Python & Machine Learning Study Memo ⑥: Number Recognition
"Deep Learning from scratch" Self-study memo (Part 12) Deep learning
Judgment of igneous rock by machine learning ②
EV3 x Pyrhon Machine Learning Part 3 Classification
Free version of DataRobot! ?? Introduction to "PyCaret", a library that automates machine learning
Predict short-lived works of Weekly Shonen Jump by machine learning (Part 2: Learning and evaluation)
A story about achieving a horse racing recovery rate of over 100% through machine learning
Machine learning learned by a high school graduate system engineer at Coursera (laps 1-2)
Summary of linux command techniques that I knew when I was a fledgling engineer
Predict short-lived works of Weekly Shonen Jump by machine learning (Part 1: Data analysis)
A memorandum of method often used in machine learning using scikit-learn (for beginners)
A memo of a tutorial on running python on heroku