0

I am trying to create an autoencoder in Keras with Tensorflow backend. I followed this tutorial in order to make my own. Input to the network is kind of arbitrary i.e. each sample is a 2d array with fixed number of columns (12 in this case) but rows range between 4 and 24.

What I have tried so far is:

# Generating random data
myTraces = []
for i in range(100):
    num_events = random.randint(4, 24) 
    traceTmp = np.random.randint(2, size=(num_events, 12))

    myTraces.append(traceTmp)

myTraces = np.array(myTraces) # (read Note down below) 

and here is my sample model

input = Input(shape=(None, 12))

x = Conv1D(64, 3, padding='same', activation='relu')(input)

x = MaxPool1D(strides=2, pool_size=2)(x)

x = Conv1D(128, 3, padding='same', activation='relu')(x)

x = UpSampling1D(2)(x)
x = Conv1D(64, 3, padding='same', activation='relu')(x)

x = Conv1D(12, 1, padding='same', activation='relu')(x)

model = Model(input, x)
model.compile(optimizer='adadelta', loss='binary_crossentropy')

model.fit(myTraces, myTraces, epochs=50, batch_size=10, shuffle=True, validation_data=(myTraces, myTraces))

NOTE: As per Keras Doc, it says that input should be a numpy array, if I do so I get following error:

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

And if I dont convert it in to numpy array and let it be a list of numpy arrays I get following error:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 100 arrays: [array([[0, 1, 0, 0 ...

I don't know what I am doing wrong here. Also I am kind of new to Keras. I would really appreciate any help regarding this.

Muaz Usmani
  • 1,298
  • 6
  • 26
  • 48

1 Answers1

3

Numpy does not know how to handle a list of arrays with varying row sizes (see this answer). When you call np.array with traceTmp, it will return a list of arrays, not a 3D array (An array with shape (100, 1) means a list of 100 arrays). Keras will need a homogeneous array as well, meaning all input arrays should have the same shape.

What you can do is pad the arrays with zeroes such that they all have the shape (24,12): then np.array can return a 3-dimensional array and the keras input layer does not complain.

lmartens
  • 1,432
  • 2
  • 12
  • 19