tensorflow02 《TensorFlow实战Google深度学习框架》笔记-03

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_gammaGamma分布形状参数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]]
'''
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值