简体   繁体   English

如何加载多个训练和有效性数据来训练和验证 keras 模型

[英]how to load multiple training and validity data to train and validate a keras model

I have both training and validity data stored in train_data and valid_data folders.In both the folders data stored in .npz file.我将训练数据和有效性数据都存储在 train_data 和 valid_data 文件夹中。在这两个文件夹中,数据都存储在 .npz 文件中。 And each .npz file contain target and label data with same shape =(1024,28)(for example target=tu.npz['data1'] and label=tu.npz['data2']. I want to load them to a Keras model by a manner similar to ImageDataGenerator and want to train and validate the model, so I wrote and tried different custom generators but it is not working, here is my code. I hope somebody will help me.Thanks.每个 .npz 文件都包含相同形状的目标和标签数据 =(1024,28)(例如 target=tu.npz['data1'] 和 label=tu.npz['data2']。我想将它们加载到以类似于 ImageDataGenerator 的方式创建 Keras 模型并希望训练和验证模型,所以我编写并尝试了不同的自定义生成器,但它不起作用,这是我的代码。我希望有人能帮助我。谢谢。

def tf_train_generator(file_list, batch_size = 1):
    i = 0
    while True:
        if i*batch_size >= len(file_list):  
            i = 0
            np.random.shuffle(file_list)
        else:
            file_chunk = file_list[i*batch_size:(i+1)*batch_size]
            print(len(file_chunk))      


            for file in file_chunk:
                print(file)
                temp = np.load(file)

               
                X = temp['data1']
               
                Y= temp['data2']  


               
                i = i + 1
                yield X, Y

Move the (i = i + 1 and yield X, Y) out of the for loop.将 (i = i + 1 and yield X, Y) 移出 for 循环。

    i= 0
    def tf_train_generator(file_list, batch_size = 1):
        global i
        while True:
            if i*batch_size >= len(file_list):  
                i = 0
                np.random.shuffle(file_list)
            else:
                file_chunk = file_list[i*batch_size:(i+1)*batch_size]
                print(len(file_chunk))      
    
    
                for file in file_chunk:
                    print(file)
                    temp = np.load(file)
    
                   
                    X = temp['data1']
                    Y= temp['data2'] 
                   
            i = i + 1
            yield X, Y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 Keras model 加载后会进行训练,即使我实际上没有提供任何新的训练数据? - Why does my Keras model train after I load it, even though I have not actually supplied any new training data? 如何用多个输入训练 Keras model? - How to train Keras model with multiple inputs? 如何在多输出 model 训练期间加载数据,而无需在 Keras 中进行迭代? - How to load data during training of a multi-output model without iteration in Keras? 如何使用多个单独的训练数据训练 LSTM 模型? - How to train a LSTM model with multiple separate training datas? 使用多个输入训练 Keras 模型 - Training Keras model with multiple inputs 如何加载 keras model 并从停止的地方重新开始训练 - How to load a keras model and restart the training from where it stopped 如何在 Tensorflow 2.2 中使用多个输入训练 Keras model? - How to train Keras model with multiple inputs in Tensorflow 2.2? 如何使用单个数据集在 tensorflow keras 中训练多个输入 model - how to use a single dataset to train multiple input model in tensorflow keras 如何使用 Tensorflow 2/Keras 保存和继续训练具有多个模型部分的 GAN - How to save and resume training a GAN with multiple model parts with Tensorflow 2/ Keras Keras load_model在训练后无法正常工作 - Keras load_model not working after training
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM