시작하기무료로 시작하기

사용자 정의 손실 함수로 신경망 학습하기

이제 방금 만든 사용자 정의 손실 함수를 사용해 보겠습니다. 이를 통해 우리 문제에 맞게 모델의 동작을 유용하게 바꿀 수 있어요 — 최소한 가격 움직임의 방향이라도 맞히도록 모델이 학습하도록 유도할 겁니다. 이제 해야 할 일은 .compile() 함수에서 loss 인자를 함수 이름인 sign_penalty로 설정하는 것뿐이에요. 학습 손실이 충분히 평평해졌는지 다시 확인해 보겠습니다.

이 연습은 강의의 일부입니다

Python으로 배우는 금융 분야 Machine Learning

강의 보기

연습 안내

  • 첫 번째 신경망 레이어의 input_dimscaled_train_features의 열 개수로 설정하세요. .shape[1] 속성을 사용하면 됩니다.
  • 사용자 정의 손실 함수 sign_penalty를 사용해 model_2.compile()하세요.
  • 학습의 history에서 손실을 그래프로 그리세요. 손실 값은 history.history['loss']에 있습니다.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Create the model
model_2 = Sequential()
model_2.add(Dense(100, input_dim=____, activation='relu'))
model_2.add(Dense(20, activation='relu'))
model_2.add(Dense(1, activation='linear'))

# Fit the model with our custom 'sign_penalty' loss function
model_2.compile(optimizer='adam', loss=____)
history = model_2.fit(scaled_train_features, train_targets, epochs=25)
plt.plot(____)
plt.title('loss:' + str(round(history.history['loss'][-1], 6)))
plt.show()
코드 편집 및 실행