Python tensorflow不规则堆栈问题

Python tensorflow不规则堆栈问题,python,tensorflow,keras,Python,Tensorflow,Keras,我试图在我的模型中使用tf.ragged.stack。 当我在玩它时,我可以做如下事情: tensor = tf.constant([[1., 2.], [3., 4.], [5., 6.]]) masks = tf.constant([[1, 1, 1], [0, 0, 0], [1, 0, 1]]) tf.ragged.stack([tf.boolean_mask(tensor, mask) for mask in masks]) 它还提供: <tf.RaggedTensor [[[

我试图在我的模型中使用tf.ragged.stack。 当我在玩它时,我可以做如下事情:

tensor = tf.constant([[1., 2.], [3., 4.], [5., 6.]])
masks = tf.constant([[1, 1, 1], [0, 0, 0], [1, 0, 1]])
tf.ragged.stack([tf.boolean_mask(tensor, mask) for mask in masks])
它还提供:

<tf.RaggedTensor [[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], [], [[1.0, 2.0], [5.0, 6.0]]]>
错误是:

---------------------------------------------------------------------------
_FallbackException                        Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py in concat_v2(values, axis, name)
   1171         _ctx._context_handle, tld.device_name, "ConcatV2", name,
-> 1172         tld.op_callbacks, values, axis)
   1173       return _result

_FallbackException: This function does not handle the case of the path where all inputs are not already EagerTensors.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-114-d762bdd6bb0d> in <module>
      3 masks = tf.keras.Input(shape=(None, 3), dtype='int32')
      4 masks = tf.reshape(masks, [3,3])
----> 5 rag = tf.ragged.stack([tf.boolean_mask(tensor, mask) for mask in masks])

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/ragged/ragged_concat_ops.py in stack(values, axis, name)
    116     values = [values]
    117   with ops.name_scope(name, 'RaggedConcat', values):
--> 118     return _ragged_stack_concat_helper(values, axis, stack_values=True)
    119 
    120 

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/ragged/ragged_concat_ops.py in _ragged_stack_concat_helper(rt_inputs, axis, stack_values)
    185     if not ragged_tensor.is_ragged(rt_inputs[i]):
    186       rt_inputs[i] = ragged_tensor.RaggedTensor.from_tensor(
--> 187           rt_inputs[i], ragged_rank=1, row_splits_dtype=row_splits_dtype)
    188 
    189   # Convert the input tensors to all have the same ragged_rank.

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/ragged/ragged_tensor.py in from_tensor(cls, tensor, lengths, padding, ragged_rank, name, row_splits_dtype)
   1779       # vector that contains no default values, and reshape the input tensor
   1780       # to form the values for the RaggedTensor.
-> 1781       values_shape = array_ops.concat([[-1], input_shape[2:]], axis=0)
   1782       values = array_ops.reshape(tensor, values_shape)
   1783       const_nrows = tensor_shape.dimension_at_index(tensor.shape, 0).value

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    178     """Call target, and fall back on dispatchers if there is a TypeError."""
    179     try:
--> 180       return target(*args, **kwargs)
    181     except (TypeError, ValueError):
    182       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py in concat(values, axis, name)
   1604           dtype=dtypes.int32).get_shape().assert_has_rank(0)
   1605       return identity(values[0], name=name)
-> 1606   return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
   1607 
   1608 

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py in concat_v2(values, axis, name)
   1175       try:
   1176         return concat_v2_eager_fallback(
-> 1177             values, axis, name=name, ctx=_ctx)
   1178       except _core._SymbolicException:
   1179         pass  # Add nodes to the TensorFlow graph.

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py in concat_v2_eager_fallback(values, axis, name, ctx)
   1207         "'concat_v2' Op, not %r." % values)
   1208   _attr_N = len(values)
-> 1209   _attr_T, values = _execute.args_to_matching_eager(list(values), ctx)
   1210   _attr_Tidx, (axis,) = _execute.args_to_matching_eager([axis], ctx, _dtypes.int32)
   1211   _inputs_flat = list(values) + [axis]

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/eager/execute.py in args_to_matching_eager(l, ctx, default_dtype)
    261       ret.append(
    262           ops.convert_to_tensor(
--> 263               t, dtype, preferred_dtype=default_dtype, ctx=ctx))
    264       if dtype is None:
    265         dtype = ret[-1].dtype

~/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
   1315       raise ValueError(
   1316           "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
-> 1317           (dtype.name, value.dtype.name, value))
   1318     return value
   1319 

