DNN_1
1.深い学習の基本概念
入力値は

このとき、出力値が任意の数より大きい場合は1を出力し、任意の数より小さい場合は0を出力する.

2. XOR


Xのw,bの和をunit,perceptronと呼ぶ.

各演算を1つのベクトル→マトリクス形式にマージします.

3.学習深度ネットワーク連携
どのようにw,bを自動的に勉強させますか?
f=wx+b,g=wx,f=g+bの場合
wがfに及ぼす影響を理解するために,xがfに及ぼす影響,bがfに対するゼロ荷重を微分した.



誤差を損失と呼び,逆伝搬損失微分によりネットワークを学習した.
chain rule
fはg+bなので、gを微分*w、gを微分

4. Neural Net(NN) for XOR

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import tensorflow as tf
tf.random.set_seed(777) # for reproducibility
x_data = [[0, 0],
[0, 1],
[1, 0],
[1, 1]]
y_data = [[0],
[1],
[1],
[0]]
plt.scatter(x_data[0][0],x_data[0][1], c='red' , marker='^')
plt.scatter(x_data[3][0],x_data[3][1], c='red' , marker='^')
plt.scatter(x_data[1][0],x_data[1][1], c='blue' , marker='^')
plt.scatter(x_data[2][0],x_data[2][1], c='blue' , marker='^')
dataset = tf.data.Dataset.from_tensor_slices((x_data, y_data)).batch(len(x_data))
def preprocess_data(features, labels):
features = tf.cast(features, tf.float32)
labels = tf.cast(labels, tf.float32)
return features, labels
#NN를 통해 XOR 해결
W1 = tf.Variable(tf.random.normal((2,1)), name='weight1')
b1 = tf.Variable(tf.random.normal((1,)), name = 'bias1')
W2 = tf.Variable(tf.random.normal((2, 1)), name='weight2')
b2 = tf.Variable(tf.random.normal((1,)), name='bias2')
W3 = tf.Variable(tf.random.normal((2, 1)), name='weight3')
b3 = tf.Variable(tf.random.normal((1,)), name='bias3')
def neural_net(features):
layer1 = tf.sigmoid(tf.matmul(features, W1) + b1)
layer2 = tf.sigmoid(tf.matmul(features, W2) + b2)
layer3 = tf.concat([layer1, layer2],-1)
layer3 = tf.reshape(layer3, shape = [-1,2])
hypothesis = tf.sigmoid(tf.matmul(layer3, W3) + b3)
return hypothesis
def loss_fn(hypothesis, labels):
cost = -tf.reduce_mean(labels * tf.math.log(hypothesis) + (1 - labels) * tf.math.log(1 - hypothesis))
return cost
optimizer = tf.keras.optimizers.SGD(learning_rate=0.01)
def accuracy_fn(hypothesis, labels):
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, labels), dtype=tf.float32))
return accuracy
def grad(hypothesis, features, labels):
with tf.GradientTape() as tape:
loss_value = loss_fn(neural_net(features),labels)
return tape.gradient(loss_value, [W1, W2, W3, b1, b2, b3])
EPOCHS = 50000
for step in range(EPOCHS):
for features, labels in dataset:
features, labels = preprocess_data(features, labels)
grads = grad(neural_net(features), features, labels)
optimizer.apply_gradients(grads_and_vars=zip(grads,[W1, W2, W3, b1, b2, b3]))
if step % 5000 == 0:
print("Iter: {}, Loss: {:.4f}".format(step, loss_fn(neural_net(features),labels)))
x_data, y_data = preprocess_data(x_data, y_data)
test_acc = accuracy_fn(neural_net(x_data),y_data)
print("Testset Accuracy: {:.4f}".format(test_acc))

5. ReLU
f(x)=max(0,x):x値が0より大きい場合はxを抽出し、0より小さい場合は0を抽出する
消失勾配:勾配が消失、すなわち0に近づき、ネットワークが伝送できない

Reference
この問題について(DNN_1), 我々は、より多くの情報をここで見つけました https://velog.io/@juliy9812/DNN1テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol