Keras错误-“我们期望张量具有静态批大小”

时间:2019-04-26 02:21:26

标签: python-3.x tensorflow keras neural-network deep-learning

我正在尝试一种多输入模型,但会出错。这是我的代码:

### Features
toks_pad = pad_sequences(toks_feat, maxlen=maxlen, dtype  = 'object', value= None)
pos_pad  = pad_sequences(pos_feat,  maxlen=maxlen, dtype  = 'object', value= None)
print(f"toks_pad shape:{toks_pad.shape} | pos_pad shape {pos_pad.shape}")
  

toks_pad形状:(143,15)| pos_pad形状(143,15)

toks_pad[0]
  

array([无,无,无,无,无,无,无,无,无,          “听力损失”,“原因”,“原因”,“影响”,“老花眼”],         dtype = object)

print(f"ntoks:{ntoks} | npos:{npos} | ndep:{ndep} | nrel:{nrel} | maxlen:{maxlen}")
toks_inp   = Input(shape=(maxlen,), dtype='string', name='toks')
toks_emb   = layers.Embedding(input_dim = ntoks, output_dim = 32, input_length= maxlen)(toks_inp) ## for small testing data use GLOVE
toks_enc   = layers.LSTM(32)(toks_emb)

pos_inp    = Input(shape=(maxlen,), dtype='string', name='pos')
pos_emb    = layers.Embedding(input_dim = npos, output_dim = 32, input_length= maxlen)(pos_inp)   ## for small testing data use GLOVE
pos_enc    = layers.LSTM(32)(pos_emb)
  

ntoks:440 | npos:43 | ndep:43 |否:22 | maxlen:15

### Model
concat     = layers.concatenate([toks_enc, pos_enc], axis=-1)
answers    = layers.Dense(nrel,activation='softmax')(concat)
model      = Model([toks_inp, pos_inp], answers)
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['acc'])

model.fit({'toks': toks_pad, 'pos': pos_pad}, answers, epochs=5, batch_size=10)
  

ValueError:将符号张量馈送到模型时,我们期望   张量具有静态批大小。得到了张量形状:(无,22)

0 个答案:

没有答案