[PYTHON] Tensorflow Glossary

The tensorflow function is difficult to understand even if it is explained, so I will explain it only with a usage example. tf.Variable()

import tensorflow as tf
x = tf.Variable(10 , name="x")
y = x * 5
print(y)

output Tensor("mul_1:0", shape=(), dtype=int32)

Looking at the code above, the output looks like 50, but the tensor is output. So how do you then get 50 output?

x = tf.Variable(10 , name="x")
y = x * 5
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
sess.run(y)

Output 50 In tensorflow, you have to build and execute a computational graph through tf.session. tf.global_variables_initializer () is responsible for initializing variables in computational graphs

tf.placeholder() tf.placeholder(dtype, shape=None, name=None)

dtype: Assigns the type to be assigned to the variable. shape: shape of the variable to be assigned name: the name of the variable

Return value Tensor of variables that are fed, not evaluated directly

Program example

x = tf.placeholder(tf.int32)
y = tf.constant(1)
z = x + y
with tf.Session() as sess:
  print(sess.run(z, feed_dict={x: 2}))

output 3 Define x = 2 at run time Then pass the array to tf.placeholder.

x = tf.placeholder(tf.int32, shape=[2])
y = tf.placeholder(tf.int32, shape=[2])
z = x*y
with tf.Session() as sess:
  print(sess.run(z, feed_dict={x: [2, 1], y: [1, 2]}))

output [2 2]

tf.placeholder_with_default By using tf.placeholder_with_default, you can set the initial value and change it at runtime.

x = tf.placeholder_with_default(1, shape=[])
y = tf.constant(1)
with tf.Session() as sess:
  print(sess.run(x + y))

output 0

x = tf.placeholder_with_default(1, shape=[])
y = tf.constant(1)
with tf.Session() as sess:
  print(sess.run(x + y, feed_dict={x: -1}))

output 2

tf.shape tf.shape is used for shapes that can be changed dynamically.

x = tf.placeholder(tf.int32, shape=[None, 3])
size = tf.shape(x)[0]
sess = tf.Session()
sess.run(size, feed_dict={x: [[1, 2, 3], [1, 2, 3]]})

output 2

.get_shape Use .get_shape for shapes that do not change.

x = tf.constant(1, shape=[2, 3, 4])
x.get_shape()[1]

output Dimension(3)

References An introduction to deep learning with Tensorflow

Recommended Posts

Tensorflow Glossary
Django Glossary
tensorflow mnist_deep.py
TensorFlow tutorial tutorial
Tensorflow API: tf.truncated_normal
Tensorflow API: FLAGS
IT Glossary [Links]
Try Distributed TensorFlow
Practice RNN TensorFlow
Tensorflow personal tips
Install tensorflow. (Only!)
Zundokokiyoshi with TensorFlow
Breakout with Tensorflow
TensorFlow API memo
Understanding TensorFlow Arithmetic
Tensorflow doesn't work!