์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- ์ ๋ฝ ๊ตํํ์
- ๋ฏธ๋์์ ํด์ธ๊ตํ
- m1 anaconda ์ค์น
- special method
- ํน๋ณ ๋ฉ์๋
- ์ ๋ฝ
- Deeplearning
- fluent python
- anaconda ๊ฐ์ํ๊ฒฝ
- ๋ฏธ๋์์ ์ฅํ์
- set add
- ์ ํํ๊ท
- Python
- li-ion
- Machine learning
- gradient descent
- 2022๋
- ์ค์คํธ๋ฆฌ์
- cost function
- ์๊ทน์ฌ
- ๋์23์ด
- electrochemical models
- Andrew ng
- ๋ฅ๋ฌ๋
- ์ฒญ์ถ ํ์ดํ
- set method
- fatigue fracture
- ์ด์ฐจ์ ์ง
- ๊ตํํ์
- Linear Regression
- Today
- Total
Done is Better Than Perfect
[๋ฅ๋ฌ๋] 3. ๋ฅ๋ฌ๋ ๋ชจ๋ธ ๊ตฌํ (์ ํ ํ๊ท, ๋น์ ํ ํ๊ท ๋ชจ๋ธ ๊ตฌํ) ๋ณธ๋ฌธ
[๋ฅ๋ฌ๋] 3. ๋ฅ๋ฌ๋ ๋ชจ๋ธ ๊ตฌํ (์ ํ ํ๊ท, ๋น์ ํ ํ๊ท ๋ชจ๋ธ ๊ตฌํ)
jimingee 2024. 6. 8. 23:29
' ๋ฅ๋ฌ๋ ๋ชจ๋ธ ๊ตฌํ ์์' ๋ ๋ค์๊ณผ ๊ฐ๋ค.
1. ๋ฐ์ดํฐ์ ์ค๋นํ๊ธฐ
2. ๋ฅ๋ฌ๋ ๋ชจ๋ธ ๊ตฌ์ถํ๊ธฐ
3. ๋ชจ๋ธ ํ์ต ์ํค๊ธฐ
4. ํ๊ฐ ๋ฐ ์์ธกํ๊ธฐ
์๋์์๋ ๊ฐ ๋จ๊ณ์์ ํ์ํ ๊ฐ๋ ์ ์ญ ํ์ด๋ณธ ํ์, tensorflow ์ฝ๋๋ก ์ ํ ํ๊ท์ ๋น์ ํ ํ๊ท๋ฅผ ๊ตฌํํด๋ณด๊ฒ ๋ค.
1. ๋ฐ์ดํฐ์ ์ค๋นํ๊ธฐ
- epoch : ํ ๋ฒ์ epoch๋ ์ ์ฒด ๋ฐ์ดํฐ ์ ์ ๋ํด ํ ๋ฒ ํ์ต์ ์๋ฃํ ์ํ
- batch : ๋๋ ์ง ๋ฐ์ดํฐ์
(๋ณดํต mini-batch๋ผ ํํ)
- iteration๋ epoch๋ฅผ ๋๋์ด์ ์คํํ๋ ํ์๋ฅผ ์๋ฏธํจ

