본문 바로가기

머신러닝공부

Linear Regression 예제 스크립트

728x90
반응형

Linear Rgression Architecture



import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import SGD.Adam
from tensorflow.keras.layers import Flatten, Dense
import numpy as np

# 모델 생성
x_data = np.array([[..], [..], ..., [..]])
t_data = np.array([..])

# 모델 구축
model = Sequential()
model.add(Dense(1, input_shape(3,), activation = 'linear'))

# 모델 컴파일
model.compile(optimizer = SGD(learning_rate = 1e-2), loss = 'mse')
model.summary()

# 모델 학습
hist = model.fit(x_data, t_data, epochs = 1000)

# 모델 평가 및 예측
test_data [[...], [...], ..., [...]]
ret_val = [2*data[0] - 3*data[1] + 2*data[2] for data in test_data]
prediction_val = model.predict(np.array(test_data))

# 모델 입력
print(model.input)
# 모델 출력
print(model.output)
# 모델 가중치
print(model.weight)

# matplotlib를 이용한 시각화
import matplotlib.pyplot as plt

plt.title('Lostt Trend')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.grid()

plt.plot(hist.history['loss'], label = 'trend loss')
plt.legend(loc = 'best')
plt.show()
반응형