美文网首页
python进阶-15-神经网络

python进阶-15-神经网络

作者: 西海岸虎皮猫大人 | 来源:发表于2020-09-24 13:22 被阅读0次

1 概述

http://playground.tensorflow.org
模拟大脑
输入层 - 隐藏层 - 输出层
同层神经元无连接
层间连接带权重
(树突-轴突) - 突触
逻辑回归 - sigmoid函数
线型可分 | 不可分
线性不可分用可用多层网络或者换激活函数

2 激活函数

sigmoid函数

S(x) = 1/(1+e^(-1))

# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt

# 定义sigmoid函数
def sigmoid(x):
    return 1.0/(1+np.exp(-x))

x = np.arange(-10, 10)
# 指定画布大小
fig, ax = plt.subplots(figsize=(12, 4))
ax.plot(x, sigmoid(x), 'r')
plt.show()
tanh函数
# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-10, 10, 100)
y = np.tanh(x)
plt.plot(x, y)
plt.show()

3 TensorFlow

图形分类 音频处理 推荐系统 自然语言处理
目前最热门的机器学习框架
分CPU和GPU版本,GPU核心更多,适合并行

# 该版本与最新python冲突,适用于3.6.5,其他版本未验证
pip install tensorflow==1.12.0
hello world
# coding=utf-8
import tensorflow as tf


# 变量求和
def add_demo():
    # tf.compat.v1.disable_eager_execution()
    a_t = tf.constant(10)
    # 类型 形状
    print('a_t', a_t)
    b_t = tf.constant(20)
    c_t = a_t + b_t
    # 开启会话
    # with tf.compat.v1.Session() as sess:
    with tf.Session() as sess:
        c_t_value = sess.run(c_t)
        print('c_t_value:', c_t_value)


# FutureWarning:
if __name__ == '__main__':
    # 1.构建图
    # 2.开启会话执行图
    # 图即指令之间依赖关系,描述数据操作步骤
    # 线表示节点间相互联系的多维数组
    add_demo()
查看默认图
# coding=utf-8
# 查看默认图
import tensorflow as tf

def default_graph_demo():
    # 版本冲突
    # tf.compat.v1.disable_eager_execution()
    a = tf.constant(10)
    b = tf.constant(20)
    c = a + b
    print(tf.get_default_graph())
    # print('tf默认图:', 15_neural_network.get_default_graph())
    print('a默认图属性:', a.graph)
    print('b默认图属性:', b.graph)
    print('c默认图属性:', c.graph)
    # 开启会话
    # with tf.compat.v1.Session() as sess:
    with tf.Session() as sess:
        c_value = sess.run(c)
        print('c_value:', c_value)


if __name__ == '__main__':
    default_graph_demo()
自定义图
# coding=utf-8
# 自定义图
import tensorflow as tf


def graph_demo():
    # 自定义图
    new_g = tf.Graph()
    with new_g.as_default():
        a_new = tf.constant(10)
        b_new = tf.constant(20)
        c_new = a_new + b_new
    # 开启会话
    # with tf.compat.v1.Session(graph=new_g) as sess:
    # 自定义图需要传递图参数
    with tf.Session(graph=new_g) as sess:
        c_new_value = sess.run(c_new)
        print('c_new_value:', c_new_value)


if __name__ == '__main__':
    graph_demo()
图可视化
# coding=utf-8
# 图可视化
import tensorflow as tf


def graph_demo():
    new_g = tf.Graph()
    with new_g.as_default():
        a_new = tf.constant(10, name='a')
        b_new = tf.constant(20, name='b')
        c_new = a_new + b_new
    # with tf.compat.v1.Session(graph=new_g) as sess:
    with tf.Session(graph=new_g) as sess:
        c_new_value = sess.run(c_new)
        print('c_new_value:', c_new_value)
        # 生成events文件
        tf.summary.FileWriter('d:/dat/events/test', graph=new_g)


