[PYTHON] [TF] About Tensorflow API

Constant Value Tensors zeros Something like numpy zeros Specify Tensor data type and Shape

import tensorflow as tf

x = tf.zeros([1], dtype=tf.float32)
with tf.Session() as sess:
    print(x.eval())

[ 0.]
import tensorflow as tf

x = tf.zeros([10], dtype=tf.float32)
with tf.Session() as sess:
    print(x.eval())

[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]

2x10 Tensor example

import tensorflow as tf

x = tf.zeros([2,10], dtype=tf.float32)
with tf.Session() as sess:
    print(x.eval())

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

ones Something like numpy ones Specify Tensor data type and Shape

import tensorflow as tf

x = tf.ones([2,10], dtype=tf.float32)
with tf.Session() as sess:
    print(x.eval())

[[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]]

fill Make a constant tensor dtype cannot be specified

import tensorflow as tf

x = tf.fill([2,10], 9.)
with tf.Session() as sess:
    print(x.eval())

[[ 9.  9.  9.  9.  9.  9.  9.  9.  9.  9.]
 [ 9.  9.  9.  9.  9.  9.  9.  9.  9.  9.]]

constant Make a constant tensor

import tensorflow as tf

x = tf.constant(9,shape=[2,10],dtype=tf.float32)
with tf.Session() as sess:
    print(x.eval())

[[ 9.  9.  9.  9.  9.  9.  9.  9.  9.  9.]
 [ 9.  9.  9.  9.  9.  9.  9.  9.  9.  9.]]
In [8]: x = tf.constant(9,shape=[2,10],dtype=tf.int32)
   ...: with tf.Session() as sess:
   ...:     print(x.eval())
   ...: 
   ...: 
[[9 9 9 9 9 9 9 9 9 9]
 [9 9 9 9 9 9 9 9 9 9]]
import tensorflow as tf
import numpy as np

x = tf.constant(np.arange(20).astype(float),shape=[2,10],dtype=tf.float32)
with tf.Session() as sess:
    print(x.eval())

[[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.]
 [ 10.  11.  12.  13.  14.  15.  16.  17.  18.  19.]]

Random Tensors Initialize the Tensor with a random value. Often used for weight initialization

random_normal Initialize the tensor with a normally distributed random value

import tensorflow as tf

x = tf.random_normal(shape=[20000],mean=0.0, stddev=1.0,dtype=tf.float32)
with tf.Session() as sess:
    y = x.eval()

random_normal.png

truncated_normal Initialize the tensor with a normal distribution and a random value up to twice the standard deviation

import tensorflow as tf

x = tf.truncated_normal(shape=[20000],mean=0.0, stddev=1.0,dtype=tf.float32)
with tf.Session() as sess:
    y = x.eval()

truncated_normal.png

random_uniform Initialize the tensor with a uniformly distributed random value

import tensorflow as tf

x = tf.random_uniform(shape=[20000], minval=-1.0,maxval=1.0,dtype=tf.float32)
with tf.Session() as sess:
    y = x.eval()

random_uniform.png

set_random_seed If you use random, the reproducibility will be lost, but if you specify seed, you can retrieve the same value. There are two ways to specify it, either by specifying it in operation or by specifying it by graph level with set_random_seed.

import tensorflow as tf

a = tf.random_uniform([1])
print('session1')
with tf.Session() as sess:
    print(a.eval())
    print(a.eval())    


print('session2')
with tf.Session() as sess:
    print(a.eval())
    print(a.eval())  

session1
[ 0.85636878]
[ 0.81764412]
session2
[ 0.36246026]
[ 0.87940264]
import tensorflow as tf

a = tf.random_uniform([1],seed=1234)
print('session1')
with tf.Session() as sess:
    print(a.eval())
    print(a.eval())    


print('session2')
with tf.Session() as sess:
    print(a.eval())
    print(a.eval())   
session1
[ 0.84830701]
[ 0.64822805]
session2
[ 0.84830701]
[ 0.64822805]
import tensorflow as tf

