tensorflow 是个好东西,自己也接触下。

Linux 上都好办,就不说了。Windows 上有个 Python 的坑,记录下:

Tensorflow 对 Windows 支持,目前是只有 Python3 .
想要做 Windows 上跑,必须要Python 3,目前 Python 最新的版本是 3.6.x,但是 Tensorflow 才支持到 3.5,所以要下载 3.5.x 版本的Python 。
再用 pip install tensorflow 才行。
等以后都支持了,就不用Care这个了。

当然还有其他糟心的问题。 比如 墙, 想学个新知识都这么难~

Tensorflow 笔记

https://www.tensorflow.org/get_started/get_started
把数据结构化,建立合适的图;执行图,降维,执行图,循环…

  1. tensors 数据
3 # a rank 0 tensor; this is a scalar with shape []
[1., 2., 3.] # a rank 1 tensor; this is a vector with shape [3]
[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]
  1. graph 图
    • 创建图
    • 执行图

开始

  1. 芝麻开门
import tensorflow as tf
  1. Tensor 实例
node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)
Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)
  1. 执行
sess = tf.Session()
print (sess.run([node1, nod2]))
  1. 构建图
# 这里最重要,可以使用更高级和复杂的算法去构建图
node3 = tf.add(node1, node2)
node4 = 算法1(node3)
node5 = 算法2(node3)
node6 = 算法x(node4, node5)
...

print("node3:", node3)
# node3: Tensor("Add:0", shape=(), dtype=float32)
print("sess.run(node3):", sess.run(node3))
# sess.run(node3): 7.0
  1. 数据和图可以分开
a = tf.placeholder(tf.float32) # 用给定的数据类型去构建图,在执行过程中填入真实的数据
b = tf.placeholder(tf.float32)
adder_node = a + b # + provides a shortcut for tf.add(a, b)

print(sess.run(adder_node, {a: 3, b: 4.5})) # 7.5
print(sess.run(adder_node, {a: [1, 3], b: [2, 4]})) # [3., 7.]
  1. 变量,测试数据在不同的变量下的结果,以得到最优解
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b
# 初始化变量
init = tf.global_variables_initializer()
sess.run(init)
# 指定变量的范围
print(sess.run(linear_model, {x: [1, 2, 3, 4]}))
# [ 0. 0.30000001 0.60000002 0.90000004]
  1. 评估
y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_deltas)
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))
# 23.66
  1. 纠正
fixW = tf.assign(W, [-1.])
fixb = tf.assign(b, [1.])
sess.run([fixW, fixb])
print(sess.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))
# 0.0
  1. 优化
# 使用 Tensorflow 提供的算法 进行优化,比如 梯度下降 等
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
sess.run(init) # reset values to incorrect defaults.
for i in range(1000):
sess.run(train, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})
print(sess.run([W, b]))
# [array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]

完整代码

import tensorflow as tf

# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)

# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

# training data
x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
sess.run(train, {x: x_train, y: y_train})

# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x: x_train, y: y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))
# W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11

可视化

tensorflow graph annimation