# Y = Wx+B
import numpy as np
import tensorflow as tf
print("NumPy Version :{}".format(np.__version__))
print("TensorFlow Version :{}".format(tf.__version__))
# Input(AGE) and Labels(BP)
x_input = tf.constant([25,25,25,35,35,35,45,45,45,55,55,55,65,65,65,73,73,73], 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)
# Weights
W = tf.Variable(tf.random.normal(()), dtype=tf.float32)
B = tf.Variable(tf.random.normal(()), dtype=tf.float32)
def Hypothesis(x):
# return x*W + B
return tf.add(tf.multiply(x, W), B)
def Cost():
return tf.reduce_mean(tf.square(Hypothesis(x_input)-labels))
#%%time
# Parameter Set
epochs = 150000
learning_rate = 0.0003
optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate)
# 학습 (Training)
for cnt in range(1, epochs+1):
if cnt % (epochs//20) == 0:
print("[{:>5}] cost = {:>10.4}, W = {:>7.4}, B = {:>7.4}".format(cnt, Cost(), W.numpy(), B.numpy()))
optimizer.minimize(Cost,[W, B])
# Predict
def Predict(x):
return Hypothesis(x).numpy()
age = 50.0
print("{} Years : {:>7.4}mmHg".format(age, Predict(age)))
결과
[60000] cost = 18.09, W = 0.2186, B = 115.0
[67500] cost = 17.49, W = 0.1976, B = 116.2
[75000] cost = 17.24, W = 0.1842, B = 116.9
[82500] cost = 17.14, W = 0.1757, B = 117.4
[90000] cost = 17.1, W = 0.1703, B = 117.7
[97500] cost = 17.08, W = 0.1668, B = 117.9
[105000] cost = 17.08, W = 0.1647, B = 118.0
[112500] cost = 17.07, W = 0.1634, B = 118.1
[120000] cost = 17.07, W = 0.1624, B = 118.1
[127500] cost = 17.07, W = 0.162, B = 118.1
[135000] cost = 17.07, W = 0.162, B = 118.1
[142500] cost = 17.07, W = 0.162, B = 118.1
[150000] cost = 17.07, W = 0.162, B = 118.1
50.0 Years : 126.2mmHg
'Tensorflow' 카테고리의 다른 글
Multi in Multi out blood pressure _Tensorflow (0) | 2023.12.28 |
---|---|
Multi variable Linear Regression (Numpy) (1) | 2023.12.27 |
keras 경사하강 기초. (0) | 2023.12.26 |
경사하강법 (0) | 2023.12.26 |
Numpy (0) | 2023.12.26 |