如何在python中调用多个生成器
我尝试了部分数据的三重丢失方法。每个数据都将输入到模型中。 我为部分数据制作了一个生成器。但我这里有个问题 如何为部分数据获取多个生成器:如何在python中调用多个生成器,python,generator,Python,Generator,我尝试了部分数据的三重丢失方法。每个数据都将输入到模型中。 我为部分数据制作了一个生成器。但我这里有个问题 如何为部分数据获取多个生成器: def batch_generator(batch_size, num_part, X): y_val = np.zeros((batch_size, 2, 1)) anchors = np.zeros((batch_size, num_part, input_shape[0], input_shape[1], input_shape[2])
def batch_generator(batch_size, num_part, X):
y_val = np.zeros((batch_size, 2, 1))
anchors = np.zeros((batch_size, num_part, input_shape[0], input_shape[1], input_shape[2])) # (16,4,3,128,128)
positives = np.zeros((batch_size, num_part, input_shape[0], input_shape[1], input_shape[2])) # (16,4,3,128,128)
negatives = np.zeros((batch_size, num_part, input_shape[0], input_shape[1], input_shape[2])) # (16,4,3,128,128)
while True:
for i in range(batch_size):
positiveFace = np.random.randint(len(X))
negativeFace = np.random.randint(len(X))
while positiveFace == negativeFace:
negativeFace = np.random.randint(len(X))
# Face encoding generate images to partial images (4 part)
positives[i] = face_img_to_encoding(X[positiveFace])
anchors[i] = face_img_to_encoding(X[positiveFace])
negatives[i] = face_img_to_encoding(X[negativeFace])
x_data_1 = {'anchor': anchors[:,0], # get data (16,0,3,128,128)
'anchorPositive': positives[:,0],
'anchorNegative': negatives[:,0]
}
x_data_2 = {'anchor': anchors[:,1], # get data (16,1,3,128,128)
'anchorPositive': positives[:,1],
'anchorNegative': negatives[:,1]
}
x_data_3 = {'anchor': anchors[:,2], # get data (16,2,3,128,128)
'anchorPositive': positives[:,2],
'anchorNegative': negatives[:,2]
}
x_data_4 = {'anchor': anchors[:,3], # get data (16,3,3,128,128)
'anchorPositive': positives[:,3],
'anchorNegative': negatives[:,3]
}
yield (x_data_1, [y_val, y_val, y_val])
yield (x_data_2, [y_val, y_val, y_val])
yield (x_data_3, [y_val, y_val, y_val])
yield (x_data_4, [y_val, y_val, y_val])
当我打电话时:
gen = batch_generator(BATCH_SIZE, NUM_PARTIAL, X)
print(type(gen)) #1
print(type(next(gen))) #2
print(type(next(gen))) #3
print(type(next(gen))) #4
其产出:
#1 <type='generator'>
#2 <type='tuple'>
#3 <type='tuple'>
#4 <type='tuple'>
#1
#2
#3
#4
如何使所有生成器输出?您只需调用next(gen)
4次即可获得值。第一个创建生成器,但在调用next(gen)
之前不会生成值:
或者,您可以将其解包为4个变量:
gen = batch_generator(BATCH_SIZE, NUM_PARTIAL, X)
x1, x2, x3, x4 = gen
您是否有理由yield
ing这些值,而不仅仅return
ing它们。这看起来像是对yield
的一种奇怪用法,您只需调用next(gen)
4次即可获得值。第一个创建生成器,但在调用next(gen)
之前不会生成值:
或者,您可以将其解包为4个变量:
gen = batch_generator(BATCH_SIZE, NUM_PARTIAL, X)
x1, x2, x3, x4 = gen
您是否有理由
yield
ing这些值,而不仅仅return
ing它们。这看起来像是yield
的一个奇怪用法,我已经试过了,但仍然有一个错误。有没有关于在多台发电机/型号上使用三重态损耗的参考资料?我已经试过了,但仍然有一个错误。是否有与使用多台发电机/型号的三重网损耗相关的参考资料?