Nim*_*z14 10 python tensorflow
我正在尝试将输入图层拆分为不同大小的部分.我正在尝试使用tf.slice这样做,但它不起作用.
一些示例代码:
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_})
Run Code Online (Sandbox Code Playgroud)
输出:
[[1 2]
[3 4]
[5 6]]
Run Code Online (Sandbox Code Playgroud)
这是有效的,大致是我想要发生的,但我必须指定第一个维度(3
在这种情况下).我不知道,虽然多少矢量我会输入,这就是为什么我使用的是placeholder
与None
摆在首位!
是否有可能以slice
这样一种方式使用它,直到运行时尺寸未知?
我尝试过使用placeholder
它的价值,ph.get_shape()[0]
如下所示:x = tf.slice(ph, [0, 0], [num_input, 2])
.但那也不起作用.
nes*_*uno 18
您可以在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_}))
Run Code Online (Sandbox Code Playgroud)
对我来说,我尝试了另一个例子来让我理解切片功能
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)
Run Code Online (Sandbox Code Playgroud)
它输出:
[[[21 22 23]]]
[[[31 32]]
[[41 42]]
[[51 52]]]
[[[12]]
[[22]]
[[32]]
[[42]]]
[]
[[[33]
[36]]
[[43]
[46]]
[[53]
[56]]]
Run Code Online (Sandbox Code Playgroud)
参数 begin 指示您要开始剪切的元素。size 参数表示该维度上需要多少个元素。
归档时间: |
|
查看次数: |
20070 次 |
最近记录: |