将可变大小的 numpy 数组转换为 Tensorflow 张量 [英] Convert a variable sized numpy array to Tensorflow Tensors

查看:45
本文介绍了将可变大小的 numpy 数组转换为 Tensorflow 张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Tensorflow 2.0 alpha 预览版并正在测试 Eager execution .我的疑问是,如果您在中间有一个可变大小的 numpy 数组,例如

I am trying Tensorflow 2.0 alpha preview and was testing the Eager execution . My doubt is that if you have a numpy array of variable size in middle like

input.shape
(10,)

input[0].shape
(109, 16)

input[1].shape
(266, 16)

等等数组的其余部分,如何急切地将它们转换为张量.

and so on for the rest of the array , how does one eagerly convert them to tensors.

当我尝试

tf.convert_to_tensor(input)

tf.Variable(input)

我明白了

ValueError:无法将 numpy ndarray 转换为张量(无法获取元素作为字节.).

ValueError: Failed to convert numpy ndarray to a Tensor (Unable to get element as bytes.).

转换每个子数组有效,但由于子数组大小不同,tf.stack 不起作用.

Converting each sub-array works , but because the sub-array size isn't same , tf.stack doesn't work.

有什么帮助或建议吗?

推荐答案

这也发生在我身上.查看 此处的文档,我最终尝试了

This was happening to me in eager as well. Looking at the docs here , I ended up trying

tf.convert_to_tensor(input, dtype=tf.float32)

这对我有用.

这篇关于将可变大小的 numpy 数组转换为 Tensorflow 张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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