tf.set_random_seed(1234)
a = tf.random_uniform([1])
b = tf.random_uniform([1])
print('session1')
with tf.Session() as sess:
    print(a.eval())
    print(a.eval())    
    print(b.eval())
    print(b.eval())


print('session2')
with tf.Session() as sess:
    print(a.eval())
    print(a.eval())
    print(b.eval())
    print(b.eval())

session1
[ 0.340114]
[ 0.65625393]
[ 0.78275204]
[ 0.14843035]
session2
[ 0.340114]
[ 0.65625393]
[ 0.78275204]
[ 0.14843035]

Shapes and Shaping Shape Take out the size (Sharp) of the tensor.

import tensorflow as tf

x = tf.constant(np.arange(60.),shape=[3,4,5],dtype=tf.float32)    
with tf.Session() as sess:
    print(x.eval())

[[[  0.   1.   2.   3.   4.]
  [  5.   6.   7.   8.   9.]
  [ 10.  11.  12.  13.  14.]
  [ 15.  16.  17.  18.  19.]]

 [[ 20.  21.  22.  23.  24.]
  [ 25.  26.  27.  28.  29.]
  [ 30.  31.  32.  33.  34.]
  [ 35.  36.  37.  38.  39.]]

 [[ 40.  41.  42.  43.  44.]
  [ 45.  46.  47.  48.  49.]
  [ 50.  51.  52.  53.  54.]
  [ 55.  56.  57.  58.  59.]]]

When getting the size with get_shape () Type is TensorShape.

import tensorflow as tf

s = x.get_shape()
print(type(s))
print(s)

<class 'tensorflow.python.framework.tensor_shape.TensorShape'>
(3, 4, 5)

When getting the size with tf.shape () Type is operation.

import tensorflow as tf

s = tf.shape(x)
print(type(s))
print(s)
<class 'tensorflow.python.framework.ops.Tensor'>
Tensor("Shape:0", shape=(3,), dtype=int32)

get_shape(), tf.shape() tf.shape () is used when the size is decided later by placeholder etc.

x = tf.placeholder(tf.float32, shape=[None,32])

For example, if you use get_shape to create a Tensor of the same size as the placeholder, an error will occur.

y = tf.ones(shape=x.get_shape())
Traceback (most recent call last):

  File "<ipython-input-23-366d94fbb0a4>", line 1, in <module>
    y = tf.ones(shape=x.get_shape())

  File "/home/user/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 699, in ones
    shape = ops.convert_to_tensor(shape, name="shape")

  File "/home/user/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 529, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)

  File "/home/user/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/constant_op.py", line 195, in _tensor_shape_tensor_conversion_function
    "Cannot convert a partially known TensorShape to a Tensor: %s" % s)

ValueError: Cannot convert a partially known TensorShape to a Tensor: (?, 32)

In such a case, use tf.shape.

y = tf.ones(shape=tf.shape(x))
import tensorflow as tf

x = tf.placeholder(tf.float32, shape=[None,32])
y = tf.ones(shape=tf.shape(x))

z = y + x
with tf.Session() as sess:    
    # sess.run(y)  
    b = np.arange(3*32).reshape((3,32))
    print(sess.run(z, feed_dict={x:b}))

[[  1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.  12.  13.  14.
   15.  16.  17.  18.  19.  20.  21.  22.  23.  24.  25.  26.  27.  28.
   29.  30.  31.  32.]
 [ 33.  34.  35.  36.  37.  38.  39.  40.  41.  42.  43.  44.  45.  46.
   47.  48.  49.  50.  51.  52.  53.  54.  55.  56.  57.  58.  59.  60.
   61.  62.  63.  64.]
 [ 65.  66.  67.  68.  69.  70.  71.  72.  73.  74.  75.  76.  77.  78.
   79.  80.  81.  82.  83.  84.  85.  86.  87.  88.  89.  90.  91.  92.
   93.  94.  95.  96.]]

If you use a part of tf.shape and enclose it in [] like this, it becomes a List.

print(type([tf.shape(x)[0],1]))

<class 'list'>

