您的位置:首页 > 其它

TensorFlow学习笔记【一】 基本使用

2018-03-05 21:31 477 查看

TensorFlow学习笔记【一】 基本使用

〇、随便写的话

看到山崩地裂和天花乱坠,每天得道,每天可以没有明天。

——冯唐

这是我写在新年开始的箴言,2017待我不薄,寄希望2018这关键的一年自己也能把握住。

毕竟“除了自渡与渡人,其他毫无所有,毫无所谓。”

虽然自认为做了一些事情,但是没有一件是坚持下来,要不就总在一些细枝末节的事情上纠结,要不就是单纯的懒,直到现在,越大,越怕。

2018到现在已经过去两个月了,而后面我只想……留下点痕迹吧。

话说我现在的体重估计能咋出个大坑,不只是痕迹…该减减了……



一、TensorFlow 学习准备

在 Windows 10 系统下,

IDE 是 PyCharm Community Edition 2017.2.4,

使用 python (3.5.4) 语言,

学习 TensorFlow (1.2.1).

主要我也不觉得搭个虚拟机,就会比在 windows 上直接操练更好,所以就先保持现状学起来吧,入个门在考虑环境的问题。

那么,第一个问题,

什么是 TensorFlow?TensorFlow中文社区

TensorFlow 是一个编程系统,用来表示计算任务,在Session(会话)中执行,计算过程使用的数据用tensor来表示,并通过variable(变量)来维护状态;

所以,TensorFlow的计算过程就是:在 Session 中执行描述计算过程的 图 ,Session 将 图 中的 op(operetion,节点)分发到运算设备(CPU、GPU)上执行,产生 tensor 并返回。

除此之外,TensorFlow 还有 feedfetch 两大机制,分别用来为操作赋值或从操作用获取数据。

通俗讲,TensorFlow 是一个通过数据流图(Data flow graphs)进行数值计算的软件库。



来自中文社区

二、TensorFlow 基本使用

使用 TensorFlow 的程序一般组织为两部分,构建阶段和执行阶段。

- 构建阶段

创建一个图来表示和训练神经网络(此阶段在 Python 中较 C/C++ 更容易);

- 执行阶段

反复执行图中的训练 op;

熟悉 TensorFlow 的基本操作 Python 程序如下:

1. 矩阵相乘

import tensorflow as tf

# 创建一个常量 op, 产生一个 1*2 的矩阵, 这个 op 作为一个节点,
# 加入到默认图中.
#
# 构造器的返回值代表该常量 op 的返回值.

matrix1 = tf.constant([[3., 3.]])

# 创建另外一个常量 op, 产生一个 2*1 的矩阵.
matrix2 = tf.constant([[2.], [2.]])

# 创建一个矩阵乘法 matmul op, 把 matrix1 和 matrix2 作为输入,
# 返回值 product 作为矩阵乘法的结果.
product = tf.matmul(matrix1, matrix2)

print(product)
# ==> Tensor("MatMul:0", shape=(1, 1), dtype=float32)

# 启动默认图 无任何创建参数
sess = tf.Session()

# 调用 sess 的 run() 方法来执行矩阵乘法 op, 传入 'product' 作为参数,
# 上面提到, 'product' 代表了矩阵乘法 op 的输出, 传入它是向方法表明, 我们希望取回
# 矩阵乘法 op 的输出.
#
# 整个执行过程是自动化的, 会话负责传递 op 所需的全部输入. op 通常是并发执行的.
#
# 函数调用 'run(product)' 触发了图中三个 op (两个常量 op 和一个矩阵乘法 op) 的执行.
#
# 返回值 'result' 是一个 numpy 'ndarray' 对象.
result = sess.run(product)
print(result)
# ==> [[ 12.]]

sess.close()
# 也可以使用 with 代码块自动完成关闭动作, 如 test02


2. 矩阵相乘(with代码块)

Python中 with 语句是与异常处理相关的功能语句。适用于对资源进行访问的场合,确保在使用后执行必要的清理操作。

语法格式:

with context_expression [as target(s)]:
with-body


import tensorflow as tf

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.], [2.]])
product = tf.matmul(matrix1, matrix2)

print(product)
# ==> Tensor("MatMul:0", shape=(1, 1), dtype=float32)

with tf.Session() as sess:
result = sess.run([product])
print(result)
# ==> [[ 12.]]
# 也可以显式调用 close() 关闭以释放资源


3. 变量计数器

import tensorflow as tf

# 创建一个变量, 初始化为标量 0.
state = tf.Variable(0, name="counter")

# 创建一个 op, 其作用是使 state 增加 1

one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)  # 图所描绘的赋值

# 启动图后, 变量必须先经过`初始化` (init) op 初始化,
# 首先必须增加一个`初始化` op 到图中.
init_op = tf.global_variables_initializer()

# 启动图, 运行 op
with tf.Session() as sess:
# 运行 'init' op
sess.run(init_op)
# 打印 'state' 的初始值
print(sess.run(state))
# 运行 op, 更新 'state', 并打印 'state'
for _ in range(3):
sess.run(update)
print(sess.run(state))

# 输出:

# 0
# 1
# 2
# 3


4. fetch 机制

向 run() 方法传入多个 tensor,以取回多个结果。

import tensorflow as tf

input1 = tf.constant(3.0)
input2 = tf.constant(5.0)
input3 = tf.constant(2.0)
intermed = tf.add(input2, input3)
mul = tf.multiply(input1, intermed)
# 这里没有 tensorflow.mul(), 但是文档里有啊?

with tf.Session() as sess:
result = sess.run([mul, intermed])
print(result)

# ==> [21.0, 7.0]


5. feed机制

import tensorflow as tf

# 使用 tf.placeholder() 方法创建占位符,将其指定为 'feed' 操作.
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)

with tf.Session() as sess:
print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
# ==> [array([ 14.], dtype=float32)]
print(sess.run(output, feed_dict={input1: [7.], input2: [2.]}))
# ==> [ 14.]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: