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

# 1. Python入門

---

## 1.2. インストール

---

### pyenv
```shell-session
brew info pyenv
brew install pyenv
```

### anaconda
```shell-session
pyenv install --list
pyenv install anaconda3-4.3.1
pyenv global anaconda3-4.3.1
```

---

## 1.5. NumPy
```shell-session
pip install numpy
```

---

## 1.6. MatPlotLib
```shell-session
pip install matplotlib
```

---

```python: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 簡単な実装

```python: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 重みとバイアスの実装

```python: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))
```

---

```python: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))
```

---

```python: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ゲートの実装

```python: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からコンピューターへ

```python: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 パーセプトロンの復習

```math
y = h(b + w_1 x_1 + w_2 x_2)
```

```math
h(x) = \left\{
\begin{array}{ll}
0 & (x \leq 0) \\
1 & (x > 0)
\end{array}
\right.
```

---

### 3.1.3 活性化関数の登場

```math
a = b + w_1 x_1 + w_2 x_2
```

```math
y = h(a)
```

---

## 3.2 活性化関数

---

## 3.2.1 シグモイド関数

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

```math
(-\infty, \infty) \rightarrow (0,1)
```

<div align=center>
<img src="https://upload.wikimedia.org/wikipedia/commons/a/ac/Logistic-curve.png" />
</div>

---

## 3.2.2 ステップ関数の実装

```python: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))
```

---

```python: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 ステップ関数のグラフ

```python: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 シグモイド関数の実装

```python: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 シグモイド関数とステップ関数の比較

```python: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関数

```math
h(x) = \left\{
\begin{array}{ll}
x & (x > 0) \\
0 & (x \leq 0)
\end{array}
\right.
```

```python: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 実装のまとめ

```python: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])}
```

---

```python:identity_function.py
def identity_function(x):
    return x
```

---

```python: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)
```

---

```python: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 恒等関数とソフトマックス関数

```math
y_k = \frac{\exp(a_k)}{\exp(a_1) + \cdots + \exp(a_n)} = \frac{\exp(a_k)}{\sum_{i = 1}^{n} \exp(a_i)}
```

```math
(i) \quad 0 < y_k < 1\\
(ii) \quad y_1 + \cdots + y_n = 1
```

---

```python: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 ソフトマックス実装上の注意

```math
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)}
```

---

```python: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データセット

```shell-session
git clone https://github.com/oreilly-japan/deep-learning-from-scratch.git
cd deep-learning-from-scratch/ch03
python mnist_show.py
```

```python:hoge
img_show(x_train[1].reshape(28, 28))
```

---

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

```shell-session
python neuralnet_mnist.py
```

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

---

### 3.6.3 バッチ処理

```shell-session
python neuralnet_mnist_batch.py 
```

---

https://github.com/oreilly-japan/deep-learning-from-scratch