01 运行环境

win10 python3.5.3 CUDA v8.0 cudnn-8.0-windows10-x64-v5.1
代码运行工具:
A Eclise Neon.2 Release (4.6.2) PyDev
B PyCharm Community Edition 2017.1

02 第三章 TensorFlow入门

tensorflow的计算模型:计算图–tf.Graph
tensorflow的数据模型:张量–tf.Tensor
tensorflow的运行模型:会话–tf.Session
tensorflow可视化工具:TensorBoard
通过集合管理资源:tf.add_to_collection、tf.get_collection
Tensor主要三个属性:名字(name)、维度(shape)、类型(type)
会话Session需要关闭才能释放资源,通过Python的上下文管理器 with ,可以自动释放资源
tensorflow设备:tf.device(‘/cpu:0’)、tf.device(‘/gpu:2’)

表3-1 TensorFlow维护的集合列表

集合名称 集合内容 使用场景
tf.GraphKeys.VARIABLES 所有变量 持久化TensorFlow模型
tf.GraphKeys.TRAINABLE_VARIABLES 可学习的变量 模型训练、生成模型可视化内容
tf.GraphKeys.SUMMARIES 日志生成相关张量 TensorFlow计算可视化
tf.GraphKeys.QUEUE_RUNNERS 处理输入的QueueRunner 输入处理
tf.GRaphKeys.MOVING_AVEGAGE_VARIABLES 所有计算了滑动平均值的变量 计算变量的滑动平均值

表3-2 TensorFlow随机数生成函数

函数名称 随机数分布 主要参数
tf.random_normal 正太分布 平均值、标准差、取值类型
tf.truncated_normal 正太分布,如果随机出来的值偏离均值超过2个标准差,重新随机 平均值、标准差、取值类型
tf.random_uniform 平均分布 最小、最大取值、取值类型
tf.random_gamma Gamma分布 形状参数alpha、尺度参数beta、取值类型

表3-3 TensorFlow常数生成函数

函数名称 功能 样例
tf.zeros 产生全0数组 tf.zeros([2,3],int32)–>[[0,0,0],[0,0,0]]
tf.ones 产生全1数组 tf.ones(2,3],int32)–>[[1,1,1],[1,1,1]]
tf.fill 产生一个给定值的数组 tf.fill([2,3],9)–>[[9,9,9],[9,9,9]]
tf.constant 产生一个给定值常量 tf.constant([1,2,3])–>[1,2,3]


训练神经网络步骤:
1. 定义神经网络结构和前向床波输出结果
2. 定义损失函数及反向传播优化算法
3. 生成会话(tf.Session)并在训练数据上反复运行反向传播优化算法

03 代码

# 《TensorFlow实战Google深度学习框架》03 TensorFlow入门
# win10 Tensorflow1.0.1 python3.5.3
# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1
# filename:ts03.01.py

'''
tensorflow的计算模型:计算图--tf.Graph
tensorflow的数据模型:张量--tf.Tensor
tensorflow的运行模型:会话--tf.Session
tensorflow可视化工具:TensorBoard
通过集合管理资源:tf.add_to_collection、tf.get_collection
Tensor主要三个属性:名字(name)、维度(shape)、类型(type)
会话Session需要关闭才能释放资源,通过Python的上下文管理器 with ,可以自动释放资源
'''
import tensorflow as tf;

print("tensorflow version: ", tf.VERSION);  # 1.0.1

def func00_def():
    a = tf.Variable([1.0, 2.0], name="a");  # 变量
    b = tf.Variable([2.0, 3.0], name="b");
    # result = tf.placeholder(tf.float32, shape=(1, 2), name="result");
    g = tf.Graph();  # 图
    with tf.Session() as sess:  # 会话
        with g.device('/cpu:0'):  # 设备
            init_op = tf.global_variables_initializer();
            sess.run(init_op);
            result = a + b;
            print(sess.run(result));


def func01_constant():
    w1 = tf.Variable(tf.random_normal([2, 3], stddev=1));
    w2 = tf.Variable(tf.random_normal([3, 1], stddev=1));

    x = tf.constant([[0.7, 0.9]]);
    a = tf.matmul(x, w1);
    y = tf.matmul(a, w2);

    sess = tf.Session();
    init_op = tf.global_variables_initializer();

    sess.run(init_op);

    print(sess.run(y));


def func02_placeholder():
    w1 = tf.Variable(tf.random_normal([2, 3], stddev=1));
    w2 = tf.Variable(tf.random_normal([3, 1], stddev=1));

    x = tf.placeholder(tf.float32, shape=(1, 2), name="input");
    a = tf.matmul(x, w1);
    y = tf.matmul(a, w2);

    sess = tf.Session();
    init_op = tf.global_variables_initializer();

    sess.run(init_op);

    # placeholder的赋值
    print(sess.run(y, feed_dict={x: [[0.7, 0.9]]}));


func00_def();
func01_constant();
func02_placeholder();
'''输出形式
[ 3.  5.]
[[ 0.5082888]]
[[-0.46202496]]
'''
# 《TensorFlow实战Google深度学习框架》03 TensorFlow入门
# win10 Tensorflow1.0.1 python3.5.3
# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1
# filename:ts03.02.py

import tensorflow as tf
from numpy.random import RandomState

# 1. 定义神经网络的参数,输入和输出节点
batch_size = 8
w1= tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2= tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))
x = tf.placeholder(tf.float32, shape=(None, 2), name="x-input")
y_= tf.placeholder(tf.float32, shape=(None, 1), name='y-input')

# 2. 定义前向传播过程,损失函数及反向传播算法
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)
cross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))
train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)

# 3. 生成模拟数据集
rdm = RandomState(1)
X = rdm.rand(128,2)
Y = [[int(x1+x2 < 1)] for (x1, x2) in X]

# 4. 创建一个会话来运行TensorFlow程序
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)

    # 输出目前(未经训练)的参数取值。
    print("w1:", sess.run(w1))
    print("w2:", sess.run(w2))
    print("\n")

    # 训练模型。
    STEPS = 5000
    for i in range(STEPS):
        start = (i * batch_size) % 128
        end = (i * batch_size) % 128 + batch_size
        sess.run(train_step, feed_dict={x: X[start:end], y_: Y[start:end]})
        if i % 1000 == 0:
            total_cross_entropy = sess.run(cross_entropy, feed_dict={x: X, y_: Y})
            print("After %d training step(s), cross entropy on all data is %g" % (i, total_cross_entropy))

    # 输出训练后的参数取值。
    print("\n")
    print("w1:", sess.run(w1))
    print("w2:", sess.run(w2))

''' 输出形式
w1: [[-0.81131822  1.48459876  0.06532937]
 [-2.4427042   0.0992484   0.59122431]]
w2: [[-0.81131822]
 [ 1.48459876]
 [ 0.06532937]]

After 0 training step(s), cross entropy on all data is 0.0674925
After 1000 training step(s), cross entropy on all data is 0.0163385
After 2000 training step(s), cross entropy on all data is 0.00907547
After 3000 training step(s), cross entropy on all data is 0.00714436
After 4000 training step(s), cross entropy on all data is 0.00578471

w1: [[-1.9618274   2.58235407  1.68203783]
 [-3.46817183  1.06982327  2.11789012]]
w2: [[-1.82471502]
 [ 2.68546653]
 [ 1.41819513]]
'''
Logo

脑启社区是一个专注类脑智能领域的开发者社区。欢迎加入社区,共建类脑智能生态。社区为开发者提供了丰富的开源类脑工具软件、类脑算法模型及数据集、类脑知识库、类脑技术培训课程以及类脑应用案例等资源。

更多推荐