如何使用张量流构建多输入图? [英] How to build a multiple input graph with tensor flow?

查看:21
本文介绍了如何使用张量流构建多输入图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to define a TensorFlow graph with more than one input? For instance, I want to give the graph two images and one text, each one is processed by a bunch of layers with a fc layer at the end. Then there is a node that computes a loss function that takes into account the three representations. The aim is to let the three nets to backpropagate considering the joint representation loss. Is it possible? any example/tutorial about it?

推荐答案

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

This is completely straight forward thing. For "one input" you would have something like:

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

并且您可以简单地添加更多这样的列"并随时合并它们

and you can simply add more such "columns" and merge them anytime you want

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

你会得到如下所示的网络:

and you will get network which looks like:

 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/

这篇关于如何使用张量流构建多输入图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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