天天看点

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf

今天我们班的大漂亮,又郁闷了,特别生气的过来找我求助,还凶我,“老师,我的代码和你的一模一样,为什么我的报错???”

具体错误如下:

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices
           

先说一下原因吧,非常明显的一个错误,就是你提供的数据和API需要的数据不一致,不用猜,就看出来了,是前一个API提供的数据类型问题。

后来我看了一下,大漂亮的代码,第一遍我也没看出来,第二遍我就笑了。

上代码:(为了还原她的错误,我还得改代码…)

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf

这个错误还真不好找:

如果不是了解后面API接口所需数据的类型,这个问题还是挺容易忽略的,后面接口需要ndarray数据,而tf.reshape返回值为tensor(张量):见下图:

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf

这个时候很明显就该想到np.reshape,原因是,见下图:

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf

对,就是它ndaray数据,ok,将tf.reshape换成np.reshape问题就解决了。

继续阅读