如何修复“至少需要一个数组来连接”错误?

2024-06-02 07:28:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经通读了有关ValueError的各种帖子,但没有得到多少令人满意的解决方案。拜托,谁能帮我一下我做错了什么??你知道吗

代码:

 assert(type(images) == list)
 # assert(type(images[0]) == np.ndarray)
 # assert(len(images[0].shape) == 3)
 # assert(np.max(images[0]) > 10)
 # assert(np.min(images[0]) >= 0.0)
 inps = []
 for img in images:
   img = img.astype(np.float32)
   inps.append(np.expand_dims(img, 0))
 bs = 100
 with tf.Session() as sess:
   preds = []
   n_batches = int(math.ceil(float(len(inps)) / float(bs)))
   for i in range(n_batches):
       sys.stdout.write(".")
       sys.stdout.flush()
       inp = inps[(i * bs):min((i + 1) * bs, len(inps))]
       inp = np.concatenate(inp, 0)
       pred = sess.run(softmax, {'ExpandDims:0': inp})
       preds.append(pred)
   preds = np.concatenate(preds, 0)
   scores = []
   for i in range(splits):
     part = preds[(i * preds.shape[0] // splits):((i + 1) * preds.shape[0] // splits), :]
     kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0)))
     kl = np.mean(np.sum(kl, 1))
     scores.append(np.exp(kl))
   return np.mean(scores), np.std(scores)

Error :

>File "/content/Inception-Score/inception_score.py", line 45, in >get_inception_score
>    preds = np.concatenate(preds, 0)
>ValueError: need at least one array to concatenate

Tags: inimglenbsnpassertimagesinp
2条回答

似乎缺少要连接的数组的参数。您指定了初始数组和要连接的轴,但没有指定第二个数组,因此“至少需要一个数组来连接”。你知道吗

你知道吗np.连接()在第一个参数中至少有两个数组,如文档here中所述。看起来“preds”只是一个数组。我不知道你想做什么,但也许连接不是你想要的?你知道吗

问题似乎出在np.concatenate中,它需要一个数组,而您没有提供

#syntax
numpy.concatenate((a1, a2, ...), axis=0, out=None)

Parameters:
a1, a2, … : sequence of array_like The arrays must have the same shape, except in the dimension corresponding to axis (the first, by default).

axis : int, optional The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.

out : ndarray, optional If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.

Returns: ndarray The concatenated array.

检查preds它返回什么

相关问题 更多 >