2. ๋ฅ๋ฌ๋ ๋ชจ๋ธ ๊ตฌ์ถํ๊ธฐ
[ keras ์์ ์ฝ๋ - ์๋ ๋๊ฐ๋ ๋์ผํ ์ฝ๋์ ]
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(10,input_dim=2, activation='sigmoid'),
tf.keras.layers.Dense(10, activation='sigmoid'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(10,input_dim=2, activation='sigmoid'))
model.add(tf.keras.layers.Dense(10, activation='sigmoid'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
3. ๋ชจ๋ธ ํ์ต ์ํค๊ธฐ
[model].compile(optimizer, loss) : ๋ชจ๋ธ์ ํ์ต์ ์ค์ ํ๊ธฐ ์ํ ํจ์
- optimizer : ๋ชจ๋ธ ํ์ต ์ต์ ํ ๋ฐฉ๋ฒ
- loss : ์์ค ํจ์ ์ค์
[model].fit(x,y) : ๋ชจ๋ธ์ ํ์ต์ํค๊ธฐ ์ํ ํจ์
- x : ํ์ต ๋ฐ์ดํฐ
- y : ํ์ต ๋ฐ์ดํฐ์ label
[ ์์ ์ฝ๋ ]
model.compile(loss='mean_squared_error', optimizer='SGD')
model.fit(dataset, epochs=100)
4. ํ๊ฐ ๋ฐ ์์ธกํ๊ธฐ
[model].evaluate(x,y) : ๋ชจ๋ธ์ ํ๊ฐํ๊ธฐ ์ํ ํจ์
- x : ํ ์คํธ ๋ฐ์ดํฐ
- y : ํ ์คํธ ๋ฐ์ดํฐ์ label
[model].predict(x) : ๋ชจ๋ธ๋ก ์์ธก์ ์ํํ๊ธฐ ์ํ ํจ์
- x : ์์ธกํ๊ณ ์ ํ๋ ๋ฐ์ดํฐ
[ ์์ ์ฝ๋ ]
# ํ
์คํธ ๋ฐ์ดํฐ ์ค๋นํ๊ธฐ
dataset_test = tf.data.Dataset.from_tensor_slices((data_test, labels_test))
dataset_test = dataset.batch(32)
# ๋ชจ๋ธ ํ๊ฐ ๋ฐ ์์ธกํ๊ธฐ
model.evaluate(dataset_test)
predicted_labels_test = model.predict(data_test)
๋ฅ๋ฌ๋ ๋ชจ๋ธ ๊ตฌํ
tensorflow ์ฝ๋๋ก ์ ํ ํ๊ท์ ๋น์ ํ ํ๊ท๋ฅผ ๊ตฌํ
1. ์ ํ ํ๊ท ๋ชจ๋ธ ๊ตฌํ
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import os
np.random.seed(100)
'''
1. ์ ํ ํ๊ท ๋ชจ๋ธ์ ํด๋์ค๋ฅผ ๊ตฌํ
Step01. ๊ฐ์ค์น ์ด๊ธฐ๊ฐ์ 1.5์ ๊ฐ์ ๊ฐ์ง ๋ณ์ ํ
์๋ก ์ค์
Step02. Bias ์ด๊ธฐ๊ฐ์ 1.5์ ๊ฐ์ ๊ฐ์ง ๋ณ์ ํ
์๋ก ์ค์
Step03. W, X, b๋ฅผ ์ฌ์ฉํด ์ ํ ๋ชจ๋ธ ๊ตฌํ
'''
class LinearModel:
def __init__(self):
self.W = tf.Variable(initial_value=1.5)
self.b = tf.Variable(initial_value=1.5)
def __call__(self, X, Y):
return X * self.W + self.b
''' 2. MSE๋ฅผ loss function์ผ๋ก ์ฌ์ฉ '''
def loss(y, pred):
return tf.reduce_mean(tf.square(y-pred))
'''
3. gradient descent ๋ฐฉ์์ผ๋ก ํ์ตํ๋ train ํจ์ ์ ์ - W(๊ฐ์ค์น)์ b(Bias) ์
๋ฐ์ดํธ
'''
def train(linear_model, x, y):
with tf.GradientTape() as t:
current_loss = loss(y, linear_model(x, y))
learning_rate = 0.001
# gradient ๊ฐ ๊ณ์ฐ
delta_W, delta_b = t.gradient(current_loss, [linear_model.W, linear_model.b])
# learning rate์ ๊ณ์ฐํ gradient ๊ฐ์ ์ด์ฉํ์ฌ ์
๋ฐ์ดํธํ ํ๋ผ๋ฏธํฐ ๋ณํ ๊ฐ ๊ณ์ฐ
W_update = (learning_rate * delta_W)
b_update = (learning_rate * delta_b)
return W_update,b_update
def main():
# ๋ฐ์ดํฐ ์์ฑ
x_data = np.linspace(0, 10, 50)
y_data = 4 * x_data + np.random.randn(*x_data.shape)*4 + 3
# ๋ฐ์ดํฐ ์ถ๋ ฅ
plt.scatter(x_data,y_data)
plt.show()
# ์ ํ ํจ์ ์ ์ฉ
linear_model = LinearModel()
epochs = 100
for epoch_count in range(epochs): # epoch ๊ฐ๋งํผ ๋ชจ๋ธ ํ์ต
# ์ ํ ๋ชจ๋ธ์ ์์ธก ๊ฐ ์ ์ฅ
y_pred_data = linear_model(x_data, y_data)
# ์์ธก ๊ฐ๊ณผ ์ค์ ๋ฐ์ดํฐ ๊ฐ๊ณผ์ loss ํจ์ ๊ฐ ์ ์ฅ
real_loss = loss(y_data, linear_model(x_data, y_data))
# ํ์ฌ์ ์ ํ ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ loss ๊ฐ์ ์ค์ด๋ ์๋ก์ด ํ๋ผ๋ฏธํฐ๋ก ๊ฐฑ์ ํ ํ๋ผ๋ฏธํฐ ๋ณํ ๊ฐ ๊ณ์ฐ
update_W, update_b = train(linear_model, x_data, y_data)
# ์ ํ ๋ชจ๋ธ์ ๊ฐ์ค์น์ Bias ์
๋ฐ์ดํธ
linear_model.W.assign_sub(update_W)
linear_model.b.assign_sub(update_b)
if (epoch_count%20==0):
print(f"Epoch count {epoch_count}: Loss value: {real_loss.numpy()}")
print('W: {}, b: {}'.format(linear_model.W.numpy(), linear_model.b.numpy()))
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(x_data,y_data)
ax1.plot(x_data,y_pred_data, color='red')
plt.savefig('prediction.png')
plt.show()
if __name__ == "__main__":
main()
[ ์ฝ๋ ์คํ ๊ฒฐ๊ณผ ]
- epoch ์งํ๋ ์๋ก loss ๊ฐ์ด ๋จ์ด์ง๋ฏ๋ก, ํ์ต์ด ์ ์ด๋ฃจ์ด์ง๊ณ ์์
- Weight, bias ๊ฐ์ด ์ ๋ฐ์ดํธ ๋จ




## output ##
Epoch count 0: Loss value: 250.49554443359375
W: 1.677997350692749, b: 1.527673602104187
Epoch count 20: Loss value: 28.3988094329834
W: 3.5059425830841064, b: 1.8219205141067505
Epoch count 40: Loss value: 15.571966171264648
W: 3.942619800567627, b: 1.907819151878357
Epoch count 60: Loss value: 14.813202857971191
W: 4.045225143432617, b: 1.9435327053070068
Epoch count 80: Loss value: 14.750727653503418
W: 4.067629814147949, b: 1.9670435190200806
2. ๋น์ ํ ํ๊ท ๋ชจ๋ธ ๊ตฌํ
import tensorflow as tf
import numpy as np
from visual import *
import os
np.random.seed(100)
tf.random.set_seed(100)
def main():
# ๋น์ ํ ๋ฐ์ดํฐ ์์ฑ
x_data = np.linspace(0, 10, 100)
y_data = 1.5 * x_data**2 -12 * x_data + np.random.randn(*x_data.shape)*2 + 0.5
''' 1. ๋ค์ธต ํผ์
ํธ๋ก ๋ชจ๋ธ ์์ฑ '''
# units : ๋ ์ด์ด์์ ๋
ธ๋ ์
# activation : ์ ์ฉํ activation function
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(units=20, input_dim=1, activation='relu'),
tf.keras.layers.Dense(units=20, activation='relu'),
tf.keras.layers.Dense(units=1)
])
''' 2. ๋ชจ๋ธ ํ์ต ๋ฐฉ๋ฒ ์ค์ '''
# ๋ชจ๋ธ์ ํ์ต์ํฌ ์์ค ํจ์(loss function) ๊ณ์ฐ ๋ฐฉ๋ฒ๊ณผ ์ต์ ํ(optimize) ๋ฐฉ๋ฒ ์ค์
model.compile(loss = 'mean_squared_error', optimizer = 'adam')
''' 3. ๋ชจ๋ธ ํ์ต '''
# ์์ฑํ ๋ชจ๋ธ์ 500 epochs ๋งํผ ํ์ต์ํด. verbose๋ ๋ชจ๋ธ ํ์ต ๊ณผ์ ์ ๋ณด๋ฅผ ์ผ๋ง๋ ์์ธํ ์ถ๋ ฅํ ์ง๋ฅผ ์ค์ ํจ
history = model.fit(x_data, y_data, epochs=500, verbose=2)
''' 4. ํ์ต๋ ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์์ธก๊ฐ ์์ฑ ๋ฐ ์ ์ฅ '''
# ํ์ตํ ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ x_data์ ๋ํ ์์ธก๊ฐ ์์ฑ
predictions = model.predict(x_data)
Visualize(x_data, y_data, predictions)
return history, model
if __name__ == '__main__':
main()
[ ์ฝ๋ ์คํ ๊ฒฐ๊ณผ ]
- epoch ์งํ๋ ์๋ก loss ๊ฐ์ด ๋จ์ด์ง๋ฏ๋ก, ํ์ต์ด ์ ์ด๋ฃจ์ด์ง๊ณ ์์
- Weight, bias ๊ฐ์ด ์ ๋ฐ์ดํธ ๋จ


Epoch 1/500
100/100 - 0s - loss: 290.6330
Epoch 250/500
100/100 - 0s - loss: 86.8522
Epoch 500/500
100/100 - 0s - loss: 15.2892