Python Tensorflow:使用 tf.slice 分割输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39054414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tensorflow: Using tf.slice to split the input
提问by Nimitz14
I'm trying to split my input layer into different sized parts. I'm trying to use tf.slice to do that but it's not working.
我试图将我的输入层分成不同大小的部分。我正在尝试使用 tf.slice 来做到这一点,但它不起作用。
Some sample code:
一些示例代码:
import tensorflow as tf
import numpy as np
ph = tf.placeholder(shape=[None,3], dtype=tf.int32)
x = tf.slice(ph, [0, 0], [3, 2])
input_ = np.array([[1,2,3],
[3,4,5],
[5,6,7]])
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print sess.run(x, feed_dict={ph: input_})
Output:
输出:
[[1 2]
[3 4]
[5 6]]
This works and is roughly what I want to happen, but I have to specify the first dimension (3
in this case). I can't know though how many vectors I'll be inputting, that's why I'm using a placeholder
with None
in the first place!
这有效并且大致是我想要发生的,但我必须指定第一个维度(3
在这种情况下)。我不知道我将输入多少个向量,这就是我首先使用placeholder
with的原因None
!
Is it possible to use slice
in such a way that it will work when a dimension is unknown until runtime?
是否有可能以slice
这样一种方式使用:当维度在运行时之前未知时它可以工作?
I've tried using a placeholder
that takes its value from ph.get_shape()[0]
like so: x = tf.slice(ph, [0, 0], [num_input, 2])
. but that didn't work either.
我已经使用试图placeholder
从将其值ph.get_shape()[0]
,像这样:x = tf.slice(ph, [0, 0], [num_input, 2])
。但这也不起作用。
回答by nessuno
You can specify one negative dimension in the size
parameter of tf.slice
. The negative dimension tells Tensorflow to dynamically determine the right value basing its decision on the other dimensions.
您可以在 的size
参数中指定一个负维度tf.slice
。负维度告诉 Tensorflow 根据其他维度的决定动态确定正确的值。
import tensorflow as tf
import numpy as np
ph = tf.placeholder(shape=[None,3], dtype=tf.int32)
# look the -1 in the first position
x = tf.slice(ph, [0, 0], [-1, 2])
input_ = np.array([[1,2,3],
[3,4,5],
[5,6,7]])
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
print(sess.run(x, feed_dict={ph: input_}))
回答by r0ng
For me, I tried another example to let me understand the slice function
对我来说,我尝试了另一个例子来让我理解切片功能
input = [
[[11, 12, 13], [14, 15, 16]],
[[21, 22, 23], [24, 25, 26]],
[[31, 32, 33], [34, 35, 36]],
[[41, 42, 43], [44, 45, 46]],
[[51, 52, 53], [54, 55, 56]],
]
s1 = tf.slice(input, [1, 0, 0], [1, 1, 3])
s2 = tf.slice(input, [2, 0, 0], [3, 1, 2])
s3 = tf.slice(input, [0, 0, 1], [4, 1, 1])
s4 = tf.slice(input, [0, 0, 1], [1, 0, 1])
s5 = tf.slice(input, [2, 0, 2], [-1, -1, -1]) # negative value means the function cutting tersors automatically
tf.global_variables_initializer()
with tf.Session() as s:
print s.run(s1)
print s.run(s2)
print s.run(s3)
print s.run(s4)
It outputs:
它输出:
[[[21 22 23]]]
[[[31 32]]
[[41 42]]
[[51 52]]]
[[[12]]
[[22]]
[[32]]
[[42]]]
[]
[[[33]
[36]]
[[43]
[46]]
[[53]
[56]]]
The parameter begin indicates which element you are going to start to cut. The size parameter means how many element you want on that dimension.
参数 begin 指示您要开始剪切的元素。size 参数表示该维度上需要多少个元素。
回答by USS
You can also try out this one
你也可以试试这个
x = tf.slice(ph, [0,0], [3, 2])
x = tf.slice(ph, [0,0], [3, 2])
As your starting point is (0,0)
second argument is [0,0]
.
You want to slice three raw and two column so your third argument is [3,2]
.
由于您的出发点是(0,0)
第二个参数是[0,0]
. 你想切片三个原始和两列,所以你的第三个参数是[3,2]
.
This will give you desired output.
这将为您提供所需的输出。