tensorflow 中的条件图和访问张量大小的 for 循环 [英] conditional graph in tensorflow and for loop that accesses tensor size

查看:43
本文介绍了tensorflow 中的条件图和访问张量大小的 for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先是广泛的问题:

  • 是否可以使用 tensorflow 构建条件图?
  • 如果是,自动梯度计算和实现的优化器是否可以使用它?
  • 我能否访问张量的形状并将其转换为整数,以便与if"条件和for i in range()"循环一起使用?

我的实际用例是我想做一个张量长度可变的一维卷积.为此,我首先需要一个 if 语句,该语句仅在长度大于 1 时才执行卷积.然后我有一个 for 循环,它通过张量进行卷积.问题是这段代码:

My actual use case is that I want to do a 1D convolution with variable tensor length. For this I first need an if statement that only executes the convolution if the length is greater then one. Then I have a for loop that goes through the tensor for the convolution. The problem is that this code:

for i in range(tf.shape(tensor)[0]): 

不起作用,因为范围运算符需要一个整数.我可以以某种方式将其转换为整数吗?

doesn't work because the range operator needs an integer. Can I turn this somehow into an integer?

最后我想用 adagrad 训练这个模型,无论是自动微分还是已经实现的优化器

In the end I want to train this model with adagrad, either with the automatic differentiation or the already implemented optimiser

这是一维卷积,稍后将成为我模型中两层中的第一层.类型错误位于触发一个的 for 循环的每个版本之后

this is the 1D convolution which will later be the first of two Layers in my Model. The type errors are behind each version of the for loop that triggers one

import tensorflow as tf 
import numpy as np 

def convolve(s, Tl, Tr, b):

    if (tf.shape(s)[0] == 1):
        return s

    sum = 0

    # for i in range(tf.shape(s)[0] - 1): # error: TypeError: range() integer end argument expected, got Tensor
    # for i in range(s._shape._dims[0]._value - 1): # error: TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
    for i in range(s.get_shape().as_list()[0] - 1): # error: TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

        sum += tf.tanh(tf.matmul(Tl,s[i]) + tf.matmul(Tr, s[i+1]) + b)

    return sum

ModelSize = 3

# tensor to be convolved
s = tf.placeholder("float", shape = [None, ModelSize])

# initialise weights
Tl = tf.Variable(tf.random_normal(shape=[ModelSize,ModelSize], stddev = 0.1 ))
Tr = tf.Variable(tf.random_normal(shape=[ModelSize,ModelSize], stddev = 0.1 ))
b = tf.Variable(tf.random_normal(shape=[ModelSize], stddev = 0.1 ))

#convolution
s_convolved = convolve(s, Tl, Tr, b)

# initialise variables.
init = tf.initialize_all_variables()

# run graph
sess = tf.Session()
sess.run(init)

# test data
s_dataLong = np.random.random((2,5,ModelSize))
s_dataShort = np.random.random((2,1,ModelSize))

for s_dataPart in s_dataLong:
    print sess.run(s_convolved, feed_dict = {s : s_dataPart})

for s_dataPart in s_dataShort:
    print sess.run(s_convolved, feed_dict = {s : s_dataPart})

推荐答案

我建议您以不同的方式编写每个问题.否则它会因为太宽而被关闭.

I recommend you to write each question differently. Otherwise it will be closed as too broad.

我只能回答你的第三个问题.如何以编程方式获取张量的形状.您正确使用 shape 来获取张量的形状,但您仍然在运行图表之前无法获得结果(查看我的此处的说明).

I can answer only your 3-rd question. How to programmatically get a shape of the tensor. You are correctly using shape to get the shape of the tensor, but you still can not get the results before you run the graph (look at my explanation here).

a = tf.truncated_normal([2, 3], mean=0.0, stddev=0.02, dtype=tf.float32, seed=1)
b = tf.shape(a)
sess = tf.Session()
print sess.run(b) # will give you [2 3]

我发现在不运行图形的情况下从常量获取形状的丑陋方法是执行以下操作(不知道为什么需要它):

The ugly way that I have found to get the shape from constants, without running the graph is to do something like (do not really know why would you need it):

print a._shape._dims[0]._value
print a._shape._dims[1]._value

要从变量中获取形状,您可以这样做:

To get the shape from a variable, you can do this:

weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35))
print weights.get_shape().as_list()

在评估之前访问张量形状的另一种方法是:tf.Tensor.get_shape()

Another way to access a shape of a Tensor before the evaluation is: tf.Tensor.get_shape()

这篇关于tensorflow 中的条件图和访问张量大小的 for 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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