이 문서 의 손실 함수를 TF 2.2에서 구현하려고합니다 ( 이 문서 의 작성자가 만든 TensorFlow 1.10.1의 기존 버전은 여기 에서 찾을 수 있음 ).
그러나 손실 함수의 이론적 세부 사항은 내 문제와 관련이 없습니다.
내 손실 기능 :
def z_score_based_deviation_loss(y_true, y_pred):
confidence_margin = 5.0
ref = K.variable(np.random.normal(loc=0., scale=1.0, size=5000), dtype='float32')
dev = (y_pred - K.mean(ref)) / K.std(ref)
inlier_loss = K.abs(dev)
outlier_loss = K.abs(K.maximum(confidence_margin - dev, 0.))
return K.mean((1 - y_true) * inlier_loss + y_true * outlier_loss)
전화 :
model.compile(optimizer='adam', loss=z_score_based_deviation_loss)
이 오류가 발생했습니다.
ValueError: tf.function-decorated function tried to create variables on non-first call.
이 주제에 대한 다른 질문이 있다는 것을 알고 있지만 Keras의 사용자 지정 손실 함수와 관련된 질문을 찾지 못했습니다. 어떻게 적응해야할지 모르겠습니다.
또한 내가 읽은 것에서 함수에 태그가 지정되면 문제가 존재해야하지만 @tf.function
(어쨌든 계산 속도를 높이기 위해 권장 됨) 추가하지 않았습니다 ...
이런 식으로 시도
def z_score_based_deviation_loss(y_true, y_pred):
confidence_margin = 5.0
ref = tf.random.normal([5000], mean=0., stddev=1.)
dev = (y_pred - K.mean(ref)) / K.std(ref)
inlier_loss = K.abs(dev)
outlier_loss = K.abs(K.maximum(confidence_margin - dev, 0.))
return K.mean((1 - y_true) * inlier_loss + y_true * outlier_loss)
X = np.random.uniform(0,1, (100,10))
y = np.random.uniform(0,1, 100)
x_input = Input((10,))
intermediate = Dense(1, activation='linear', name = 'score')(x_input)
model = Model(x_input, intermediate)
model.compile('adam', z_score_based_deviation_loss)
model.fit(X, y, epochs=10)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다