当“必须指定轴...”时如何使用Keras的valuate_generator()

时间:2018-11-25 05:50:23

标签: numpy keras

为什么Keras告诉我重量和批次大小不同?我怎样才能解决这个问题? (在此处添加print(li[0:-3:-2]) # is [] 无效)。 提前致谢;有些东西我只是没来这里。

错误发生在Evaluation_generator()上:next(...)

TypeError: Axis must be specified when shapes of a and weights differ.

为了进行比较,下面的代码可以在相同的发电机上运行。

from sklearn.utils import shuffle as identical_shuffle

SAMPLES_PER_BATCH=1
BATCHES_PER_EPOCH=1
BATCHES_PER_VALIDATION=1
EPOCHS_PER_SIMULATION=2

def generate_training(batch_size=64):
    validation_length = (int(len(data.train)*0.25) // batch_size) * batch_size
    while True:
        for i in range(validation_length,len(data.train),batch_size):
            x,y,n = identical_shuffle(data.train[i:i+batch_size],data.target[i:i+batch_size],data.context[i:i+batch_size])
            yield {'input':x,'target':n}, y

def generate_validation(batch_size=64):
    validation_length = (int(len(data.train)*0.25) // batch_size) * batch_size
    while True:
        for i in range(0,validation_length,batch_size):
            x,y,n = identical_shuffle(data.train[i:i+batch_size],data.target[i:i+batch_size],data.context[i:i+batch_size])
            yield {'input':x,'target':n}, y

for epoch in range(EPOCHS_PER_SIMULATION):
  for batch in range(BATCHES_PER_EPOCH):
    result_training = model.train_on_batch( *next(generate_training(batch_size=SAMPLES_PER_BATCH)) )
    # <redacted operations on result_training>
  result_validation = model.evaluate_generator( generate_validation(batch_size=SAMPLES_PER_BATCH), steps=BATCHES_PER_VALIDATION )

完整追溯

model.fit_generator(generator=generate_training(batch_size=SAMPLES_PER_BATCH),
                    validation_data=next(generate_validation(batch_size=SAMPLES_PER_BATCH)),
                    validation_steps=1,
                    steps_per_epoch=1,
                    epochs=1)

0 个答案:

没有答案