[PYTHON] Deep Learning von Grund auf 1-3 Kapitel

1. Einführung in Python


1.2. Installation


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. Perceptron


2.3. Implementierung von Perceptron


2.3.1 Einfache Implementierung

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 Implementierung von Gewichten und Vorurteilen

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 Mehrschichtiges Perceptron


2.5.2 XOR-Gate-Implementierung

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 Vom NAND zum Computer

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. Neuronales Netz


3.1 Vom Perzeptron zum neuronalen Netz


3.1.2 Überprüfung von Perceptron

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 Aktivierungsfunktion einführen

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

3.2 Aktivierungsfunktion


3.2.1 Sigmaid-Funktion

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

3.2.2 Implementierung der Schrittfunktion

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 Schrittfunktionsdiagramm

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 Implementierung der Sigmoidfunktion

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 Vergleich von Sigmoidfunktion und Schrittfunktion

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 Nichtlineare Funktion


3.2.7 ReLU-Funktion

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 Mehrdimensionale Berechnung


3.4 3-lagiges neuronales Netzwerk


3.4.3 Implementierungszusammenfassung

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 Design der Ausgabeschicht


3.5.1 Gleichfunktion und Softmax-Funktion

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 Vorsichtsmaßnahmen für die Implementierung von Softmax

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 Handschriftliche Nummernerkennung


3.6.1 MNIST-Datensatz

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 Inferenzverarbeitung für neuronale Netze

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 Stapelverarbeitung

python neuralnet_mnist_batch.py 

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

Recommended Posts

Deep Learning von Grund auf 1-3 Kapitel
Deep Learning von Grund auf neu
Tiefes Lernen von Grund auf neu (Kostenberechnung)
Deep Learning Memo von Grund auf neu gemacht
[Lernnotiz] Deep Learning von Grund auf neu gemacht [Kapitel 7]
Tiefes Lernen von Grund auf neu (Vorwärtsausbreitung)
Tiefes Lernen / Tiefes Lernen von Grund auf 2-Versuchen Sie, GRU zu bewegen
Deep Learning / Deep Learning von Grund auf neu Kapitel 6 Memo
[Lernnotiz] Deep Learning von Grund auf neu gemacht [Kapitel 5]
[Lernnotiz] Deep Learning von Grund auf neu gemacht [Kapitel 6]
"Deep Learning von Grund auf neu" mit Haskell (unvollendet)
Deep Learning / Deep Learning von Grund auf neu Kapitel 7 Memo
[Windows 10] Aufbau einer "Deep Learning from Scratch" -Umgebung
Lernbericht über das Lesen von "Deep Learning von Grund auf neu"
[Deep Learning von Grund auf neu] Über die Optimierung von Hyperparametern
"Deep Learning from Grund" Memo zum Selbststudium (Teil 12) Deep Learning
[Lernnotiz] Deep Learning von Grund auf neu gemacht [~ Kapitel 4]
"Deep Learning from Grund" Memo zum Selbststudium (Nr. 9) MultiLayerNet-Klasse
Deep Learning von Grund auf neu ① Kapitel 6 "Lerntechniken"
GitHub des guten Buches "Deep Learning von Grund auf neu"
Deep Learning von Grund auf neu Kapitel 2 Perceptron (Memo lesen)
[Lernnotiz] Deep Learning von Grund auf ~ Implementierung von Dropout ~
Python vs Ruby "Deep Learning von Grund auf neu" Zusammenfassung
"Deep Learning from Grund" Memo zum Selbststudium (10) MultiLayerNet-Klasse
"Deep Learning from Grund" Memo zum Selbststudium (Nr. 11) CNN
Deep Learning / LSTM Scratch Code
[Deep Learning von Grund auf neu] Ich habe die Affine-Ebene implementiert
"Deep Learning from Grund" Memo zum Selbststudium (Nr. 19) Datenerweiterung
Anwendung von Deep Learning 2 von Grund auf neu Spam-Filter
Tiefes Lernen
Ich habe versucht, Dropout zu erklären
Deep Learning / Deep Learning von Grund auf neu 2 Kapitel 4 Memo
Deep Learning / Deep Learning von Grund auf neu Kapitel 3 Memo
Deep Learning / Deep Learning von Null 2 Kapitel 5 Memo
Deep Learning / Deep Learning von Null 2 Kapitel 7 Memo
Deep Learning / Deep Learning von Null 2 Kapitel 8 Memo
Deep Learning / Deep Learning von Grund auf neu Kapitel 5 Memo
Deep Learning / Deep Learning von Grund auf neu Kapitel 4 Memo
Deep Learning / Deep Learning von Grund auf neu 2 Kapitel 3 Memo
Deep Learning / Deep Learning von Null 2 Kapitel 6 Memo
Deep Learning Tutorial aus dem Umgebungsbau
[Deep Learning von Grund auf neu] Implementierung der Momentum-Methode und der AdaGrad-Methode
Ein Amateur stolperte in Deep Learning von Grund auf neu Hinweis: Kapitel 1
Ein Amateur stolperte über Deep Learning ❷ von Grund auf neu Hinweis: Kapitel 5
Ein Amateur stolperte über Deep Learning ❷ von Grund auf neu Hinweis: Kapitel 2
Erstellen Sie mit Docker eine Umgebung für "Deep Learning von Grund auf neu"
Ein Amateur stolperte in Deep Learning von Grund auf neu Hinweis: Kapitel 3
Ein Amateur stolperte in Deep Learning von Grund auf neu. Hinweis: Kapitel 7
Ein Amateur stolperte in Deep Learning von Grund auf neu Hinweis: Kapitel 5
Ein Amateur stolperte über Deep Learning ❷ von Grund auf neu Hinweis: Kapitel 1
Ein Amateur stolperte über Deep Learning ❷ von Grund auf neu Hinweis: Kapitel 4
Selbststudien-Memo "Deep Learning from Grund" (Nr. 18) Eins! Miau! Grad-CAM!
Ein Amateur stolperte in Deep Learning von Grund auf neu Hinweis: Kapitel 4
Ein Amateur stolperte in Deep Learning von Grund auf neu Hinweis: Kapitel 2
Ich habe versucht, Perceptron Teil 1 [Deep Learning von Grund auf neu] zu implementieren.
Selbststudien-Memo "Deep Learning from Grund" (Nr. 15) TensorFlow-Anfänger-Tutorial
[Deep Learning von Grund auf neu] Methoden zur Aktualisierung der wichtigsten Parameter für neuronale Netze