How to define a keras custom loss function in simple mathematical operation

https://blog.csdn.net/xiewenbo/article/details/89255424

I define a custom function my_sigmoid as following:

import math
def my_sigmoid(x):
    a =  1/  ( 1+math.exp( -(x-300)/30 ) )
    return a

And then define a custom loss function called my_cross_entropy:

import keras.backend as K

def my_cross_entropy(y_true, y_pred):
    diff = abs(y_true-y_pred)
    y_pred_transform = my_sigmoid(diff)
    return K.categorical_crossentropy(0, y_pred_transform)

My keras backend is using tensorflow. And the error shows

TypeError: must be real number, not Tensor

I'm not familiar with tensorflow and don't know how to use custom loss.

The following are my model structure and error message:

import keras.backend as K
from keras.models import Sequential
from keras.layers import Conv2D, Dropout, Flatten, Dense

model=Sequential()
model.add(Conv2D(512,(5,X_train.shape[2]),input_shape=X_train.shape[1:4],activation="relu"))
model.add(Flatten())
model.add(Dropout(0.1))
model.add(Dense(100,activation="relu"))
model.add(Dense(100,activation="relu"))
model.add(Dense(50,activation="relu"))
model.add(Dense(10,activation="relu"))
model.add(Dense(1,activation="relu"))
model.compile(optimizer='adam', loss=my_cross_entropy)
model.fit(X_train,Y_train,batch_size = 10,epochs=200,validation_data=(X_test,Y_test))

 

And the shape of X_train and Y_train is : (120, 30, 80, 1) and (120,)

python-3.x tensorflow keras loss-function

shareimprove this question

edited Oct 9 '18 at 8:27

asked Oct 9 '18 at 7:43

Jim Chen

727618

add a comment

1 Answer

activeoldestvotes

 

Change

diff = abs(y_true-y_pred)

into

diff = K.abs(y_true-y_pred)

same for

math.exp()

change that into

K.exp()

abs and Math.exp are functions that cannot handle Tensors. If you still have problems refer to : Custom Loss function Keras Tensorflow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值