# 执行
# tensorboard --logdir='d:/dat/events/test'
# 异常: No dashboards are active for the current data set. todo
if __name__ == '__main__':
    import warnings
    warnings.filterwarnings("ignore")
    graph_demo()
会话
# 会话
import tensorflow as tf


def session_demo():
    a = tf.constant(20)
    b = tf.constant(30)
    c = a + b
    print(c)
    # 输出设备信息
    # with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
    #     log_device_placement=True)) as sess:
    with tf.Session() as sess:
        c_v = sess.run(c)
        # print(c_v)
        # 参数可以传入列表,还可传入元组
        # v = sess.run([a, b, c])
        # print(v)
        a_v, b_v, c_v = sess.run((a, b, c))
        # print(a_v, b_v, c_v)
        # 可以同eval直接获取结果
        print(c.eval())


if __name__ == '__main__':
    session_demo()
占位
# 占位
# 可以是任意维度
# 与placeholder搭配
import tensorflow as tf

a = tf.placeholder(tf.float32)
b = tf.placeholder('float32')
# c = a + b
cc = tf.add(a, b)
x = tf.placeholder(tf.float32, None)
y = x * 20 + 100

with tf.Session() as sess:
    # feed_dict传入参数
    # c_v = sess.run(c, feed_dict={a:20, b:30})
    # c_v = sess.run(cc, feed_dict={a:20, b:30})
    # print(c_v)
    # y_v = sess.run(y, feed_dict={x:10})
    # 可以传入列表
    y_v = sess.run(y, feed_dict={x:[10, 20, 30]})
    print(y_v)
张量
# 张量即n维数组
# 秩为0的张量即标量
# 秩为1即向量
# 秩为2即矩阵
# 张量维度称为阶

import tensorflow as tf


def tensor_demo():
    # 固定张量创建
    tensor1 = tf.constant(3)
    print(tensor1)
    tensor2 = tf.constant([1, 2, 3, 4, 5])
    print(tensor2)
    tensor3 = tf.constant([[1, 2, 3], [4, 5, 6]])
    print(tensor3)

    # 随机张量创建
    t_ones = tf.ones(shape=[2, 3])
    t_zeros = tf.zeros(shape=[3, 4])
    print(t_ones)
    print(t_zeros)

    # 均匀分布
    t_uniform = tf.random_uniform(shape=[1, 2], minval=1, maxval=4)
    print(t_uniform)

    # 正态分布 均值 | 方差
    t_random = tf.random_normal(shape=[2, 2], mean=1.0, stddev=3)

    with tf.Session() as sess:
        t_ones_v = sess.run(t_ones)
        print(t_ones_v)
        t_zeros_v = sess.run(t_zeros)
        print(t_zeros_v)
        t_uniform_v = sess.run(t_uniform)
        print(t_uniform_v)
        t_random_v = sess.run(t_random)
        print(t_random_v)


if __name__ == '__main__':
    tensor_demo()
张量形状变换
# 张量形状变换
# 动态修改 静态修改

import tensorflow as tf


def tensor_shape_demo():
    # 静态形状修改
    a_p = tf.placeholder(dtype=tf.float32, shape=[None, None])
    a_p.set_shape([3, 4])
    print(a_p)
    # 已经固定好形状, 不能再次修改
    # a_p.set_shape([2, 5])
    # print(a_p)

    b_p = tf.placeholder(dtype=tf.float32, shape=[None, 5])
    # 列数必须为5
    b_p.set_shape([5, 5])
    print(b_p)

    # 动态修改
    c_p = tf.placeholder(dtype=tf.float32, shape=[3, 4])
    # 保持总个数一致
    # 修改后重新接收
    c_p = tf.reshape(c_p, shape=[2, 6])
    print(c_p)
    # tf.reshape(c_p, shape=[2, 2])
    # print(c_p)


