- Practical Convolutional Neural Networks
- Mohit Sewak Md. Rezaul Karim Pradeep Pujari
- 470字
- 2021-06-24 18:58:50
TensorFlow basics
In TensorFlow, data isn't stored as integers, floats, strings, or other primitives. These values are encapsulated in an object called a tensor. It consists of a set of primitive values shaped into an array of any number of dimensions. The number of dimensions in a tensor is called its rank. In the preceding example, hello_constant is a constant string tensor with rank zero. A few more examples of constant tensors are as follows:
# A is an int32 tensor with rank = 0
A = tf.constant(123)
# B is an int32 tensor with dimension of 1 ( rank = 1 )
B = tf.constant([123,456,789])
# C is an int32 2- dimensional tensor
C = tf.constant([ [123,456,789], [222,333,444] ])
TensorFlow's core program is based on the idea of a computational graph. A computational graph is a directed graph consisting of the following two parts:
- Building a computational graph
- Running a computational graph
A computational graph executes within a session. A TensorFlow session is a runtime environment for the computational graph. It allocates the CPU or GPU and maintains the state of the TensorFlow runtime. The following code creates a session instance named sess using tf.Session. Then the sess.run() function evaluates the tensor and returns the results stored in the output variable. It finally prints as Hello World!:
with tf.Session() as sess:
# Run the tf.constant operation in the session
output = sess.run(hello_constant)
print(output)
Using TensorBoard, we can visualize the graph. To run TensorBoard, use the following command:
tensorboard --logdir=path/to/log-directory
Let's create a piece of simple addition code as follows. Create a constant integer x with value 5, set the value of a new variable y after adding 5 to it, and print it:
constant_x = tf.constant(5, name='constant_x')
variable_y = tf.Variable(x + 5, name='variable_y')
print (variable_y)
The difference is that variable_y isn't given the current value of x + 5 as it should in Python code. Instead, it is an equation; that means, when variable_y is computed, take the value of x at that point in time and add 5 to it. The computation of the value of variable_y is never actually performed in the preceding code. This piece of code actually belongs to the computational graph building section of a typical TensorFlow program. After running this, you'll get something like <tensorflow.python.ops.variables.Variable object at 0x7f074bfd9ef0> and not the actual value of variable_y as 10. To fix this, we have to execute the code section of the computational graph, which looks like this:
#initialize all variables
init = tf.global_variables_initializer()
# All variables are now initialized
with tf.Session() as sess:
sess.run(init)
print(sess.run(variable_y))
Here is the execution of some basic math functions, such as addition, subtraction, multiplication, and division with tensors. For more math functions, please refer to the documentation:
For TensorFlow math functions, go to https://www.tensorflow.org/versions/r0.12/api_docs/python/math_ops/basic_math_functions.
- 數(shù)據(jù)庫(kù)基礎(chǔ)與應(yīng)用:Access 2010
- 信息系統(tǒng)與數(shù)據(jù)科學(xué)
- Hadoop與大數(shù)據(jù)挖掘(第2版)
- Live Longer with AI
- 數(shù)據(jù)庫(kù)應(yīng)用基礎(chǔ)教程(Visual FoxPro 9.0)
- Spark大數(shù)據(jù)編程實(shí)用教程
- 深入淺出 Hyperscan:高性能正則表達(dá)式算法原理與設(shè)計(jì)
- 大數(shù)據(jù)治理與安全:從理論到開(kāi)源實(shí)踐
- Oracle 11g+ASP.NET數(shù)據(jù)庫(kù)系統(tǒng)開(kāi)發(fā)案例教程
- 數(shù)據(jù)挖掘算法實(shí)踐與案例詳解
- 標(biāo)簽類(lèi)目體系:面向業(yè)務(wù)的數(shù)據(jù)資產(chǎn)設(shè)計(jì)方法論
- Arquillian Testing Guide
- Trino權(quán)威指南(原書(shū)第2版)
- Mastering Java for Data Science
- Creating Mobile Apps with Appcelerator Titanium