在keras中训练时,如何在每次迭代中将新模型追加到先前的模型中?

时间:2019-07-04 07:04:21

标签: python keras neural-network conv-neural-network training-data

在这里,我尝试通过在每次操作后保存模型来训练我的模型。与我不同,尽管几乎所有正则化技术都已应用,但模型总是过拟合。我认为我可能已经解决了以下详细描述的问题。

代码如下:

tr= mymodel()

def train(itr):
    for k in range (0,itr):
        #Select a random batch of 32 from the real images data set
        idx = np.random.randint(0, image.shape[0]*0.75, batch_size)
        idxx = np.random.randint(image.shape[0]*0.75+1,image.shape[0],8)

        #Select a random batch of 32 from the fake images data set
        idx1=np.random.randint(0,image1.shape[0]*0.75,batch_size)
        idxx1 = np.random.randint(image1.shape[0]*0.75+1,image1.shape[0],8)
        imgs = image[idx]
        igs  = image1[idx1]
        imgs1= image[idxx]
        igs1= image1[idxx1]
        #Concatenate the real and fake images (without shuffling)
        igss=np.concatenate((imgs,igs),axis=0)
        igss1=np.concatenate((imgs1,igs1),axis=0)
        es_callback = EarlyStopping(monitor='val_loss',patience=3)
        out=tr.fit(igss,valid,epochs=10,shuffle=True,validation_data=(igss1,valid1),callbacks=[es_callback])
        tr.save("VGG-scenetext-train-model2.h5")

在每次迭代中,我从训练数据中选择一个随机的32个批处理大小的图像,从验证数据中选择一个8个批处理大小的图像,然后在此数据上训练我的模型。

有效和有效1分别是训练和验证数据的标签。每次迭代后我都会保存模型。

我认为在每次迭代中都会替换上一步保存的模型,但是我需要做一些修改。我相信这就是模型过拟合的原因

我知道我可以在最后保存,但是我只想在所有的检查点都融合在一起的情况下实现检查点的概念

有可能吗?

如果是,请提供工作代码。

0 个答案:

没有答案