Machine learning memo of a fledgling engineer Part 1

Introduction

This is part 1 of the learning memo of "Deep-Learning made from scratch".

Slice operation that is easy to forget

slice


x=[1,2,3,4,5]
#Full display
x[:]
#1 to 2
x[0:2]
#Skip two of 1, 3 and 5
x[::2]
#Reverse order,-If set to 2, skip two from the opposite
x[::-1]

Numpy shape match

numpy


#3x2 shape
A = np.array([[1,2], 
              [3,4],
              [5,6]])
#2 x 1 shape
B = np.array([7,
              8])
#Inner product calculation
np.dot(A,B)
>>>array([23,53,83])

Neural network implementation (Chapter 3)

mount


from google.colab import drive
drive.mount('/content/drive')

cd drive/My Drive/deep-learning-from-scratch-master/ch03

neuralnet


# coding: utf-8
import sys, os
sys.path.append(os.pardir)  #Settings for importing files in the parent directory
import numpy as np
import pickle
from dataset.mnist import load_mnist
from common.functions import sigmoid, softmax

neuralnet


def get_data():
    (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False)
    return x_test, t_test

neuralnet


def init_network():
    with open("sample_weight.pkl", 'rb') as f:
        network = pickle.load(f)
    return network

neuralnet


def predict(network, x):
    W1, W2, W3 = network['W1'], network['W2'], network['W3']
    b1, b2, b3 = network['b1'], network['b2'], network['b3']

    a1 = np.dot(x, W1) + b1
    z1 = sigmoid(a1)
    a2 = np.dot(z1, W2) + b2
    z2 = sigmoid(a2)
    a3 = np.dot(z2, W3) + b3
    y = softmax(a3)

    return y

neuralnet


x, t = get_data()
network = init_network()
accuracy_cnt = 0
for i in range(len(x)):
    y = predict(network, x[i])
    p= np.argmax(y) #Get the index of the most probable element
    if p == t[i]:
        accuracy_cnt += 1

print("Accuracy:" + str(float(accuracy_cnt) / len(x)))

Batch processing (continued in Chapter 3)

Batch processing


x, t = get_data()
network = init_network()

batch_size = 100 #Number of batches
accuracy_cnt = 0

#range(start,stop,step)
for i in range(0, len(x), batch_size):
    x_batch = x[i:i+batch_size]
    y_batch = predict(network, x_batch)
    p = np.argmax(y_batch, axis=1)
    accuracy_cnt += np.sum(p == t[i:i+batch_size])

print("Accuracy:" + str(float(accuracy_cnt) / len(x)))

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
Python & Machine Learning Study Memo ②: Introduction of Library
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
Installation of TensorFlow, a machine learning library from Google
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
Implementation of a model that predicts the exchange rate (dollar-yen rate) by machine learning