if __name__ == '__main__':
    tensor_shape_demo()
矩阵
# 矩阵
import tensorflow as tf

A = tf.constant([[1, 2, 3], [4, 5, 6]])
# 3行4列 值3.5
B = tf.fill([3, 4], 3)
C = tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]])
with tf.Session() as sess:
    A_v = sess.run(A)
    B_v = sess.run(B)
    # 矩阵乘法
    M = tf.matmul(A, B)
    print(M)
    M_v = sess.run(M)
    print(M_v)

    # 矩阵加法
    ADD = tf.add(M_v, C)
    print(ADD)
    ADD_v = sess.run(ADD)
    print(ADD_v)
变量
# 变量
# 训练模型需要调节权重 偏置等
# 变量可以持久化 修改
import tensorflow as tf
x = tf.constant([10, 20, 30, 40])
# 定义变量
y = tf.Variable(initial_value=x*20+10)
# b = tf.Variable(initial_value=100)
# 修改命名空间
with tf.variable_scope('my_scope'):
    b = tf.Variable(initial_value=100)
print(b)
print(x)
print(y)
model = tf.global_variables_initializer()
with tf.Session() as sess:
    # 变量需要初始化才能获取
    sess.run(model)
    y_v = sess.run(y)
    print(y_v)
线性回归
# 线型回归
# 1.构造模型 y = w1*x1 + w2*x2 + ... + b
# 2.构建损失函数
# 3.优化损失,使用梯度下降
# 损失最小的权重和偏置

# 假定100个点 关系满足 y = kx + b
# x 100行1列
# 真实y 100行1列
# 预测y = weight(1, 1) * x + bias(100, 1)
# 损失函数: 均方误差  tf.reduce_mean(tf.square(y_predict-y_true))
# 优化损失 GradientDescentOptimizer
# 不断迭代使损失最小,获得权重和偏置

import tensorflow as tf


def linear_regression():
    # 1.准备数据
    X = tf.random_normal(shape=[100, 1])
    # 真实y
    y_true = tf.matmul(X, [[0.8]]) + 0.7
    # 构造权重和偏置,使用变量
    weight = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]))
    bias = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]))
    # 预测
    y_predict = tf.matmul(X, weight) + bias
    # 2.损失函数
    error = tf.reduce_mean(tf.square(y_predict-y_true))
    # 3.优化损失
    # 增大学习率可减少学习次数
    # 学习率不是越高越好, 太高会爆炸
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(error)
    # 初始化变量
    init = tf.global_variables_initializer()
    # 4.训练
    with tf.Session() as sess:
        sess.run(init)
        # 训练前查看偏置和权重
        print('训练前模型参数 权重:%f, 偏置: %f, 损失: %f'%(weight.eval(), bias.eval(), error.eval()))
        # 训练1000次
        for i in range(1000):
            sess.run(optimizer)
            print('训练%d后模型参数 权重:%f, 偏置: %f, 损失: %f'%((i+1), weight.eval(), bias.eval(), error.eval()))


if __name__ == '__main__':
    linear_regression()
添加变量显示
# 添加变量显示

import tensorflow as tf


