LoginSignup
2

More than 5 years have passed since last update.

ゼロから作る Deep Learning 1-3章

Last updated at Posted at 2017-07-17
1 / 45

1. Python入門


1.2. インストール


pyenv

brew info pyenv
brew install pyenv

anaconda

pyenv install --list
pyenv install anaconda3-4.3.1
pyenv global anaconda3-4.3.1

1.5. NumPy

pip install numpy

1.6. MatPlotLib

pip install matplotlib

01.py
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import imread

x = np.arange(0, 6, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, label="sin")
plt.plot(x, y2, label="cos", linestyle="--")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

img = imread('/Users/atsushi/lena.png')
plt.imshow(img)
plt.show()

2. パーセプトロン


2.3. パーセプトロンの実装


2.3.1 簡単な実装

my_and.py
def my_and(x1, x2):
    w1, w2, theta = 0.5, 0.5, 0.8
    tmp = x1 * w1 + x2 * w2
    if tmp <= theta:
        return 0
    else:
        return 1

print('and')
print(my_and(0, 0))
print(my_and(1, 0))
print(my_and(0, 1))
print(my_and(1, 1))

2.3.3 重みとバイアスの実装

and.py
def and_perceptron(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.7
    value = np.sum(w * x) + b
    if value > 0:
        return 1
    else:
        return 0

print('and perceptron')
print(and_perceptron(0, 0))
print(and_perceptron(1, 0))
print(and_perceptron(0, 1))
print(and_perceptron(1, 1))

nand.py
def nand_perceptron(x1, x2):
    x = np.array([x1, x2])
    w = np.array([-0.5, -0.5])
    b = 0.7
    value = np.sum(w * x) + b
    if value > 0:
        return 1
    else:
        return 0

print('nand perceptron')
print(nand_perceptron(0, 0))
print(nand_perceptron(1, 0))
print(nand_perceptron(0, 1))
print(nand_perceptron(1, 1))

or.py
def or_perceptron(x1, x2):
    x = np.array([x1, x2])
    w = np.array([1, 1])
    b = -0.7
    value = np.sum(w * x) + b
    if value > 0:
        return 1
    else:
        return 0

print('or perceptron')
print(or_perceptron(0, 0))
print(or_perceptron(1, 0))
print(or_perceptron(0, 1))
print(or_perceptron(1, 1))

2.5 多層パーセプトロン


2.5.2 XORゲートの実装

xor.py
def xor_perceptron(x1, x2):
    x3 = nand_perceptron(x1, x2)
    x4 = or_perceptron(x1, x2)
    return and_perceptron(x3, x4)

print('xor perceptron')
print(xor_perceptron(0, 0))
print(xor_perceptron(1, 0))
print(xor_perceptron(0, 1))
print(xor_perceptron(1, 1))

2.6 NANDからコンピューターへ

nand_only_xor.py
def nand_only_xor_perceptron(x1, x2):
    x3 = nand_perceptron(x1, x2)
    x4 = nand_perceptron(x1, x3)
    x5 = nand_perceptron(x2, x3)
    return nand_perceptron(x4, x5)

print('nand only xor perceptron')
print(nand_only_xor_perceptron(0, 0))
print(nand_only_xor_perceptron(1, 0))
print(nand_only_xor_perceptron(0, 1))
print(nand_only_xor_perceptron(1, 1))

3. ニューラルネットワーク


3.1. パーセプトロンからニューラルネットワークへ


3.1.2 パーセプトロンの復習

y = h(b + w_1 x_1 + w_2 x_2)
h(x) = \left\{
\begin{array}{ll}
0 & (x \leq 0) \\
1 & (x > 0)
\end{array}
\right.

3.1.3 活性化関数の登場

a = b + w_1 x_1 + w_2 x_2
y = h(a)

3.2 活性化関数


3.2.1 シグモイド関数

h(x) = \frac{1}{1 + \exp(-x)}
(-\infty, \infty) \rightarrow (0,1)

3.2.2 ステップ関数の実装

step_func.py
def step_func(x):
    if x > 0:
        return 1
    else:
        return 0

print('step_function')
print(step_func(-1))
print(step_func(0))
print(step_func(1))
print(step_func(2))

step_function.py
def step_function(x):
    y = x > 0
    return y.astype(np.int)

print('step_function')
print(step_function(np.array([-1, 0, 1, 2])))

3.2.3 ステップ関数のグラフ

step_funtion.py
def step_function(x):
    return np.array(x > 0, dtype = np.int)

x = np.arange(-5.0, 5.0, 0.1)
y = step_function(x)
plt.plot(x, y)
plt.ylim(-0.1, 1.1)
plt.show()

3.2.4 シグモイド関数の実装

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

print('sigmoid')
print(sigmoid(np.array([-1, 0, 1, 2])))

x = np.arange(-5.0, 5.0, 0.1)
y = sigmoid(x)
plt.plot(x, y)
plt.ylim(-0.1, 1.1)
plt.show()

3.2.5 シグモイド関数とステップ関数の比較

diff.py
x = np.arange(-5.0, 5.0, 0.1)
y = sigmoid(x)
z = step_function(x)
plt.plot(x, y)
plt.plot(x, z)
plt.ylim(-0.1, 1.1)
plt.show()

3.2.6 非線形関数


3.2.7 ReLU関数

h(x) = \left\{
\begin{array}{ll}
x & (x > 0) \\
0 & (x \leq 0)
\end{array}
\right.
relu.py
def relu(x):
    return np.maximum(0, x)

print('relu')
print(relu(np.array([-1, 0, 1, 2])))

x = np.arange(-5.0, 5.0, 0.1)
y = relu(x)
plt.plot(x, y)
plt.ylim(-0.1, 5.1)
plt.show()

3.3 多次元の計算


3.4 3層ニューラルネットワーク


3.4.3 実装のまとめ

init_network.py
def init_network():
    return {'W1': np.array([[0.1, 0.3, 0.5], [0.2, 0.4, 0.6]]),
            'b1': np.array([0.1, 0.2, 0.3]),
            'W2': np.array([[0.1, 0.4], [0.2, 0.5], [0.3, 0.6]]),
            'b2': np.array([0.1, 0.2]),
            'W3': np.array([[0.1, 0.3], [0.2, 0.4]]),
            'b3': np.array([0.1, 0.2])}

identity_function.py
def identity_function(x):
    return x

forward.py
def forward(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
    return identity_function(a3)

test.py
network = init_network()
x = np.array([1.0, 0.5])
y = forward(network, x)
print(y)
x = np.array([0.0, 0.1])
y = forward(network, x)
print(y)

3.5 出力層の設計


3.5.1 恒等関数とソフトマックス関数

y_k = \frac{\exp(a_k)}{\exp(a_1) + \cdots + \exp(a_n)} = \frac{\exp(a_k)}{\sum_{i = 1}^{n} \exp(a_i)}
(i) \quad 0 < y_k < 1\\
(ii) \quad y_1 + \cdots + y_n = 1

softmax.py
def softmax(a):
    exp_a = np.exp(a)
    sum_exp_a = np.sum(exp_a)
    return exp_a / sum_exp_a

a = np.array([0.3, 2.0, 4.0])
print(softmax(a))

3.5.2 ソフトマックス実装上の注意

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

softmax.py
def softmax(a):
    c = np.max(a)
    exp_a = np.exp(a - c)
    sum_exp_a = np.sum(exp_a)
    return exp_a / sum_exp_a

a = np.array([100, 1000, 10000])
print(softmax(a))

3.6 手書き数字認識


3.6.1 MNISTデータセット

git clone https://github.com/oreilly-japan/deep-learning-from-scratch.git
cd deep-learning-from-scratch/ch03
python mnist_show.py
hoge
img_show(x_train[1].reshape(28, 28))

3.6.2 ニューラルネットワークの推論処理

python neuralnet_mnist.py
hoge
print(network['W1'])
img_show((x[1] * 255).reshape(28, 28))
predict(network, x[1])
print(t[1])

3.6.3 バッチ処理

python neuralnet_mnist_batch.py 

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2