错误输入形状不是预期的形状

时间:2018-01-20 13:28:04

标签: python keras

我在 Keras 上看过很多关于这种输入形状错误的帖子,但我仍然没有得到它。

我正在尝试解决Keras的分类问题。我的rfcn := function(const X: extended): extended begin result := Math.ArcSec(x); end; (例如!)是一个数组数组(4个长度为40的数组)。最后要预测的类数是5.

第一个输入层的期望与我给出的内容之间存在不匹配。

这是我的模特:

x_data

我认为形状应该是self.model = Sequential() self.model.add(Dense(32,input_shape=(4,40))) self.model.add(Dense(5, activation='softmax')) ,因为我给出了一个长度为40的4个数组的数组。我认为最后一层的输出维数应该是5,因为我有5个可能的类。

以下是1 X的格式/类型/形状:

(4,40)

我在这行代码中遇到错误:

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)

这是错误:

self.model.predict(config_repr)

我尝试更改输入形状,我尝试连接数组以使一个长度为160的数组来解决问题(并使用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) 更改输入形状)但我仍然会收到错误。

2 个答案:

答案 0 :(得分:0)

试试这个(batch_input_shape代替input_shape):

self.model.add(Dense(units=32, batch_input_shape=[4, 40]))

另见this answer

答案 1 :(得分:0)

问题不在input_shape中,而在于您提供给model.predict的数组形状。

model.predict需要一批示例,这意味着为了获得单个示例,其形状(在您的情况下)应为(1,4,40)。您可以使用numpy.expand_dims执行此操作:

self.model.predict(np.expand_dims(config_repr, axis = 0))