gpt4 book ai didi

python - 使用 Keras 在图形模式下将张量转换为不规则张量

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:17 24 4
gpt4 key购买 nike

我想使用 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])

最佳答案

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

关于python - 使用 Keras 在图形模式下将张量转换为不规则张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59632120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com