Python 使用Keras在图形模式下将张量转换为参差不齐张量

Python 使用Keras在图形模式下将张量转换为参差不齐张量,python,tensorflow2.0,tf.keras,ragged,Python,Tensorflow2.0,Tf.keras,Ragged,我想用Keras把我的图中的张量转换成参差不齐的张量。 但是,函数RaggedTensor.from_row_length在我的图中失败 Tensorflow版本:tf nightly 2.1.0.dev20191203 下面是一个代码示例: 将tensorflow导入为tf 将numpy作为np导入 输入_序列=np。重塑( 数组([1,2,3,4,5,6,7,8],dtype=np.int32), (2, 4)) 标签=np。重塑( 数组([1.0,0.0,],dtype=np.float3

我想用Keras把我的图中的张量转换成参差不齐的张量。 但是,函数
RaggedTensor.from_row_length
在我的图中失败

Tensorflow版本:
tf nightly 2.1.0.dev20191203

下面是一个代码示例:

将tensorflow导入为tf
将numpy作为np导入
输入_序列=np。重塑(
数组([1,2,3,4,5,6,7,8],dtype=np.int32),
(2, 4))
标签=np。重塑(
数组([1.0,0.0,],dtype=np.float32),
(2, 1))
dataset=tf.data.dataset.from_tensor_切片((输入序列,标签)).batch(1)
sequence_in=tf.keras.layers.Input(shape=(无),dtype=tf.int32)
#失败的线,其余的工作没有下面的线
ragged_in=tf.RaggedTensor.from_row_长度(序列[2,2])
嵌入张量=tf.keras.layers.embedded(9,4)(序列)
平面张量=tf.重塑(嵌入张量,[-1,16])
预测=tf.keras.layers.Dense(2)(平面张量)
模型=tf.keras.model(输入=序列输入,输出=预测)
model.compile(
tf.keras.optimizers.Adam(),
损失=tf.keras.loss.CategoricalCrossentropy(),
指标=['acc'])
拟合(数据集,每个历元的步数=1)
错误似乎与用于检查张量形状的验证有关:

Traceback (most recent call last):
  File "myscript.py", line 18, in <module>
    ragged_in = tf.RaggedTensor.from_row_lengths(sequence_in, [4, 1])
  File "python3.6/site-packages/tensorflow_core/python/ops/ragged/ragged_tensor.py", line 510, in from_row_lengths
    check_ops.assert_equal(nvals1, nvals2, message=msg)
  File "python3.6/site-packages/tensorflow_core/python/ops/check_ops.py", line 506, in assert_equal
    if not condition:
  File "python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 765, in __bool__
    self._disallow_bool_casting()
  File "python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 534, in _disallow_bool_casting
    self._disallow_in_graph_mode("using a `tf.Tensor` as a Python `bool`")
  File "python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 523, in _disallow_in_graph_mode
    " this function with @tf.function.".format(task))
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function
我想知道这是否与批量大小和“序列”张量不固定有关。 所以我也试着把第一个观测值转换成参差不齐的张量,但同样的错误依然存在

ragged_in=tf.RaggedTensor.from_row_长度(序列[0],[2,2])

keras
张量视为标准
tf。张量
多次对我产生反作用。我建议你做以下事情

ragged_in = tf.keras.layers.Lambda(lambda x: tf.RaggedTensor.from_row_lengths(x, [2, 2]))(sequence_in)
print(ragged_in)
输出

>>> tf.RaggedTensor(values=Tensor("input_3:0", shape=(None, None), dtype=int32), row_splits=Tensor("lambda_1/RaggedFromRowLengths/concat:0", shape=(3,), dtype=int64))

keras
张量视为标准
tf。张量
多次对我产生反作用。我建议你做以下事情

ragged_in = tf.keras.layers.Lambda(lambda x: tf.RaggedTensor.from_row_lengths(x, [2, 2]))(sequence_in)
print(ragged_in)
输出

>>> tf.RaggedTensor(values=Tensor("input_3:0", shape=(None, None), dtype=int32), row_splits=Tensor("lambda_1/RaggedFromRowLengths/concat:0", shape=(3,), dtype=int64))

注:为了让参差张量通过后面的另一层,如我的原始示例中的嵌入层,我必须安装TF 2.1.0上面有任何错误问题吗?注:为了让参差张量通过后面的另一层,如我的原始示例中的嵌入层,我必须安装TF 2.1.0上面有任何错误问题吗?