我想贴上我的标签,这样它们的长度就相等了,可以传递到ctc_loss函数中。显然,-1是不允许的。如果我申请填充物,填充值是否应该是ctc标签的一部分?
更新
我有这样的代码,它将密集的标签转换成稀疏的标签,然后传递给ctc_loss函数,我认为这与问题有关。
def dense_to_sparse(dense_tensor, out_type):
indices = tf.where(tf.not_equal(dense_tensor, tf.constant(0, dense_tensor.dtype)
values = tf.gather_nd(dense_tensor, indices)
shape = tf.shape(dense_tensor, out_type=out_type)
return tf.SparseTensor(indices, values, shape)发布于 2018-03-02 14:46:34
我就是这样做的。我有一个稠密的张量labels,其中包括填充-1,所以一个批处理中的所有目标都有相同的长度。然后我用
labels_sparse = dense_to_sparse(labels, sparse_val=-1)哪里
def dense_to_sparse(dense_tensor, sparse_val=0):
"""Inverse of tf.sparse_to_dense.
Parameters:
dense_tensor: The dense tensor. Duh.
sparse_val: The value to "ignore": Occurrences of this value in the
dense tensor will not be represented in the sparse tensor.
NOTE: When/if later restoring this to a dense tensor, you
will probably want to choose this as the default value.
Returns:
SparseTensor equivalent to the dense input.
"""
with tf.name_scope("dense_to_sparse"):
sparse_inds = tf.where(tf.not_equal(dense_tensor, sparse_val),
name="sparse_inds")
sparse_vals = tf.gather_nd(dense_tensor, sparse_inds,
name="sparse_vals")
dense_shape = tf.shape(dense_tensor, name="dense_shape",
out_type=tf.int64)
return tf.SparseTensor(sparse_inds, sparse_vals, dense_shape)这会造成标签的稀疏张量,这就是您需要将其放入ctc损失中的内容。也就是说,您将tf.nn.ctc_loss(labels=labels_sparse, ...)称为填充(即稠密张量中的所有值等于-1 )在这个稀疏张量中根本不表示。
发布于 2019-11-16 11:27:59
实际上,-1值允许出现在成本的y_true参数中,但有一个限制--它们不应该出现在label_length指定的实际标签"content“中(在这里,i-th标签"content”将从索引0开始,以索引label_length[i]结尾)。
所以用-1贴标签是非常好的,这样它们的长度就会和你想的一样长。唯一需要注意的是正确计算和传递相应的label_length值。
下面是示例代码,它是test_ctc 从角角进行单元测试的修改版本
import numpy as np
from tensorflow.keras import backend as K
number_of_categories = 4
number_of_timesteps = 5
labels = np.asarray([[0, 1, 2, 1, 0], [0, 1, 1, 0, -1]])
label_lens = np.expand_dims(np.asarray([5, 4]), 1)
# dimensions are batch x time x categories
inputs = np.zeros((2, number_of_timesteps, number_of_categories), dtype=np.float32)
input_lens = np.expand_dims(np.asarray([5, 5]), 1)
k_labels = K.variable(labels, dtype="int32")
k_inputs = K.variable(inputs, dtype="float32")
k_input_lens = K.variable(input_lens, dtype="int32")
k_label_lens = K.variable(label_lens, dtype="int32")
res = K.eval(K.ctc_batch_cost(k_labels, k_inputs, k_input_lens, k_label_lens))即使使用-1作为(第二个) labels序列的最后一个元素,它也运行得非常好,因为对应的label_lens项(第二个)指定其长度为4。
如果我们将它更改为5,或者如果我们将其他一些标签值更改为-1,那么我们就有了您提到的All labels must be nonnegative integers异常。但这只是意味着我们的label_lens是无效的。
https://stackoverflow.com/questions/49063938
复制相似问题