tensorflow 非线性回归
来源:互联网 发布:数据堂 王大亮 编辑:程序博客网 时间:2024/05/20 11:27
#encoding:utf-8#encoding:utf-8import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt# 神经层函数参数, 输入值, 输入的大小, 输出的大小,激励函数(默认为空)def add_layer(inputs, in_size, out_size, activation_function=None): # 生成初始参数时,随机变量回比全部为0要好很多,所以weights为一个in_size # out_size列的随机变量矩阵 Weights = tf.Variable(tf.random_normal([in_size, out_size])) # 在机器学习中biases的推荐值不为0,所以在0的基础上加了0.1 biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) # Wx_plus_b,即神经网络未激活的值。其中tf.matmul()是矩阵的乘法 Wx_plus_b = tf.matmul(inputs, Weights) + biases # activation_function 激励函数为None,输出就是当前的预测值Wx_plus_b # 不为空时就把Wx_plus_b传到activation_function()函数中得到 if activation_function == None: outputs = Wx_plus_b else: outputs = activation_function(Wx_plus_b) return outputs# 导入数据x_data = np.linspace(-1, 1, 300, dtype=np.float32)[:, np.newaxis]noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32)y_data = np.square(x_data) - 0.5 + noisexs = tf.placeholder(tf.float32, [None, 1])ys = tf.placeholder(tf.float32, [None, 1])# matplotlib 可视化fig = plt.figure()ax = fig.add_subplot(1, 1, 1)ax.scatter(x_data, y_data)plt.ion()#plt.ion()用于连续显示plt.show()# 搭建网络# 隐藏层 10个神经元l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu) # tf自带激励函数tf.nn.relu# 输出层prediction = add_layer(l1, 10, 1, activation_function=None)# 误差 二者差的平方和再取平均loss = tf.reduce_mean(tf.square(ys - prediction))# loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))# 梯度下降优化器 学习率为0.1 最小话损失函数train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)init = tf.global_variables_initializer()with tf.Session() as sess: sess.run(init) # print sess.run(loss1, feed_dict= {xs:x_data, ys:y_data}) # print sess.run(loss, feed_dict= {xs:x_data, ys:y_data}) for i in range(1000): # 训练模型 sess.run(train_op, feed_dict={xs: x_data, ys: y_data})# 每隔50次训练刷新一次图形,用红色、宽度为5的线来显示我们的预测数据和输入之间的关系,#并暂停0.1s。 if i % 50 == 0: try: ax.lines.remove(lines[0]) except Exception: pass prediction_value = sess.run(prediction, feed_dict={xs: x_data, ys: y_data}) lines = ax.plot(x_data, prediction_value, 'r-', lw=5) plt.pause(0.1) # 每隔50输出误差# print sess.run(loss, feed_dict= { xs :x_data, ys : y_data})
画出的图
每训练50次输出的误差
0.6859760.02003310.009531590.007653440.007193560.006942520.006724910.006480860.00622440.005982850.005725460.00548090.005247320.00500340.004777880.004578420.004401710.004240590.004098680.00397596
阅读全文
0 0
- tensorflow实现非线性回归
- Tensorflow-非线性回归
- tensorflow 非线性回归
- tensorflow 非线性回归
- 非线性回归
- 非线性回归(逻辑回归)
- Tensorflow深度学习笔记(四)-利用神经网络预测非线性回归示例
- 一元非线性回归模型
- MATLAB多元非线性回归
- 非线性回归分析
- 7.5 非线性回归算法
- 非线性回归(上)
- tensorflow之非线性方程
- 深度学习框架TensorFlow学习与应用(二)——非线性回归、MINST数据集分类
- 多元非线性回归问题算法
- SAS进行多元非线性回归
- §7 非线性回归模型
- 7.6 非线性回归算法应用
- 数据流
- 关于失业(三)
- CSS3自定义滚动条样式 -webkit-scrollbar
- NOIP 普及组 车站分级 [拓扑排序][线段树优化连边][虚点优化]
- 第七周java作业
- tensorflow 非线性回归
- c#中常用地字符串处理函数
- 坚持整洁亦或拥抱混乱——新书《混乱: 改变我们生活的无序力量》解读
- Graph Theory NO.6 HDU_1232_畅通工程_并查集
- VS中出现Stack Overflow的解决方案
- 17 tcp
- 057 不定积分之换元积分法
- Relection2017.10.12
- WinToUSB Enterprise(U盘安装系统工具)官方注册版V3.8下载 | wintousb 3.8 破解版