簡體   English   中英

如何組合(合並)不同的回歸模型

[英]how to combine (merge) different regression models

我正在為不同的人體姿勢問題估計訓練不同的模型。 實際上,我需要的是從人體不同關節的回歸模型中獲得不同的輸出。 在我搜索這個問題之后,我想出了這個想法,我有兩種方法:

  1. 訓練不同的模型並結合它們的最終結果。
  2. 以鏈狀訓練模型。 (第二個模型的輸入是第一個模型的輸出和...)

我知道 Keras 有一個名為 concatenate 的函數,它是一個合並模型的兩個輸出的層。 但是如果我不想使用 Keras,是否有可能有 6 個模型,然后以一種最終訓練的模型可以一次估計這些不同模型的所有輸出的方式合並它們?

我的模型是這樣的(它們根據我擁有的不同數據集而不同):

 ## 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 來實現這一點。 我添加了一個簡單的示例,您可以根據您的用例將此示例調整為更復雜的模型。

代碼:

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() 訓練這個模型:

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 行。

您可以像這樣使用此模型進行預測:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM