2017-04-20 152 views
2

我试图通过Tensorflow写Keras 2 LSTM使用自定义损失函数:异常在Tensorflow功能作为Keras定制损失

model.compile(loss=in_top_k_loss, optimizer='rmsprop', metrics=[bin_crossent_true_only, 'binary_crossentropy', 'mean_squared_error', 'accuracy']) 

我的训练集有不同大小的时间维度的例子,因此,我使用train_on_batch,其中每批只包含具有相同时间维度的实例。批量大小为256 下面的代码抛出的第一个时代一个很讨厌的例外(当train_on_batch首次调用):

# takes 2 1D arrays of equal length, returns a single value (the negative of my own "precision" measure) 
def in_top_k_loss_single(y_true, y_pred): 
    y_true_labels = tf.cast(tf.transpose(tf.where(y_true > 0))[0], tf.int32) 
    y_pred = tf.reshape(y_pred, [1, tf.shape(y_pred)[0]]) 
    y_topk_tensor = tf.nn.top_k(y_pred, k=7) 
    y_topk_ixs = y_topk_tensor[0][0][:7] 
    y_topk = y_topk_tensor[1][0][:7] 
    y_topk_len = tf.cast(tf.count_nonzero(y_topk_ixs), tf.int32) 
    y_topk = y_topk[:y_topk_len] 
    y_topk0 = tf.expand_dims(y_topk, 1) 
    y_true_labels0 = tf.expand_dims(y_true_labels, 0) 
    re = tf.cast(tf.reduce_any(tf.equal(y_topk0, y_true_labels0), 1), tf.int32)/tf.range(1,y_topk_len+1) 
    return (-1) * tf.where(tf.equal(tf.reduce_sum(y_pred), tf.constant(0.0)), tf.constant(0.0), tf.cast(tf.reduce_mean(re),tf.float32)) 

# takes 2 matrices of equal sizes, 
# applies the upper function for y_true[i] & y_pred[i] for each row i, 
# returns a single value (mean of all row-wise values) 
def in_top_k_loss(y_true, y_pred): 
    # if I change `in_top_k_loss_single` to `keras.metrics.binary_crossentropy` (for instance) it runs 
    return K.mean(tf.map_fn(lambda x: in_top_k_loss_single(x[0], x[1]), (y_true, y_pred), dtype=tf.float32)) 

其中in_top_k_loss是我在Keras模型定制损失函数。 当我用不同的输入(甚至是棘手的)分别测试它们时,这些函数似乎可以工作。看起来只有Keras有问题 - 或许它需要不同的数据类型/形状/等等。

来自互联网的一些聪明的想法:试图改变批量大小,更改优化器并剪裁渐变 - 没有成功。还试过在train_on_batch之前拨打evaluate - 没有成功。

代码的其余产品来自Keras损失以及像这样的损失:

def bin_crossent_true_only(y_true, y_pred): 
    return (1 + keras.backend.sum(y_pred)) * keras.metrics.binary_crossentropy(y_true, y_true * y_pred) 

功能in_top_k_loss作品并且如果metrics阵列中使用返回有意义的结果。 所有输入(y_true,y_pred)不是NaN。 y_true可以具有0和1(每行0或更多1,即训练集的每个实例)。

异常本身:

Traceback (most recent call last): 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 491, in apply_op 
    preferred_dtype=default_dtype) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 702, in internal_convert_to_tensor 
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 110, in _constant_tensor_conversion_function 
    return constant(v, dtype=dtype, name=name) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 99, in constant 
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape)) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 360, in make_tensor_proto 
    raise ValueError("None values not supported.") 
ValueError: None values not supported. 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "<stdin>", line 9, in <module> 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\models.py", line 941, in train_on_batch 
    class_weight=class_weight) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1620, in train_on_batch 
    self._make_train_function() 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1002, in _make_train_function 
    self.total_loss) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\optimizers.py", line 210, in get_updates 
    new_a = self.rho * a + (1. - self.rho) * K.square(g) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 1225, in square 
    return tf.square(x) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\math_ops.py", line 384, in square 
    return gen_math_ops.square(x, name=name) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 2733, in square 
    result = _op_def_lib.apply_op("Square", x=x, name=name) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 504, in apply_op 
    values, as_ref=input_arg.is_ref).dtype.name 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 702, in internal_convert_to_tensor 
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 110, in _constant_tensor_conversion_function 
    return constant(v, dtype=dtype, name=name) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 99, in constant 
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape)) 
    File "C:\Users\myname\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 360, in make_tensor_proto 
    raise ValueError("None values not supported.") 
ValueError: None values not supported. 

回答

3

在TensorFlow的优化要求的损失函数是可微分的,其由所有的损失结果和在具有定义的梯度TensorFlow图中的变量之间的操作的确定。 tf.where()操作没有定义的梯度,这意味着整体损失函数是不可微分的。试图计算TensorFlow中不可微分函数的梯度的结果是None,这会导致您在Keras尝试更新变量时看到的错误。

+1

这回答了我的问题。不幸的是,我想不出去除函数体中2'where'的用法。 :( 我可以使用MAP @ k - 预测的最高K类(一个实例可能有多个标签)的平均平均精度 - 作为损失以某种方式(负数,因为我们将损失最小化)?或者我的替代选择 - 在'metrics'中使用它并单独监控它(然后在度量值最大时使用回调来保存模型)? – altier2856

+1

这是否意味着没有简单的方法来惩罚损失中的符号变化?有点像这样:https://codeburst.io/neural-networks-for-algorithmic-trading-volatility-forecasting-and-custom-loss-functions-c030e316ea7e使用K.switch(它是Theano) – wordsforthewise

相关问题