tensorflow - Keras 中的 Concat 参差不齐的数组
问题描述
我有几个要连接的 RaggedTensor;我正在使用 Keras。Vanilla Tensorflow 很乐意将它们连接起来,所以我尝试了代码:
card_feature = layers.concatenate([ragged1, ragged2, ragged3])
但它给出了错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 925, in __call__
return self._functional_construction_call(inputs, args, kwargs,
File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1084, in _functional_construction_call
base_layer_utils.create_keras_history(inputs)
File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 191, in create_keras_history
_, created_layers = _create_keras_history_helper(tensors, set(), [])
File "/home/timeroot/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer_utils.py", line 222, in _create_keras_history_helper
raise ValueError('Tensorflow ops that generate ragged or sparse tensor '
ValueError: Tensorflow ops that generate ragged or sparse tensor outputs are currently not supported by Keras automatic op wrapping. Please wrap these ops in a Lambda layer:
```
weights_mult = lambda x: tf.sparse.sparse_dense_matmul(x, weights)
output = tf.keras.layers.Lambda(weights_mult)(input)
```
所以我尝试了:
concat_lambda = lambda xs: tf.concat(xs, axis=2)
card_feature = layers.Lambda(concat_lambda)([ragged1, ragged2, ragged3])
但它给出了完全相同的错误,即使我已经包装了它。这是一个错误/有解决方法吗?
解决方案
连接代码3 Ragged Tensors
如下所示:
import tensorflow as tf
print(tf.__version__)
Ragged_Tensor1 = tf.ragged.constant([[3, 1, 4, 1], [], [5, 9, 2], [6], []])
Ragged_Tensor2 = tf.ragged.constant([[5, 3]])
Ragged_Tensor3 = tf.ragged.constant([[6,7,8], [9,10]])
print(tf.concat([Ragged_Tensor1, Ragged_Tensor2, Ragged_Tensor3], axis=0))
输出如下所示:
2.3.0
<tf.RaggedTensor [[3, 1, 4, 1], [], [5, 9, 2], [6], [], [5, 3], [6, 7, 8], [9, 10]]>
但看起来您正在尝试连接不规则张量操作。请分享您的完整代码,以便我们尝试帮助您。
推荐阅读
- spring - Spring Boot 执行 maven 存储库
- go-gorm - go-gorm 如何将“不在”查询与另一个查询一起使用
- vb.net - Listview 项目文本未显示
- php - 电子邮件中的 PHP 表单输入
- c# - 如何使用 Reactive Extensions 检测大文件中唯一的行
- eclipse - macOS Mojave 10.14 apache-tomcat-9.0.13 在 eclipse 2018-09 (4.9.0) 中运行时出错
- git - Git 命令重置到以前的提交而不会丢失提交
- html - 如何创建以下系列零件设计表
- angular - 如何将整个组件转换为库?
- mysql - 在应用引擎中通过nodejs使用Sequilize连接GCP CloudSql时出错?