ValueError:无法将非矩形 Python 序列转换为张量 [英] ValueError: Can't convert non-rectangular Python sequence to Tensor

查看:246
本文介绍了ValueError:无法将非矩形 Python 序列转换为张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 tf.convert_to_tensor 将列表更改为张量,数据如下:

I want to change list to tensor with tf.convert_to_tensor, data is following:

data=[

array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   1., 0., 0.]), 
array([0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.]), 
array([0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
   0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.,
   0., 0., 0.]), 
array([0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])
    ]

它没有用,系统说:

ValueError: 无法将非矩形 Python 序列转换为张量.

ValueError: Can't convert non-rectangular Python sequence to Tensor.

如何解决这个问题?

推荐答案

我不确定它们是否存在于 TensorFlow 1 中,但 TensorFlow 2.0 支持 RaggedTensors,文档将其描述为...... TensorFlow 等效于嵌套变量-长度列表."

I'm not sure whether they exist in TensorFlow 1 but TensorFlow 2.0 supports RaggedTensors, which the documentation describes as "... the TensorFlow equivalent of nested variable-length lists."

我认为将您的数据转换为 RaggedTensors 是微不足道的.它甚至可能就像:

I think it would be trivial to convert your data to RaggedTensors. It might even be as easy as:

data_tensor = tf.ragged.constant(data)

这篇关于ValueError:无法将非矩形 Python 序列转换为张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