728x90
Sung Kim님(김성훈 교수님)의 유튜브를 보고 공부한 AI 입니다.
Sung Kim님(김성훈 교수님)의 유튜브 주소 : www.youtube.com/channel/UCML9R2ol-l0Ab9OXoNnr7Lw
Sung Kim
컴퓨터 소프트웨어와 딥러닝, 영어등 다양한 재미있는 이야기들을 나누는 곳입니다.
www.youtube.com
import tensorflow as tf
#tensorflow 사용을 위해 import
import numpy as np
#numpy 사용을 위해 import
tensorflow와 배열생성을위한 numpy를 import 합니다.
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
#학습에 사용할 Data set 구성
학습에 사용할 data set을 구성합니다. 현재는 예시이므로 간단하게 구성했습니다.
tf.model = tf.keras.Sequential() #1입력 1출력에 적합한 Sequential모델 사용
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=1))
#layer에 units은 output shpae을 지정, input_dim은 input shape지정
sgd = tf.keras.optimizers.SGD(lr=0.1)
#SGD는 학습 결과가 최소값에 접급하기 위한 알고리즘, lr은 학습 비율
tf.model.compile(loss='mse', optimizer=sgd)
#loss는 mean_squared_error 즉 평균제곱 에러를 주고
#optimizer로는 sgd를 주어 변수가 sgd에 따라 변화 할 수 있게 하는것
#model을 컴파일 하여 완성
tf.model.summary()
#완성된 모델을 확인
모델 종류를 먼저 선택한 후 layer의 shape을 지정합니다.
그런다음 sgd를 이용해 학습 결과가 최소값에 접근하기 위한 알고리즘을 지정합니다.
그 후 모델을 컴파일하는데 loss와 optimizer를 지정하여 컴파일 후 모델을 완성합니다.
tf.model.fit(x_train, y_train, epochs=200)
#모델에 Data Set을 넣어 훈련하는 과정으로 epochs를 200으로 지정하여 200번 수행
y_predict = tf.model.predict(np.array([5,4]))
#훈련된 모델에 input을 넣어 결과를 얻어내는과정
print(y_predict)
#나온 결과를 프린트
완성된 모델에 data set을 지정하여 훈련을 시키는데 epochs로 훈련횟수를 지정합니다.
학습된 모델에 predict를 이용하여 결과를 얻어낼 수 있습니다.
최종 코드
import tensorflow as tf
import numpy as np
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
tf.model = tf.keras.Sequential()
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=1))
sgd = tf.keras.optimizers.SGD(lr=0.1)
tf.model.compile(loss='mse', optimizer=sgd)
tf.model.summary()
tf.model.fit(x_train, y_train, epochs=200)
y_predict = tf.model.predict(np.array([5,4]))
print(y_predict)
예제 출처 : github.com/hunkim/DeepLearningZeroToAll/blob/master/tf2/tf2-02-1-linear_regression.py
'학습(구) > AI' 카테고리의 다른 글
Linear Regression minimize cost 실습 (0) | 2020.09.07 |
---|---|
Linear Regression minimize cost (0) | 2020.09.06 |
Linear Regression (0) | 2020.09.06 |
TensorFlow의 기본/jupyter notebook 설치 (0) | 2020.09.06 |
ML(Machine Learning)의 기본 (0) | 2020.09.06 |