TensorFlow基本使用

TensorFlow基本使用

flyfish

To use TensorFlow you need to understand how TensorFlow:

Represents computations as graphs.
使用graph来表示计算

Executes graphs in the context of Sessions.
在session上下文中执行graph

Represents data as tensors.
使用tensor表示data

Maintains state with Variables.
variable维护state

Uses feeds and fetches to get data into and out of arbitrary operations.
feed和fetch为任意operation赋值和获取数据.

TensorFlow is a programming system in which you represent computations as

graphs. Nodes in the graph are called ops (short for operations). An op takes

zero or more Tensors, performs some computation, and produces zero or more

Tensors。 In TensorFlow terminology, a Tensor is a typed multi-dimensional

array。

TensorFlow 是一个程序设计系统, 使用graph来表示计算. graph中的节点被称之为 op

(operation).一个 op获得 0 个或多个 Tensor , 执行一些计算, 产生 0 个或多个

Tensor . 在TensorFlow术语中 Tensor 是一个类型化的多维数组。

import tensorflow as tf

# Create a Constant op that produces a 1x2 matrix.  The op is
# added as a node to the default graph.
#
# The value returned by the constructor represents the output
# of the Constant op.
matrix1 = tf.constant([[3., 3.]])

# Create another Constant that produces a 2x1 matrix.
matrix2 = tf.constant([[2.],[2.]])

# Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs.
# The returned value, 'product', represents the result of the matrix
# multiplication.
product = tf.matmul(matrix1, matrix2)
# Launch the default graph.
sess = tf.Session()

# To run the matmul op we call the session 'run()' method, passing 'product'
# which represents the output of the matmul op.  This indicates to the call
# that we want to get the output of the matmul op back.
#
# All inputs needed by the op are run automatically by the session.  They
# typically are run in parallel.
#
# The call 'run(product)' thus causes the execution of three ops in the
# graph: the two constants and matmul.
#
# The output of the matmul is returned in 'result' as a numpy `ndarray` 

object.
result = sess.run(product)
print(result)
# ==> [[ 12.]]

# Close the Session when we're done.
sess.close()

m行n列的矩阵,简称m*n矩阵
创建一个constant op, 产生一个 1*2 矩阵. 这个 op 被作为一个节点加到default

graph中.
创建另外一个constant op, 产生一个 2*1 矩阵
创建一个矩阵乘法 matmul op , 把 ‘matrix1’ 和 ‘matrix2’ 作为输入.
返回值 ‘product’ 表示矩阵乘法的结果.

函数调用 ‘run(product)’ 触发了图中三个 op (两个constant op 和一个matmul op)

的执行.
返回值 ‘result’ 是一个numpy ndarray对象.

win10+python3 下调试通过
最终输出[[ 12.]]

3*2+3*2=12

你可能感兴趣的:(深度学习)