ValueError:不支持任何值Tensorflow中的Keras自定义丢失功能

时间:2017-01-23 20:29:32

标签: python tensorflow neural-network keras

使用TensorFlow编写的自定义目标函数,我在Keras序列模型的拟合阶段出现以下错误。

File "basicCNN.py", line 110, in <module>
callbacks=[TensorBoard(log_dir="./logs/{}".format(now))])
File "/home/garethjones/.local/lib/python2.7/site-packages/keras/models.py", line 664, in fit
sample_weight=sample_weight)
File "/home/garethjones/.local/lib/python2.7/site-packages/keras/engine/training.py", line 1115, in fit
self._make_train_function()
File "/home/garethjones/.local/lib/python2.7/site-packages/keras/engine/training.py", line 713, in _make_train_function
self.total_loss)
File "/home/garethjones/.local/lib/python2.7/site-packages/keras/optimizers.py", line 391, in get_updates
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 813, in binary_op_wrapper
y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 669, in convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 176, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 165, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 360, in make_tensor_proto
raise ValueError("None values not supported.")

我的自定义功能就是这个

def PAI(y_true, y_pred, k):
    '''
    Args:
        y_true (tensor): (batch x numCells)
        y_pred (tensor): (batch x numCells)
        k: The optimal number of hotspots
        area: 
    Returns:
        cfsRatio (tensor): The inverse of the percentage of crimes in hotspots per observation
    '''
    # Compute total crime for each obs
    totalCFS =  tf.reduce_sum(y_true,  axis=1)  # batch x 1
    # Flatten for gather
    flatTruth = tf.reshape(y_true, [-1])  # 1 x batch * numCells
    # Select top candidate cells
    _, predHS = tf.nn.top_k(y_true, k)
    # Convert indices for gather
    predHSFlat = tf.range(0, tf.shape(y_true)[0]) * tf.shape(y_true)[1] + predHS)
    # Map hotspot predictions to crimes
    hsCFS = tf.gather(flatTruth, predHSFlat)
    # Number of crimes commited in hotspots
    hsCFSsum = tf.reduce_sum(hsCFS, axis=1) # batch x 1
    # Ratio of crimes committed in hotspots and inverted for minimization
    cfsRatio = tf.truediv(1.0, tf.truediv(hsCFSsum, totalCFS))

    return cfsRatio

当我有一个交互式会话时,我可以运行它。该函数主要依赖于此Tensorflow问题https://github.com/tensorflow/tensorflow/issues/206中的代码。

1 个答案:

答案 0 :(得分:0)

Keras中的自定义丢失函数仅在调用时构建图形(如果使用TensorFlow作为后端)。 TensorFlow代码实际上并没有运行(图表没有被执行),直到调用fit(),正如您所注意到的那样。

因此,您无法使用典型的调试器来逐步执行代码并查找有问题的行,或查看数据。

一些调试技巧:

  • 将张量值打印到控制台

使用tf.Print用控制台输出包装执行,例如:

total = tf.Print(total, [total], 'total', summarize=10)

打印张量total的前10个值。

  • 删除计算依赖项

请注意,第一个值是计算中使用的值(分配给等式的LHS)。第二个参数是印刷的。

因此,要测试TF计算并查看输出,请将它们作为第二个参数传递,这样它们在调试期间不会影响输出。

从返回的值中消除变量,以找到有问题的计算。例如:

knownGoodValue = K.sum(tf.square(y_true - y_pred))  # any expr known to work
printExpr = tf.reshape(y_true, [-1])  # 1 x batch * numCells
knownGoodValue = tf.Print(knownGoodValue, [printExpr], 'flatTruth', summarize=10)

return knownGoodValue

这会打印并返回一些你知道有效的表达式,同时允许通过包装测试表达式来测试/打印任何TF expr,而不会动态地使用它的结果。

  • 使用TF调试器

https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python/debug

可以通过在fit函数之前添加以下代码来调用TensorFlow调试器:

import keras.backend as K
from tensorflow.python import debug as tf_debug
sess = K.get_session()
sess = tf_debug.LocalCLIDebugWrapperSession(sess)
K.set_session(sess)

然后在调用fit()时呈现类似GDB的cmdline接口。