def linear_regression():
    # 1.准备数据
    X = tf.random_normal(shape=[100, 1])
    # 真实y
    y_true = tf.matmul(X, [[0.8]]) + 0.7
    # 构造权重和偏置,使用变量
    weight = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]))
    bias = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]))
    # 预测
    y_predict = tf.matmul(X, weight) + bias
    # 2.损失函数
    error = tf.reduce_mean(tf.square(y_predict-y_true))
    # 3.优化损失
    # 增大学习率可减少学习次数
    # 学习率不是越高越好, 太高会爆炸
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(error)
    # 变量显示2: 收集变量
    tf.summary.scalar('error', error)
    # 收集高维
    tf.summary.histogram('weights', weight)
    tf.summary.histogram('bias', bias)
    # 变量显示3: 合并变量
    merged = tf.summary.merge_all()

    # 初始化变量
    init = tf.global_variables_initializer()
    # 4.训练
    with tf.Session() as sess:
        sess.run(init)
        # 训练前查看偏置和权重
        print('训练前模型参数 权重:%f, 偏置: %f, 损失: %f'%(weight.eval(), bias.eval(), error.eval()))
        # 变量显示1: 创建事件文件,增加变量显示
        file_writer = tf.summary.FileWriter('D:/dat/test', graph=sess.graph)
        # 训练1000次
        for i in range(1000):
            sess.run(optimizer)
            print('训练%d后模型参数 权重:%f, 偏置: %f, 损失: %f'%((i+1), weight.eval(), bias.eval(), error.eval()))
            # 变量显示4: 运行合并变量
            summary = sess.run(merged)
            # 变量显示5: 变量写入事件文件
            file_writer.add_summary(summary, i)


# 命令行运行 tensorboard --logdir="D:/dat/test"
# 异常: No graph definition files were found. todo
if __name__ == '__main__':
    linear_regression()
添加命名空间
# 添加命名空间
# 变量命名
# 可视化更加清晰

import tensorflow as tf


def linear_regression():
    with tf.variable_scope('prepare_data'):
        # 1.准备数据 添加名称
        X = tf.random_normal(shape=[100, 1], name='feature')
        # 真实y
        y_true = tf.matmul(X, [[0.8]]) + 0.7
    with tf.variable_scope('create_mode'):
        # 构造权重和偏置,使用变量
        weight = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]), name='weight')
        bias = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]), name='bias')
        # 预测
        y_predict = tf.matmul(X, weight) + bias
    with tf.variable_scope('loss_function'):
        # 2.损失函数
        error = tf.reduce_mean(tf.square(y_predict-y_true))
    with tf.variable_scope('optimizer'):
        # 3.优化损失
        # 增大学习率可减少学习次数
        # 学习率不是越高越好, 太高会爆炸
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(error)
    # 变量显示2: 收集变量
    tf.summary.scalar('error', error)
    # 收集高维
    tf.summary.histogram('weights', weight)
    tf.summary.histogram('bias', bias)
    # 变量显示3: 合并变量
    merged = tf.summary.merge_all()

    # 初始化变量
    init = tf.global_variables_initializer()
    # 4.训练
    with tf.Session() as sess:
        sess.run(init)
        # 训练前查看偏置和权重
        print('训练前模型参数 权重:%f, 偏置: %f, 损失: %f'%(weight.eval(), bias.eval(), error.eval()))
        # 变量显示1: 创建事件文件,增加变量显示
        file_writer = tf.summary.FileWriter('D:/dat/test', graph=sess.graph)
        # 训练1000次
        for i in range(1000):
            sess.run(optimizer)
            print('训练%d后模型参数 权重:%f, 偏置: %f, 损失: %f'%((i+1), weight.eval(), bias.eval(), error.eval()))
            # 变量显示4: 运行合并变量
            summary = sess.run(merged)
            # 变量显示5: 变量写入事件文件
            file_writer.add_summary(summary, i)


# 命令行运行 tensorboard --logdir="D:/dat/test"
# 异常: No graph definition files were found. todo
if __name__ == '__main__':
    linear_regression()
保存模型
# 保存模型
# 实际训练可能不是一次完成
import tensorflow as tf


