【问题标题】:how to load multiple training and validity data to train and validate a keras model如何加载多个训练和有效性数据来训练和验证 keras 模型
【发布时间】:2021-01-05 23:44:03
【问题描述】:

我将训练数据和有效性数据都存储在 train_data 和 valid_data 文件夹中。这两个文件夹中的数据都存储在 .npz 文件中。并且每个 .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

【问题讨论】:

    标签: python numpy keras deep-learning generator


    【解决方案1】:

    将 (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
    

    【讨论】:

    • @srhturc 这里是赋值前引用的错误局部变量“X”
    猜你喜欢
    • 2017-07-28
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2018-01-30
    相关资源
    最近更新 更多