# 14-TensorFlow入门和基本模型

### 一.什么是TensorFlow

Tensor(张量)意味着 N 维数组，Flow(流)意味着基于数据流图的计算，TensorFlow即为张量从图的一端流动到另一端;

### 三.TensorFlow原理

TensorFlow是用数据流图(data flow graphs)技术来进行数值计算的。

### 五.TensorFlow：Helloworld

``````#导包
import tensorflow as tf
``````
``````#声明常量
hello = tf.constant('Hello, TensorFlow!')
``````
``````#创建会话
sess = tf.Session()
``````
``````#执行
print(sess.run(hello))

sess.close()
``````

### 六.TensorFlow：基本操作

• Basic constant operations
``````import tensorflow as tf

a = tf.constant(2)
b = tf.constant(3)

with tf.Session() as sess:
print("a: %i" % sess.run(a), "b: %i" % sess.run(b))
print("Addition with constants: %i" % sess.run(a+b))
print("Multiplication with constants: %i" % sess.run(a*b))
``````
• 赋值assign操作
``````v = tf.Variable(0,name = 'a')

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(v))
for i in range(5):
``````
• Basic Operations with variable as graph input
``````import tensorflow as tf

a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)

mul = tf.multiply(a, b)

with tf.Session() as sess:
# Run every operation with variable input
print("Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3}))
``````
• 矩阵操作
``````matrix1 = tf.constant([[1., 6.]])

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

product = tf.matmul(matrix1, matrix2)

with tf.Session() as sess:
result = sess.run(product)
print(result)
``````
• 根据Graph完成操作

Graph.png

### 七.TensorFlow：基本模型

• 梯度下降
``````#梯度下降示例
import numpy as np
import matplotlib.pyplot as plt
import time
x=np.arange(-5, 5, 0.001)
y=x**4-3*x**3+2

plt.plot(x,y)
plt.show()

old = 0
# 范围就是-5 到5
new= 5

# 步幅
step = 0.001

# 精确度
precision = 0.001

# 导数
def derivative(x):
return 4*x**3-9*x**2

# 进行梯度下降
while abs(new - old) > precision:
print('------------------------------>',new)
time.sleep(1)
old = new
new = new - step * derivative(new)

print (new)
``````
• 线性回归
``````#导包
mport tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
rng = numpy.random
``````
``````#定义常量
##定义训练次数learning_epochs，卷曲神经的学习率learning_rate显示打印数据的步幅display_step
learning_rate = 0.01
training_epochs = 1000
display_step = 50
``````
``````#训练数据
train_X = np.linspace(0,10,num= 20)+np.random.randn(20)
train_Y = np.linspace(1,4,num = 20)+np.random.randn(20)
n_samples = train_X.shape[0]
``````
``````#线性模型
``````
``````#创建TensorFlow均方误差cost以及梯度下降优化器optimizer
# 均方误差，平均误差
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/n_samples
# 实现梯度下降算法的优化器
``````

``````#TensorFlow进行初始化
init = tf.global_variables_initializer()
``````
``````#进行训练
# 训练开始
with tf.Session() as sess:
sess.run(init)

# 训练所有数据
for epoch in range(training_epochs):
for (x, y) in zip(train_X, train_Y):
sess.run(optimizer, feed_dict={X: x, Y: y})

#每执行50次显示运算结果
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')

#数据可视化
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()
``````
• 类逻辑斯蒂

softmax:

softmax.png

steps:

``````#导包
import tensorflow as tf

# Import MINST data
from tensorflow.examples.tutorials.mnist import input_data
``````
``````#声明算法
# Parameters
learning_rate = 0.01
training_epochs = 25
batch_size = 100
display_step = 1

# tf Graph Input
x = tf.placeholder(tf.float32, shape = [None,784]) # mnist data image of shape 28*28=784
y = tf.placeholder(tf.float32, shape = [None,10]) # 0-9 digits recognition => 10 classes

# Set model weights
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# Construct model
pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax

# Minimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
``````
``````#训练
# Start training
with tf.Session() as sess:
sess.run(init)

# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(mnist.train.num_examples/batch_size)
# Loop over all batches
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
# Fit training using batch data
_, c = sess.run([optimizer, cost], feed_dict={x: batch_xs,
y: batch_ys})
# Compute average loss
avg_cost += c / total_batch
# Display logs per epoch step
if (epoch+1) % display_step == 0:
print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))

print("Optimization Finished!")

# Test model
correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
# Calculate accuracy for 3000 examples
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("Accuracy:", accuracy.eval({x: mnist.test.images[:3000], y: mnist.test.labels[:3000]}))
``````
• 了解KNN

• L1 Distance：曼哈顿距离
``````distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), axis=1)
``````

• L2 Distance：欧氏距离
``````distance = tf.sqrt(tf.reduce_sum(tf.pow((xtr-xte),2),axis = -1))
``````

``````#导包
import numpy as np
import tensorflow as tf

# Import MINST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./", one_hot = True)
``````
``````#算法
Xtr, Ytr = mnist.train.next_batch(5000) #5000 for training (nn candidates)
Xte, Yte = mnist.test.next_batch(200) #200 for testing

# tf Graph Input
xtr = tf.placeholder("float", [None, 784])
xte = tf.placeholder("float", [784])

# 使用 L1 Distance 算法计算距离
# Calculate L1 Distance
# distance = tf.sqrt(tf.reduce_sum(tf.pow((xtr-xte),2),axis = -1))
# Prediction: Get min distance index (Nearest neighbor)
pred = tf.argmin(distance, 0)

accuracy = 0.

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()
``````
``````#训练
# Start training
with tf.Session() as sess:
sess.run(init)

# loop over test data
for i in range(len(Xte)):
# Get nearest neighbor
nn_index = sess.run(pred, feed_dict={xtr: Xtr, xte: Xte[i, :]})
# Get nearest neighbor class label and compare it to its true label
print("Test", i, "Prediction:", np.argmax(Ytr[nn_index]), \
"True Class:", np.argmax(Yte[i]))
# Calculate accuracy
if np.argmax(Ytr[nn_index]) == np.argmax(Yte[i]):
accuracy += 1./len(Xte)
print("Done!")
print("Accuracy:", accuracy)
``````

pip安装指定源
conda install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow