如何组合(合并)不同的回归模型 [英] how to combine (merge) different regression models

查看:66
本文介绍了如何组合(合并)不同的回归模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为不同的人体姿势问题估计训练不同的模型.实际上,我需要的是从人体不同关节的回归模型中获得不同的输出.在我搜索这个问题之后,我想出了这个想法,我有两种方法:

I am working on training different models for different estimation of human pose problems. actually, what I need is to get different outputs from a regression model for different joints of the human body. After I did searches for this problem, I come up with this idea that I have two ways:

  1. 训练不同的模型并结合它们的最终结果.
  2. 以链状训练模型.(第二个模型的输入是第一个模型的输出和...)

我知道 Keras 有一个名为 concatenate 的函数,它是一个合并模型的两个输出的层.但是如果我不想用 Keras 是不是可以有 6 个模型然后合并它们,这样最终训练的模型可以一次估计这些不同模型的所有输出?

I know Keras has a function called concatenate that is such a layer to merge two outputs of the models. But If I don't want to use Keras is it possible to have 6 models and then merge them in a way that the final trained model can estimate all the output of these different models at once?

我的模型是这样的(它们因我拥有的不同数据集而不同):

my models are something like this(they are different based on different datasets i have):

 ## conv1 layer
 W_conv1 = weight_func([3, 3, 1, 32])  
 b_conv1 = bias_func([32])
 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) 
 # h_pool1 = max_pool_2x2(h_conv1)     
 #h_drop1 = tf.nn.dropout(h_conv1, keep_prob) 

 ## conv2 layer
 W_conv2 = weight_func([3, 3, 32, 64])  # patch 2x2, in size 32, out size 64
 b_conv2 = bias_func([64])
 h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2)
 #h_drop2 = tf.nn.dropout(h_conv2, keep_prob)

 ## conv3 layer
 W_conv3 = weight_func([3, 3, 64, 128])  
 b_conv3 = bias_func([128])
 h_conv3 = tf.nn.relu(conv2d(h_conv2, W_conv3) + b_conv3)  
 #h_drop3 = tf.nn.dropout(h_conv3, keep_prob)  

 ## conv4 layer 
 W_conv4 = weight_func([3, 3, 128,256])  # patch 3*3, in size 32, out size 64
 b_conv4 = bias_func([256])
 h_conv4 = tf.nn.relu(conv2d(h_conv3, W_conv4) + b_conv4) 
 #h_drop4 = tf.nn.dropout(h_conv4, keep_prob)  

 ## fc1 layer
 W_fc1 = weight_func([6 * 6 * 256, 9216])
 b_fc1 = bias_func([9216])

 h_pool2_flat = tf.reshape(h_conv4, [-1, 6 * 6 * 256]) 
 h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
 h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 

 # fc2 layer 
 W_fc2 = weight_func([9216, 1])
 b_fc2 = bias_func([1])

 prediction = tf.add(tf.matmul(h_fc1_drop, W_fc2) , b_fc2, name= 'output_node')
 cross_entropy = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))

推荐答案

您可以使用 Functional API 来实现这一点.我添加了一个简单的示例,您可以根据您的用例将此示例调整为更复杂的模型.

You can use Functional API to achieve this. I have added a simple example you can adapt this example to more complicated models according to your usecase.

代码:

import tensorflow as tf
import numpy as np

# Here I have generated to different data and labels containing different number of features.
x1 = tf.constant(np.random.randint(50, size =(1000,13)), dtype = tf.float32)
y1 = tf.constant(np.random.randint(2, size =(1000,)), dtype = tf.int32)

x2 = tf.constant(np.random.randint(50, size =(1000,6)), dtype = tf.float32)
y2 = tf.constant(np.random.randint(2, size =(1000,)), dtype = tf.int32)

# Creation of model
def create_model3():
    input1 = tf.keras.Input(shape=(13,), name = 'I1')
    input2 = tf.keras.Input(shape=(6,), name = 'I2')
    
    hidden1 = tf.keras.layers.Dense(units = 4, activation='relu')(input1)
    hidden2 = tf.keras.layers.Dense(units = 4, activation='relu')(input2)
    hidden3 = tf.keras.layers.Dense(units = 3, activation='relu')(hidden1)
    hidden4 = tf.keras.layers.Dense(units = 3, activation='relu')(hidden2)
    output1 = tf.keras.layers.Dense(units = 2, activation='softmax', name ='O1')(hidden3)
    output2 = tf.keras.layers.Dense(units = 2, activation='softmax', name = 'O2')(hidden4)
    
    model = tf.keras.models.Model(inputs = [input1,input2], outputs = [output1,output2])
    
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    return model
model = create_model3()

tf.keras.utils.plot_model(model, 'my_first_model.png', show_shapes=True)

模型架构:

你可以像这样使用 model.fit() 训练这个模型:

You can train this model using model.fit() like this:

history = model.fit(
    x = {'I1':x1, 'I2':x2}, 
    y = {'O1':y1, 'O2': y2},
    batch_size = 32,
    epochs = 10,
    verbose = 1,
    callbacks = None,
#     validation_data = [(val_data,new_val_data),(val_labels, new_val_labels)]
)

注意:为了训练工作,所有输入数据中的样本数量应该相同.即 x1 包含 1000 行,所以 x2 也应该包含 1000 行.

Note: For training to work the number of samples in all your input data should be the same. ie x1 contains 1000 rows so x2 should also contain 1000 rows.

您可以像这样使用此模型进行预测:

You can predict using this model like this:

model.predict(x = {'I1':x1, 'I2':x2})

这篇关于如何组合(合并)不同的回归模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