2017-01-23 114 views
3

Keras自定义损失函数我碰到下面的错误使用使用TensorFlow编写自定义的目标函数Keras顺序模型的拟合阶段。ValueError异常:不支持无值Tensorflow

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中的代码。

+0

我有类似的问题。你找到解决方案吗? – user2962197

回答

0

在Keras定制损失函数仅调用时(如果使用TensorFlow作为后端)建立的曲线图。正如您已经注意到的,TensorFlow代码实际上并未运行(图形未执行),直到调用fit()。

所以你不能用一个典型的调试器来单步执行代码,并找到问题的行,或者看数据。

一些技术用于调试:

  • 打印张量的值到控制台

裹使用tf.Print执行与控制台输出,如:

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

到打印张量的前10个值total

  • 删除计算依赖

。注意,第一个值是在计算(转让给该方程的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来进行测试/通过包装测试表达式,而不actaully使用其结果打印出来。

  • 使用TF调试

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

的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) 

然后,它提出了一个GDB样调用fit()时调用cmdline接口。

相关问题