• tensorflow2generate训练多输入,多输出


     np.random.seed(1)
        def combineGenerator(x_l,x_a,x_v,batch_size):
            index = 0
            while True:
                start = index % x_l.shape[0]
                end = start + batch_size
                ################ l_mask ################
                bat_l = x_l[start:end]
                l_mask1 = get_mask(bat_l, mask_rate=0.1, mask_lenth=1)
                l_mask2 = get_mask(bat_l, mask_rate=0.1, mask_lenth=2)
                l_mask = l_mask1 + l_mask2
                l_mask = np.where(l_mask >= 1, 1, 0)
                l_output = l_mask * bat_l
                l_output = l_output.astype(np.float32)
                ################ a_mask ################
                bat_a = x_a[start:end]
                a_mask1 = get_mask(bat_a, mask_rate=0.1, mask_lenth=1)
                a_mask2 = get_mask(bat_a, mask_rate=0.1, mask_lenth=2)
                a_mask = a_mask1 + a_mask2
                a_mask = np.where(a_mask >= 1, 1, 0)
                a_output = a_mask * bat_a
                a_output = a_output.astype(np.float32)
                ################ v_mask ################
                bat_v = x_v[start:end]
                v_mask1 = get_mask(bat_v, mask_rate=0.1, mask_lenth=1)
                v_mask2 = get_mask(bat_v, mask_rate=0.1, mask_lenth=2)
                v_mask = v_mask1 + v_mask2
                v_mask = np.where(v_mask >= 1, 1, 0)
                v_output = v_mask * bat_v
                v_output = v_output.astype(np.float32)
                # bat_y = y[start:end] * (1-mask_mat[:,:,:,np.newaxis])
                index += batch_size
    
                # yield (bat_x_mask, bat_y_mask)
                yield [bat_l,bat_a,bat_v,l_mask,a_mask,v_mask],{'mult__model':l_output,'mult__model_1':a_output,'mult__model_2':v_output}
    
    
        train_generator = combineGenerator(l_train,a_train,v_train,batch_size=bat)
        test_generator = combineGenerator(l_val,a_val,v_val, batch_size=bat)
    
        v_input = tf.keras.layers.Input(shape=(500,35))
        a_input = tf.keras.layers.Input(shape=(500,74))
        l_input = tf.keras.layers.Input(shape=(50,300))
        v_mask_input = tf.keras.layers.Input(shape=(500, 1))
        a_mask_input = tf.keras.layers.Input(shape=(500, 1))
        l_mask_input = tf.keras.layers.Input(shape=(50, 1))
        out_l,out_a,out_v,out = model(l_input,a_input,v_input,l_mask =l_mask_input,a_mask =a_mask_input,v_mask =v_mask_input)
        pre_model = keras.models.Model(inputs = [l_input,a_input,v_input,l_mask_input,a_mask_input,v_mask_input],outputs = [out_l,out_a,out_v])
        opt = tf.keras.optimizers.Adam(lr=lr_rate,clipvalue=1.)
        history2 = LossHistory_early_stop(which_test, epochs, bat, lr_rate,)
        # early_stoping = EarlyStopping(monitor='val_loss', patience=patience, restore_best_weights=True, mode='min')
        early_stoping = EarlyStopping(monitor='val_weighted_accuracy', patience=patience, restore_best_weights=True, mode='max')
        pre_model.compile(loss={'mult__model':tf.losses.MSE,'mult__model_1':tf.losses.MSE,'mult__model_2':tf.losses.MSE}, optimizer=opt, loss_weights=[100,0.5,30])
        # my_model.compile(loss=tf.losses.MSE, optimizer=opt, metrics=[weighted_accuracy])
        # my_model.compile(loss=tf.losses.MSE, optimizer=opt)
        pre_model.fit(train_generator,validation_data=test_generator,steps_per_epoch=v_train.shape[0]//bat, validation_steps=v_val.shape[0]//bat,
                      epochs=epochs, batch_size=bat,callbacks=[early_stoping,history2])
  • 相关阅读:
    Windows性能调优: Perfomn.exe 和Perfmon /res
    WPF:逻辑树和视觉树
    AD FS 概述
    SQL Server : TRUSTWORTHY 数据库属性
    WCF:在开发期间应该注意的问题
    ASP.NET MVC 2中的数据验证
    SQL Server:如何在Service Broker发送消息验证失败后获取源消息
    GDI+:自定义控件时如何使用Region来输出特定区域
    LINQ to XML:如何替换XCData的内容
    javascript是否真的就不能实现跨站请求呢?
  • 原文地址:https://www.cnblogs.com/cxhzy/p/16249605.html
Copyright © 2020-2023  润新知