~~ In this case, use tf.pack for operation. ~~ In this case, use tf.stack for operation. (Pack seems to be a stack)

print(type(tf.stack([tf.shape(x)[0],1])))

<class 'tensorflow.python.framework.ops.Tensor'>

Slicing and Joining

slice Take out a part of the tensor. Specify the start location with begin and the size to cut out with size.

import tensorflow as tf

n = np.arange(25).reshape((1,5,5))
x = tf.concat([n, n*10, n*100],0)
with tf.Session() as sess:   
    print(x.eval())

[[[   0    1    2    3    4]
  [   5    6    7    8    9]
  [  10   11   12   13   14]
  [  15   16   17   18   19]
  [  20   21   22   23   24]]

 [[   0   10   20   30   40]
  [  50   60   70   80   90]
  [ 100  110  120  130  140]
  [ 150  160  170  180  190]
  [ 200  210  220  230  240]]

 [[   0  100  200  300  400]
  [ 500  600  700  800  900]
  [1000 1100 1200 1300 1400]
  [1500 1600 1700 1800 1900]
  [2000 2100 2200 2300 2400]]]

In the example below, four pieces are taken out from the position 6 in the 0-dimensional direction, 3 in the 1-dimensional direction, and 2 in the 3-dimensional direction.

import tensorflow as tf

y = tf.slice(x, [0,1,1], [3,2,4])    
with tf.Session() as sess:   
    print(y.eval())

[[[   6    7    8    9]
  [  11   12   13   14]]

 [[  60   70   80   90]
  [ 110  120  130  140]]

 [[ 600  700  800  900]
  [1100 1200 1300 1400]]]

concat Combine Tensors.

import tensorflow as tf

x = tf.ones([3,4], dtype=tf.float32)
y = tf.constant(2,shape=[3,4], dtype=tf.float32)

with tf.Session() as sess:   
    print(x.eval())
    print(y.eval())

[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]]
[[ 2.  2.  2.  2.]
 [ 2.  2.  2.  2.]
 [ 2.  2.  2.  2.]]

~~ First argument ~~ Determine the Dimension to combine with the second argument. In a two-dimensional Tensor, when Dimension is 0, the result is a Tensor in the order of [[xxxx], [xxxx], [yyyy], [yyyy] ...] like z1. When Dimension is 1, the result is a Tensor in the order of [[xxxxyyyy], [xxxxyyyy], ...] like z2.

import tensorflow as tf

z1 = tf.concat([x, y],0)
z2 = tf.concat([x, y],1)
with tf.Session() as sess:   
    print('z1')
    print(z1.eval())
    print('z2')
    print(z2.eval())

z1
[[ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 1.  1.  1.  1.]
 [ 2.  2.  2.  2.]
 [ 2.  2.  2.  2.]
 [ 2.  2.  2.  2.]]
z2
[[ 1.  1.  1.  1.  2.  2.  2.  2.]
 [ 1.  1.  1.  1.  2.  2.  2.  2.]
 [ 1.  1.  1.  1.  2.  2.  2.  2.]]
import tensorflow as tf

x = tf.ones([3,4,5], dtype=tf.float32)
y = tf.constant(2,shape=[3,4,5], dtype=tf.float32)
with tf.Session() as sess:   
    print(x.eval())
    print(y.eval())

[[[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]]

 [[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]]

 [[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]]]
[[[ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]]

 [[ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]]

 [[ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]]]
import tensorflow as tf

z1 = tf.concat([x, y],0)
z2 = tf.concat([x, y],1)
z3 = tf.concat([x, y],2)
with tf.Session() as sess:   
    print('z1')
    print(z1.eval())
    print('z2')
    print(z2.eval())
    print('z3')
    print(z3.eval())

z1
[[[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]]

 [[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]]

 [[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]]

 [[ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]]

 [[ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]]

 [[ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]]]
z2
[[[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]]

 [[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]]

 [[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]
  [ 2.  2.  2.  2.  2.]]]
