1.Tensorflow的基本概念:

1.Tensorflow的基本概念:

  • 1.使用图(graphs)来表示计算任务
  • 2.在被称之为会话(Session)的上下文(context)中执行图

  • 3.使用tensor表示数据

  • 4.通过变量(Variable)维护状态

  • 5.使用feed和fetch可以为任意的操作赋值或者从其中获取数据

Tensorflow是一个编程系统,使用图(graphs)来表示任务,图(graphs)中的节点称之为op(operation),一个获得0个或多个Tensor,执行计算,产生0个或多个Tensor.Tensor看做是一个n维的数组或列表。图必须在会话(Session)里被启动。

                                           图的基本框架

 1 import tensorflow as tf
 2 
 3 a1 = tf.constant([[2, 3]])  # 定义一个常量
 4 a2 = tf.constant([[3], [3]])
 5 result = tf.matmul(a1, a2)  # 将两个常量相乘
 6 print(result)  # result是一个tonsor,所有的graphs都必须在会话(session)中执行
 7 # Tensor("MatMul:0", shape=(1, 1), dtype=int32)
 8 sess = tf.Session()  # 创建会话
 9 result = sess.run(result)
10 print(result)  # 返回计算的结果
11 sess.close()  # 关闭会话
12 # [[15]]
13 """
14 可以用python的with来自行关闭会话:
15 with tf.Session() as sess:
16     result = sess.run(result)
17     print(result)
18 """

猜你喜欢

转载自www.cnblogs.com/ivy-blogs/p/10571098.html