[PYTHON] perceptron

Deep Learning from scratch-The theory and implementation of deep learning learned in Python Chapter 3 code

Inner product (dot product)

#Number of columns in the first dimension of matrix A(3)And the number of rows in the 0th dimension of matrix B(3)Is the same
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[1,2],[3,4],[5,6]])
print(a.shape) # (2, 3)With the number of columns in the first dimension
print(b.shape) # (3, 2)Matching the number of rows in the 0th dimension is required
c = np.dot(a,b)
print(c)
print(c.shape) # (2, 2)Number of rows in A, number of columns in B
>> 
	[[22 28]
	 [49 64]]

output


e = np.dot(b,a)
print(e)
>>
	[[ 9 12 15]
	 [19 26 33]
	 [29 40 51]]

Exponential function (exp)

a = np.array([1,2,3,4,5])
b = np.exp(a)
>>>
	array([   2.71828183,    7.3890561 ,   20.08553692,   54.59815003,
        148.4131591 ])

Napier number e=2.71828183

Sigmoid function

Smoothed step function

h(x)=\frac{1}{1+\exp(-x)}\

Implementation


def sigmoid(x):
    return 1 / (1 + np.exp(-x))

output


x = np.arange(-10,10,1)
y = sigmoid(x)
>>>
	array([  4.53978687e-05,   1.23394576e-04,   3.35350130e-04,
         9.11051194e-04,   2.47262316e-03,   6.69285092e-03,
         1.79862100e-02,   4.74258732e-02,   1.19202922e-01,
         2.68941421e-01,   5.00000000e-01,   7.31058579e-01,
         8.80797078e-01,   9.52574127e-01,   9.82013790e-01,
         9.93307149e-01,   9.97527377e-01,   9.99088949e-01,
         9.99664650e-01,   9.99876605e-01])

Softmax function

y_k=\frac{\exp(a_k)}{\sum_{i=1}^{n}\exp(a_i)}

Implementation


def softmax(a):
    c = np.max(a) #Overflow measures
    exp_a = np.exp(a - c)
    sum_a = np.sum(exp_a)
    return exp_a / sum_a

output


a = np.array([1,2,3,4,5])
softmax(a)
>>>
	array([ 0.01165623,  0.03168492,  0.08612854,  0.23412166,  0.63640865])

Sample code

Easy-to-use functions ・ Regression problem: identity function ・ Classification problem: Softmax function

load_mnist is one of the generalization functions summarized below https://github.com/oreilly-japan/deep-learning-from-scratch/blob/master/dataset/mnist.py

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

def get_data():
    # flatten :Make a one-dimensional array
    # normalize : 0 - 1
    # one-hot :1 if True Assign to 0 if False
    (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False)
    # (Training data,Training label) (test data,Test label)
    return x_test, t_test

def init_network():
    #pickle Save a running object as a file
    with open("sample_weight.pkl", 'rb') as f:
        network = pickle.load(f)
    return network

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

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

#Input layer: 784->Output layer: 10
print(x.shape) # (10000, 784)
print(network['W1'].shape) # (784, 50)
print(network['W2'].shape) # (50, 100)
print(network['W3'].shape) # (100, 10)

print(network['b1'].shape) # (50,)
print(network['b2'].shape) # (100,)
print(network['b3'].shape) # (10,)

batch_size = 100 #Number of batches
accuracy_cnt = 0

#Take out 100 badges at a time (speed up calculation)
for i in range(0, len(x), batch_size):
    x_batch = x[i:i+batch_size] # x[0:100], x[100:200] ...
    y_batch = predict(network, x_batch)
    p = np.argmax(y_batch, axis=1) #Of the most probable factors'index'Get
    accuracy_cnt += np.sum(p == t[i:i+batch_size])

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

Reference Machine learning that even high school graduates can understand (2) Simple perceptron Introduction to Neural Networks Try building a simple perceptron with Python

Recommended Posts

perceptron
Perceptron thinking in covector
Perceptron basics and implementation
Basics of Perceptron Foundation
Machine Learning_Nonlinearize Simple Perceptron