简单的Tensorflow实现NN

这篇博客记录了使用Tensorflow实现神经网络的过程,通过展示训练过程中损失(loss)逐渐下降的详细数值,展现了模型的拟合效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# coding:utf-8
'''
Student: Danny Hou
Date: 2017-06-14
Content:简单的神经网络练习
'''
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt



# def add layer, it would be very convenient that you build a CNN when adding a layer

def add_layer(inputs,in_size,out_size,activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size,out_size])) # 初始化weight,为什么不用0?据说是效果不好。
    biases = tf.Variable(tf.zeros(shape=[1,out_size])+0.1) # 初始化又不为0,据说是推荐这个值!!
    Wx_plus_b = tf.matmul(inputs,Weights)+biases
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs=activation_function(Wx_plus_b)
    return outputs

# 开始捏造数据

X = np.linspace(-1,1,3000,dtype=np.float32)[:, np.newaxis] # newaxis 就是把横变成竖的吧,(3000,1)
noise = np.random.normal(0,0.05,X.shape).astype(np.float32) # 加点酌料,搞乱原本的数据,更接近现实
y = np.square(X) - 0.5 + noise # y已经被搞乱了 :-P


# 可以看下数据的情况

fig = plt.figure()
ax = fig.add_subplot(1,1,1) # 一行,一列,第一个
ax.scatter(X,y)
plt.ion() # 用于连续显示
plt.show()



# 定义占位符,我感觉是传入参数的定义,个人理解 :-P

xs = tf.placeholder(tf.float32,[None,1]) # None 代表N个,N到底是多少个?
ys = tf.placeholder(tf.float32,[None,1])



# 神经网络有多层,大致可以分为:输入层、隐藏层和输出层。
# 这里定义隐藏层,xs为接收到的输入矩阵,1就是只有一个神经元(一个特征?),10代表输出给隐藏层有10个神经元,激活函数用relu。
# xs为输入层

l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
# l2 = add_layer(l1,10,15,activation_function=tf.nn.relu)
# l3 = add_layer(l2,15,10,activation_function=tf.nn.relu)

# 定义输出层,

prediction = add_layer(l1, 10, 1, activation_function=None)


# 定义loss/cost function,由于这里的问题是回归问题,所以一般都会用MSE(应该没有写错吧)

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


# 定义训练步骤

train_step = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss=loss)


# 所有变量和步骤都定义好了,开始初始化

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)


# 定制训练次数并为模型传入对应的数据

for i in range(1000):
    sess.run(train_step,feed_dict={xs:X,ys:y})
    if i % 50 ==0:
        print(sess.run(loss,feed_dict={xs:X,ys:y}))
        try:
            ax.lines.remove(lines[0])
        except Exception:
            pass
        prediction_value = sess.run(prediction,feed_dict={xs:X})
        lines = ax.plot(X,prediction_value,'r-',lw=5)
        plt.pause(0.5)

loss
0.188886
0.0142108
0.0086079
0.00696789
0.00589828
0.00524938
0.00492084
0.00475221
0.00464395
0.00456124
0.00449256
0.00442923
0.0043689
0.00431515
0.00426623
0.00422012
0.00417505
0.00413331
0.00409412
0.00405706
拟合过程:
拟合过程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值