简体   繁体   English

Tensorflow索引

[英]Tensorflow indexing

I would like to choose a tensor by range of rows with all columns. 我想按所有列的行范围选择张量。

Something like: 就像是:

x[10:20,:]

Rows 10 to 20 with all columns. 第10到20行,所有列。

I have tried to use: 我尝试使用:

tf.gather_nd

What is the way to do that? 这样做的方式是什么?

Tensorflow supports numpy style indexing: x[10:20,:] . Tensorflow支持numpy样式索引: x[10:20,:]

An example: 一个例子:

 x = tf.Variable(tf.truncated_normal(shape=(100, 100)))
 y = x[10:20,]

 sess = tf.InteractiveSession()
 tf.global_variables_initializer().run()
 y.eval().shape
 #output
 #(10, 100)

I believe you are looking for tf.slice 我相信您正在寻找tf.slice

So assuming you have a 2D tensor as your example seems to indicated, to get 10:20 of the first dimension you would do: tf.slice(x, begin = [10,0], size = [10, x.get_shape().as_list()[1]]) 因此,如您的示例所示,假设您具有一个二维张量, tf.slice(x, begin = [10,0], size = [10, x.get_shape().as_list()[1]])获得第一维的10:20,可以这样做: tf.slice(x, begin = [10,0], size = [10, x.get_shape().as_list()[1]])

Note: begin is 0 indexed and and size is 1 indexed. 注意: begin为0索引, size为1索引。 So the size I put will give you dimension 1 of length 10 from starting/begin point and all of dimension 2. 因此,我输入的尺寸将为您提供从起点/起点起长度为10的尺寸1和所有尺寸为2的尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM