声明式编程与命令式编程的对比讨论
数据流图: tensorflow 1.2.0
张量:Tensor
稀疏张量:SparseTensor类,以键值对的形式表示高维稀疏数据,它包含indices、values和dense_shape这3个属性。
计算节点:Operation类定义在tensorflow/python/framework/ops.py
存储节点:Variable类定义在tensorflow/python/ops/variables.py
数据节点:tf.placeholder操作
普通会话:Session类定义在tensorflow/python/client/session.py
交互式会话:InteractiveSession,用户可以不借助with上下文语句块,直接使用Tensor.eval和Operation.run方法求解张量、执行操作
损失函数和优化算法:
优化器概述:
Y = W^T * X + b
可分为8个步骤:
(1)定义超参数:
(2)输入数据:
(3)构建模型:
(4)定义损失函数:
(5)创建优化器:
(6)定义单步训练操作:
(7)创建会话:
(8)迭代训练:
import tensorflow as tf
import matplotlib.pyplot as plt
# 1 超参数
learning_rate = 0.01
max_train_steps = 1000
log_step = 10
# 2 输入数据
train_X = np.array([[3.3],[4.4],[5.5],[6.71],[6.93],[4.168],[9.779],
[6.182],[7.59],[2.167],[7.042],[10.791],[5.313],
[7.997],[5.654],[9.27],[3.1]], dtype=np.float32)
train_Y = np.array([[1.7],[2.76],[2.09],[3.19],[1.694],[1.573],[3.366],
[2.596],[2.53],[1.221],[2.827],[3.465],[1.65],[2.904],
[2.42],[2.94],[1.3]], dtype=np.float32)
total_samples = train_X.shape[0]
# 3 构建模型
X = tf.placeholder(tf.float32, [None, 1])
W = tf.Variable(tf.random_normal([1,1]), name="weight")
b = tf.Variable(tf.zeros([1]), name="bias")
Y = tf.matmul(X, W) + b
# 4 定义损失函数
Y_ = tf.placeholder(tf.float32, [None, 1])
loss = tf.reduce_sum(tf.pow(Y-Y_, 2))/(total_samples)
# 5 创建优化器
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
# 6 定义单步训练操作
train_op = optimizer.minimize(loss)
# 7 创建会话
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 8 迭代训练
print("Start training:")
for step in xrange(max_train_steps):
sess.run(train_op, feed_dict={X: train_X, Y_: train_Y})
# 每隔 log_step 步打印一次日志
if step % log_step == 0:
c = sess.run(loss, feed_dict={X: train_X, Y_: train_Y})
print("Step:%d, loss==%.4f, W==%.4f, b==%.4f" %
(step, c, sess.run(W), sess.run(b)))
# 输出指标
final_loss = sess.run(loss, feed_dict={X: train_X, Y_: train_Y})
# 训练完毕的模型参数
weight, bias = sess.run([W, b])
print("Step:%d, loss==%.4f, W==%.4f, b==%.4f" %
(max_train_steps, final_loss, sess.run(W), sess.run(b)))
print("Linear Regression Model: Y==%.4f*X+%.4f" % (weight, bias))
# 9 可视化
#初始化后端
%matplotlib
plt.plot(train_X, train_Y, 'ro', label='Training data')
plt.plot(train_X, weight*train_X + bias, label='Fitted line')
plt.legend()
plt.show()
Step:1000, loss==0.1545, W==0.2621, b==0.7246 Linear Regression Model: Y==0.2621*X+0.7246