运行某网上源码时,报错: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