def linear_regression():
    with tf.variable_scope('prepare_data'):
        # 1.准备数据 添加名称
        X = tf.random_normal(shape=[100, 1], name='feature')
        # 真实y
        y_true = tf.matmul(X, [[0.8]]) + 0.7
    with tf.variable_scope('create_mode'):
        # 构造权重和偏置,使用变量
        weight = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]), name='weight')
        bias = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]), name='bias')
        # 预测
        y_predict = tf.matmul(X, weight) + bias
    with tf.variable_scope('loss_function'):
        # 2.损失函数
        error = tf.reduce_mean(tf.square(y_predict-y_true))
    with tf.variable_scope('optimizer'):
        # 3.优化损失
        # 增大学习率可减少学习次数
        # 学习率不是越高越好, 太高会爆炸
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(error)
    # 变量显示2: 收集变量
    tf.summary.scalar('error', error)
    # 收集高维
    tf.summary.histogram('weights', weight)
    tf.summary.histogram('bias', bias)
    # 变量显示3: 合并变量
    merged = tf.summary.merge_all()

    # 保存模型1: 创建saver对象
    saver = tf.train.Saver()

    # 初始化变量
    init = tf.global_variables_initializer()
    # 4.训练
    with tf.Session() as sess:
        sess.run(init)
        # 训练前查看偏置和权重
        print('训练前模型参数 权重:%f, 偏置: %f, 损失: %f'%(weight.eval(), bias.eval(), error.eval()))
        # 变量显示1: 创建事件文件,增加变量显示
        file_writer = tf.summary.FileWriter('D:/dat/test', graph=sess.graph)
        # 训练1000次
        for i in range(1000):
            sess.run(optimizer)
            print('训练%d后模型参数 权重:%f, 偏置: %f, 损失: %f'%((i+1), weight.eval(), bias.eval(), error.eval()))
            # 变量显示4: 运行合并变量
            summary = sess.run(merged)
            # 变量显示5: 变量写入事件文件
            file_writer.add_summary(summary, i)
            # 保存模型2:
            if i% 10 == 0:
                saver.save(sess, './ckpt/linear_regression.ckpt')
# 命令行运行 tensorboard --logdir="D:/dat/test"
# 异常: No graph definition files were found. todo
if __name__ == '__main__':
    linear_regression()

读取模型
# 读取模型
import tensorflow as tf


def linear_regression():
    with tf.variable_scope('prepare_data'):
        # 1.准备数据 添加名称
        X = tf.random_normal(shape=[100, 1], name='feature')
        # 真实y
        y_true = tf.matmul(X, [[0.8]]) + 0.7
    with tf.variable_scope('create_mode'):
        # 构造权重和偏置,使用变量
        weight = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]), name='weight')
        bias = tf.Variable(initial_value=tf.random_normal(shape=[1, 1]), name='bias')
        # 预测
        y_predict = tf.matmul(X, weight) + bias
    with tf.variable_scope('loss_function'):
        # 2.损失函数
        error = tf.reduce_mean(tf.square(y_predict-y_true))
    with tf.variable_scope('optimizer'):
        # 3.优化损失
        # 增大学习率可减少学习次数
        # 学习率不是越高越好, 太高会爆炸
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(error)
    # 变量显示2: 收集变量
    tf.summary.scalar('error', error)
    # 收集高维
    tf.summary.histogram('weights', weight)
    tf.summary.histogram('bias', bias)
    # 变量显示3: 合并变量
    merged = tf.summary.merge_all()

    # 保存模型1: 创建saver对象
    saver = tf.train.Saver()

    # 初始化变量
    init = tf.global_variables_initializer()
    # 4.训练
    with tf.Session() as sess:
        sess.run(init)
        # 训练前查看偏置和权重
        print('训练前模型参数 权重:%f, 偏置: %f, 损失: %f'%(weight.eval(), bias.eval(), error.eval()))
        # 变量显示1: 创建事件文件,增加变量显示
        file_writer = tf.summary.FileWriter('D:/dat/test', graph=sess.graph)

        # 读取模型
        # 判断模型是否存
        ckpt = tf.train.get_checkpoint_state('./ckpt/')
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, "./ckpt/linear_regression.ckpt")
        print('训练后模型参数 权重:%f, 偏置: %f, 损失: %f'%(weight.eval(), bias.eval(), error.eval()))


