A, session tensor FIG.
1, tensor classification
- 0-order: scalar
- 1 Order: Vector
- n order: tensor
2, FIG calculation: the calculation process neural network structures, network structures only, non-operation.
3, the session: calculates Process
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)
Achieve two, simple two-layer fully connected neural network - feeding a single set of data
#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)
Third, the simple two-layer fully connected neural network - feeding a plurality of sets of data
#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)
Fourth, the syntax summary
1, tf.random_normal ([2,3], stddev = 2, mean = 0, seed = 1)
parameters represent: generating a 2 × 3 matrix, a standard deviation of 2, mean of 0 and a random seed (remove the random seed will get a different random number)
2, tf.truncated_normal () remove excessive deviation from the normal distribution point
3, tf.random_uniform () evenly distributed
4, tf.zeros ([3,2], int32) to generate [[0 , 0], [0,0], [0,0]] 0 full array
5, tf.ones ([3,2], int32) to generate [[1,1], [1,1], [1 , 1]] a full array
6, tf.fill ([3,2], 6) to generate [[6,6], [6,6], [6,6]] of the whole array of values given
7, tf .constant ([3,2,1]) are generated directly setting the array
Five forward backward propagation gradient descent optimization algorithm +
#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)
Sixth, build stereotyped neural network
0, preparation
1, import (tensorflow, numpy etc.)
2, constant definition
3, the random number generator
1, the forward propagation
1, Input: X, Y_
2, parameters: W1, w2 of
3, output: A, Y
. 4, FIG calculated structures
2, the propagation reaction
1, loss loss function
2, optimization train_step
. 3, generates a session, training iteration STEPS wheel
1, with tf.Session () AS Sess:
2, defined STEPS
. 3, repetitive training, loop iteration
4 , with the print prompt a loss of function after a certain number of rounds, to facilitate the observation loss decreased
×, or to continue to practice, written six times still wrong, vegetables and x_x