whitepanda
whitepanda

Reputation: 488

Value Error: Incompatible layer in prediction method

During my training steps, I am using predict method to keep track of the test error at each iteration. For instance, when I call the method at a given step with the array shown below.

 [[ 85.  99.   1.  39.  97.  45.  73.  26.  11.  93.]
 [ 85.  99.   1.  39.  97.  39.  73.  22.  16.  93.]
 [ 31.   5.   5. 100.  86.   5.  61.  38.  12.  65.]
 [ 31.   7.  15. 100.  86.  16.  61.  38.  57.  65.]
 [ 73.   4.  22.  21.  14.  49.  27.  54.  94.  87.]
 [ 73.   1.   6.   2.   2.  49.  27.  54.  94.  87.]]

I am getting my prediction result properly as

[[288.843  ]
 [297.50165]
 [214.63228]
 [234.74095]
 [240.8646 ]
 [238.9101 ]]

where the function that I call is

print(model.predict(my_data_set))

For some reason, I am receiving an incompatible layer error, when I call print(model.predict(my_data_set[0])) which corresponds to the first row of my data set.

ValueError: Input 0 of layer dense_22 is incompatible with the layer: expected axis -1 of input shape to have value 10 but received input with shape (None, 1)

When I print my_data_set[0], what I obtain is [ 85. 99. 1. 39. 97. 45. 73. 26. 11. 93.]

I just don't know what potentially I am doing wrong.

Upvotes: 2

Views: 273

Answers (1)

Kaveh
Kaveh

Reputation: 4960

You should consider the batch dimension for your input data. Your input data shape should be like (number_of_samples, number_of_features) (a 2D array).

When you pass my_data_set it is a 2D array, and its shape represents (6 samples, 10 features), but when you pass my_data_set[0] it is a 1D array and its shape is like (10 features), while it should be 2D like (1 sample, 10 features). Check it like this:

print(np.array(my_data_set[0]).shape)
# (10,)
print(np.array(my_data_set[0]).reshape(1,-1).shape)
# (1, 10) 

So, try to pass it like this to add the first dimension as 1:

model.predict(np.array(my_data_set[0]).reshape(1,-1))

Upvotes: 1

Related Questions