-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_regression.py
49 lines (34 loc) · 1.02 KB
/
linear_regression.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from models.linear_model import LinearModel
# 데이터 불러오기
train_data = np.load(".\\datasets\\linear_train.npy")
test_x = np.load(".\\datasets\\linear_test_x.npy")
print(train_data.shape)
# tf 형식에 맞게 변환
x_data=train_data[:,0]
x_data = np.expand_dims(train_data[:,0], axis=1)
y_data = train_data[:,1]
print(x_data[:,0])
# 모델 생성
model = LinearModel(num_units=1)
# 최적화 함수, 손실함수와 모델 바인딩
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),
loss=tf.keras.losses.MSE,
metrics=[tf.keras.metrics.MeanSquaredError()])
# 모델 학습
model.fit(x=x_data,
y=y_data,
epochs=10,
batch_size=32)
# 모델 테스트
prediction = model.predict(x=test_x,
batch_size=None)
# 결과 시각화
plt.scatter(x_data,y_data,s=5,label="train data")
plt.scatter(test_x,prediction,s=5,label="prediction data")
plt.legend()
plt.show()
# 모델 정리
model.summary()