Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently extract all slices of given length using tensorflow

I am trying to extract all slices of length 4 along 0th axis of a 2-dim tensor. So far I can do it mixing pure Python with tensorflow.

r = test.shape[0] # test should be a tensor
n = 4
a_list = list(range(r))
the_list = np.array([a_list[slice(i, i+n)] for i in range(r - n+1)])
test_stacked = tf.stack(tf.gather(test, the_list))

What would be an efficient way of doing that without using pure Python? Note that the "test" array is actually supposed to be a tensor, thus its shape isn't known before I execute the first part of the graph.

A full vanilla example:

array = np.array([[0, 1],[1, 2],[2, 3],[3, 4],[4, 5],[5, 6]])
array.shape # (6,2)

r = array.shape[0]
n = 4
a_list = list(range(r))
the_list = np.array([a_list[slice(i, i+n)] for i in range(r - n+1)])

result = array[the_list] # all possible slices of length 4 of the array along 0th axis
result.shape # (3, 4, 2)

result:

[[[0 1]
  [1 2]
  [2 3]
  [3 4]]

 [[1 2]
  [2 3]
  [3 4]
  [4 5]]

 [[2 3]
  [3 4]
  [4 5]
  [5 6]]]
like image 334
m3h0w Avatar asked Oct 27 '18 15:10

m3h0w


People also ask

How do you slice in TensorFlow?

You can use tf. slice on higher dimensional tensors as well. You can also use tf. strided_slice to extract slices of tensors by 'striding' over the tensor dimensions.

Which are the three main methods of getting data into a TensorFlow program?

Feeding: Python code provides the data when running each step. Reading from files: an input pipeline reads the data from files at the beginning of a TensorFlow graph. Preloaded data: a constant or variable in the TensorFlow graph holds all the data (for small data sets).

Which API is used to build performant complex input pipelines from simple re usable pieces that will feed your model's training or evaluation loops?

Dataset API to build a pipeline for feeding data to your model. tf. data. Dataset is used to build performant, complex input pipelines from simple, re-usable pieces that will feed your model's training or evaluation loops.

How do you subset a tensor?

Basically to subset a tensor for some indexes [a,b,c] It needs to get in the format [[0,a],[1,b],[2,c]] and then use gather_nd() to get the subset.


2 Answers

You may want to try the more general tf.extract_image_patches.

import tensorflow as tf

a = tf.constant([[0, 1],[1, 2],[2, 3],[3, 4],[4, 5],[5, 6]])
# tf.extract_image_patches requires a [batch, in_rows, in_cols, depth] tensor
a = a[None, :, :, None]
b = tf.extract_image_patches(a,
  ksizes=[1, 4, 2, 1],
  strides=[1, 1, 1, 1],
  rates=[1, 1, 1, 1],
  padding='VALID')
b = tf.reshape(tf.squeeze(b), [-1, 4, 2])

sess = tf.InteractiveSession()
print(b.eval())
like image 100
P-Gn Avatar answered Oct 20 '22 04:10

P-Gn


I believe gather_nd is what you are looking for.

# a is a tensor of size (6, 2)

def get_indices(l, d):
    return [[[j] for j in range(i, i + d)] for i in range(l - d + 1)]

b = tf.gather_nd(a, get_indices(6, 4))
# b is a tensor of shape (3, 4, 2)
like image 29
Kevin He Avatar answered Oct 20 '22 06:10

Kevin He