【问题标题】:How to build a multiple input graph with tensor flow?如何使用 tensorflow 构建多输入图?
【发布时间】:2017-03-12 03:15:39
【问题描述】:

是否可以定义具有多个输入的 TensorFlow 图? 例如,我想给图表两个图像和一个文本,每个都由一堆层处理,最后一个 fc 层。然后有一个节点计算考虑到这三种表示的损失函数。目的是让三个网络在考虑联合表示损失的情况下进行反向传播。 可能吗?有任何关于它的示例/教程吗?

【问题讨论】:

    标签: neural-network tensorflow backpropagation


    【解决方案1】:

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

    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/
    

    【讨论】:

    • 感谢 Lejlot!一个后续问题:上面的 build_column() 没有被重用,对吗?如果我们想对两个图像输入使用相同的 build_column() 参数然后连接输出怎么办?
    猜你喜欢
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2018-08-11
    • 2018-06-23
    相关资源
    最近更新 更多