반응형
설명
- y = x 방정식 학습 예제
기본 코드
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
model = Sequential()
model.add(Dense(1, input_dim=1, activation='relu'))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
model.fit(x, y, epochs=500, batch_size=1)
loss, acc = model.evaluate(x, y, batch_size=1)
print("loss : ", loss)
print("acc : ", acc)
print(model.predict(x))
loss : 0.0
acc : 0.10000000149011612
[[ 1.]
[ 2.]
[ 3.]
[ 4.]
[ 5.]
[ 6.]
[ 7.]
[ 8.]
[ 9.]
[10.]]
학습 데이터와 테스트 데이터를 나눠 학습 코드
import numpy as np
from keras.layers import Dense
from keras.models import Sequential
x_train = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y_train = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
x_test = np.array([101, 102, 103, 104, 105, 106, 107, 108, 109, 110])
y_test = np.array([101, 102, 103, 104, 105, 106, 107, 108, 109, 110])
model = Sequential()
model.add(Dense(5, input_dim=1, activation='relu'))
model.add(Dense(3))
model.add(Dense(1, activation='relu'))
model.summary()
model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=300, batch_size=1, validation_data=(x_test, y_test))
loss, acc = model.evaluate(x_test, y_test, batch_size=1)
print("loss : ", loss)
print("acc : ", acc)
print(model.predict(x_test))
loss : 0.8712489008903503
acc : 0.0
[[101.89025 ]
[102.899734]
[103.90925 ]
[104.91874 ]
[105.92826 ]
[106.93776 ]
[107.947266]
[108.95677 ]
[109.966286]
[110.97578 ]]
Train, Val, Test 데이터를 준비하여 학습
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
x = np.array(range(1, 101))
y = np.array(range(1, 101))
# Train:Val:Test 데이터 비율을 6:2:2 비율로 세팅
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=66, test_size=0.4, shuffle=False)
x_val, x_test, y_val, y_test = train_test_split(x_test, y_test, random_state=66, test_size=0.5, shuffle=False)
model = Sequential()
# model.add(Dense(5, input_dim=1, activation="relu"))
model.add(Dense(5, input_shape=(1,), activation="relu"))
model.add(Dense(3))
model.add(Dense(4))
model.add(Dense(1))
model.compile(loss="mse", optimizer="adam", metrics=["mse"])
model.fit(x_train, y_train, epochs=300, batch_size=1, validation_data=(x_val, y_val))
mse = model.evaluate(x_test, y_test, batch_size=1)
print("mse : ", mse)
y_predict = model.predict(x_test)
print("y_predict : ", y_predict)
mse : [6.411573938436277e-09, 6.411573938436277e-09]
y_predict : [[ 81.000084]
[ 82.00007 ]
[ 83.00008 ]
[ 84.00008 ]
[ 85.00008 ]
[ 86.00008 ]
[ 87.000084]
[ 88.000084]
[ 89.00008 ]
[ 90.00008 ]
[ 91.000084]
[ 92.000084]
[ 93.000084]
[ 94.00008 ]
[ 95.00008 ]
[ 96.00008 ]
[ 97.00009 ]
[ 98.00009 ]
[ 99.00009 ]
[100.00008 ]]
1입력 1출력 예제
import numpy as np
from keras.layers import Dense, Input
from keras.models import Model
from sklearn.model_selection import train_test_split
x = np.array(range(1, 101))
y = np.array(range(1, 101))
# Train:Val:Test 데이터 비율을 6:2:2 비율로 세팅
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=66, test_size=0.4, shuffle=False)
x_val, x_test, y_val, y_test = train_test_split(x_test, y_test, random_state=66, test_size=0.5, shuffle=False)
input1 = Input(shape=(1,)) # 1 입력
dense1 = Dense(5, activation='relu')(input1)
dense2 = Dense(3)(dense1)
dense3 = Dense(4)(dense2)
output1 = Dense(1)(dense3) # 1 출력
model = Model(inputs=input1, outputs=output1)
# model.summary()
model.compile(loss="mse", optimizer="adam", metrics=["mse"])
model.fit(x_train, y_train, epochs=100, batch_size=1, validation_data=(x_val, y_val))
mse = model.evaluate(x_test, y_test, batch_size=1)
print("mse : ", mse)
y_predict = model.predict(x_test)
print("y_predict : ", y_predict)
mse : [1.65891828052267e-10, 1.65891828052267e-10]
y_predict : [[ 81. ]
[ 82. ]
[ 83. ]
[ 84. ]
[ 85.000015]
[ 86.00001 ]
[ 87. ]
[ 88.00001 ]
[ 89.000015]
[ 90.000015]
[ 91.000015]
[ 92.000015]
[ 93.000015]
[ 94.000015]
[ 95.00001 ]
[ 96.00001 ]
[ 97. ]
[ 98.00003 ]
[ 99.000015]
[100. ]]
N입력 N출력 예제
import numpy as np
from keras.layers import Dense
from keras.models import Sequential
from sklearn.model_selection import train_test_split
x = np.array([range(100), range(301, 401)])
y = np.array([range(100), range(301, 401)])
x = np.transpose(x)
y = np.transpose(y)
# Train:Val:Test 데이터 비율을 6:2:2 비율로 세팅
x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=66, test_size=0.4, shuffle=False)
x_val, x_test, y_val, y_test = train_test_split(x_test, y_test, random_state=66, test_size=0.5, shuffle=False)
model = Sequential()
# model.add(Dense(5, input_dim=2, activation="relu"))
model.add(Dense(5, input_shape=(2,), activation="relu")) # 2 입력
model.add(Dense(3))
model.add(Dense(4))
model.add(Dense(2)) # 2 출력
model.compile(loss="mse", optimizer="adam", metrics=["mse"])
model.fit(x_train, y_train, epochs=300, batch_size=1, validation_data=(x_val, y_val))
mse = model.evaluate(x_test, y_test, batch_size=1)
print("mse : ", mse)
y_predict = model.predict(x_test)
print("y_predict : ", y_predict)
mse : [1.9659782424241712e-07, 1.9659782424241712e-07]
y_predict : [[ 80.0005 380.99988 ]
[ 81.0005 381.99985 ]
[ 82.0005 382.99985 ]
[ 83.00054 383.99985 ]
[ 84.000534 384.99985 ]
[ 85.00054 385.99985 ]
[ 86.000565 386.99985 ]
[ 87.00058 387.99985 ]
[ 88.0006 388.99985 ]
[ 89.00059 389.99985 ]
[ 90.00057 390.99982 ]
[ 91.000626 391.99985 ]
[ 92.00065 392.99982 ]
[ 93.00065 393.99982 ]
[ 94.00063 394.9998 ]
[ 95.00063 395.9998 ]
[ 96.00065 396.9998 ]
[ 97.000694 397.9998 ]
[ 98.00071 398.9998 ]
[ 99.000725 399.9998 ]]
반응형
'Development > Deep Learning' 카테고리의 다른 글
[Deep Learning] LSTM (0) | 2020.12.28 |
---|---|
[Deep Learning] Multi Variable Linear Regression (0) | 2020.12.28 |
[Deep Learning] Linear Regression (0) | 2020.12.28 |
[Deep Learning] 기본 (0) | 2020.12.28 |