I've seen a lot of posts about this kind of input shape error with Keras but I still don't get it.
I am trying to resolve a classification problem with Keras. My x_data
(for 1 example!) is an array of arrays (4 arrays of length 40). The number of classes to predict at the end is 5.
There is a mismatch between what the first input layer expect and what I give.
Here is my model:
self.model = Sequential()
self.model.add(Dense(32,input_shape=(4,40)))
self.model.add(Dense(5, activation='softmax'))
I think the shape should be (4,40)
because I give an array of 4 arrays of length 40. I think the output dimension of the last layer should be 5 because I have 5 possible classes.
Here is the format/type/shape of 1 X :
listVecteursStackBuffer: [[ 0.05371463 0.03648512 -0.01680853 0.06098862 0.01041014 0.02132057
0.06100727 -0.06400045 0.04484718 0.00590606 -0.03110709 0.02772235
0.0506371 0.01413589 -0.00423097 -0.03948138 0.01807033 -0.03295397
0.0076106 -0.01106769 -0.06772092 -0.03330935 0.03172214 0.03577476
-0.01993138 0.02483661 0.02799152 0.02824581 -0.03390008 0.04535389
0.04143018 0.06847147 -0.02706441 -0.01303677 -0.03396893 -0.02202406
-0.05207641 -0.00764029 -0.02636093 0.05962789]
[ 0.05371463 0.03648512 -0.01680853 0.06098862 0.01041014 0.02132057
0.06100727 -0.06400045 0.04484718 0.00590606 -0.03110709 0.02772235
0.0506371 0.01413589 -0.00423097 -0.03948138 0.01807033 -0.03295397
0.0076106 -0.01106769 -0.06772092 -0.03330935 0.03172214 0.03577476
-0.01993138 0.02483661 0.02799152 0.02824581 -0.03390008 0.04535389
0.04143018 0.06847147 -0.02706441 -0.01303677 -0.03396893 -0.02202406
-0.05207641 -0.00764029 -0.02636093 0.05962789]
[ 0.27279449 0.17842792 -0.63197666 0.27222419 -0.35665196 0.65482062
-0.04437634 -0.02701205 0.05825038 0.52313662 -0.47016799 0.66000813
-0.10626637 0.44926745 0.57915986 0.17286475 0.17684783 -0.30725989
0.61364847 0.0497554 0.00937996 -0.10325015 0.27216262 0.06826591
-0.58873838 0.46360832 0.39356059 0.31463432 -0.00937746 0.53426528
0.06023555 -0.08230764 -0.51719254 -0.45349783 -0.38294923 -0.29791942
0.01553993 -0.02182218 -0.06228495 -0.01348175]
[0.09909084 0.06724796 -0.02498062 0.14459084 0.09951171 0.04242199
0.0788445 -0.11882415 0.06127726 -0.10427625 -0.14457461 0.10393921
0.08281568 -0.02970352 -0.08088119 0.04706496 0.13566148 -0.06522857
0.05304977 -0.1190851 -0.13986765 0.02461384 -0.04025699 -0.02088463
-0.07833831 0.10640202 0.01630253 0.17543195 0.04066397 0.09964696
-0.05360637 0.1918643 -0.02574791 0.04924118 0.05970795 0.03912469
-0.180988 0.07615147 0.01991141 0.01771036]]
taille listVecteursStackBuffer: 4
type listVecteursStackBuffer: <class 'numpy.ndarray'>
shape listVecteursStackBuffer Tensor("Shape:0", shape=(2,), dtype=int32)
1er élément listVecteursStackBuffer: [ 0.05371463 0.03648512 -0.01680853 0.06098862 0.01041014 0.02132057
0.06100727 -0.06400045 0.04484718 0.00590606 -0.03110709 0.02772235
0.0506371 0.01413589 -0.00423097 -0.03948138 0.01807033 -0.03295397
0.0076106 -0.01106769 -0.06772092 -0.03330935 0.03172214 0.03577476
-0.01993138 0.02483661 0.02799152 0.02824581 -0.03390008 0.04535389
0.04143018 0.06847147 -0.02706441 -0.01303677 -0.03396893 -0.02202406
-0.05207641 -0.00764029 -0.02636093 0.05962789]
taille 1er element listVecteursStackBuffer: 40
type 1er element listVecteursStackBuffer <class 'numpy.ndarray'>
shape 1 elelistvSB Tensor("Shape_1:0", shape=(1,), dtype=int32)
I get an error with this line of code:
self.model.predict(config_repr)
This is the error:
Traceback (most recent call last):
File "arc_eagerTagger.py", line 818, in <module>
p.static_train2(depTreeList[1:2900]) # p.dynamic_train( treebank, step_size = 1, max_epochs = 10)
File "arc_eagerTagger.py", line 683, in static_train2
derivation = self.static_oracle_derivation(dtree,listVecteurs,dictionnaireMots)
File "arc_eagerTagger.py", line 260, in static_oracle_derivation
C = self.shift(C,sentence,listVecteurs,dictionnaireMots)
File "arc_eagerTagger.py", line 283, in shift
return (S + (w0,),B[1:],A,score+self.score(configuration,ArcEagerTransitionParser.SHIFT,tokens,listVecteurs,dictionnaireMots))
File "arc_eagerTagger.py", line 392, in score
return self.model.predict(config_repr)[0][index_action]
File "/home/netbook/anaconda3/lib/python3.6/site-packages/keras/models.py", line 909, in predict
return self.model.predict(x, batch_size=batch_size, verbose=verbose)
File "/home/netbook/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1499, in predict
check_batch_axis=False)
File "/home/netbook/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 128, in _standardize_input_data
str(array.shape))
ValueError: Error when checking : expected dense_1_input to have 3 dimensions, but got array with shape (4, 40)
I tried to change the input shape, I tried to concatenate the arrays to have one array of length 160 to facilitate the problem (and change the input shape with input_dim=160
) but I still get errors.