tf.split 报错 ValueError: Rank-0 tensors are not supported as the num_or_size_splits argument to split

运行某网上源码时,报错:ValueError: Rank-0 tensors are not supported as the num_or_size_splits argument to split. Argument provided: 4
 

报错的这一行内容是:

split_seqs = tf.split(sequence, num_splits, axis=1)

调试打印出来的 sequence的shape=(256, ?, 490),  num_splits为4, 看起来都很正常,应该能split成功。
后来发现, num_splits值虽然为4,但类型是numpy.int64, 其实报错的日志已经提示了,是 the num_or_size_splits argument 的错误。改成  split_seqs = tf.split(sequence,  int(num_splits), axis=1)就好了

import numpy as np
import tensorflow as tf

nparr= np.array([1,2,3,4])
aaa = tf.placeholder(shape=[256, None, 490], dtype=tf.float32)

tf.split(aaa, nparr[3], axis=1)
----------------------------------------
ValueError                         Traceback (most recent call last):
<ipython-input-5-94bcb2e494e1> in <modue>
----> 1 tf.split(aaa, nparr[3], axis=1)
 
/usr/python/lib/python3.6/site-packeges/tensorflow/python/ops/array_ops.py in split(value, num_or_size_splits, axis, num, name)
    1566    rasie ValueError(
    1567       "Rank-0 tensors are not supported as the num_or_size_splits argument "
->  1568       "to split. Argument provided: %s" %(num_or_size_splits,))
    1569
    1570    if num is None:

ValueError: Rank-0 tensors are not supported as the num_or_size_splits argument to split. Argument provided: 4
 
In [10]: type(nparr[3])
Out[10]: numpy.int64
 
Out[11]: tf.split(aaa, 4, axis=1)
Out[11]: 
[<tf.Tensor 'split_2:0' shape=(256, ?, 490) dtype=float32>,
 <tf.Tensor 'split_2:1' shape=(256, ?, 490) dtype=float32>,
 <tf.Tensor 'split_2:2' shape=(256, ?, 490) dtype=float32>,
 <tf.Tensor 'split_2:3' shape=(256, ?, 490) dtype=float32>]

貌似只有CentOS下有这个问题,Ubantu和windows 下好像都没有这个问题..

 

补充:

要了解 numpy.int类型和pytho内置的int类型 是有区别的,可能会造成坑

“numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数据类型对应上,其中部分类型对应为 Python 内置的类型。”

详见:

https://www.runoob.com/numpy/numpy-dtype.html

参考:

https://blog.csdn.net/weixin_33769125/article/details/91878728

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值