第3章 TensorFlow进阶

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
注册配置 MSE Nacos/ZooKeeper,118元/月
简介: 第3章 TensorFlow进阶

第3章 TensorFlow进阶

3.1 TensorFlow 的计算模型

3.1.1 计算图的工作原理
import tensorflow as tf
import numpy as np
sess = tf.Session()
x_input = np.array([1.0,2.0,3.0,4.0,5.0])
x_output = tf.placeholder(tf.float32)
y = tf.constant(3.0)
z = tf.add(x_output, y)
for x in x_input:
    print(sess.run(z, feed_dict={x_output:x}))
'''

计算图的使用

import tensorflow as tf
import numpy as np
sess = tf.Session()
x_input = np.array([1.0,2.0,3.0,4.0,5.0])
x_output = tf.placeholder(tf.float32)
y = tf.constant(3.0)
z = tf.add(x_output, y)
for x in x_input:
    print(sess.run(z, feed_dict={x_output:x}))
print(z.graph is tf.get_default_graph())
'''
4.0
5.0
6.0
7.0
8.0
True
'''
3.1.2 在不同计算图上定义和使用张量进行计算
import tensorflow as tf
graph_1 = tf.Graph()
with graph_1.as_default():
    #在计算图graph_1中定义一个变量“v”,初始化变量“v”为维度为[1,3] 的张量[1,2,3]
    v = tf.get_variable("v", initializer =
tf.constant_initializer([1,2,3])(shape=[1,3]))
graph_2 = tf.Graph()
with graph_2.as_default():
    #在计算图graph_2中定义一个变量“v”,初始化变量“v”为维度为[2,3]的零张量
    v = tf.get_variable("v", initializer =
tf.zeros_initializer()(shape=[2,3]))
with tf.Session(graph = graph_1) as sess:
    tf.global_variables_initializer().run()
    with tf.variable_scope("", reuse = True):
        print(sess.run(tf.get_variable("v")))
with tf.Session(graph = graph_2) as sess:
    tf.global_variables_initializer().run()
    with tf.variable_scope("", reuse = True):
        print(sess.run(tf.get_variable("v")))
'''
[[1. 2. 3.]]
[[0. 0. 0.]
 [0. 0. 0.]]
 '''
3.2.1 在 GPU 上执行简单的算术运算
import tensorflow as tf
graph_1 = tf.Graph()
x = tf.constant([1.0,2.0,3.0])
y = tf.ones([1,3])
result = x + y
with graph_1.device("/gpu:0"):
    with tf.Session() as sess:
        print(sess.run(result))
        
'''
[[2. 3. 4.]]
'''

3.2 TensorFlow 的嵌入层

在一个计算图中进行多个乘法操作

import numpy as np
import tensorflow as tf
# 创建一个矩阵与运算符
x = np.array([[1.0, 2.0, 3.0],
              [4.0, 5.0, 6.0],
              [7.0, 8.0, 9.0]])
x_vals = np.array([x, x+1])
x_data = tf.placeholder(tf.float32, shape = (3, 3))
# 在矩阵乘法和加法中使用的常量矩阵
m_1 = tf.constant([[1.0], [2.0], [3.0]])
m_2 = tf.constant([[5.0]])
a_1 = tf.constant([[6.0]])
# 进行声明操作,将矩阵乘法和矩阵加法表示成计算图
prod_1 = tf.matmul(x_data, m_1)
prod_2 = tf.matmul(prod_1, m_2)
add_1 = tf.add(prod_2, a_1)
# 输出量矩阵经过相乘操作后的和
with tf.Session() as sess:
    for x_val in x_vals:
        print(sess.run(add_1, feed_dict = {x_data:x_val}))
'''
[[ 76.]
 [166.]
 [256.]]
