目录
1. 计算图
tensorflow使用计算图的模型来进行计算。tensorflow程序一般可以分为两个阶段,第一个是定义计算图中所有的计算,第二个是执行计算。
通过python上下文管理器,可以在管理器推出时自动释放资源
import tensorflow as tf
a=tf.constant([1.0,2.0],name='a')
b=tf.constant([1.0,2.5],name='b')
result=a+b
with tf.Session() as sess:
sess.run(result)
2. 神经网络实现
a. 提取特征向量作为输入 b. 定义神经网络结构 c. 通过训练数据来调整参数 d. 使用训练好的神经网络预测未知的数据
训练神经网络的过程:
a. 定义神经网络的结构和前向传播的输出结果
b. 定义损失函数以及选择反向传播优化的算法
c. 生成会话(tf.Session)并且在训练数据上反复运行反向传播优化算法
2.1 前向传播算法
最简单的全连接神经网络:相邻两层之间任意两个节点之间都有连接。以下是一个简单的前向网络的实现过程。
2.2 参数表示
import tensorflow as tf
w1=tf.Variable(tf.random_normal([2,3],stddev=1,seed=1)) #stddev其实是standar deviation
w2=tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))
x=tf.constant([[0.7,0.9]])
a=tf.matmul(x,w1)
y=tf.matmul(a,w2)
sess=tf.Session()
init_op=tf.initialize_all_variables() #对所有变量进行初始化,因为在变量定义时知识给出了初始化的方法,没有运行
sess.run(init_op)
print(sess.run(y))
sess.close()
2.3 反向传播算法
降低图的大小方法:tensorflow中提供了placeholder机制用于提供输入数据,placeholder相当于定义了一个位置,这个位置中的数据在程序运行时再指定。但需要提供一个字典feed_dict来只当x的取值
实例1还是前向传播算法
import tensorflow as tf
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=(1,2),name="input")
a=tf.matmul(x,w1)
y=tf.matmul(a,w2)
sess=tf.Session()
init_op=tf.initialize_all_variables()
sess.run(init_op)
print(sess.run(y,feed_dict={x:[[0.7,0.9]]}))
还可以定义n*2个输入
x=tf.placeholder(tf.float32,shape=(3,2),name="input")
print(sess.run(y,feed_dict={x:[[0.7,0.9],[0.1,0.4],[0.5,0.8]]}))
定义损失函数
#定义损失函数
cross_entropy=-tf.reduce_mean(y_*tf.log(tf.clip_by_value(y,1e-10,1.0)))
learning_rate=0.001
train_step=tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)
神经网络具体实现的完整例子
# -*- coding: utf-8 -*-
# 神经网络实现
import tensorflow as tf
from numpy.random import RandomState
#定义训练数据batch的大小
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))
# 在shape的一个维度使用None可以方便使用不大的batch大小,在训练时把数据分成比较小的batch
# 但是在测试时,可以一次性使用全部的数据
x = tf.placeholder(tf.float32, shape=(None, 2), name="x-input")
y_= tf.placeholder(tf.float32, shape=(None, 1), name='y-input')
# 定义神经网络前向传播过程
a = tf.matmul(x, w1)
y = tf.matmul(a, w2)
y=tf.sigmoid(y)
learning_rate=0.001
cross_entropy = -tf.reduce_mean(y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0))
+ (1 - y_) * tf.log(tf.clip_by_value(1 - y, 1e-10, 1.0)))
train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)
# 随机生成一个模拟数据集
rdm=RandomState(1)
dataset_size=128
X=rdm.rand(dataset_size,2)
Y=[[int(x1+x2)<1] for (x1,x2) in X]
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
# 输出目前(未经训练)的参数取值。
print(sess.run(w1))
print(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, y, y_], 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(sess.run(w1))
print(sess.run(w2))