Paulo
Paulo

Reputation: 62

Numpy ndarray with different shapes for learning model in Keras

I have a list of numpy array with different shapes. I try to convert this list into a numpy array to create a batch sample for Keras. On output I want an array with the shapes (batch_size, ?, 20) where '?' is the variable dimension. I try this:

a = np.random.random((5,20))
b = np.random.random((2,20))
c = np.random.random((7,20))
d = [a,b,c]
np.array(d).shape
> (3,)

When I send this batch to Keras I have the following issue:

ValueError: Error when checking input: expected Input_Dim to have 3 dimensions, but got array with shape (3, 1)

Upvotes: 1

Views: 395

Answers (1)

Manualmsdos
Manualmsdos

Reputation: 1547

Maybe this simple example can help:

import numpy as np

a = np.random.random((5,20))
b = np.random.random((2,20))
c = np.random.random((7,20))

d = np.array([a,b,c])
print(d.shape) # (3,)

d = d[np.newaxis]
print(d.shape) # (1, 3)

Upvotes: 1

Related Questions