RNN CTC TensorFlow v2.0没有为“ time_distributed_1”提供数据

时间:2019-11-22 13:54:08

标签: python tensorflow keras ctc

我编写了一个代码,以使用TensorFlow v2.0实现用于自动语音识别的RNN CTC模型。我收到以下代码的错误No data provided for "time_distributed_1". Need data for each key in: ['time_distributed_1']。据我了解,上一层的输出将作为连续层的输入,但是该错误对我来说没有任何意义。

model = Sequential()
model.add(Input((None, 12), batch_size=1, dtype=tf.float32))
model.add(Bidirectional(LSTM(128, return_sequences=True), merge_Mode='sum'))
model.add(BatchNormalization())
model.add(Bidirectional(LSTM(128, return_sequences=True), merge_Mode='sum'))
model.add(BatchNormalization())
model.add(Bidirectional(LSTM(128, return_sequences=True), merge_Mode='sum'))
model.add(BatchNormalization())
model.add(TimeDistributed(Dense(28)))
model.add(Activation('softmax'))

seq_lens = tf.keras.Input((), batch_size=1, dtype=tf.int32, name='SequenceLength')

def loss(lbl_indices, y_pred):
    # Make Sparse Tensor
    lbl_len = lbl_indices.shape[1]
    indices = tf.keras.Input((2), batch_size=lbl_len, dtype=tf.int64, name='lblIndices')
    values = tf.keras.Input((), batch_size=lbl_len, dtype=tf.int32, name='lblValues')
    shape = tf.keras.Input((), batch_size=2, dtype=tf.int64, name='lblShape')
    labels = tf.SparseTensor(indices, values, dense_shape=shape)

    cost = tf.nn.ctc_loss(labels=labels,
                          logits=y_pred,
                          label_length=None,
                          logit_length=seq_lens,
                          logits_time_major=False,
                          blank_index=0)
    return cost

    # Decoder

x = tf.keras.Input((None, 12), batch_size=1)
decoder = tf.keras.backend.ctc_decode(y_pred=model(x), 
                                      input_length=tf.reshape(seq_lens, [-1]), 
                                      greedy=False, 
                                      beam_width=10, 
                                      top_paths=1)
best_path = decoder[0]

optimizer = tf.keras.optimizers.Adadelta(learning_rate=0.01, rho=0.95)
model.compile(optimizer=optimizer,
              loss=loss,
              metrics=['accuracy'],
              experimental_run_tf_function=False)

model.fit({'InputData':sample },
          {'lblIndices':lbl_indices2, 
           'lblValues':lbl_values, 
           'lblShape':lbl_shape, 
           'SequenceLength':[sample.shape[1]]
           },
          batch_size=1,
          epochs=10)

0 个答案:

没有答案