简体   繁体   English

将数据拟合到模型中时出现Keras错误

[英]Keras Error when fitting the data into the model

I have developed a keras model and trying to fit my data into the model. 我已经开发了一个keras模型,并试图将我的数据适合该模型。 Here is the code for model.fit 这是model.fit的代码

def train(run_name, start_epoch, stop_epoch, img_w):
# Input Parameters
    img_h = 64
    words_per_epoch = 300
    val_split = 0.2
    val_words = int(words_per_epoch * (val_split))

    # Network parameters
    conv_filters = 16
    kernel_size = (3, 3)
    pool_size = 2
    time_dense_size = 32
    rnn_size = 256
    minibatch_size = 32

    if K.image_data_format() == 'channels_first':
        input_shape = (1, img_w, img_h)
    else:
        input_shape = (img_w, img_h, 1)


    act = 'relu'
    input_data = Input(name='the_input', shape=input_shape, dtype='float32')
    inner = Conv2D(conv_filters, kernel_size, padding='same',
                   activation=act, kernel_initializer='he_normal',
                   name='conv1')(input_data)
    inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max1')(inner)
    inner = Conv2D(conv_filters, kernel_size, padding='same',
                   activation=act, kernel_initializer='he_normal',
                   name='conv2')(inner)
    inner = MaxPooling2D(pool_size=(pool_size, pool_size), name='max2')(inner)

    conv_to_rnn_dims = (img_w // (pool_size ** 2), (img_h // (pool_size ** 2)) * conv_filters)
    inner = Reshape(target_shape=conv_to_rnn_dims, name='reshape')(inner)

    # cuts down input size going into RNN:
    inner = Dense(time_dense_size, activation=act, name='dense1')(inner)

    # Two layers of bidirectional GRUs
    # GRU seems to work as well, if not better than LSTM:
    gru_1 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru1')(inner)
    gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru1_b')(inner)
    gru1_merged = add([gru_1, gru_1b])
    gru_2 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru2')(gru1_merged)
    gru_2b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru2_b')(gru1_merged)

    # transforms RNN output to character activations:
    # print("Output Size",img_gen.get_output_size())

    inner = Dense(47, kernel_initializer='he_normal',
                  name='dense2')(concatenate([gru_2, gru_2b]))
    y_pred = Activation('softmax', name='softmax')(inner)
    # Model(inputs=input_data, outputs=y_pred).summary()

    labels = Input(name='the_labels', shape=[10], dtype='float32')
    input_length = Input(name='input_length', shape=[1], dtype='int64')
    label_length = Input(name='label_length', shape=[1], dtype='int64')
    # Keras doesn't currently support loss funcs with extra parameters
    # so CTC loss is implemented in a lambda layer
    loss_out = Lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length])

    # clipnorm seems to speeds up convergence
    sgd = SGD(lr=0.02, decay=1e-6, momentum=0.9, nesterov=True, clipnorm=5)

    model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)

    # the loss calc occurs elsewhere, so use a dummy lambda func for the loss
    model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd)

    test_func = K.function([input_data], [y_pred])

    # viz_cb = VizCallback(run_name, test_func, img_gen.next_val())
    (X_train, y_train, train_input_length, train_labels_length), (X_test, y_test, test_input_length, test_labels_length) = dataset_load('./OCR_BanglaData.pkl.gz')
    print(y_train[0])

    X_train = X_train.reshape(X_train.shape[0], 128,64,1)
    X_test = X_test.reshape(X_test.shape[0], 128,64,1)
    X_train = X_train.astype('float32')
    X_test = X_test.astype('float32')

    model.fit((np.array(X_train), np.array(y_train),np.array(train_input_length), np.array(train_labels_length)), batch_size=32, epochs=120, verbose=1,validation_data=[np.array(X_test), np.array(y_test),np.array(test_input_length), np.array(test_labels_length)])

After this I am getting this error 之后,我得到这个错误

TypeError: Error when checking model input: data should be a Numpy array, or list/dict of Numpy arrays.

I tried to print the data types of each array. 我试图打印每个数组的数据类型。 The result is 结果是

<type 'numpy.ndarray'>

But still I am getting this error. 但是我仍然收到此错误。 Is there any specific reason for it? 有什么具体原因吗? I am using tensorflow model as my backend of keras. 我正在使用tensorflow模型作为我的keras后端。

Here you have 4 inputs: 在这里,您有4个输入:

model = Model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)

Each of the four inputs must be a numpy array, and the fit method wants them in a list (you're using a tuple): 四个输入中的每个必须是一个numpy数组,并且fit方法希望它们在列表中(您使用的是元组):

model.fit([X_train, y_train,train_input_length, train_labels_length],...)

And you're missing the outputs in the fit method. 而且您缺少fit方法中的输出。 Something must match what you defined as loss_out when creating the model. 在创建模型时,必须符合您定义为loss_out的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM