【问题标题】:Converting Tensor to Ragged Tensor in Graph mode using Keras使用 Keras 在图形模式下将张量转换为不规则张量
【发布时间】:2020-01-07 16:13:18
【问题描述】:

我想使用 Keras 在我的图表中将张量转换为不规则张量。 但是,函数 RaggedTensor.from_row_lengths 在我的图表中失败了。

Tensorflow 版本:tf-nightly 2.1.0.dev20191203

这是一个代码示例:

import tensorflow as tf
import numpy as np


input_sequence = np.reshape(
    np.array([1, 2, 3, 4, 5, 6, 7, 8], dtype=np.int32),
    (2, 4))

labels = np.reshape(
    np.array([1.0, 0.0, ], dtype=np.float32),
    (2, 1))

dataset = tf.data.Dataset.from_tensor_slices((input_sequence, labels)).batch(1)

sequence_in = tf.keras.layers.Input(shape=(None,), dtype=tf.int32)

# Failing line, the rest works without the line below
ragged_in = tf.RaggedTensor.from_row_lengths(sequence_in, [2, 2])

embedded_tensor = tf.keras.layers.Embedding(9, 4)(sequence_in)

flat_tensor = tf.reshape(embedded_tensor, [-1, 16])
prediction = tf.keras.layers.Dense(2)(flat_tensor)

model = tf.keras.Model(inputs=sequence_in, outputs=prediction)
model.compile(
    tf.keras.optimizers.Adam(),
    loss=tf.keras.losses.CategoricalCrossentropy(),
    metrics=['acc'])

model.fit(dataset, steps_per_epoch=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

我可以使用validate=False 忽略该错误,但在下一层会失败:

ragged_in = tf.RaggedTensor.from_row_lengths(sequence_in, [2, 2], validate=False)
embedded_ragged = tf.keras.layers.Embedding(9, 4)(ragged_in)

我想知道这是否与批量大小和“sequence_in”张量不固定这一事实有关。 所以我也尝试只将第一个观察值转换为不规则张量,但同样的错误仍然存​​在。

ragged_in = tf.RaggedTensor.from_row_lengths(sequence_in[0], [2, 2])

【问题讨论】:

    标签: python tensorflow2.0 tf.keras ragged


    【解决方案1】:

    keras 张量视为标准 tf.Tensors 多次适得其反。我建议您执行以下操作。

    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))
    

    【讨论】:

    • 注意:为了让 Ragged Tensor 通过另一层,例如我原始示例中的嵌入层,我必须安装 TF 2.1.0
    • 以上有什么错误问题吗?
    猜你喜欢
    • 2019-11-20
    • 2020-11-18
    • 1970-01-01
    • 2018-12-02
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多