if __name__ == '__main__':
    linear_regression()

4 案例 - 手写数字识别

mnist数据集
# minist数据集
# 经典数据集
# 28*28像素的手写数字图片
# 下载地址:
# http://yann.lecun.com/exdb/mnist/
# 使用one-hot编码
# 1 [0, 1, ...]
# 5 [0, 0, 0, 0, 0, 5, ...]

from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
import tensorflow as tf

# 加载数据集
mnist = input_data.read_data_sets('D:/dat/ai/mnist_data', one_hot=True)
# 加载训练集样本
train_x = mnist.train.images
# 验证集样本
validation_x = mnist.validation.images
# 测试集样本
test_x = mnist.test.images
# 训练集标签
train_y = mnist.train.labels
# 验证集标签
valadation_y = mnist.validation.labels
# 测试集标签
test_y = mnist.test.labels
print('train_x.shape: ', train_x.shape, ' train_y.shape: ', train_y.shape)
# 查看训练集第一个样本的内容和标签
print(train_x[0])
print(train_y[0])
# 获取数据数据前100个
images, labels = mnist.train.next_batch(100)
print('images.shape: ', images.shape, ' labels.shape: ', labels.shape)
# 绘制前20个样本
fig, ax = plt.subplots(nrows=4, ncols=5)
ax = ax.flatten()
for i in range(20):
    img = train_x[i].reshape(28, 28)
    # 灰度
    ax[i].imshow(img, cmap='Greys')
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.show()
数字识别
# 手写数字识别
# 增加命名空间
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

def mnist_demo():
    mnist = input_data.read_data_sets('D:/dat/ai/mnist_data', one_hot=True)
    images, labels = mnist.train.next_batch(100)
    print('images.shape: ', images.shape, ' labels.shape: ', labels.shape)
    # 1.准备数据
    with tf.variable_scope('prepare_data'):
        x = tf.placeholder(dtype=tf.float32, shape=[None, 784], name='feature')
        y_true = tf.placeholder(dtype=tf.float32, shape=[None, 10], name='y_true')
    # 2.构建模型 y(None, 10) = x(None, 10) * weight(784, 10) + bias
    with tf.variable_scope('create_mode'):
        weight = tf.Variable(initial_value=tf.random_normal(shape=[784, 10]), name='weight')
        bias = tf.Variable(initial_value=tf.random_normal(shape=[10]), name='bias')
        y_predict = tf.matmul(x, weight) + bias
    # 3.构建损失函数 softmax多分类 交叉商损失
    with tf.variable_scope('loss_function'):
        error = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
    # 4.优化损失
    with tf.variable_scope('optimizer'):
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(error)
    tf.summary.scalar('error', error)
    tf.summary.histogram('weight', weight)
    tf.summary.histogram('bias', bias)
    merged = tf.summary.merge_all()
    # 5.训练
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        print('训练前损失:%f'%(sess.run(error, feed_dict= {x:images, y_true: labels})))
        # 变量显示1: 创建事件文件
        file_writer = tf.summary.FileWriter('D:/dat/events', graph=sess.graph)
        for i in range(200):
            op, loss = sess.run([optimizer, error], feed_dict={x:images, y_true: labels})
            print('第%d次训练模型的损失: %f'%((i+1), loss))
            summary = sess.run(merged, feed_dict={x: images, y_true: labels})
            file_writer.add_summary(summary, i)
            file_writer.close()


# tendorboard --logdir="d:/dat/events"
if __name__ == '__main__':
    mnist_demo()
保存模型
# 手写数字识别
# 保存模型
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

