TensorFlow笔记-03-张量,计算图,会话
- 搭建你的第一个神经网络,总结搭建八股
- 基于TensorFlow的NN:用张量表示数据,用计算图搭建神经网络,用会话执行计算图,优化线上的权重(参数),得到模型
- 张量(tensor):多维数组(列表)
- 阶:张量的维数
···维数······阶 ··········名字············例子·············
···0-D········0·······标量 scalar·····s=1 2 3
···1-D········0·······向量 vector·····s=[1,2,3]
···2-D········0·······矩阵 matrix·····s=[ [1,2,3], [4,5,6],[7,8,9] ]
···n-D········0·······标量 tensor·····s=[[[[[….n个
- 张量可以表示0阶到n阶的数组(列表)
- 代码tf02文件:https://xpwi.github.io/py/TensorFlow/tf02.py
# 两个张量的加法
import tensorflow as tf
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
result = a+b
print(result)
- 运行结果:
- 结果分析:
- 计算图(Graph):搭建神经网络的计算过程,只搭建,不运算
- 代码tf03文件:https://xpwi.github.io/py/TensorFlow/tf03.py
# 两个张量的加法
import tensorflow as tf
# x 是一个一行两列的张量
x = tf.constant([[1.0, 2.0]])
# x 是一个两行一列的张量
w = tf.constant([[3.0], [4.0]])
'''
构建计算图,但不运算
y = XW
= x1*w1 + x2*w2
'''
# 矩阵相乘
y = tf.matmul(x, w)
print(y)
运行结果
Tensor(“MatMul:0”, shape=(1, 1), dtype=float32)
- 会话(Session):执行计算图中的结点运算
- 代码04文件:https://xpwi.github.io/py/TensorFlow/tf04.py
# 两个张量的加法
import tensorflow as tf
# x 是一个一行两列的张量
x = tf.constant([[1.0, 2.0]])
# x 是一个两行一列的张量
w = tf.constant([[3.0], [4.0]])
'''
构建计算图,但不运算
y = XW
= x1*w1 + x2*w2
'''
# 矩阵相乘
y = tf.matmul(x, w)
print(y)
# 会话:执行节点运算
with tf.Session() as sess:
print(sess.run(y))
运行结果
y = 1.0*3.0 + 2.0*4.0 = 11
拜拜
- 本笔记不允许任何个人和组织转载