Home > Software design >  Use 2d matrix as input in Neural Network (Dense layer)
Use 2d matrix as input in Neural Network (Dense layer)

Time:09-24

I'm trying to fit a simple network using a 2D input.

Each sample is a 2D matrix of size (69,11), there are 100 samples in the following example.

After reading tons of doc and examples, I still can't wrap my head around how the input should be reshaped to make this work.

Below is a complete toy example of what I'm trying to debug


from random import randint, choice
import random
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D
import numpy as np

########## Create dummy data
n = 100 # Number of sample
y = np.array([randint(0,1) for i in range(n)])

dim1 = 69
dim2 = 11
X = []
for i in range(n):
    m = [ np.array([random.uniform(0, 1) for z in range(dim2)])
         for j in range(dim1)]
    X.append(m)

print(np.shape(X))
print(np.shape(y))

########## Build model
# define model
model = Sequential()

model.add(Dense(units=11, 
                input_shape =(dim1, dim2), # The input for each sample if matrix of size (dim1, dim2)
                activation='relu'))
model.add(Dense(units=11, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))

# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.build()
model.summary()

Model summary:

Model: "sequential_16"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_42 (Dense)             (None, 11)                770       
_________________________________________________________________
dense_43 (Dense)             (None, 1)                 12        
_________________________________________________________________
dense_44 (Dense)             (None, 1)                 2         
=================================================================
Total params: 784
Trainable params: 784
Non-trainable params: 0
_________________________________________________________________
######## Fit model
model.fit(X, y)

I've try different shape/reshape but I always the same error:

ValueError: Data cardinality is ambiguous: 
'Make sure all arrays contain the same number of samples'

Any help to understand what I am doing wrong would be much appreciated. Thanks!

CodePudding user response:

Try this

model.fit(np.asarray(X),y)

The reason is you're passing a list of lists of separate arrays (where each X row consists of multiple 1 dimensional arrays) i.e.:

X[0] = array([0.45123387, 0.87071843, 0.69511518, 0.26712839, 0.188283  ,
        0.08668649, 0.74399605, 0.86054076, 0.57857035, 0.84557558,
        0.89003992]),
 array([0.2674208 , 0.57081607, 0.40440186, 0.61519382, 0.31803269,
        0.50105886, 0.23100507, 0.39379568, 0.93477072, 0.44786942,
        0.83466262]),
 array([0.80509187, 0.89998605, 0.05668083, 0.23385173, 0.16405601,
        0.09092941, 0.30496969, 0.15214225, 0.66206999, 0.74718661,
        0.41340515]),
 array([0.89004127, 0.58602522, 0.27362169, 0.62423875, 0.24323739,
        0.3250751 , 0.93245436, 0.73747682, 0.01293388, 0.57064239,
        0.83064691]),
 array([0.14388339, 0.28463899, 0.19126419, 0.04180422, 0.22636071,
        0.54998281, 0.18228747, 0.46457567, 0.73606774, 0.11796856,
        0.87206122]),
 array([0.15999211, 0.52850844, 0.07298599, 0.34844709, 0.98739649,
        0.93398197, 0.87649617, 0.34047258, 0.4279549 , 0.2019767 ,
        0.98478869])]

Whereas doing np.asarray(X) will correctly join them together into a single 2 dim array per row:

np.asarray(X)[0] = array([[4.86305839e-01, 2.56291260e-01, 7.29621764e-01, 2.06063874e-01,
        5.83952341e-01, 2.53645318e-01, 9.04828007e-02, 3.64097487e-01,
        7.82886665e-01, 1.01666319e-01, 3.10953920e-01],
       [9.42449100e-01, 9.16760076e-01, 3.53338416e-01, 8.50019096e-01,
        6.11333665e-01, 1.48142485e-01, 4.51559551e-01, 5.05683894e-01,
        7.10124178e-01, 2.50465115e-02, 8.04407535e-01],
       [1.77165127e-01, 6.62900151e-01, 8.54372391e-01, 2.63979924e-01,
        5.49613031e-01, 8.01454545e-01, 9.55028259e-01, 6.99782063e-01,
        2.32189729e-02, 1.56046184e-01, 8.07771437e-01],
       [4.43235102e-01, 3.76750360e-01, 8.59820103e-02, 7.04480200e-01,
        4.27619788e-01, 6.30606778e-01, 5.55465460e-01, 2.05762014e-01,
        6.08090365e-01, 5.62663484e-01, 8.21047097e-01],

  • Related