欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TensorFlow 0.12.0 全连接层

程序员文章站 2022-05-27 09:44:51
...

TensorFlow 0.12.0 全连接层 (Batch * Width)

源代码

import tensorflow as tf
import numpy as np
import random

# 超参数
BATCH_SIZE = 5  # 批大小
MAX_EPOCH = 101  # 循环终止轮数


# 随机种子
def set_seed(manual_seed):
    random.seed(manual_seed)
    np.random.seed(manual_seed)
    tf.set_random_seed(manual_seed)


set_seed(0)


# 定义全连接网络层
def fc_layer(inputs, in_size, out_size, activation_function=None):
    """
    :param inputs: 1 x m
    :param in_size: m
    :param out_size: n
    :param activation_function:
    :return: 1 x n
    """
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases  # 加权求和层

    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)     # **函数层
    return outputs


if __name__ == '__main__':
    # 数据集读取(创建)
    x_data = np.linspace(-1, 1, BATCH_SIZE)[:, np.newaxis]      # data  shape=(B, W), B=BATCH_SIZE, W=1
    # print('x_data is:\n', x_data)
    # print('x_data shape:', x_data.shape)
    noise = np.random.normal(0, 0.05, x_data.shape)
    # print('n_data is:\n', noise)
    # print('n_data shape:', noise.shape)
    y_data = np.square(x_data) - 0.5 + noise                    # label
    # print('y_data is:\n', y_data)
    # print('y_data shape:', y_data.shape)

    # 模型架构,前向传播计算图
    input_x = tf.placeholder(dtype=tf.float32, shape=[None, 1])  # Batch * Width
    hidden_v = fc_layer(inputs=input_x, in_size=1, out_size=10, activation_function=tf.nn.relu)
    output_y = fc_layer(hidden_v, in_size=10, out_size=1, activation_function=None)

    # 权值初始化策略
    init = tf.global_variables_initializer()

    # 优化器,学习率设定
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)

    # 目标函数,代价,损失,误差
    label_y = tf.placeholder(dtype=tf.float32, shape=[None, 1])  # Batch * Width
    # loss = tf.reduce_mean(tf.reduce_sum(tf.square(label_y - output_y), reduction_indices=[1]))
    loss = tf.reduce_mean(tf.square(label_y - output_y), reduction_indices=[0, 1])

    # 反向传播,梯度下降,权值更新策略
    update = optimizer.minimize(loss)

    # 建立会话
    sess = tf.Session()
    # 权值初始化
    sess.run(init)
    # 迭代训练
    for i in range(MAX_EPOCH):
        sess.run(update, feed_dict={input_x: x_data, label_y: y_data})
        # 打印过程信息
        if i % (MAX_EPOCH // 10) == 0:
            print('epoch =', i, ', loss =', sess.run(loss, feed_dict={input_x: x_data, label_y: y_data}))
        if i == MAX_EPOCH - 1:
            print('#######################################################################')
            print('########## epoch == {}: ##########'.format(i))
            print('output:', sess.run(output_y, feed_dict={input_x: x_data, label_y: y_data}))
            print('#######################################################################')
            print('error:', sess.run(label_y - output_y, feed_dict={input_x: x_data, label_y: y_data}))
            print('#######################################################################')
            print('square:', sess.run(tf.square(label_y - output_y), feed_dict={input_x: x_data, label_y: y_data}))
            print('#######################################################################')
            print('reduce_sum_axis1:', sess.run(tf.reduce_sum(tf.square(label_y - output_y), reduction_indices=[1]),
                                                feed_dict={input_x: x_data, label_y: y_data}))
            print('#######################################################################')
            print('reduce_mean:', sess.run(tf.reduce_mean(tf.reduce_sum(tf.square(label_y - output_y), reduction_indices=[1])),
                                           feed_dict={input_x: x_data, label_y: y_data}))
            print('#######################################################################')

运行结果

D:\Anaconda3\envs\py35tf012\python.exe E:/PySpace/TF0Study/全连接层BxW.py
epoch = 0 , loss = 0.661514
epoch = 10 , loss = 0.0903889
epoch = 20 , loss = 0.0460918
epoch = 30 , loss = 0.0280291
epoch = 40 , loss = 0.0194787
epoch = 50 , loss = 0.0149407
epoch = 60 , loss = 0.0121535
epoch = 70 , loss = 0.0101927
epoch = 80 , loss = 0.00867454
epoch = 90 , loss = 0.00743103
epoch = 100 , loss = 0.0063873
#######################################################################
########## epoch == 100: ##########
output: [[ 0.50809157]
 [-0.09274025]
 [-0.51904196]
 [-0.09954993]
 [ 0.5692001 ]]
#######################################################################
error: [[ 0.08011103]
 [-0.13725188]
 [ 0.06797886]
 [-0.0384054 ]
 [ 0.02417779]]
#######################################################################
square: [[ 0.00641778]
 [ 0.01883808]
 [ 0.00462113]
 [ 0.00147498]
 [ 0.00058457]]
#######################################################################
reduce_sum_axis1: [ 0.00641778  0.01883808  0.00462113  0.00147498  0.00058457]
#######################################################################
reduce_mean: 0.0063873
#######################################################################

Process finished with exit code 0

运行环境:
Python=3.5
TensorFlow=0.12.0

深度学习训练算法五大步骤

1、数据
2、模型
3、优化器
4、目标函数
5、迭代训练

基于静态图的TensorFlow的会话机制( tf.Session() )

“会话是TensorFlow的主要交互方式。一般而言,TensorFlow处理数据的流程是:建立会话、生成一张空图、添加各个结点和边,形成一个有连接点的图,然后启动图,进行系统的执行。”
——摘自:
TensorFlow 深度学习应用实践 / 王晓华著. — 北京:清华大学出版社,2018
ISBN 978-7-302-48795-1
(90页)

建立并使用tf.Session的方法:

# tf 0.12.0

import tensorflow as tf

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

product = tf.matmul(matrix1, matrix2)

# ####################### method 1
flag = 0
# flag = 1
if flag:
    sess = tf.Session()
    result = sess.run(product)
    print('method 1:', result)
    sess.close()

# ####################### method 2
# flag = 0
flag = 1
if flag:
    with tf.Session() as sess:
        result = sess.run(product)
        print('method 2:', result)

Many thanks to Teacher MorvanZhou and TingsongYu!