728x90
Sung Kim님(김성훈 교수님)의 유튜브를 보고 공부한 AI 입니다.
Sung Kim님(김성훈 교수님)의 유튜브 주소 : www.youtube.com/channel/UCML9R2ol-l0Ab9OXoNnr7Lw
Sung Kim
컴퓨터 소프트웨어와 딥러닝, 영어등 다양한 재미있는 이야기들을 나누는 곳입니다.
www.youtube.com
import tensorflow as tf
x_data = [[1, 2],
[2, 3],
[3, 1],
[4, 3],
[5, 3],
[6, 2]]
y_data = [[0],
[0],
[0],
[1],
[1],
[1]]
필요한 모듈을 import하고 dataset을 지정합니다.
tf.model = tf.keras.Sequential()
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=2))
#2개의 input에 1개의 output이 나오며 적합한 모델을 설정
tf.model.add(tf.keras.layers.Activation('sigmoid'))
#sigmoid를 기본으로 설정하여 0~1사이의 결과값이 나오게 설정
2개의 input에 1개의 output이 나오게 설정하며 0~1사이의 결과값이 나오게 sigmoid를 기본값으로 설정합니다.
tf.model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.SGD(lr=0.01), metrics=['accuracy'])
#하강기울기로 0.01을 lr로 설정하여 lr만큼 학습되며 평균값이 나오게 설정
tf.model.summary()
#완성된 모델을 요약하여 보여줌
lr을 설정하고 accuracy로 지정하여 평균값이 나오게 설정한 후 완성된 모델을 확인합니다.
history = tf.model.fit(x_data, y_data, epochs=5000)
#dataset을 지정하고 5000회 학습
완성된 모델에 dataset을 지정하고 5000회 학습하여 모델을 학습시킵니다.
print("Accuracy: ", history.history['accuracy'][-1])
#결과값 확인
임의 값을 넣고 결과값을 확인합니다.
최종 코드입니다.
import tensorflow as tf
x_data = [[1, 2],
[2, 3],
[3, 1],
[4, 3],
[5, 3],
[6, 2]]
y_data = [[0],
[0],
[0],
[1],
[1],
[1]]
tf.model = tf.keras.Sequential()
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=2))
tf.model.add(tf.keras.layers.Activation('sigmoid'))
tf.model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.SGD(lr=0.01), metrics=['accuracy'])
tf.model.summary()
history = tf.model.fit(x_data, y_data, epochs=5000)
print("Accuracy: ", history.history['accuracy'][-1])
출처 : github.com/hunkim/DeepLearningZeroToAll/blob/master/tf2/tf2-05-1-logistic_regression.py
'학습(구) > AI' 카테고리의 다른 글
Multinomial classification - SoftMax 실습 (0) | 2020.11.10 |
---|---|
Multinomial classification - SoftMax (0) | 2020.11.10 |
Logistic classification (0) | 2020.10.04 |
Multi-variable linear regression 실습 (0) | 2020.10.04 |
Multivariable linear regression (0) | 2020.09.10 |