[[106.]
 [196.]
 [286.]]
 '''

3.3 TensorFlow 的多层

import numpy as np
import tensorflow as tf
# 将size设为[1, 3, 3, 1]是因为tf中图像函数是处理四维图片的。
# 这四维依次是: 图片数量,高度, 宽度, 颜色通道
x_shape = [1,3,3,1]
x = np.random.uniform(size = x_shape)
# tf.nn.conv2d 中 name 表明该 layer 命名为 “Moving_Avg_Window”
# 卷积核为 [[0.25,0.25],[0.25,0.25]]
x_input = tf.placeholder(tf.float32, shape = x_shape)
my_filter = tf.constant(0.25, shape = [2, 2, 1, 1])
my_strides = [1, 2, 2, 1]
mov_avg_layer = tf.nn.conv2d(x_input, my_filter, my_strides, padding = "SAME", name = "Moving_Avg_Window")
# 自定义 layer,对卷积操作之后的输出做操作
def custom_layer(input_matrix):
    input_matrix_sqeezed = tf.squeeze(input_matrix)
    a = tf.constant([[1.0, 2.0], [-1.0, 3.0]])
    b = tf.constant(1.0, shape= [2,2])
    temp1 = tf.matmul(a, input_matrix_sqeezed)
    temp = tf.add(temp1, b)
    return tf.sigmoid(temp)
# 把刚刚自定义的 layer 加入到计算图中,并给予自定义的命名(利用tf.name_scope())
with tf.name_scope("Custom_layer") as scope:
    with tf.Session() as sess:
        custom_layer1 = custom_layer(mov_avg_layer)
        # 为占位符传入 3*3 图片,并执行计算图
        print(sess.run(custom_layer1, feed_dict = {x_input: x}))
'''
[[0.8483084  0.79479134]
 [0.77359766 0.75352585]]
 '''

3.4 TensorFlow 实现损失函数

3.4.1 softmax 损失函数
import tensorflow as tf
with tf.Session() as sess:
  unscaled_logits = tf.constant([[1.0,2.0,3.0,4.0]])
  target = tf.constant([[0.5,1.5,2.0,2.5]])
  softmax_xentropy = tf.nn.softmax_cross_entropy_with_logits(logits = unscaled_logits, labels = target)
  print(sess.run(softmax_xentropy))
    
'''
WARNING:tensorflow:From <ipython-input-9-421f451e4523>:5: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.
See `tf.nn.softmax_cross_entropy_with_logits_v2`.
[9.361233]
'''
3.4.1 稀疏矩阵 Softmax 交叉熵损失函数
import tensorflow as tf
with tf.Session() as sess:
    unscaled_logits = tf.constant([[1.0,2.0,3.0]])
    sparse_target = tf.constant([2])
    sparse_xentropy =tf.nn.sparse_softmax_cross_entropy_with_logits(logits = unscaled_logits, labels = sparse_target)
    print(sess.run(sparse_xentropy))
    
'''
[0.40760595]
'''
3.4.2 损失函数工作原理及实现
3.4.2 常见分类算法损失函数的图像
import tensorflow as tf
import matplotlib.pyplot as plt
x = tf.linspace(-3.0,5.0,500)
target = tf.constant(1.0)
targets = tf.fill([500,], 1.0)
hinge_y = tf.maximum(0.0, 1.0-tf.multiply(target, x))
xentropy_y = -tf.multiply(target, tf.log(x)) - tf.multiply((1.0 - target), tf.log(1.0 - x))
xentropy_sigmoid_y = tf.nn.sigmoid_cross_entropy_with_logits(logits = x, labels = targets)
weight = tf.constant(0.5)
xentropy_weighted_y = tf.nn.weighted_cross_entropy_with_logits(logits = x, targets = targets, pos_weight = weight)
with tf.Session() as sess:
    x_array = sess.run(x)
    hinge_y_output = sess.run(hinge_y)
    xentropy_y_output = sess.run(xentropy_y)
    xentropy_sigmoid_y_output = sess.run(xentropy_sigmoid_y)
    xentropy_weighted_y_output = sess.run(xentropy_weighted_y)
    plt.plot(x_array, hinge_y_output, "b-", label = "Hinge Loss")
    plt.plot(x_array, xentropy_y_output, "r--", label = "Cross Entropy Loss")
    plt.plot(x_array, xentropy_sigmoid_y_output, "k-.", label = "Cross Entropy Sigmoid Loss")
    plt.plot(x_array, xentropy_weighted_y_output, "g:", label = "Weighted Cross Entropy Sigmoid Loss(x0.5)")
    # 设置y轴刻度的范围,从0到3
    plt.ylim(-1.5, 3)
    #设置图例位置为图标下部靠右侧,字号为11
    plt.legend(loc = "lower right", prop = {"size":11})
    #输出图形
    plt.show()

3.4.2 用 matplotlib 绘制回归算法的损失函数
import tensorflow as tf
import matplotlib.pyplot as plt
x = tf.linspace(-1.0,1.0,500)
target = tf.constant(0.0)
l1_y = tf.abs(target - x)
l2_y = tf.square(target - x)
delta1 = tf.constant(.1)
phuber1_y = tf.multiply(tf.square(delta1),tf.sqrt(1.0 + tf.square((target - x)/delta1))-1.0)
delta2 = tf.constant(3.0)
phuber2_y = tf.multiply(tf.square(delta2), tf.sqrt(1.0 + tf.square((target - x)/delta2))-1.0)
with tf.Session() as sess:
    x_array = sess.run(x)
    l1_y_output = sess.run(l1_y)
    l2_y_output = sess.run(l2_y)
    phuber1_y_output = sess.run(phuber1_y)
    phuber2_y_output = sess.run(phuber2_y)
    plt.plot(x_array, l1_y_output, "g:", label = "L1 Loss")
    plt.plot(x_array, l2_y_output, "r-", label = "L2 Loss")
    plt.plot(x_array, phuber1_y_output, "k--", label = "P-Huber  Loss(1.0)")
    plt.plot(x_array, phuber2_y_output,"b-.", label = "P-Huber Loss(5.0)")
    # 设置y轴刻度的范围,从0到1
    plt.ylim(0, 1)
    #设置图例处于图标上部中心位置,字号为11
    plt.legend(loc = " upper center ", prop = {"size": 11})
    # 输出图形
    plt.show()
'''
D:\software\Anaconda3\lib\site-packages\ipykernel_launcher.py:24: MatplotlibDeprecationWarning: Unrecognized location ' upper center '. Falling back on 'best'; valid locations are
  best
  upper right
  upper left
  lower left
  lower right
  right
  center left
  center right
  lower center
  upper center
  center
