Python Keras Lambda Layer给出ValueError:尝试拟合模型时不支持无值

Python Keras Lambda Layer给出ValueError:尝试拟合模型时不支持无值,python,machine-learning,tensorflow,keras,Python,Machine Learning,Tensorflow,Keras,我在Keras中编写了一个lambda层来稀疏输入: def sparsify(x, percent_zero_dims): k_val = int((1.0 - percent_zero_dims / 100.0) * K.int_shape(x)[1]) values, indices = tf.nn.top_k(x, k=k_val, sorted=False) # We need to create full indices like [[0, 0], [0,

我在Keras中编写了一个lambda层来稀疏输入:

def sparsify(x, percent_zero_dims):
    k_val = int((1.0 - percent_zero_dims / 100.0) * K.int_shape(x)[1])

    values, indices = tf.nn.top_k(x, k=k_val, sorted=False)
    # We need to create full indices like [[0, 0], [0, 1], [1, 2], [1, 1]]
    my_range = tf.expand_dims(tf.range(0, K.shape(indices)[0]), 1)  # will be [[0], [1]]
    my_range_repeated = tf.tile(my_range, [1, k_val])  # will be [[0, 0], [1, 1]]

    # change shapes to [N, k, 1] and [N, k, 1], to concatenate into [N, k, 2]
    full_indices = tf.concat([tf.expand_dims(my_range_repeated, 2), tf.expand_dims(indices, 2)], 2)
    full_indices = tf.reshape(full_indices, [-1, 2])
    output = tf.sparse_to_dense(full_indices, K.shape(x), tf.reshape(values, [-1]), default_value=0.,
                            validate_indices=False)
    return output
我通过以下方式调用模型中的lambda层:

sparse = Lambda(lambda x: sparsify(x, sparse_perc))(relu)
输入形状为(None,32)且(None,32)正在获取输出。我还将这个lambda层作为自定义层编写,这会抛出相同的错误。如果我将模型输出设置为relu,它训练得很好,但是当我使用sparse作为输出时,模型会抛出

line 360, in make_tensor_proto
raise ValueError("None values not supported.")
我可以预测模型,编译,没有问题,并得到预期的结果,但当我尝试拟合它时,模型抛出了一个错误。我认为这与从堆栈跟踪中看到的输出生成的无效损失有关:

line 1014, in _make_train_function
self.total_loss)...
我已尝试从稀疏层的输出中删除任何NAN(我知道这不是一个好的做法,但我只是想弄清楚它在哪里/如何破坏):

这发生在模型中的其他几个自定义层上,但不是所有的自定义层上,这就是我感到困惑的原因

规格: python 3.5

MacOSX

Keras 2.0.4

TF1.1.0

Lambda(lambda x: tf.where(tf.is_nan(x), tf.zeros_like(x), x))(sparse)