def mnist_demo():
    mnist = input_data.read_data_sets('D:/dat/ai/mnist_data', one_hot=True)
    images, labels = mnist.train.next_batch(100)
    print('images.shape: ', images.shape, ' labels.shape: ', labels.shape)
    # 1.准备数据
    with tf.variable_scope('prepare_data'):
        x = tf.placeholder(dtype=tf.float32, shape=[None, 784], name='feature')
        y_true = tf.placeholder(dtype=tf.float32, shape=[None, 10], name='y_true')
    # 2.构建模型 y(None, 10) = x(None, 10) * weight(784, 10) + bias
    with tf.variable_scope('create_mode'):
        weight = tf.Variable(initial_value=tf.random_normal(shape=[784, 10]), name='weight')
        bias = tf.Variable(initial_value=tf.random_normal(shape=[10]), name='bias')
        y_predict = tf.matmul(x, weight) + bias
    # 3.构建损失函数 softmax多分类 交叉商损失
    with tf.variable_scope('loss_function'):
        error = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
    # 4.优化损失
    with tf.variable_scope('optimizer'):
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(error)
    tf.summary.scalar('error', error)
    tf.summary.histogram('weight', weight)
    tf.summary.histogram('bias', bias)
    merged = tf.summary.merge_all()
    saver = tf.train.Saver()
    # 5.训练
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        print('训练前损失:%f'%(sess.run(error, feed_dict= {x:images, y_true: labels})))
        # 变量显示1: 创建事件文件
        file_writer = tf.summary.FileWriter('D:/dat/events', graph=sess.graph)
        for i in range(200):
            op, loss = sess.run([optimizer, error], feed_dict={x:images, y_true: labels})
            print('第%d次训练模型的损失: %f'%((i+1), loss))
            summary = sess.run(merged, feed_dict={x: images, y_true: labels})
            file_writer.add_summary(summary, i)
            file_writer.close()
            if i%10 == 0:
                saver.save(sess, './ckpt/mnist.ckpt')


# tendorboard --logdir="d:/dat/events"
if __name__ == '__main__':
    mnist_demo()
读取模型
# 手写数字识别
# 保存模型
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf


def mnist_demo():
    mnist = input_data.read_data_sets('D:/dat/ai/mnist_data', one_hot=True)
    images, labels = mnist.train.next_batch(100)
    print('images.shape: ', images.shape, ' labels.shape: ', labels.shape)
    # 1.准备数据
    with tf.variable_scope('prepare_data'):
        x = tf.placeholder(dtype=tf.float32, shape=[None, 784], name='feature')
        y_true = tf.placeholder(dtype=tf.float32, shape=[None, 10], name='y_true')
    # 2.构建模型 y(None, 10) = x(None, 10) * weight(784, 10) + bias
    with tf.variable_scope('create_mode'):
        weight = tf.Variable(initial_value=tf.random_normal(shape=[784, 10]), name='weight')
        bias = tf.Variable(initial_value=tf.random_normal(shape=[10]), name='bias')
        y_predict = tf.matmul(x, weight) + bias
    # 3.构建损失函数 softmax多分类 交叉商损失
    with tf.variable_scope('loss_function'):
        error = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
    # 4.优化损失
    with tf.variable_scope('optimizer'):
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(error)
    tf.summary.scalar('error', error)
    tf.summary.histogram('weight', weight)
    tf.summary.histogram('bias', bias)
    merged = tf.summary.merge_all()
    saver = tf.train.Saver()
    # 5.训练
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        print('训练前损失:%f'%(sess.run(error, feed_dict= {x:images, y_true: labels})))
        # 变量显示1: 创建事件文件
        file_writer = tf.summary.FileWriter('D:/dat/events', graph=sess.graph)
        ckpt = tf.train.get_checkpoint_state('./ckpt')
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, './ckpt/mnist.ckpt')
        print('训练后损失:%f'%(sess.run(error, feed_dict= {x:images, y_true: labels})))


# tendorboard --logdir="d:/dat/events"
if __name__ == '__main__':
    mnist_demo()

相关文章

网友评论

      本文标题:python进阶-15-神经网络

      本文链接:https://www.haomeiwen.com/subject/lgsayktx.html