是否有可能具有一个以上输出的回归模型?

问题描述

我正在使用Tensorflow处理CNN回归模型。我想知道是否可以使用回归从我的数据集中估计多个数据? (换句话说,我想通过头部和双手的位置和旋转来估计人体的肩膀和肘部的位置(x,y,z)和旋转(俯仰,偏航,侧倾))

因此,我的模型的输出应该像每个关节的6个值一样。 (例如:肘部) 这也是我的代码示例:(我使用tf.session来训练模型)

# define placeholder for inputs to network
xs = tf.placeholder(tf.float32,[None,42],name ='input_node')
ys = tf.placeholder(tf.float32,1]) 
keep_prob = tf.placeholder(tf.float32) 

#Network computations and Layers
x_image = tf.reshape(xs,[-1,3,1])  
## conv1 layer
W_conv1 = weight_func([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,32,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,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,128,256]) 
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([3 * 3 * 256,2304])
b_fc1 = bias_func([256])

h_pool2_flat = tf.reshape(h_conv4,3* 3 * 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 ## full connection
W_fc2 = weight_func([2304,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]))

sess = tf.Session()
train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy)
sess.run(tf.global_variables_initializer())
init=tf.global_variables_initializer()
for i in range(50):
  sess.run([train_step],Feed_dict = {xs: train_x,ys: train_y,keep_prob: 0.7})
prediction_value = sess.run(prediction,Feed_dict={xs: test_x,ys: test_y,keep_prob: 1.0})`

解决方法

当然,只需创建第二个输出。不幸的是,我无法告诉您如何针对Tensorflow 1.14做到这一点,但是在TF2.0中就像在其他模型中一样:

output_categorical = layer.Dense(5,activation="softmax")(dense_layer_out_cat)
output_continuus = layer.Dense(1,activation="sigmoid")(dense_layer_out_con)
model = tf.keras.Model(inputs=[layer_input_categorical,layer_input_categorical_2,layer_input_continuus],\
                       outputs=[output_categorical,output_continuus])
model.compile(optimizer="Nadam",loss=["mse","sparse_categorical_crossentropy"])

在这段代码中,您可以看到一个模型,输出一个回归值和一个分类值(用于分类)。就是这样,没有大的魔力。只需创建两个具有S型激活的输出层,告诉模型将两个层都用作输出,定义 two 即可!损失函数(如果进行6次回归,则可能是6次),仅此而已。 当然,拥有多个输出也意味着您需要与输出一样多的y标签值,因此在准备数据时请记住这一点。