一、张量 计算图 会话
1、张量分类
- 0阶:标量
- 1阶:向量
- n阶:张量
2、计算图:搭建神经网络的计算过程,只搭建网络,不运算。
3、会话:进行运算过程
import tensorflow as tf
x = tf.constant([[1.0,2.0]])
w = tf.constant([[3.0],[4.0]])
y = tf.matmul(x,w)
print y
with tf.Session() as sess
print sess.run(y)
二、两层全连接简单神经网络的实现——喂入单组数据
#coding: utf-8
#两层简单神经网络(全连接)
import tensorflow as tf
#定义输入和参数
x = tf.constant([[0.7,0.5]])
w1 = tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))
w2 = tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))
#定义前向传播过程(由一层计算层构成)计算图
a = tf.matmul(x,w1)
y = tf.matmul(a,w2)
#利用会话进行计算
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
print"y in tf3_3.py is :\n",sess.run(y)
三、两层全连接简单神经网络的实现——喂入多组数据
#coding:utf-8
#两层的简单神经网络(全连接)
import tensorflow as tf
#定义输入和参数
#用placeholder定义输入(sess.run喂多组数据)
x = tf.placeholder(tf.float32,shape=(None,2))
w1 = tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))
w2 = tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))
#定义向前传播过程
a = tf.matmul(x,w1)
y = tf.matmul(a,w2)
#利用会话来计算结果
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
print"y in tf3_5.py is: \n",sess.run(y,feed_dict={x:[[0.7,0.5],[0.2,0.3],[0.3,0.4],[0.4,0.5]]})
print"w1: ",sess.run(w1)
print"w2: ",sess.run(w2)
四、语法总结
1、tf.random_normal([2,3],stddev=2,mean=0,seed=1)
参数分别代表:产生2×3矩阵、标准差为2、均值为0、随机种子(去掉随机种子将会得到不同的随机数)
2、tf.truncated_normal() 去掉过大偏离点的正态分布
3、tf.random_uniform() 平均分布
4、tf.zeros([3,2],int32)生成[[0,0],[0,0],[0,0]]的全0数组
5、tf.ones ([3,2],int32)生成[[1,1],[1,1],[1,1]]的全1数组
6、tf.fill([3,2],6)生成[[6,6],[6,6],[6,6]]的全定值数组
7、tf.constant([3,2,1])生成直接定值数组
五、向前向后传播 + 梯度下降优化算法
#coding:utf-8
import tensorflow as tf
import numpy as np
# BATCH_SIZE表示一次喂入神经网络多少组数据
BATCH_SIZE = 8
# 保证随机生成的结果相同
seed = 23455
# 基于seed产生随机数
rng = np.random.RandomState(seed)
# 随机数返回32行2列的矩阵,表示32组体积和重量,作为输入数据集
X = rng.rand(32,2)
# 从X这个32行2列的矩阵中,抽出一行,如果和小于1,则Y赋值1,否则赋值0
Y = [[int(x0+x1<1)] for (x0,x1) in X]
print"打印数据集X和Y"
print"X:\n",X
print"Y:\n",Y
print"\n"
# 定义输入、输出、参数、计算图
x = tf.placeholder(tf.float32,shape=(None,2))
y_ = tf.placeholder(tf.float32,shape=(None,1))
w1 = tf.Variable(tf.random_normal([2,3],stddev=1,seed=1))
w2 = tf.Variable(tf.random_normal([3,1],stddev=1,seed=1))
a = tf.matmul(x,w1)
y = tf.matmul(a,w2)
# 定义损失函数
# 损失函数:均方误差,优化方法:梯度下降学习过程
loss = tf.reduce_mean(tf.square(y-y_))
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
#Optimizer = tf.trian.GradientDescentOptimizer(0.001)
#train_step = Optimizer.minimize(loss)
# 选用优化方法1:
#train_step = tf.train.MomentumOptimizer(0.001,0.9).minimize(loss)
# 备选优化方法2:
#train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
# 生成会话 训练STEPS轮
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
# 输出目前的(未经过训练的)参数:
print"w1:\n",sess.run(w1)
print"w2:\n",sess.run(w2)
print"原始参数输出完毕\n"
# 训练模型过程如下:
STEPS = 3000
for i in range(STEPS):
start = (i*BATCH_SIZE) % 32
end = start + BATCH_SIZE
sess.run(train_step,feed_dict={x:X[start:end],y_:Y[start:end]})
if i % 500 == 0:
total_loss = sess.run(loss,feed_dict={x:X,y_:Y})
print"After %d training step(s),loss on all data is %g"%(i,total_loss)
#输出训练后的参数
print"\n"
print"w1:\n",sess.run(w1)
print"w2:\n",sess.run(w2)
六、搭建神经网络的八股
0、准备
1、import (tensorflow、numpy等)
2、常量定义
3、随机数生成
1、前向传播过程
1、输入:x,y_
2、参数:w1,w2
3、输出:a,y
4、计算图搭建
2、反响传播
1、损失函数loss
2、优化方法train_step
3、生成会话,训练STEPS轮进行迭代
1、with tf.Session() as sess:
2、定义STEPS
3、重复训练、循环迭代
4、用print提示一定轮数后的损失函数,方便观察loss下降
×、还是要继续练,写了六遍仍然有错,菜及 x_x