keras 다 중 태 스 크 다 중 loss 반전 에 대한 사고
예 를 들 어 아래:
model = Model(inputs = input, outputs = [y1, y2])
l1 = 0.5
l2 = 0.3
model.compile(loss = [loss1, loss2], loss_weights=[l1, l2], ...)
사실 우리 가 얻 은 loss 는
final_loss = l1 * loss1 + l2 * loss2
우리 의 최종 최적화 효 과 는 final 최소 화 입 니 다.loss。문제 가 생 겼 습 니 다.훈련 과정 에서 loss 2 는 y2 의 네트워크 채널 만 업데이트 하 시 겠 습 니까?아니면 loss 2 는 모든 네트워크 층 을 업데이트 하 시 겠 습 니까?
이 문제 의 관건 은 경사도 반전,즉 역방향 전파 알고리즘 이다.
그래서 loss 1 은 x1 과 x2 에 만 영향 을 미 치고 loss 2 는 x1 과 x3 에 만 영향 을 미친다.
추가:keras 여러 LOSS 총화 정의
사전 형식 으로 이름 은 모델 에서 그 층 의 이름 을 출력 하 는 것 입 니 다.이곳 의 loss 는 스스로 정의 할 수도 있 고 자체 적 으로 가 져 온 것 입 니 다.
추가:keras 실전-다 중 분류 분할 loss 실현
본 논문 의 사례 는 모두 3d 데이터 의 onehot 태그 형식,즉 y 이다.true(batch_size,x,y,z,class_num)
1、dice loss
def dice_coef_fun(smooth=1):
def dice_coef(y_true, y_pred):
# sample dice
intersection = K.sum(y_true * y_pred, axis=(1,2,3))
union = K.sum(y_true, axis=(1,2,3)) + K.sum(y_pred, axis=(1,2,3))
sample_dices=(2. * intersection + smooth) / (union + smooth) # dice
# dice
dices=K.mean(sample_dices,axis=0)
return K.mean(dices) # dice dice
return dice_coef
def dice_coef_loss_fun(smooth=0):
def dice_coef_loss(y_true,y_pred):
return 1-1-dice_coef_fun(smooth=smooth)(y_true=y_true,y_pred=y_pred)
return dice_coef_loss
2、generalized dice loss
def generalized_dice_coef_fun(smooth=0):
def generalized_dice(y_true, y_pred):
# Compute weights: "the contribution of each label is corrected by the inverse of its volume"
w = K.sum(y_true, axis=(0, 1, 2, 3))
w = 1 / (w ** 2 + 0.00001)
# w , ,
# Compute gen dice coef:
numerator = y_true * y_pred
numerator = w * K.sum(numerator, axis=(0, 1, 2, 3))
numerator = K.sum(numerator)
denominator = y_true + y_pred
denominator = w * K.sum(denominator, axis=(0, 1, 2, 3))
denominator = K.sum(denominator)
gen_dice_coef = numerator / denominator
return 2 * gen_dice_coef
return generalized_dice
def generalized_dice_loss_fun(smooth=0):
def generalized_dice_loss(y_true,y_pred):
return 1 - generalized_dice_coef_fun(smooth=smooth)(y_true=y_true,y_pred=y_pred)
return generalized_dice_loss
3、tversky coefficient loss
# Ref: salehi17, "Twersky loss function for image segmentation using 3D FCDN"
# -> the score is computed for each class separately and then summed
# alpha=beta=0.5 : dice coefficient
# alpha=beta=1 : tanimoto coefficient (also known as jaccard)
# alpha+beta=1 : produces set of F*-scores
# implemented by E. Moebel, 06/04/18
def tversky_coef_fun(alpha,beta):
def tversky_coef(y_true, y_pred):
p0 = y_pred # proba that voxels are class i
p1 = 1 - y_pred # proba that voxels are not class i
g0 = y_true
g1 = 1 - y_true
# sample dice
num = K.sum(p0 * g0, axis=( 1, 2, 3))
den = num + alpha * K.sum(p0 * g1,axis= ( 1, 2, 3)) + beta * K.sum(p1 * g0, axis=( 1, 2, 3))
T = num / den #[batch_size,class_num]
# dice
dices=K.mean(T,axis=0) #[class_num]
return K.mean(dices)
return tversky_coef
def tversky_coef_loss_fun(alpha,beta):
def tversky_coef_loss(y_true,y_pred):
return 1-tversky_coef_fun(alpha=alpha,beta=beta)(y_true=y_true,y_pred=y_pred)
return tversky_coef_loss
4、IoU loss
def IoU_fun(eps=1e-6):
def IoU(y_true, y_pred):
# if np.max(y_true) == 0.0:
# return IoU(1-y_true, 1-y_pred) ## empty image; calc IoU of zeros
intersection = K.sum(y_true * y_pred, axis=[1,2,3])
union = K.sum(y_true, axis=[1,2,3]) + K.sum(y_pred, axis=[1,2,3]) - intersection
#
ious=K.mean((intersection + eps) / (union + eps),axis=0)
return K.mean(ious)
return IoU
def IoU_loss_fun(eps=1e-6):
def IoU_loss(y_true,y_pred):
return 1-IoU_fun(eps=eps)(y_true=y_true,y_pred=y_pred)
return IoU_loss
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
KERA의 반복 레이어 시각적 안내서따라서 그림에서 우리는 크기가 4인 숨겨진 상태가 어떻게 RNN 블록 따라서 전체 문장에 대해 우리는 RNN 층에서 크기가 4인 벡터를 출력합니다. 그림에서 보듯이 우리는 무작위 입력 데이터를 만들었는데 그 중 한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.