'''

3.5.2 实现反向传播
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# 创建计算图
sess = tf.Session()
# 生成数据,100 个均值为 5,标准差为 0.1 的随机数”x”和 100 个值为 10 的目标数”y”
x = np.random.normal(5, 0.1, 100)
y = np.repeat(10.0, 100)
# 声明占位符
x_data = tf.placeholder(shape = [1], dtype = tf.float32)
y_target = tf.placeholder(shape = [1], dtype = tf.float32)
# 声明变量 A
A = tf.Variable(tf.random_normal(shape = [1]))
# 创建函数X×A=target
my_output = tf.multiply(x_data, A)
# 指定损失函数为 delta 参数为 0.25 的 Pseudo-Huber 函数,并初始化所有变量
deltal = tf.constant(0.25)
loss = tf.multiply(tf.square(deltal), tf.sqrt(1.0 + tf.square((my_output - y_target)/deltal)) - 1.0)
init = tf.global_variables_initializer()
sess.run(init)
# 声明变量的优化器
# 建了一个名为 my_opt 的梯度下降优化器对象,学习率为 0.05。然后,它使用该优化器对象的 minimize 方法来最小化损失函数 loss,返回的结果是一个训练操作(train_step),可以在 TensorFlow 会话中运行来执行一次训练步骤。在训练过程中,优化器会根据损失函数的梯度信息来更新模型参数,以使得损失函数的值逐渐降低,从而提高模型的性能。
my_opt = tf.train.GradientDescentOptimizer(0.05)
train_step = my_opt.minimize(loss)
# 将损失值导入创建的batch
loss_batch = []
# 开始训练算法并将每次迭代数据加载到matplotlib图中
for i in range(100):
    rand_index = np.random.choice(100)
    rand_x = [x[rand_index]]
    rand_y = [y[rand_index]]
    sess.run(train_step, feed_dict = {x_data: rand_x, y_target: rand_y})
    print("第" + str(i+1) + "次 A = " + str(sess.run(A)))
    print("损失值为 " + str(sess.run(loss, feed_dict = {x_data: rand_x, y_target: rand_y})))
    temp_loss = sess.run(loss, feed_dict = {x_data: rand_x, y_target: rand_y})
    loss_batch.append(temp_loss)
plt.plot(loss_batch, "g-.", label = "Batch Loss, size = 20")
plt.legend(loc = "upper right", prop = {"size": 11})
plt.show()
'''
第98次 A = [2.0645618]
损失值为 [0.00248083]
第99次 A = [2.0044377]
损失值为 [0.04232274]
第100次 A = [2.0397568]
损失值为 [5.453825e-06]
'''

3.6 通过随机训练个批量训练两种方法实现回归算法

#随机训练和批量训练
#导入相应模块并开始一个计算图会话
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.python.framework import ops
sess = tf.Session()
#批量训练
#声明批量大小为20
batch_size = 20
#声明数据并创建占位符
x = np.random.normal(1, 0.1, 200)
y = np.repeat(15.0, 200)
x_data = tf.placeholder(shape = [None, 1], dtype = tf.float32)
y_target = tf.placeholder(shape = [None, 1], dtype = tf.float32)
# 声明变量A
A = tf.Variable(tf.random_normal(shape = [1, 1]))
#在计算图中添加矩阵乘法操作
my_output = tf.matmul(x_data, A)
#设定损失函数为每个数据的L2损失的平均值
loss = tf.reduce_mean(tf.square(my_output - y_target))
#初始化全部变量
init = tf.global_variables_initializer()
sess.run(init)
#声明优化器
my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)
loss_batch = []
#开始迭代
for i in range(200):
    rand_index = np.random.choice(200, size = batch_size)
    rand_x = np.transpose([x[rand_index]])
    rand_y = np.transpose([y[rand_index]])
    sess.run(train_step, feed_dict = {x_data: rand_x, y_target:rand_y})
    #只输出迭代次数能整除20的结果
    if(i+1)%20 == 0:
        print("批量训练 第" + str(i+1) + "次迭代 A的值为" + str(sess.run(A)))
        temp_loss = sess.run(loss, feed_dict = {x_data: rand_x, y_target: rand_y})
        print("损失值为" + str(temp_loss))
        loss_batch.append(temp_loss)
#随机训练
#重置计算图
ops.reset_default_graph()
sess = tf.Session()
#声明数据
x = np.random.normal(1, 0.1, 200)
y = np.repeat(15.0, 200)
x_data = tf.placeholder(shape = [1], dtype = tf.float32)
y_target = tf.placeholder(shape = [1], dtype = tf.float32)
#声明变量A
A = tf.Variable(tf.random_normal(shape = [1]))
#在计算图中加入矩阵乘法操作
my_output = tf.multiply(x_data, A)
#设定损失函数
loss = tf.reduce_mean(tf.square(my_output - y_target))
#初始化所有变量的值
init = tf.global_variables_initializer()
sess.run(init)
#声明优化器
my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)
loss_stochastic = []
#开始迭代
for i in range(200):
    rand_index = np.random.choice(200)
    rand_x = [x[rand_index]]
    rand_y = [y[rand_index]]
    sess.run(train_step, feed_dict = {x_data: rand_x, y_target: rand_y})
    #只输出迭代次数能整除20的结果
    if (i+1)%20 == 0:
        print("随机训练第" + str(i+1) + "次迭代 A的值为" + str(sess.run(A)))
        temp_loss = sess.run(loss, feed_dict = {x_data: rand_x, y_target: rand_y})
        print("损失值为" + str(temp_loss))
        loss_stochastic.append(temp_loss)
#通过matplotlib绘制运行结果
plt.plot(range(0, 200, 20),loss_batch, "g-.", label = "Batch Loss, size = 20")
plt.plot(range(0, 200, 20),loss_stochastic, "r--", label = "Stochastic Loss")
plt.ylim(0, 100)
plt.legend(loc = "upper right", prop = {"size": 11})
plt.show()
'''损失值为10.310818
随机训练第140次迭代 A的值为[13.931806]
损失值为3.8222299
随机训练第160次迭代 A的值为[14.105816]
损失值为0.00593027
随机训练第180次迭代 A的值为[14.348159]
损失值为0.04666819
随机训练第200次迭代 A的值为[14.416402]
损失值为1.080638
'''

##3.7 TensorFlow 创建分类器

3.7 对鸢尾花进行分类

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
# 导入iris数据集
# 根据目标数据是否为山鸢尾将其转换成1或者0。
# 由于iris数据集将山鸢尾标记为0,将该标记默认对应数值从0置为1,同时把其他物种标记为0。
# 本次训练只使用两种特征:花瓣长度和花瓣宽度,这两个特征在x-value的第三列和第四列
# iris.target = {0, 1, 2}, where '0' is setosa
# iris.data ~ [sepal.width, sepal.length, pedal.width, pedal.length]
iris = datasets.load_iris()
binary_target = np.array([1. if x==0 else 0. for x in iris.target])
iris_2d = np.array([[x[2], x[3]] for x in iris.data])
# 声明批量训练大小
batch_size = 20
# 初始化计算图
sess = tf.Session()
# 声明数据占位符
x1_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
x2_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)
# 声明模型变量
# Create variables A and b (0 = x1 - A*x2 + b)
A = tf.Variable(tf.random_normal(shape=[1, 1]))
b = tf.Variable(tf.random_normal(shape=[1, 1]))
# 定义线性模型:
# 如果找到的数据点在直线以上,则将数据点代入x2-x1*A-b计算出的结果大于0;
# 同理找到的数据点在直线以下,则将数据点代入x2-x1*A-b计算出的结果小于0。
# x1 - A*x2 + b
my_mult = tf.matmul(x2_data, A)
my_add = tf.add(my_mult, b)
my_output = tf.subtract(x1_data, my_add)
# 增加TensorFlow的sigmoid交叉熵损失函数(cross entropy)
xentropy = tf.nn.sigmoid_cross_entropy_with_logits(logits=my_output, labels=y_target)
# 声明优化器方法
my_opt = tf.train.GradientDescentOptimizer(0.05)
train_step = my_opt.minimize(xentropy)
# 创建一个变量初始化操作
init = tf.global_variables_initializer()
sess.run(init)
# 运行迭代1000次
for i in range(1000):
  rand_index = np.random.choice(len(iris_2d), size=batch_size)
  # rand_x = np.transpose([iris_2d[rand_index]])
  # 传入三种数据:花瓣长度、花瓣宽度和目标变量
  rand_x = iris_2d[rand_index]
  rand_x1 = np.array([[x[0]] for x in rand_x])
  rand_x2 = np.array([[x[1]] for x in rand_x])
  #rand_y = np.transpose([binary_target[rand_index]])
  rand_y = np.array([[y] for y in binary_target[rand_index]])
  sess.run(train_step, feed_dict={x1_data: rand_x1, x2_data: rand_x2, y_target: rand_y})
  if (i+1)%200==0:
    print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)) + ', b = ' + str(sess.run(b)))
# 绘图
# 获取斜率/截距
# Pull out slope/intercept
[[slope]] = sess.run(A)
[[intercept]] = sess.run(b)
# 创建拟合线
x = np.linspace(0, 3, num=50)
ablineValues = []
for i in x:
 ablineValues.append(slope*i+intercept)
# 绘制拟合曲线
setosa_x = [a[1] for i,a in enumerate(iris_2d) if binary_target[i]==1]
setosa_y = [a[0] for i,a in enumerate(iris_2d) if binary_target[i]==1]
non_setosa_x = [a[1] for i,a in enumerate(iris_2d) if binary_target[i]==0]
non_setosa_y = [a[0] for i,a in enumerate(iris_2d) if binary_target[i]==0]
plt.plot(setosa_x, setosa_y, 'rx', ms=10, mew=2, label='setosa')
plt.plot(non_setosa_x, non_setosa_y, 'ro', label='Non-setosa')
plt.plot(x, ablineValues, 'b-')
plt.xlim([0.0, 2.7])
plt.ylim([0.0, 7.1])
plt.suptitle('Linear Separator For I.setosa', fontsize=20)
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
plt.legend(loc='lower right')
plt.show()
'''
Step #200 A = [[8.660623]], b = [[-3.5443878]]
Step #400 A = [[10.249058]], b = [[-4.655245]]
Step #600 A = [[11.182921]], b = [[-5.409725]]
Step #800 A = [[11.828324]], b = [[-5.999825]]
Step #1000 A = [[12.405066]], b = [[-6.357325]]
'''

![3.7 对鸢尾花进行分类1](C:\Users\L\Documents\深度学习\images\3.7 对鸢尾花进行分类1.png)# TensorFlow模型评估
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
# 创建计算图
sess = tf.Session()
# 回归例子:
# We will create sample data as follows:
# x-data: 100 random samples from a normal ~ N(1, 0.1)
# target: 100 values of the value 10.
# We will fit the model:
# x-data * A = target
# 理论上, A = 10.
# 声明批量大小
batch_size = 25
# 创建数据集
x_vals = np.random.normal(1, 0.1, 100)
y_vals = np.repeat(10., 100)
x_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)
# 八二分训练/测试数据 train/test = 80%/20%
train_indices = np.random.choice(len(x_vals), round(len(x_vals)*0.8), replace=False)
test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
x_vals_train = x_vals[train_indices]
x_vals_test = x_vals[test_indices]
y_vals_train = y_vals[train_indices]
y_vals_test = y_vals[test_indices]
# 创建变量 (one model parameter = A)
A = tf.Variable(tf.random_normal(shape=[1,1]))
# 增加操作到计算图
my_output = tf.matmul(x_data, A)
# 增加L2损失函数到计算图
loss = tf.reduce_mean(tf.square(my_output - y_target))
# 创建优化器
my_opt = tf.train.GradientDescentOptimizer(0.02)
train_step = my_opt.minimize(loss)
# 初始化变量
init = tf.global_variables_initializer()
sess.run(init)
# 迭代运行
# 如果在损失函数中使用的模型输出结果经过转换操作,例如,sigmoid_cross_entropy_with_logits()函数,
# 为了精确计算预测结果,别忘了在模型评估中也要进行转换操作。
for i in range(100):
    rand_index = np.random.choice(len(x_vals_train), size=batch_size)
    rand_x = np.transpose([x_vals_train[rand_index]])
    rand_y = np.transpose([y_vals_train[rand_index]])
    sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
    if (i+1)%25==0:
        print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))
        print('Loss = ' + str(sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})))
# 评估准确率(loss)
mse_test = sess.run(loss, feed_dict={x_data: np.transpose([x_vals_test]), y_target: np.transpose([y_vals_test])})
mse_train = sess.run(loss, feed_dict={x_data: np.transpose([x_vals_train]), y_target: np.transpose([y_vals_train])})
print('MSE on test:' + str(np.round(mse_test, 2)))
print('MSE on train:' + str(np.round(mse_train, 2)))
# 分类算法案例
# We will create sample data as follows:
# x-data: sample 50 random values from a normal = N(-1, 1)
#         + sample 50 random values from a normal = N(1, 1)
# target: 50 values of 0 + 50 values of 1.
#         These are essentially 100 values of the corresponding output index
# We will fit the binary classification model:
# If sigmoid(x+A) < 0.5 -> 0 else 1
# Theoretically, A should be -(mean1 + mean2)/2
# 重置计算图
ops.reset_default_graph()
# 加载计算图
sess = tf.Session()
# 声明批量大小
batch_size = 25
# 创建数据集
x_vals = np.concatenate((np.random.normal(-1, 1, 50), np.random.normal(2, 1, 50)))
y_vals = np.concatenate((np.repeat(0., 50), np.repeat(1., 50)))
x_data = tf.placeholder(shape=[1, None], dtype=tf.float32)
y_target = tf.placeholder(shape=[1, None], dtype=tf.float32)
# 分割数据集 train/test = 80%/20%
train_indices = np.random.choice(len(x_vals), round(len(x_vals)*0.8), replace=False)
test_indices = np.array(list(set(range(len(x_vals))) - set(train_indices)))
x_vals_train = x_vals[train_indices]
x_vals_test = x_vals[test_indices]
y_vals_train = y_vals[train_indices]
y_vals_test = y_vals[test_indices]
# 创建变量 (one model parameter = A)
A = tf.Variable(tf.random_normal(mean=10, shape=[1]))
# Add operation to graph
# Want to create the operstion sigmoid(x + A)
# Note, the sigmoid() part is in the loss function
my_output = tf.add(x_data, A)
# 增加分类损失函数 (cross entropy)
xentropy = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=my_output, labels=y_target))
# 创建优化器
my_opt = tf.train.GradientDescentOptimizer(0.05)
train_step = my_opt.minimize(xentropy)
# 初始化变量
init = tf.global_variables_initializer()
sess.run(init)
# 运行迭代
for i in range(1800):
    rand_index = np.random.choice(len(x_vals_train), size=batch_size)
    rand_x = [x_vals_train[rand_index]]
    rand_y = [y_vals_train[rand_index]]
    sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
    if (i+1)%200==0:
        print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))
        print('Loss = ' + str(sess.run(xentropy, feed_dict={x_data: rand_x, y_target: rand_y})))
# 评估预测
# 用squeeze()函数封装预测操作,使得预测值和目标值有相同的维度。
y_prediction = tf.squeeze(tf.round(tf.nn.sigmoid(tf.add(x_data, A))))
# 通过equal()函数检测输出是否相等,
# 把得到的true或false的boolean型张量转化成float32型,
# 再对其取平均值,得到一个准确度值。
correct_prediction = tf.equal(y_prediction, y_target)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
acc_value_test = sess.run(accuracy, feed_dict={x_data: [x_vals_test], y_target: [y_vals_test]})
acc_value_train = sess.run(accuracy, feed_dict={x_data: [x_vals_train], y_target: [y_vals_train]})
print('Accuracy on train set: ' + str(acc_value_train))
print('Accuracy on test set: ' + str(acc_value_test))
# 绘制分类结果
A_result = -sess.run(A)
bins = np.linspace(-5, 5, 50)
plt.hist(x_vals[0:50], bins, alpha=0.5, label='N(-1,1)', color='blue')
plt.hist(x_vals[50:100], bins[0:50], alpha=0.5, label='N(2,1)', color='red')
plt.plot((A_result, A_result), (0, 8), 'k--', linewidth=3, label='A = '+ str(np.round(A_result, 2)))
plt.legend(loc='upper right')
plt.title('Binary Classifier, Accuracy=' + str(np.round(acc_value_test, 2)))
plt.show()
'''
Loss = 0.2785458
Step #1200 A = [-0.47475502]
Loss = 0.29810706
Step #1400 A = [-0.4993782]
Loss = 0.27858698
Step #1600 A = [-0.50035167]
Loss = 0.32330224
Step #1800 A = [-0.479477]
Loss = 0.28394452
Accuracy on train set: 0.9125
Accuracy on test set: 0.8
'''


相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
目录
相关文章
|
机器学习/深度学习 算法 TensorFlow
TensorFlow 进阶:定制模型和训练算法
本文将为你提供关于 TensorFlow 的中级知识,你将学习如何通过子类化构建自定义的神经网络层,以及如何自定义训练算法。
|
机器学习/深度学习 人工智能 TensorFlow
AI开发者大会之AI学习与进阶实践:2020年7月3日《如何转型搞AI?》、《基于AI行业价值的AI学习与进阶路径》、《自动机器学习与前沿AI开源项目》、《使用TensorFlow实现经典模型》
AI开发者大会之AI学习与进阶实践:2020年7月3日《如何转型搞AI?》、《基于AI行业价值的AI学习与进阶路径》、《自动机器学习与前沿AI开源项目》、《使用TensorFlow实现经典模型》
AI开发者大会之AI学习与进阶实践:2020年7月3日《如何转型搞AI?》、《基于AI行业价值的AI学习与进阶路径》、《自动机器学习与前沿AI开源项目》、《使用TensorFlow实现经典模型》
|
机器学习/深度学习 TensorFlow 算法框架/工具
深度学习进阶:多分类与TensorFlow(三)
深度学习进阶:多分类与TensorFlow(三)
169 0
深度学习进阶:多分类与TensorFlow(三)
|
机器学习/深度学习 TensorFlow API
深度学习进阶:多分类与TensorFlow(二)
深度学习进阶:多分类与TensorFlow(二)
143 0
深度学习进阶:多分类与TensorFlow(二)
|
机器学习/深度学习 人工智能 算法
深度学习进阶:多分类与TensorFlow(一)
深度学习进阶:多分类与TensorFlow(一)
231 0
深度学习进阶:多分类与TensorFlow(一)
|
算法框架/工具 TensorFlow 算法
带你读《TensorFlow机器学习实战指南(原书第2版)》之二:TensorFlow进阶
本书由资深数据科学家撰写,从实战角度系统讲解TensorFlow基本概念及各种应用实践。真实的应用场景和数据,丰富的代码实例,详尽的操作步骤,带领读者由浅入深系统掌握TensorFlow机器学习算法及其实现。本书第1章和第2章介绍了关于TensorFlow使用的基础知识,后续章节则针对一些典型算法和典型应用场景进行了实现,并配有较详细的程序说明,可读性非常强。读者如果能对其中代码进行复现,则必定会对TensorFlow的使用了如指掌。
|
机器学习/深度学习 存储 TensorFlow
学习笔记CB014:TensorFlow seq2seq模型步步进阶
神经网络。《Make Your Own Neural Network》,用非常通俗易懂描述讲解人工神经网络原理用代码实现,试验效果非常好。 循环神经网络和LSTM。Christopher Olah http://colah.github.io/posts/2015-08-Understanding-LSTMs/ 。
2602 0
|
数据可视化 TensorFlow 算法框架/工具
|
机器学习/深度学习 数据可视化 TensorFlow

热门文章

最新文章