代码之家  ›  专栏  ›  技术社区  ›  Kleyson Rios

将fit_生成器与连体网络一起使用时出错

  •  0
  • Kleyson Rios  · 技术社区  · 7 年前

    我正在尝试适应 Keras MNIST Siamese example 使用发电机。

    example 我们有:

    model.fit([tr_pairs[:, 0], tr_pairs[:, 1]], tr_y,
              batch_size=128,
              epochs=epochs,
              validation_data=([te_pairs[:, 0], te_pairs[:, 1]], te_y))
    

    为了找出发电机需要返回的形状,我做到了:

    np.array([tr_pairs[:, 0], tr_pairs[:, 1]]).shape
    

    并且得到

    (2, 108400, 28, 28)
    

    然后我的发电机会返回这个:

    (data, labels) = my_generator
    data.shape
    (2, 6, 300, 300, 3)
    labels.shape
    (6,)
    

    所以,它是两个数组(用于nn输入),有6个图像(批量大小)大小 300x300x3 (RGB)。

    下面是 fit_generator() 用途:

    ...
    input_shape = (300, 300, 3)
    ...
    model.fit_generator(kbg.generate(set='train'), 
                        steps_per_epoch=training_steps,
                        epochs=1,
                        verbose=1,
                        callbacks=[],
                        validation_data=kbg.generate(set='test'),
                        validation_steps=validation_steps,
                        use_multiprocessing=False,
                        workers=0)  
    

    我想我是用同样的形状喂神经网络,但我得到了以下错误:

    ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead gotthe following list of 1 arrays: [array([[[[[0.49803922, 0.48235294, 0.55686275],
              [0.63137255, 0.61176471, 0.64313725],
              [0.8627451 , 0.84313725, 0.84313725],
              ...,
              [0.58823529, 0.64705882, 0.631...
    

    怎么了?

    1 回复  |  直到 7 年前
        1
  •  1
  •   today    7 年前

    因为模型已经 两个输入层 发电机应输出 两个数组的列表 作为两个输入层对应的输入样本,如下所示:

    def my_generator(args):
        # ...
        yield [first_pair, second_pair], labels
    

    哪里 first_pair second_pair 两者的形状都是 (n_samples, 300, 300, 3) .

    推荐文章