Multi-variable linear regression 실습
Sung Kim님(김성훈 교수님)의 유튜브를 보고 공부한 AI 입니다.
Sung Kim님(김성훈 교수님)의 유튜브 주소 : www.youtube.com/channel/UCML9R2ol-l0Ab9OXoNnr7Lw
Sung Kim
컴퓨터 소프트웨어와 딥러닝, 영어등 다양한 재미있는 이야기들을 나누는 곳입니다.
www.youtube.com
import tensorflow as tf
import numpy as np
x_data = [[73., 80., 75.],
[93., 88., 93.],
[89., 91., 90.],
[96., 98., 100.],
[73., 66., 70.]]
y_data = [[152.],
[185.],
[180.],
[196.],
[142.]]
모델생성과 학습을 하는데 필요한 모듈을 import 시키고 필요한 데이터셋을 준비합니다.
tf.model = tf.keras.Sequential() #Sequential 모델 설정
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=3)) #3개의 input에 결과값은 1개
tf.model.add(tf.keras.layers.Activation('linear')) #linear형태가 디폴트값이란걸 지정
tf.model.compile(loss='mse', optimizer=tf.keras.optimizers.SGD(lr=1e-5))
#모델을 완성하는 과정으로 하강기울기에 1e-5만큼 lr을 설정하여 움직이게 하는 모델을 컴파일 하여 완성
tf.model.summary()
#완성된 모델 확인
모델을 본격적으로 생성하며 multi_variable 이므로 3개의 input에 1나의 output을 지정하고 하강기울기에 1e-5만큼 lr을 설정하여 lr만큼 움직이는 모델을 완성합니다.
history = tf.model.fit(x_data, y_data, epochs=100)
#dataset 적용후 100회 학습
y_predict = tf.model.predict(np.array([[72., 93., 90.]]))
#값을 넣고 예측 값을 확인
print(y_predict)
완성된 모델을 사용하여 학습횟수와 dataset을 적용하여 완성시킵니다.
그런다음 예측하고 싶은 값을 넣고 결과 값을 확인합니다.
최종 코드입니다.
import tensorflow as tf
import numpy as np
x_data = [[73., 80., 75.],
[93., 88., 93.],
[89., 91., 90.],
[96., 98., 100.],
[73., 66., 70.]]
y_data = [[152.],
[185.],
[180.],
[196.],
[142.]]
tf.model = tf.keras.Sequential()
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=3))
tf.model.add(tf.keras.layers.Activation('linear'))
tf.model.compile(loss='mse', optimizer=tf.keras.optimizers.SGD(lr=1e-5))
tf.model.summary()
history = tf.model.fit(x_data, y_data, epochs=100)
y_predict = tf.model.predict(np.array([[72., 93., 90.]]))
print(y_predict)
다음은 dataset을 외부파일로 준비했을 경우입니다.
xy = np.loadtxt('./data-01-test-score.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
#준바한 데이터를 읽어와 x와 y의 형태로 나눕니다.
외부파일을 생성 후 데이터를 읽어와 x와 y 데이터를 준비합니다.
tf.model = tf.keras.Sequential() #Sequential 모델 설정
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=3, activation='linear'))
#3개의 input에 1개의 output이 나오며 linear가 디폴트인 모델을 설정 후 생성합니다.
tf.model.compile(loss='mse', optimizer=tf.keras.optimizers.SGD(lr=1e-5))
#모델을 완성하는 과정으로 하강기울기에 1e-5만큼 lr을 설정하여 움직이게 하는 모델을 컴파일 하여 완성
모델을 본격적으로 생성하며 multi_variable 이므로 3개의 input에 1나의 output을 지정하고 하강기울기에 1e-5만큼 lr을 설정하여 lr만큼 움직이는 모델을 완성합니다.
history = tf.model.fit(x_data, y_data, epochs=2000)
#dataset 적용후 2000회 학습
print("Your score will be ", tf.model.predict([[100, 70, 101]]))
print("Other scores will be ", tf.model.predict([[60, 70, 110], [90, 100, 80]]))
#학습된 모델에 값을 넣어 결과 값을 봅니다.
완성된 모델을 사용하여 학습횟수와 dataset을 적용하여 완성시킵니다.
그런다음 예측하고 싶은 값을 넣고 결과 값을 확인합니다.
최종코드입니다.
import tensorflow as tf
import numpy as np
xy = np.loadtxt('../data-01-test-score.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
tf.model = tf.keras.Sequential()
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=3, activation='linear'))
tf.model.summary()
tf.model.compile(loss='mse', optimizer=tf.keras.optimizers.SGD(lr=1e-5))
history = tf.model.fit(x_data, y_data, epochs=2000)
print("Your score will be ", tf.model.predict([[100, 70, 101]]))
print("Other scores will be ", tf.model.predict([[60, 70, 110], [90, 100, 80]]))
예제 출처1 : github.com/hunkim/DeepLearningZeroToAll/blob/master/tf2/tf2-04-1-multi_variable_linear_regression.py
예제 출처2 : github.com/hunkim/DeepLearningZeroToAll/blob/master/tf2/tf2-04-3-file_input_linear_regression.py