z3
[[[ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]
  [ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]
  [ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]
  [ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]]

 [[ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]
  [ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]
  [ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]
  [ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]]

 [[ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]
  [ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]
  [ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]
  [ 1.  1.  1.  1.  1.  2.  2.  2.  2.  2.]]]

split Divide the tensor in the specified dimensional direction

import tensorflow as tf

n = np.arange(25).reshape((1,5,5))
x = tf.concat([n, n*10, n*100],0)
with tf.Session() as sess:   
    print(x.eval())

[[[   0    1    2    3    4]
  [   5    6    7    8    9]
  [  10   11   12   13   14]
  [  15   16   17   18   19]
  [  20   21   22   23   24]]

 [[   0   10   20   30   40]
  [  50   60   70   80   90]
  [ 100  110  120  130  140]
  [ 150  160  170  180  190]
  [ 200  210  220  230  240]]

 [[   0  100  200  300  400]
  [ 500  600  700  800  900]
  [1000 1100 1200 1300 1400]
  [1500 1600 1700 1800 1900]
  [2000 2100 2200 2300 2400]]]

Example of dividing into three in the 0-dimensional direction

import tensorflow as tf

y1, y2, y3 = tf.split(x, 3, 0)
with tf.Session() as sess:
    print('y1')
    print(y1.eval())
    print('y2')
    print(y2.eval())
    print('y3')
    print(y3.eval())

y1
[[[ 0  1  2  3  4]
  [ 5  6  7  8  9]
  [10 11 12 13 14]
  [15 16 17 18 19]
  [20 21 22 23 24]]]
y2
[[[  0  10  20  30  40]
  [ 50  60  70  80  90]
  [100 110 120 130 140]
  [150 160 170 180 190]
  [200 210 220 230 240]]]
y3
[[[   0  100  200  300  400]
  [ 500  600  700  800  900]
  [1000 1100 1200 1300 1400]
  [1500 1600 1700 1800 1900]
  [2000 2100 2200 2300 2400]]]

tile

import tensorflow as tf

x = tf.constant([[1,0],[0,1]])
with tf.Session() as sess:   
    print(x.eval())

[[1 0]
 [0 1]]

Specify the dimension direction you want to repeat. The following is an example of repeating twice in the 0-dimensional direction.

import tensorflow as tf

y = tf.tile(x, [2,1])
with tf.Session() as sess:   
    print(y.eval())

[[1 0]
 [0 1]
 [1 0]
 [0 1]]

Example of repeating twice in the one-dimensional direction

import tensorflow as tf

y = tf.tile(x, [1,2])
with tf.Session() as sess:   
    print(y.eval())

[[1 0 1 0]
 [0 1 0 1]]

Example of repeating twice in the 0-dimensional direction and the 1-dimensional direction

import tensorflow as tf

y = tf.tile(x, [2,2])
with tf.Session() as sess:   
    print(y.eval())

[[1 0 1 0]
 [0 1 0 1]
 [1 0 1 0]
 [0 1 0 1]]

pad 0 padding

import tensorflow as tf

n = np.arange(25).reshape((5,5))
x = tf.constant(n)
with tf.Session() as sess:   
    print(x.eval())

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]

Example of 0 padding for 1 on top, 2 on bottom, 3 on left, and 4 on right

import tensorflow as tf

y = tf.pad(x,[[1,2],[3,4]])
with tf.Session() as sess:   
    print(y.eval())

[[ 0  0  0  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  1  2  3  4  0  0  0  0]
 [ 0  0  0  5  6  7  8  9  0  0  0  0]
 [ 0  0  0 10 11 12 13 14  0  0  0  0]
 [ 0  0  0 15 16 17 18 19  0  0  0  0]
 [ 0  0  0 20 21 22 23 24  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0  0  0  0  0]]

Execution environment Anaconda 4.4.0 python 3.5.3 tensorflow 1.3.0

Recommended Posts

[TF] About Tensorflow API
Tensorflow API: tf.truncated_normal
Tensorflow API: FLAGS
Tensorflow API: tf.reverse
TensorFlow API memo
TensorFlow API memo (Python)
A note about TensorFlow Introduction