-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
108 lines (75 loc) · 2.34 KB
/
functions.py
File metadata and controls
108 lines (75 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# coding: utf-8
import numpy as np
def identity_function(x):
return x
def step_function(x):
return np.array(x > 0, dtype=np.int)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_grad(x):
return (1.0 - sigmoid(x)) * sigmoid(x)
def relu(x):
return np.maximum(0, x)
def relu_grad(x):
grad = np.zeros(x)
grad[x>=0] = 1
return grad
def softmax(x):
if x.ndim == 2:
x = x.T
x = x - np.max(x, axis=0)
y = np.exp(x) / np.sum(np.exp(x), axis=0)
return y.T
x = x - np.max(x) # 오버플로우 대책
return np.exp(x) / np.sum(np.exp(x))
def mean_squared_error(y, t):
return 0.5 * np.sum((y-t)**2)
def cross_entropy_error(y, t):
if y.ndim == 1:
t = t.reshape(1, t.size)
y = y.reshape(1, y.size)
# 훈련 데이터가 원-핫 벡터라면 정답 레이블의 인덱스로 반환
if t.size == y.size:
t = t.argmax(axis=1)
batch_size = y.shape[0]
return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size
def softmax_loss(X, t):
y = softmax(X)
return cross_entropy_error(y, t)
def cross_entropy_error(y, t):
if y.ndim == 1:
t = t.reshape(1, t.size)
y = y.reshape(1, y.size)
batch_size = y.shape[0]
result = np.sum(t * -np.log(y + np.finfo(float).eps))# / batch_size
return result
def cross_entropy_error_label(y, t_label):
epsilon = 1e-7
if y.ndim == 1:
y = y.reshape(1, y.size)
t_label = t_label.reshape(1, t_label.size)
batch_size = y.shape[0]
result = -np.sum(np.log(y[np.arange(batch_size), t_label])) / batch_size
return result
# 수치미분 함수
def numerical_difference(f, x):
h = 1e-4
return (f(x+h)-f(x-h)) / (2*h)
# 편미분 함수
def numerical_gradient(f, x):
h = 1e-4
grad = np.zeros_like(x) # x와 같은 형상의 배열 생성
for i in range(x.size):
xi = x[i]
x[i] = xi + h
fx1 = f(x)
x[i] = xi - h
fx2 = f(x)
grad[i] = (fx1-fx2) / (2*h)
x[i] = xi
return grad
def gradient_descent(f, init_x, lr=0.1, epoch=100):
x = init_x
for i in range(epoch):
x -= lr * numerical_gradient(f, x)
return x