Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为tf.split()的num_splits使用变量_Python_Tensorflow - Fatal编程技术网

Python 为tf.split()的num_splits使用变量

Python 为tf.split()的num_splits使用变量,python,tensorflow,Python,Tensorflow,可以为tf.split()的num_split参数使用占位符输入吗 理想情况下,我想做如下工作: num_splits = tf.placeholder(tf.int32) inputs = tf.placeholder(tf.int32, [5, None]) split_inputs = tf.split(1, num_splits, inputs) TypeError:参数“num\u split”应为int,而不是 我的方法可能有问题。我想用一个可变形状张量来列举一个维度。谢谢 核心

可以为tf.split()的num_split参数使用占位符输入吗

理想情况下,我想做如下工作:

num_splits = tf.placeholder(tf.int32)
inputs = tf.placeholder(tf.int32, [5, None])
split_inputs = tf.split(1, num_splits, inputs)
TypeError:参数“num\u split”应为int,而不是


我的方法可能有问题。我想用一个可变形状张量来列举一个维度。谢谢

核心图运算有一个“张量输入张量输出”的一般原理,因此,如果您可以重新构造计算,以处理可变大小的单个张量,而不是可变数量的张量,则可以简化计算

pack
unpack
split
这样的操作处理多个张量,但它们在图形构造期间编译为“张量输入/张量输出”操作,这就是为什么
num_splits
需要固定的原因。像
动态分割
动态缝合
出列
这样的操作会接管变量
0
-第个维度的单个张量的一些功能

如果您真的需要处理可变数量的张量,典型的方法是将计算分解为多个
会话。运行
调用,每个
运行
调用一个输入张量,并使用队列将事情联系在一起。有一个
slice\u input\u producer
,它将可变大小的输入沿0维分割,并为每一行生成一个张量,因此如果您想在
输入的每一行上的循环中计算
myfunction
,您可以这样做

def myfunction(向量):
结果=tf.减少_和(向量)
print_result=tf.print(结果,[result],“调用myfunction”)
返回打印结果
最大行数=10

#具有两列和未知行数的输入矩阵(在不相关的情况下,类似问题的解决方案是使用占位符作为最大大小的固定长度。假设我有一个长度为40的序列。
t=tf.range(40)
。 现在在运行时,我得到一个拆分(比如说
x=[6,9,10,5,1]

步骤1: 确定可能存在的最大拆分数,例如:19

步骤2

    num_splits = tf.placeholder(tf.int32, [19]) 
    y= tf.split(t, num_or_size_splits=num_splits, axis=0)
这将把序列分成运行时确定的拆分大小

步骤4: 在运行时:

    x = [6,9,10,5,1] 
    x += [40-sum(x)] + [0 for i in range(19-1-len(x))]
第一行表示我们需要的实际分割尺寸
拆分要求拆分大小之和应达到总拆分大小,即在这种情况下为40,0是剩余拆分的拆分大小

session.run(y,feed\u dict={num\u splits:x})
将显示如下结果:

[0, 1, 2, 3, 4, 5]  
[ 6,  7,  8,  9, 10, 11, 12, 13, 14]  
[15, 16, 17, 18, 19, 20, 21, 22, 23, 24]  
[25, 26, 27, 28, 29]  
[30]  
[31, 32, 33, 34, 35, 36, 37, 38, 39]  
[]
.
.
[]  
步骤5: (可选,首选)用零填充,直到序列的最大长度

    def pad_up_to(t, max_in_dims, constant_values):
        s = tf.shape(t)
        paddings = [[0, m-s[i]] for (i,m) in enumerate(max_in_dims)]
        return tf.pad(t, paddings, 'CONSTANT', constant_values=constant_values)


    m = []
    for t1 in y :
      t1=tf.reshape(t1,[1,-1])
      t_padded = pad_up_to(t1, [1,15], 0)
      session.run(t_padded  , feed_dict={num_splits:x})  
      m+=  [t_padded]
    m= tf.concat(m,0)
这将用零填充块以创建大小相等的块

注:上述方法帮助我将NLP相关任务的序列转换为句子(句子数量可变)

函数:pad_up_to()取自

    def pad_up_to(t, max_in_dims, constant_values):
        s = tf.shape(t)
        paddings = [[0, m-s[i]] for (i,m) in enumerate(max_in_dims)]
        return tf.pad(t, paddings, 'CONSTANT', constant_values=constant_values)


    m = []
    for t1 in y :
      t1=tf.reshape(t1,[1,-1])
      t_padded = pad_up_to(t1, [1,15], 0)
      session.run(t_padded  , feed_dict={num_splits:x})  
      m+=  [t_padded]
    m= tf.concat(m,0)