Tensorflow_Basic Usage
来源:互联网 发布:围棋软件免费下载 编辑:程序博客网 时间:2024/06/11 16:06
Basic Usage
https://www.tensorflow.org/versions/r0.12/get_started/basic_usage.html
To use TensorFlow you need to understand how TensorFlow:
- Represents computations as graphs.//将运算模型视为图
- Executes graphs in the context of
Sessions
. 在回话中运行模型,先初始化 - Represents data as tensors. 将数据作为tensor
- Maintains state with
Variables
. 用Variables描述运算状态 - Uses feeds and fetches to get data into and out of arbitrary operations. 应用feed 和fetch在任意操作中获取或输入数据
Overview
TensorFlow is a programming system in which you represent computations as graphs. Nodes in the graph are called ops (short for operations).模型图中的结点node视为一个运算操作
A TensorFlow graph is a description of computations. To compute anything, a graph must be launched in a
Session
. ASession
places the graph ops ontoDevices
, such as CPUs or GPUs, and provides methods to execute them. 任何图的运算都要在一个回话中执行,回话会将模型图放在一个设备上运行,比如,CPU或GPU Building the graph
To build a graph start with ops that do not need any input (source ops), such as
Constant
, and pass their output to other ops that do computation.The ops constructors in the Python library return objects that stand for the output of the constructed ops. You can pass these to other ops constructors to use as inputs.
The TensorFlow Python library has a default graph to which ops constructors add nodes. The default graph is sufficient for many applications. See the Graph class documentation for how to explicitly manage multiple graphs.
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)
The default graph now has three nodes: two
constant()
ops and onematmul()
op. To actually multiply the matrices, and get the result of the multiplication, you must launch the graph in a session.默认模型图又3个结点:两个常量矩阵,一个乘积矩阵。Launching the graph in a session
Launching follows construction. To launch a graph, create a
Session
object. Without arguments the session constructor launches the default graph. 创建一个session回话,自动加载默认图See the Session class for the complete session API.
# 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()
Sessions should be closed to release resources. You can also enter a
Session
with a "with" block. TheSession
closes automatically at the end of thewith
block.with tf.Session() as sess: result = sess.run([product]) print(result)
The TensorFlow implementation translates the graph definition into executable operations distributed across available compute resources, such as the CPU or one of your computer's GPU cards. In general you do not have to specify CPUs or GPUs explicitly. TensorFlow uses your first GPU, if you have one, for as many operations as possible.
If you have more than one GPU available on your machine, to use a GPU beyond the first you must assign ops to it explicitly. Use
with...Device
statements to specify which CPU or GPU to use for operations:with tf.Session() as sess: with tf.device("/gpu:1"): matrix1 = tf.constant([[3., 3.]]) matrix2 = tf.constant([[2.],[2.]]) product = tf.matmul(matrix1, matrix2) ...
Devices are specified with strings. The currently supported devices are:指定运算的CPU或GPU
"/cpu:0"
: The CPU of your machine."/gpu:0"
: The GPU of your machine, if you have one."/gpu:1"
: The second GPU of your machine, etc.
See Using GPUs for more information about GPUs and TensorFlow.
- Tensorflow_Basic Usage
- usage
- WinDbg Usage
- cscope usage
- doxygen usage
- Maven Usage
- Vss usage
- mq_notify usage
- CVS Usage
- NETLINK usage
- sdcard usage
- RMAN USAGE
- diff usage
- ifconfig usage
- Sendrecv Usage
- kthread usage
- RPM usage
- merge usage
- 设计模式之模板模式
- 哈夫曼树简介及实现
- java 提交表单、文件防止脏数据
- SQL语句中limit的用法
- 二叉树前序、中序、后序非递归遍历实现(C语言)
- Tensorflow_Basic Usage
- 二叉树的建立,遍历查找
- ViewPager 详解(二)——四大函数
- Java vs. Kotlin:应该使用Kotlin进行Android开发吗?
- 【BZOJ2440】完全平方数(中山市选2011)-二分答案+莫比乌斯函数应用
- (135)点光源
- 设计模式-----观察者模式(Obsever)
- react-native 学习之旅
- Apache Cassandra3.10集群安装部署