gpt4 book ai didi

python - 在暹罗网络中使用 fit_generator 时出错

转载 作者:行者123 更新时间:2023-11-30 08:57:28 29 4
gpt4 key购买 nike

我正在尝试调整 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) 的图像 (batch_size)。

以下是 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)

我想我正在向 NN 提供相同的形状,但出现以下错误:

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...

出了什么问题?

最佳答案

由于模型有两个输入层,生成器应该生成两个数组的列表作为与两个输入层对应的输入样本,如下所示:

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

其中 first_pairsecond_pair 的形状均为 (n_samples, 300, 300, 3)

关于python - 在暹罗网络中使用 fit_generator 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53978295/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com