ValueError: Tensor conversion requested dtype int32 for Tensor with dtype int64: <tf.Tensor 'strided_slice_142103:0' shape=(0,) dtype=int64>
---------------------------------------------------------------------------
_FallbackException回溯(上次最近的调用)
concat_v2中的~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py(值、轴、名称)
1171 \u ctx.\u上下文\u句柄,tld.device\u名称,“ConcatV2”,名称,
->1172 tld.op_回调、值、轴)
1173返回结果
_FallbackException:此函数不处理所有输入都不是张量的路径的情况。
在处理上述异常期间,发生了另一个异常:
ValueError回溯(最近一次调用上次)
在里面
3掩码=tf.keras.Input(shape=(无,3),dtype='int32')
4个遮罩=tf.重塑(遮罩[3,3])
---->5 rag=tf.ragged.stack([tf.boolean_mask(张量,mask)表示掩码中的掩码])
堆栈中的~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/ragged/ragged\u concat\u ops.py(值、轴、名称)
116值=[值]
117带有操作名称\u范围(名称“RaggedConcat”,值):
-->118返回_ragged_stack_concat_helper(值、轴、堆栈值=真)
119
120
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/ragged/ragged\u concat\u ops.py in\u ragged\u stack\u concat\u helper(rt\u输入、轴、堆栈值)
185如果不参差不齐,张量是否参差不齐(rt_输入[i]):
186 rt_输入[i]=不规则张量。不规则张量传感器。来自不规则张量(
-->187 rt_输入[i],参差不齐_排名=1,行分割_数据类型=行分割_数据类型)
188
189#将输入张量转换为所有具有相同参差不齐秩的张量。
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/ragged/ragged\u tensor.py from\u tensor(cls,tensor,length,padding,ragged\u rank,name,row\u splits\u dtype)
1779#不包含默认值的向量,并重塑输入张量
1780#形成RaggedSensor的值。
->1781个值\u shape=array\u ops.concat([-1],输入\u shape[2:],轴=0)
1782值=数组操作。重塑(张量、值和形状)
1783 const_nrows=张量_形状。索引处的尺寸(张量。形状,0)。值
包装器中的~/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py(*args,**kwargs)
178“调用目标,如果出现类型错误,则返回调度程序。”
179尝试:
-->180返回目标(*args,**kwargs)
181除外(类型错误、值错误):
182#注意:将_转换为_急切_张量当前会引发一个值错误,而不是
concat中的~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py(值、轴、名称)
1604 dtype=dtypes.int32)。获取\u形状()。断言\u具有\u秩(0)
1605返回标识(值[0],name=name)
->1606返回gen_数组操作concat_v2(值=值,轴=轴,名称=名称)
1607
1608
concat_v2中的~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py(值、轴、名称)
1175尝试:
1176返回concat\u v2\u earge\u回退(
->1177值,轴,名称=名称,ctx=_ctx)
1178除“核心符号”外例外:
1179通过#将节点添加到TensorFlow图。
concat\u v2\u eager\u fallback中的~/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/gen\u array\u ops.py(值、轴、名称、ctx)
1207“'concat_v2'Op,非%r.%值)
1208_attr_N=len(值)
->1209\u attr\u T,values=\u execute.args\u to\u matching\u eager(列表(值),ctx)
1210\u attr\u Tidx,(axis,)=\u execute.args\u to\u matching\u eager([axis],ctx,\u dtypes.int32)
1211_输入_平面=列表(值)+[轴]
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/eager/execute.py在args\u-to\u-matching\u-eager(l,ctx,默认数据类型)中
261检索附加(
262运算。将_转换为_张量(
-->263 t,数据类型,首选(数据类型=默认值(数据类型,ctx=ctx))
264如果数据类型为无:
265 dtype=ret[-1]。dtype
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in convert\u to\u tensor(值、数据类型、名称、as\u ref、首选数据类型、数据类型提示、ctx、接受的结果类型)
1315上升值错误(
1316“张量转换请求了数据类型为%s的张量的数据类型%s:%r”%
->1317(dtype.name,value.dtype.name,value))
1318返回值
1319
ValueError:对于数据类型为int64的Tensor,Tensor转换请求的数据类型为int32:
谁能告诉我发生了什么事


我认为tf.ragged.stack方法对tf.keras.Input等占位符不起作用。

您使用的是什么版本的tf?以下代码在Colab(使用TF2.4)上测试时有效

然而,主要问题似乎来自数据类型

ValueError:dtype int64的张量请求了dtype int32的张量转换:
您需要将输入转换为
int32
格式,以使
tf.ragged.stack
happy:

converted_masks = tf.cast([tf.boolean_mask(tensor, mask) for mask in masks], tf.int32)
rag = tf.ragged.stack(converted_masks)


你使用的是什么版本的TF?以下代码在Colab(使用TF2.4)上测试时有效

然而,主要问题似乎来自数据类型

ValueError:dtype int64的张量请求了dtype int32的张量转换:
您需要将输入转换为
int32
格式,以使
tf.ragged.stack
happy:

converted_masks = tf.cast([tf.boolean_mask(tensor, mask) for mask in masks], tf.int32)
rag = tf.ragged.stack(converted_masks)


谢谢你的跟进。我使用的tensorflow版本是2.2.0。使用您的建议后,我终于合并了此代码!谢谢你的跟进。我使用的tensorflow版本是2.2.0。使用您的建议后,我终于合并了此代码!