搭建神经网络

摘自这里

1.搭建神经网络基本流程


定义添加神经层的函数
1.训练的数据
2.定义节点准备接收数据
3.定义神经层:隐藏层和预测层
4.定义loss表达式
5.选择optimizer 使loss达到最小
然后对所有变量进行初始化,通过sess.run optimizer,迭代1000次进行学习:

import tensorflow as tf
import numpy as np

#添加层
def add_layer(inputs,in_size,out_size,activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size,out_size]))# 权重
    biases = tf.Variable(tf.zeros([1,out_size]) + 0.1)  #偏置
    Wx_plus_b = tf.matual(inputs ,Weights) + biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

#1.训练的数据
x_data = np.linspace(-1,1,300)[:,np.newaxis]
noise = np.random.normal(0,0.5,x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

#2.定义节点准备接收数据
xs = tf.placeholder(tf.float32,[None,1])
ys = tf.placeholder(tf.float32,[None,1])

#3.定义神经层:隐藏层和预测层
l1 = add_layer(xs,1,10,activation_fiunction = tf.nn.relu)   #####输入1,输出10????
prediction = add_layer(l1,10,1,activation_function=None)

#4.定义loss表达式
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))

#5.选择optimize使loss达到最小
train_step = tf.train.GradientDesentOptimizer(0,1).minimize(loss)

#important step 对所有变量进行初始化
init = tf.initialize_all_variables()
sess = tf.Session()
#上面的定义都没有运算,直到sess.run 才会开始运算
sess.run(init)

#迭代1000次学习,sess.run optimizer
for i in range(1000):
    if i % 50 == 0:
        sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
        print(ses.run(loss,feed_dict = {xs:x_data,ys:y_data}))

2.主要步骤的解释


import tensorflow as tf
import numpy as np

#导入或者随机定义训练的数据x 和 y:
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3

#先定义出参数Weights,biases,拟合公式y,误差公式loss:
Weights = tf.Variable(tf.random_uniform[1],-1.0,1.0)
biases = tf.Variable(tf.zeros([1]))
y = Weights * x_data + biases
loss = tf.reduce_mean(tf.square(y - y_data))

#选择GradientDescent 这个最基本的Optimizer:
optimizer = tf.train.GradientDescentOptimizer(0.5)

#神经网络的key idea,就是让loss达到最小:
train = optimizer.minimize(loss)

#前面是定义,在运行模型前先要初始化所有变量:
init = tf.initialize_all_variables()

#激活init
sess = tf.Session()
sess.run(init)

#训练201步
for step in range(201):
    sess.run(train)
    if step % 20 == 0:# 每20步打印一下结果
        print(step,sess.run(Weights),sess.run(biases))

3.可视化Tensorboard


tensorboard :显示我们所建造的神经网络流程图
就是用 with tf.name_scope 定义各个框架

没能运行成功,不知道是否和显卡有关

import tensorflow as tf

def add_layer(inputs,in_size,out_size,activation_function=None):
    with tf.name_scope('layer'):  #大框架,定义层layer
        with tf.name_scope('weights'):#小部件
            Weights = tf.Variable(tf.random_normal([in_size,out_size]),name='W')
        with tf.name_scope('biases'):
            biases = tf.Variable(tf.zeros([1,out_size]) + 0.1,name='b')
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b = tf.add(tf.matmul(inputs,Weights),biases)
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b)
        return outputs
with tf.name_scope('inputs'):
    xs = tf.placeholder(tf.float32, [None, 1],name = 'x_input')
    ys = tf.placeholder(tf.float32, [None, 1],name = 'y_input')

l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
prediction = add_layer(l1,10,1,activation_function=None)

with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))

with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
sess = tf.Session()

4.保存文件和数据


准备下次训练再使用

import tensorflow as tf
import numpy as np
#  保  存  数  据
W = tf.Variable(np.arange(6).reshape((2,3)),dtype=tf.float32,name="weights")
b = tf.Variable(np.arange(3).reshape((1,3)),dtype=tf.float32,name="biases")

saver = tf.train.Saver()
with tf.Session() as sess:
    saver.restore(sess,"my_net/save_net.ckpt")
    print("weights",sess.run(W))
    print("biases", sess.run(b))
import tensorflow as tf
import numpy as np
#  保  存  文  件
#--------------------------------------------------------------------------------------
W = tf.Variable([[1,2,3],[3,4,5]],dtype = tf.float32,name='weights')
b = tf.Variable([[1,2,3]],dtype=tf.float32,name='biases')

init = tf.initialize_all_variables()

saver = tf.train.Saver()

with tf.Session() as sess:
    sess.run(init)
    save_path = saver.save(sess,"my_net/save_net.ckpt")
    print("save to path:",save_path)

猜你喜欢

转载自blog.csdn.net/acbattle/article/details/80204696