Search code examples
pythonnumpykerasregression

How to predict a list of values in keras?


I would like to train a neural network with keras. The input data are multiple arrays with shape (1023, 256) and the labels are a list of 10 values for each input.
I expect a list with 10 Values as output.

I simplified my code:

input1 = np.random.rand(1023, 256)
input2 = np.random.rand(1023, 256)
label1 = np.random.rand(10)
label2 = np.random.rand(10)
test_data = np.random.rand(1023, 256)


inputs = np.stack((input1, input2))
labels = np.stack((label1, label2))

print(inputs.shape)
print(labels.shape)




model = Sequential()


model.add(Dense(256, input_shape=(1023, 256), activation='relu'))


model.add(Dense(128, activation='relu'))


model.add(Dense(10, activation='linear'))

model.compile(optimizer='adam', loss='mean_squared_error')


model.fit(inputs, labels, epochs=10, batch_size=1, verbose=1)


predictions = model.predict(test_data)

The prediction should look something like this:

[ 8.06436593,  9.44189802, 10.44503002, 11.95312031, 14.43248845,
         3.63374982,  4.72397662,  5.54989524, 15.2114616 ,  7.70812219]

Output:

ValueError: Input 0 of layer "sequential_51" is incompatible with the layer: expected shape=(None, 1023, 256), found shape=(None, 256)

Update:

input1 = np.random.rand(1023, 256)
input2 = np.random.rand(1023, 256)
label1 = np.random.rand(10)
label2 = np.random.rand(10)
test_data = np.random.rand(1, 1023, 256)


inputs = np.stack((input1, input2))
labels = np.stack((label1, label2))

print("Input shape: ", inputs.shape)
print("Label shape: ",labels.shape)




model = Sequential()


model.add(Dense(256, input_shape=(1023, 256), activation='relu'))


model.add(Dense(128, activation='relu'))


model.add(Dense(10, activation='linear'))

model.compile(optimizer='adam', loss='mean_squared_error')


model.fit(inputs, labels, epochs=10, batch_size=1, verbose=0)


predictions = model.predict(test_data)

print(predictions.shape)

Output:

Input shape: (2, 1023, 256) Label shape: (2, 10) 1/1 [==============================] - 0s 83ms/step Prediction shape:
(1, 1023, 10)


Solution

  • You need to add the batch dimension in your test data, since Keras will consider always the first axis as the batches. So test data must have the shape (1, 1023, 256) instead of (1023, 256).

    test_data = np.expand_dims(test_data, axis=0)
    predictions = model.predict(test_data)
    

    I edited you model layers by adding Flatten before the last one (Dense 10) to get 1-dimensional data:

    input1 = np.random.rand(1023, 256)
    input2 = np.random.rand(1023, 256)
    label1 = np.random.rand(10)
    label2 = np.random.rand(10)
    test_data = np.random.rand(1, 1023, 256)
    
    
    inputs = np.stack((input1, input2))
    labels = np.stack((label1, label2))
    
    print("Input shape: ", inputs.shape)
    print("Label shape: ",labels.shape)
    
    
    
    
    model = Sequential()
    
    
    model.add(Dense(256, input_shape=(1023, 256), activation='relu'))
    
    
    model.add(Dense(128, activation='relu'))
    
    model.add(Flatten())
    model.add(Dense(10, activation='linear'))
    
    model.compile(optimizer='adam', loss='mean_squared_error')
    
    model.summary()
    model.fit(inputs, labels, epochs=10, batch_size=1, verbose=1)
    
    
    predictions = model.predict(test_data, batch_size=1)
    
    print(predictions.shape)