2017, Apr 29

# tf tips. 1 - tf.strided_slice

## [TODO] use TensorFlow to slice tensor with strides

import tensorflow as tf


Let’s say we have the following tensor.

sample = tf.constant(
[[[11, 12, 13], [21, 22, 23]],
[[31, 32, 33], [41, 42, 43]],
[[51, 52, 53], [61, 62, 63]]])


The tensor’s shape is (3, 2, 3). It has 3 rows, 2 columns and 3 items. It’s a bit tricky to visually understand at first.
When it’s written as above, approach it from row to get your head around its shape.

print(sample)

Tensor("Const:0", shape=(3, 2, 3), dtype=int32)


Here I’m going to use tf.strided_slice to slice the tensor into various shapes.
tf.strided_slice takes a lot of parameters, and the core ones are begin, end and strides.
As our tensor has 3 dimensions, so do these params.

Each point in begin corresponds to the ones in end and strides.
Let’s have a look at the sample below.

### 1. first row

slice = tf.strided_slice(sample, begin=[0, 0, 0], end=[1, 2, 3], strides=[1, 1, 1])
with tf.Session() as sess:
result = sess.run(slice)
print(result)

[[[11 12 13]
[21 22 23]]]


To get the first row from the given tensor, I passed [0, 0, 0] as begin and [1, 2, 3] as end. Here we set strides as [1, 1, 1] because we’re not skipping any items. We want row from 0 to 1, column from 0 to 2, items from 0 to 3. Therefore, I get 1 row, 2 columns and 3 items. I could have just passed int(sample.shape[1]) instead of column value to generalise it.

slice = tf.strided_slice(sample, begin=[0, 0, 0], end=[1, int(sample.shape[1]), int(sample.shape[2])], strides=[1, 1, 1])
with tf.Session() as sess:
result = sess.run(slice)
print(result)

[[[11 12 13]
[21 22 23]]]


### 2. all of the list without the last item

slice = tf.strided_slice(sample, begin=[0, 0, 0], end=[3, 2, -1], strides=[1, 1, 1])
with tf.Session() as sess:
result = sess.run(slice)
print(result)

[[[11 12]
[21 22]]

[[31 32]
[41 42]]

[[51 52]
[61 62]]]


To remove the last item, I just need to pass -1 in ‘end’.

### 3. Strides

slice = tf.strided_slice(sample, begin=[0, 0, 0], end=[3, 2, 3], strides=[2, 2, 2])
with tf.Session() as sess:
result = sess.run(slice)
print(result)

[[[11 13]]

[[51 53]]]


By changing the values of strides from 1 to 2, I skip every other item in the tensor.

# tf tips. 2 - tf.fill

## [TODO] use TensorFlow to create a tnesor filled with scalar value.

sample_fill = tf.fill(dims=[2, 3], value=9)
with tf.Session() as sess:
result = sess.run(sample_fill)
print(result)

[[9 9 9]
[9 9 9]]


This function generates a tensor with a list of dims and fill it with a scalar value. You cannot assign a list as value.

# tf tips. 3 - tf.concat

## [TODO] use TensorFlow to concatnate tensors along one dimension.

t1 = [[1, 2, 3],
[4, 5, 6]]
t2 = [[7, 8, 9],
[10, 11, 12]]
concat1 = tf.concat([t1, t2], 0)
concat2 = tf.concat([t1, t2], 1)

with tf.Session() as sess:
result1 = sess.run(concat1)
print("concat example -- 1: row-wise")
print(result1)
print("\n")

result2 = sess.run(concat2)
print("concat example -- 2: column-wise")
print(result2)

concat example -- 1: row-wise
[[ 1  2  3]
[ 4  5  6]
[ 7  8  9]
[10 11 12]]

concat example -- 2: column-wise
[[ 1  2  3  7  8  9]
[ 4  5  6 10 11 12]]