keras 升级_在定制的keras损失中使用keras模型

I have a regular keras model called e and I would like to compare its output for both y_pred and y_true in my custom loss function.

from keras import backend as K

def custom_loss(y_true, y_pred):

return K.mean(K.square(e.predict(y_pred)-e.predict(y_true)), axis=-1)

I am getting the error: AttributeError: 'Tensor' object has no attribute 'ndim'

This is because y_true and y_pred are both tensor object and keras.model.predict expects to be passed a numpy.array.

Any idea how I may succeed in using my keras.model in my custom loss function?

I am open to getting the output of a specified layer if need be or to converting my keras.model to a tf.estimator object (or anything else).

解决方案

First, let's try to understand the error message you're getting:

AttributeError: 'Tensor' object has no attribute 'ndim'

Let's take a look at the Keras documentation and find the predict method of Keras model. We can see the description of the function parameters:

x: the input data, as a Numpy array.

So, the model is trying to get a ndims property of a numpy array, because it expects an array as input. On other hand, the custom loss function of the Keras framework gets tensors as inputs. So, don't write any python code inside it - it will never be executed during evaluation. This function is just called to construct the computational graph.

Okay, now that we found out the meaning behind that error message, how can we use a Keras model inside custom loss function? Simple! We just need to get the evaluation graph of the model.

Update

The use of global keyword is a bad coding practice. Also, now in 2020 we have better functional API in Keras that makes hacks with layers uneccessary. Better use something like this:

from keras import backend as K

def make_custom_loss(model):

"""Creates a loss function that uses `model` for evaluation

"""

def custom_loss(y_true, y_pred):

return K.mean(K.square(model(y_pred) - model(y_true)), axis=-1)

return custom_loss

custom_loss = make_custom_loss(e)

Deprecated

Try something like this (only for Sequential models and very old API):

def custom_loss(y_true, y_pred):

# Your model exists in global scope

global e

# Get the layers of your model

layers = [l for l in e.layers]

# Construct a graph to evaluate your other model on y_pred

eval_pred = y_pred

for i in range(len(layers)):

eval_pred = layers[i](eval_pred)

# Construct a graph to evaluate your other model on y_true

eval_true = y_true

for i in range(len(layers)):

eval_true = layers[i](eval_true)

# Now do what you wanted to do with outputs.

# Note that we are not returning the values, but a tensor.

return K.mean(K.square(eval_pred - eval_true), axis=-1)

Please note that the code above is not tested. However, the general idea will stay the same regardless of the implementation: you need to construct a graph, in which the y_true and y_pred will flow through it to the final operations.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值