我正在尝试在tensorflow中自定义一个图层。该层必须将参差不齐的,长度不明的tesnor作为输入。但是,在尝试构建层时,代码卡住了。甚至下面附带的简单代码也无法正常工作。
import tensorflow as tf
class myLayer(tf.keras.layers.Layer):
def __init__(self):
super(myLayer, self).__init__()
self._supports_ragged_inputs = True
def call(self, inputs):
# Try to loop over ragged tensor
for x in inputs:
pass
return tf.constant(0)
# Input is ragged tensor
inputs = tf.keras.layers.Input(shape=(None, 1), ragged=True)
layer1 = myLayer()
output = layer1(inputs)
[当我在Tensorflow version 2.2.0
中运行您的代码时,在for
循环中出现以下错误-
错误-
ValueError: in user code:
<ipython-input-24-1681d59017fc>:10 call *
for x in inputs:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/operators/control_flow.py:359 for_stmt
iter_, extra_test, body, get_state, set_state, symbol_names, opts)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/operators/control_flow.py:491 _tf_ragged_for_stmt
opts)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/operators/control_flow.py:885 _tf_while_stmt
aug_test, aug_body, init_vars, **opts)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/control_flow_ops.py:2688 while_loop
back_prop=back_prop)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/while_v2.py:104 while_loop
maximum_iterations)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/while_v2.py:1258 _build_maximum_iterations_loop_var
maximum_iterations, dtype=dtypes.int32, name="maximum_iterations")
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1317 convert_to_tensor
(dtype.name, value.dtype.name, value))
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype int64: <tf.Tensor 'my_layer_15/strided_slice:0' shape=() dtype=int64>
所以我只是执行以下实验来了解使用for
时enumerate
循环和inputs
产生的数据类型。 for
循环生成tensor
类,而enumerate
生成int
类。
实验代码-
inputs = tf.keras.layers.Input(shape=(None, 1), ragged=True)
for x in inputs:
print(type(x))
break
for i,x in enumerate(inputs):
print(type(i))
break
输出-
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'int'>
所以我如下修改了您的代码,它正常工作-
固定代码-
import tensorflow as tf
class myLayer(tf.keras.layers.Layer):
def __init__(self):
super(myLayer, self).__init__()
self._supports_ragged_inputs = True
def call(self, inputs):
# Try to loop over ragged tensor
# for x in inputs: # Throws Error
for i,x in enumerate(inputs): #Enumerate Works fine
break #Using break as pass will go into loop
return tf.constant(0)
# Input is ragged tensor
inputs = tf.keras.layers.Input(shape=(None, 1), ragged=True)
layer1 = myLayer()
output = layer1(inputs)
print(output)
输出-
Tensor("my_layer_17/Identity:0", shape=(), dtype=int32)
希望这能回答您的问题。祝您学习愉快。