Valueerror: Could Not Broadcast Input Array from Shape (224,224,3) into Shape (224,224)

ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)

At least one item in your list is either not three dimensional, or its second or third dimension does not match the other elements. If only the first dimension does not match, the arrays are still matched, but as individual objects, no attempt is made to reconcile them into a new (four dimensional) array. Some examples are below:

That is, the offending element's shape != (?, 224, 3),

or ndim != 3 (with the ? being non-negative integer).

That is what is giving you the error.

You'll need to fix that, to be able to turn your list into a four (or three) dimensional array. Without context, it is impossible to say if you want to lose a dimension from the 3D items or add one to the 2D items (in the first case), or change the second or third dimension (in the second case).


Here's an example of the error:

>>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224))]
>>> np.array(a)
ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)

or, different type of input, but the same error:

>>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,224,13))]
>>> np.array(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (224,224,3) into shape (224,224)

Alternatively, similar but with a different error message:

>>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((224,100,3))]
>>> np.array(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (224,224,3) into shape (224)

But the following will work, albeit with different results than (presumably) intended:

>>> a = [np.zeros((224,224,3)), np.zeros((224,224,3)), np.zeros((10,224,3))]
>>> np.array(a)
# long output omitted
>>> newa = np.array(a)
>>> newa.shape
3 # oops
>>> newa.dtype
dtype('O')
>>> newa[0].shape
(224, 224, 3)
>>> newa[1].shape
(224, 224, 3)
>>> newa[2].shape
(10, 224, 3)
>>>

ValueError: could not broadcast input array from shape (3,3) into shape (3,)

The correct function for this case is np.dot not np.multiply.

could not broadcast input array from shape (3) into shape (2)

From the insert docs:

>>> a = np.array([[1, 1], [2, 2], [3, 3]])
>>> a
array([[1, 1],
[2, 2],
[3, 3]])
>>> np.insert(a, 1, 5, axis=1)
array([[1, 5, 1],
[2, 5, 2],
[3, 5, 3]])

So taking your case - act on the whole array, and specify the axis:

In [184]: arr = np.random.randint(0,9,(3,2))
In [185]: arr
Out[185]:
array([[7, 5],
[2, 0],
[2, 4]])
In [186]: arr1 = np.insert(arr,0,1,axis=1)
In [187]: arr1
Out[187]:
array([[1, 7, 5],
[1, 2, 0],
[1, 2, 4]])

Many of the numpy functions take an axis (or axes) parameter. Use it.

Can't broadcast input array from shape (3,1) into shape (3,)

V[k:m,k] = v; v has shape (3,1), but the target is (3,). k:m is a 3 term slice; k is a scalar.

Try using v.ravel(). Or V[k:m,[k]].

But also understand why v has its shape.

ValueError: could not broadcast input array from shape (32,32,3) into shape (32,32)

Try if this works,
I can't check myself since you did not provide a complete code (with VAE model generator)

n = 15  # figure with 15x15 digits
digit_size = 32
figure = np.zeros((digit_size * n, digit_size * n, 3))
# We will sample n points within [-15, 15] standard deviations
grid_x = np.linspace(-15, 15, n)
grid_y = np.linspace(-15, 15, n)

for i, yi in enumerate(grid_x):
for j, xi in enumerate(grid_y):
z_sample = np.array([[xi, yi]])
x_decoded = decoder.predict(z_sample)
digit = x_decoded[0].reshape(digit_size, digit_size, 3)
figure[i * digit_size: (i + 1) * digit_size,
j * digit_size: (j + 1) * digit_size,
: ] = digit

plt.figure(figsize=(10, 10))
plt.imshow(figure)
plt.show()


Related Topics



Leave a reply



Submit