keras basics1

¾ÕÀå¿¡¼­ Äɶ󽺸¦ ±×´ë·Î ¹«ÀÛÁ¤ µû¶ó ÇßÁö¸¸ ½ÇÁ¦°ª°ú ¿¹Ãø°ªÀÇ Â÷ÀÌ°¡ ³Ê¹« Å©°Ô ³ª¿Ô´Ù.

¹«ÀÛÁ¤ µû¶óÇÑ °Í - keras basics :tutorial04.html

À̹øÀå¿¡¼­ ½ÇÁ¦°ª°ú ¿¹Ãø°ªÀÇ Â÷À̸¦ ÁÙ¿©º»´Ù.

¾ÕÀå¿¡¼­´Â ¸ðµ¨ ¿¹Ãø±îÁö 6´Ü°è ¿´Áö¸¸ 4´Ü°è·Î ÁÙ¿´´Ù.

1. µ¥ÀÌÅÍ Àüó¸®
2. ¸ðµ¨ ±¸¼º
3. ¸ðµ¨ ÈÆ·Ã
4. ¸ðµ¨ Æò°¡ ¿¹Ãø

# 1. µ¥ÀÌÅÍ Àüó¸®
from keras.models import Sequential
from keras.layers import Dense
import numpy as np

x = np.array([1,2,3,4,5])
y = np.array([1,2,3,4,5])

# 2. ¸ðµ¨ ±¸¼º
model = Sequential()
model.add(Dense(10, input_dim=1, activation='relu'))
model.add(Dense(10))
model.add(Dense(8))
model.add(Dense(1))
 
# 3. ¸ðµ¨ ÈÆ·Ã
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
model.fit(x, y, epochs=100, verbose=0)
 
# 4. ¸ðµ¨ Æò°¡ ¿¹Ãø
loss, mse = model.evaluate(x, y, batch_size=1)
print('acc : ', mse)
 
predict = model.predict(x)
print('y', y, ' predict: \n', predict)

1. µ¥ÀÌÅÍ Àüó¸®

models, layers ¶óÀ̺귯¸®¿¡¼­ Sequential, Dense ¿ÀºêÁ§Æ®¸¦ ÀÓÆ÷Æ® ÇÑ´Ù.
numpyµµ ÀÓÆ÷Æ® ÇÑ´Ù.

numpy·Î x, y¿¡ µ¥ÀÌÅ͸¦ ÀÔ·ÂÇÑ´Ù.

2. ¸ðµ¨ ±¸¼º

model = Sequential()
model.add(Dense(10, input_dim=1, activation='relu'))
model.add(Dense(10))
model.add(Dense(8))
model.add(Dense(1))

Dense ·¹À̾ 4°³ ¸¸µé¾ú´Ù.
Dense ·¹ÀÌ¾î °¹¼ö¿Í Ãâ·Â ³ëµåÀÇ °¹¼ö¿¡ µû¶ó ¿¹Ãø °ªÀÌ  ´Þ¶óÁö´Â°ÍÀ» È®ÀÎ ÇÒ¼ö ÀÖ´Ù.

¿¹) Dense·¹ÀÌ¾î »èÁ¦
     Dense·¹À̾îÀÇ Ãâ·Â ³ëµå °¹¼ö º¯°æ Çغ¸±â

3. ¸ðµ¨ ÈÆ·Ã

model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
model.fit(x, y, epochs=100, verbose=0)

¾ÕÀå ¼³¸íÀ» Âü°í ÇÑ´Ù.

4. ¸ðµ¨ Æò°¡ ¿¹Ãø

loss, mse = model.evaluate(x, y, batch_size=1)
print('acc : ', mse)
 
predict = model.predict(x)
print('y', y, ' predict: \n', predict)

°á°ú)
5/5
[==============================] - 0s 3ms/step - loss: 0.0115 -
accuracy: 0.2000
acc : 0.20000000298023224
1/1 [==============================] - 0s 79ms/step
y [1 2 3 4 5] predict:
[[1.180387 ]
[2.10944 ]
[3.0384932]
[3.9675462]
[4.8965983]]
°ªÀÇ ±Ù»çÄ¡´Â ¿Ã¶ó°¬Áö¸¸ Á¤È®µµ´Â 0.2ÀÌ´Ù.

RMSE¿Í R2µµ °°ÀÌ ±¸ÇØ º¸ÀÚ.

# 1. µ¥ÀÌÅÍ Àüó¸®
from keras.models import Sequential
from keras.layers import Dense
import numpy as np

x = np.array([1,2,3,4,5])
y = np.array([1,2,3,4,5])

# 2. ¸ðµ¨ ±¸¼º
model = Sequential()
model.add(Dense(10, input_dim=1, activation='relu'))
model.add(Dense(10))
model.add(Dense(8))
model.add(Dense(1))
 
# 3. ¸ðµ¨ ÈÆ·Ã
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
model.fit(x, y, epochs=100, verbose=0)
 
# 4. ¸ðµ¨ Æò°¡ ¿¹Ãø
loss, mse = model.evaluate(x, y, batch_size=1)
print('acc : ', mse)
 
predict = model.predict(x)
print('y', y, ' predict: \n', predict)

# RMSE ±¸Çϱâ
from sklearn.metrics import mean_squared_error
def RMSE(y_test, y_predict):
    return np.sqrt(mean_squared_error(y_test, y_predict))
print('RMSE : ', RMSE(y, predict))

# R2 ±¸Çϱâ
from sklearn.metrics import r2_score
r2_predict = r2_score(y, predict)
print('R2 : ', r2_predict)

°á°ú)
5/5 [==============================] - 0s 3ms/step - loss: 0.0115 - accuracy: 0.2000
acc : 0.20000000298023224
1/1 [==============================] - 0s 79ms/step
y [1 2 3 4 5] predict:
[[1.180387 ]
[2.10944 ]
[3.0384932]
[3.9675462]
[4.8965983]]
RMSE : 0.10746486314762292
R2 : 0.9942256515943313
R2 °ªÀº ±¦ÂúÀºµ¥, accuracy °ªÀº ¿©ÀüÈ÷ º°·Î´Ù.

Âü°í)
¿©±â¼­ ¼Ò½º¸¦ °¡Á® ¿Ô´Ù.
https://ebbnflow.tistory.com/125