본문 바로가기

텐서플로공부

텐서플로 정리 3탄

반응형
# 그림 4.5 출력 코드
import math
def sigmoid(x):
    return 1 / (1 + math.exp(-x))

x = np.arange(-5, 5, 0.01)
sigmoid_x = [sigmoid(z) for z in x]
tanh_x = [math.tanh(z) for z in x]
relu = [0 if z < 0 else z for z in x]

plt.axhline(0, color='gray')
plt.axvline(0, color='gray')
plt.plot(x, sigmoid_x, 'b-', label='sigmoid')
plt.plot(x, tanh_x, 'r--', label='tanh')
plt.plot(x, relu, 'g.', label='relu')
plt.legend()
plt.show()

위코드는 3가지 함수를 비교하는 코드이다.

추가된것은 relu이다. 0이하의 숫자는 가중치를 주지않고 이후의 숫자에게만가중치를 주는 기법이다.

결과는 다음과같다.

 

그리고 텐서플로 학습 예제코드는 다음과같다.

 

# 4.18 모델 재정의 및 학습
model = tf.keras.Sequential([
    tf.keras.layers.Dense(units=52, activation='relu', input_shape=(13,)),
    tf.keras.layers.Dense(units=39, activation='relu'),
    tf.keras.layers.Dense(units=26, activation='relu'),
    tf.keras.layers.Dense(units=1)
])

model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.07), loss='mse')

history = model.fit(train_X, train_Y, epochs=25, batch_size=32, validation_split=0.25, callbacks=[tf.keras.callbacks.EarlyStopping(patience=3, monitor='val_loss')])
반응형

'텐서플로공부' 카테고리의 다른 글

텐서플로 정리 4탄  (1) 2024.01.11
텐서플로 정리 3탄  (0) 2024.01.11
텐서플로 정리 2탄  (1) 2024.01.11
텐서플로 정리 1탄  (1) 2024.01.11