如何用张量流建立多输入图?

时间:2016-10-29 11:07:43

标签: neural-network tensorflow backpropagation

是否可以定义具有多个输入的TensorFlow图? 例如,我想给图形两个图像和一个文本,每个图像由一堆图层处理,最后有一个fc图层。然后有一个节点计算有损函数,该函数考虑了三个表示。目的是考虑到联合代表性的损失,让三个网络反向传播。 可能吗?有关它的任何示例/教程? 提前谢谢!

1 个答案:

答案 0 :(得分:15)

这是完全直截了当的事情。对于“一个输入”,你会有类似的东西:

def build_column(x, input_size):

    w = tf.Variable(tf.random_normal([input_size, 20]))
    b = tf.Variable(tf.random_normal([20]))
    processing1 = tf.nn.sigmoid(tf.matmul(x, w) + b)

    w = tf.Variable(tf.random_normal([20, 3]))
    b = tf.Variable(tf.random_normal([3]))
    return tf.nn.sigmoid(tf.matmul(processing1, w) + b)

input1 = tf.placeholder(tf.float32, [None, 2])
output1 = build_column(input1, 2) # 2-20-3 network

您只需添加更多此类“列”并随时将其合并

input1 = tf.placeholder(tf.float32, [None, 2])
output1 = build_column(input1, 2)

input2 = tf.placeholder(tf.float32, [None, 10])
output2 = build_column(input1, 10)

input3 = tf.placeholder(tf.float32, [None, 5])
output3 = build_column(input1, 5)


whole_model = output1 + output2 + output3 # since they are all the same size

你会得到一个类似的网络:

 2-20-3\
        \
10-20-3--SUM (dimension-wise)
        /
 5-20-3/

或制作单值输出

w1 = tf.Variable(tf.random_normal([3, 1]))
w2 = tf.Variable(tf.random_normal([3, 1]))
w3 = tf.Variable(tf.random_normal([3, 1]))

whole_model = tf.matmul(output1, w1) + tf.matmul(output2, w2) + tf.matmul(output3, w3)

获取

 2-20-3\
        \
10-20-3--1---
        /
 5-20-3/