import tensorflow as tf import numpy import matplotlib.pyplot as plt rng = numpy.random # Parameters learning_rate = 0.01 training_epochs = 1000 display_step = 50 # Training Data train_X = numpy.asarray([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]) train_Y = numpy.asarray([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]) n_samples = train_X.shape[0] # tf Graph Input X = tf.placeholder("float") Y = tf.placeholder("float") # Set model weights W = tf.Variable(rng.randn(), name="weight") b = tf.Variable(rng.randn(), name="bias") # Construct a linear model pred = tf.add(tf.multiply(X, W), b) # Mean squared error cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples) # Gradient descent # Note, minimize() knows to modify W and b because Variable objects are trainable=True by default optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # Initialize the variables (i.e. assign their default value) init = tf.global_variables_initializer() # Start training with tf.Session() as sess: # Run the initializer sess.run(init) # Fit all training data for epoch in range(training_epochs): for (x, y) in zip(train_X, train_Y): sess.run(optimizer, feed_dict={X: x, Y: y}) # Display logs per epoch step if (epoch+1) % display_step == 0: c = sess.run(cost, feed_dict={X: train_X, Y:train_Y}) print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \ "W=", sess.run(W), "b=", sess.run(b)) print("Optimization Finished!") training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y}) print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n') # Graphic display plt.plot(train_X, train_Y, 'ro', label='Original data') plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line') plt.legend() plt.show() # Testing example, as requested (Issue #2) test_X = numpy.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1]) test_Y = numpy.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03]) print("Testing... (Mean square loss Comparison)") testing_cost = sess.run( tf.reduce_sum(tf.pow(pred - Y, 2)) / (2 * test_X.shape[0]), feed_dict={X: test_X, Y: test_Y}) # same function as cost above print("Testing cost=", testing_cost) print("Absolute mean square loss difference:", abs( training_cost - testing_cost)) plt.plot(test_X, test_Y, 'bo', label='Testing data') plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line') plt.legend() plt.show()
Variable和Tensor类似,但是有几点不同:
1. Variable可更改,assign函数
2. Variable常用于存储网络权重矩阵等变量,Tensor大多是中间结果
3. Variable会直接分配内存空间,而Tensor则是在运行时才分配
pred = tf.add(tf.multiply(X, W), b) # 线性回归模型,即预测值Y'=WX+b
tf.reduce_sum(
input_tensor,
axis=None,
keepdims=None, # axis没有体现的轴保持原有的维度
name=None,
reduction_indices=None, #弃用参数,使用axis
keep_dims=None #弃用参数,使用keepdims
)# 从维度上对张量进行缩减求和
x = tf.constant([[1, 1, 1], [1, 1, 1]]) # [2,3]
tf.reduce_sum(x) # 6 默认全部相加得到一个数
tf.reduce_sum(x, 0) # [2, 2, 2] 沿shape[0]轴缩减 得到shape为[3]
tf.reduce_sum(x, 1) # [3, 3] 沿shape[1]轴缩减 得到shape[2]
tf.reduce_sum(x, 1, keepdims=True) # [[3], [3]] 原shape为[2,3] keepdims之后缩减为shape[2,1]
tf.reduce_sum(x, [0, 1]) # 6 对0轴和1轴同时进行缩减
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples) # 最小二乘法 对所有样本计算 1/2 * (Y'-Y)^2 / n
__init__(
learning_rate,
use_locking=False,
name='GradientDescent'
)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
__init__(
learning_rate, # 学习速率 梯度下降的步长
use_locking=False,
name='GradientDescent'
) # tf.train.GradientDescentOptimizer构造函数
minimize(
loss, # 包含需要求最小值的张量
global_step=None,
var_list=None,
gate_gradients=GATE_OP,
aggregation_method=None,
colocate_gradients_with_ops=False,
name=None,
grad_loss=None
)
init = tf.global_variables_initializer() # 初始化所有的Variable,在Session.run函数中执行会自动获取所有Variable并进行初始化
# Fit all training data 线性回归模型迭代 for epoch in range(training_epochs): for (x, y) in zip(train_X, train_Y): # 逐个样本进行计算 sess.run(optimizer, feed_dict={X: x, Y: y}) # Display logs per epoch step if (epoch+1) % display_step == 0: c = sess.run(cost, feed_dict={X: train_X, Y:train_Y}) # 计算整体损失 print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \ "W=", sess.run(W), "b=", sess.run(b)) # 计算W和b迭代完成后,W和b的值已经确定,测试的时候对使用W和b的计算图都会按照更新后的W和b进行计算