1. Linear Hypothesis
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
if tf.__version__ >= '2.17.0':
from tf_keras import optimizers
else:
from tensorflow.keras import optimizers
print("NumPy Version :{}".format(np.__version__))
print("TensorFlow Version :{}".format(tf.__version__))
print("Matplotlib Version :{}".format(plt.matplotlib.__version__))
#########################################################################################
# 데이터 전처리 -- Linear Equation / MSE
#########################################################################################
#Input(AGE) and Labels(BP)
x_input = tf.constant([[25,22],[25,26],[25,30],[35,22],[35,26],[35,30],[45,22],[45,26],[45,30],[55,22],[55,26],[55,30],[65,22],[65,26],[65,30],[73,22],[73,26],[73,30]], dtype= tf.float32)
labels = tf.constant([[118],[125],[130],[118],[126],[123],[120],[124],[130],[122],[125],[130],[127],[130],[130],[125.5],[130],[138]], dtype= tf.float32)
W = tf.Variable(tf.random.normal((2, 1)), dtype=tf.float32)
B = tf.Variable(tf.random.normal(()), dtype=tf.float32)
# Min Max Scaler
x_input_org = x_input # 나중에 원본데이터 확인을 위해 백업
x_min, x_max = np.min(x_input, axis=0), np.max(x_input, axis=0)
x_input = (x_input-x_min)/(x_max-x_min)
def Hypothesis(x):
return tf.matmul(x, W) + B
def Cost():
return tf.reduce_mean(tf.square(Hypothesis(x_input)-labels))
#########################################################################################
# Training
#########################################################################################
# Parameter Set
epochs = 5000
learning_rate = 0.01
optimizer = optimizers.SGD(learning_rate=learning_rate)
training_idx = np.arange(0, epochs+1, 1)
cost_graph = np.zeros(epochs+1)
for cnt in range(0, epochs+1):
cost_graph[cnt] = Cost()
if cnt % (epochs//20) == 0:
print("[{:>6}] cost = {:>10.4}, W = [ {:>7.4} {:>7.4} ], B = {:>7.4}".format(cnt, cost_graph[cnt], W[0,0], W[1,0], B.numpy()))
optimizer.minimize(Cost,[W, B])
#########################################################################################
# Inference
#########################################################################################
# predict
def predict(x):
return Hypothesis((x-x_min)/(x_max-x_min))
# predict
x_test = tf.constant([[50.0, 25.0]], dtype= tf.float32)
H_x = predict(x_test)
print("\n[ Prediction by specific data ]")
print("Age : {}, BMI : {} = > BP : {:>7.4}".format(x_test[0,0],x_test[0,1],H_x[0,0]))
#########################################################################################
# Cost-Epoch Plot
#########################################################################################
# Training 상황에 대한 그래프 출력
# Training 회수 별 Cost 값
plt.title("'Cost / Epochs' Graph")
plt.xlabel("Epochs")
plt.ylabel("Cost")
plt.plot(training_idx, cost_graph)
plt.xlim(0, epochs)
plt.grid(True)
plt.semilogy()
plt.show()
2. Logistic Hypothesis
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
if tf.__version__ >= '2.17.0':
from tf_keras import optimizers
else:
from tensorflow.keras import optimizers
print("NumPy Version :{}".format(np.__version__))
print("TensorFlow Version :{}".format(tf.__version__))
print("Matplotlib Version :{}".format(plt.matplotlib.__version__))
#########################################################################################
# 데이터 전처리 -- Sigmoid / Cross-Entropy
#########################################################################################
#Input(AGE) and Labels(BP)
x_input = tf.constant([[25,22],[25,26],[25,30],[35,22],[35,26],[35,30],[45,22],[45,26],[45,30],[55,22],[55,26],[55,30],[65,22],[65,26],[65,30],[73,22],[73,26],[73,30]], dtype= tf.float32)
labels = tf.constant([[0],[0],[1],[0],[1],[0],[0],[0],[1],[0],[0],[1],[1],[1],[1],[0],[1],[1]], dtype= tf.float32)
W = tf.Variable(tf.random.normal((2, 1)), dtype=tf.float32)
B = tf.Variable(tf.random.normal((1,)), dtype=tf.float32)
# Min Max Scaler
x_input_org = x_input
x_min, x_max = np.min(x_input, axis=0), np.max(x_input, axis=0)
x_input = (x_input-x_min)/(x_max-x_min)
def Hypothesis(x):
return tf.sigmoid(tf.add(tf.matmul(x ,W), B))
def Cost():
return -tf.reduce_mean(labels * tf.math.log(Hypothesis(x_input)) + (1 - labels) * tf.math.log(1 - Hypothesis(x_input)))
#########################################################################################
# Training
#########################################################################################
# Parameter Set
epochs = 10000
learning_rate = 0.1
# 최적화를 위한 optimizer 생성 필요
optimizer = optimizers.SGD(learning_rate=learning_rate)
# W, B 학습을 위한 코드 구현 필요
training_idx = np.arange(0, epochs+1, 1)
cost_graph = np.zeros(epochs+1)
check = np.array([0, epochs*0.01, epochs*0.08, epochs*0.2, epochs*0.4, epochs])
W_trained = []
b_trained = []
check_idx = 0
# 학습 (Training)
for cnt in range(0, epochs+1):
cost_graph[cnt] = Cost()
if cnt % (epochs//20) == 0:
print("[{:>5}] cost = {:>10.4}, W = [[{:>7.4}] [{:>7.4}]], B = [[{:>7.4}]]".format(cnt, cost_graph[cnt], W[0,0], W[1,0], B[0]))
if check[check_idx] == cnt:
W_trained.append(W.numpy())
b_trained.append(B.numpy())
check_idx += 1
optimizer.minimize(Cost,[W, B])
#########################################################################################
# Inference
#########################################################################################
def predict(x):
return Hypothesis((x-x_min)/(x_max-x_min))
# predict
print("\n[ Prediction by specific data ]")
x_test = tf.constant([[50.0, 25.0]], dtype= tf.float32)
H_x = predict(x_test)
print("Age : {}, BMI : {} => Result : {:>7.4}".format(x_test[0,0],x_test[0,1],H_x[0,0]))
#########################################################################################
# Cost-Epoch Plot
#########################################################################################
# Training 상황에 대한 그래프 출력
# Training 회수 별 Cost 값
plt.title("'Cost / Epochs' Graph")
plt.xlabel("Epochs")
plt.ylabel("Cost")
plt.plot(training_idx, cost_graph)
plt.xlim(0, epochs)
plt.grid(True)
plt.semilogy()
plt.show()
# 구분선 그리기
x_decision = np.linspace(-0.2, 1.2, 1000)
fig, axes = plt.subplots(2, 3, figsize=(15, 11))
fig.suptitle("'Hypothesis / Training Count' Graph")
for ax_idx in range(check.size):
W = W_trained[ax_idx]
B = b_trained[ax_idx]
y_decision = -(W[0] * x_decision + B[0])/W[1]
ax = axes[ax_idx // 3][ax_idx % 3]
# label의 값에 따라서 blue 또는 red 점 찍기
for i in range(labels.shape[0]):
if(labels[i][0] == 0):
ax.scatter(x_input[i][0], x_input[i][1], color='blue')
else:
ax.scatter(x_input[i][0], x_input[i][1], color='red')
ax.plot(x_decision, y_decision, label=' Decision Boundary', color='green')
ax.set_title("Epochs : {}".format(check[ax_idx]))
ax.set_xlim((-0.2, 1.2))
ax.set_ylim((-0.2, 1.2))
ax.set_xlabel("x0")
ax.set_ylabel("x1")
ax.grid(True)
ax.legend()
plt.show()
3. Softmax Hypothesis
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
if tf.__version__ >= '2.17.0':
from tf_keras import optimizers
else:
from tensorflow.keras import optimizers
print("NumPy Version :{}".format(np.__version__))
print("TensorFlow Version :{}".format(tf.__version__))
print("Matplotlib Version :{}".format(plt.matplotlib.__version__))
#########################################################################################
# 데이터 전처리 -- Softmax / Cross Entropy Error
#########################################################################################
#Input(AGE) and Labels(BP)
x_input = tf.constant([[25,22],[25,26],[25,30],[35,22],[35,26],[35,30],[45,22],[45,26],[45,30],[55,22],[55,26],[55,30],[65,22],[65,26],[65,30],[73,22],[73,26],[73,30]], dtype= tf.float32)
labels = tf.constant([[1,0,0],[0,1,0],[0,0,1],[1,0,0],[0,1,0],[1,0,0],[1,0,0],[1,0,0],[0,1,0],[1,0,0],[0,1,0],[0,0,1],[0,1,0],[0,0,1],[0,0,1],[0,1,0],[0,0,1],[0,0,1]], dtype= tf.float32)
# W, B 정의
W = tf.Variable(tf.random.normal((2,3)), dtype=tf.float32)
B = tf.Variable(tf.random.normal((3,)), dtype=tf.float32)
x_input_org = x_input
x_min, x_max = np.min(x_input, axis=0), np.max(x_input, axis=0)
x_input = (x_input-x_min)/(x_max-x_min)
# Hypothesis 구현
def logits(x):
return tf.matmul(x, W) + B
def Hypothesis(x):
return tf.nn.softmax(logits(x))
def Cost():
return tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits(x_input), labels=labels))
#########################################################################################
# Training
#########################################################################################
# Parameter Set
epochs = 30000
learning_rate = 0.05
optimizer = optimizers.SGD(learning_rate=learning_rate)
# W, B 학습을 위한 코드 구현 필요
training_idx = np.arange(0, epochs+1, 1)
cost_graph = np.zeros(epochs+1)
for cnt in range(0, epochs+1):
cost_graph[cnt] = Cost()
if cnt % (epochs//20) == 0:
print("[{:>6}] cost = {:>10.4}".format(cnt, cost_graph[cnt]))
optimizer.minimize(Cost,[W, B])
#########################################################################################
# Inference
#########################################################################################
# predict
def predict(x):
return Hypothesis((x-x_min)/(x_max-x_min))
x_test = tf.constant([[50.0, 25.0]], dtype= tf.float32)
H_x = predict(x_test)
print("\n[ Prediction by specific data ]")
print("Age : {}, BMI : {} => Class: {}".format(x_test[0,0],x_test[0,1],np.argmax(H_x[0])),H_x[0].numpy())
#########################################################################################
# Cost-Epoch Plot
#########################################################################################
# Training 상황에 대한 그래프 출력
# Training 회수 별 Cost 값
plt.title("'Cost / Epochs' Graph")
plt.xlabel("Epochs")
plt.ylabel("Cost")
plt.plot(training_idx, cost_graph)
plt.xlim(0, epochs)
plt.grid(True)
plt.semilogy()
plt.show()