使用简单BP神经网络拟合二次函数

当拥有两层神经元时候,拟合程度明显比一层好

并出现如下警告:

C:\Program Files\Python36\lib\site-packages\matplotlib\backend_bases.py:2453: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented warnings.warn(str, mplDeprecation)

偶尔画出直线,不知为何

当Learning Rate越高,或者层数越多

画出直线或者罢工的几率就越大,愿评论区能出现解答

罢工的原因: NaN

直线:未知?,NN学错东西了

Code(nn with 2 hidden layer)

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np # refenrence: http://blog.csdn.net/jacke121/article/details/74938031 ''' numpy.linspace test
import numpy as np
# list_random1 = np.linspace(-1, 1, 300)
# list_random2 = np.linspace(-1, 1, 300)[:, np.newaxis]
# print(list_random1)
# print(list_random1)
# print(np.shape(list_random1), np.shape(list_random2))
# (300) (300, 1) x_data = np.linspace(-1,1,10)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise
x_data2 = tf.random_uniform([10, 1], -1, 1)
y_data2 = tf.square(x_data2) - tf.random_normal(x_data2.shape, 2)
print(x_data, '\n\n', x_data.shape, '\n\n') # (10, 1)
print(y_data, '\n\n', y_data.shape, '\n\n')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(x_data2))
print(x_data2) # Tensor("random_uniform:0", shape=(10, 1), dtype=float32)
print(sess.run(y_data2))
print(y_data2)
''' # add layer function
def add_layer(inputs, input_size, output_size, activation_function = None):
# add one more layer and return the output of this layer
Weights = tf.Variable(tf.random_normal([input_size, output_size]))
biases = tf.Variable(tf.zeros([1, output_size]) + 0.1)
outputs = tf.matmul(inputs, Weights) + biases
if activation_function is not None:
outputs = activation_function(outputs)
return outputs # creat data
# x_data: random[-1, 1) shape=[100,1] uniform distribute
# y_data: normal distribute(2) of random numbers, shape = [100, 1]
x_data = np.linspace(-1,1,100)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise # define placehold for inputs of nn
x = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, 1]) # hidden layer
layer1 = add_layer(x, 1, 10, activation_function=tf.nn.relu)
# hidden layer2
layer2 = add_layer(layer1, 10, 10, activation_function=tf.nn.relu)
# output layer
output_layer = add_layer(layer2, 10, 1, activation_function=None) # define loss for nn
loss = tf.reduce_mean(tf.reduce_sum(tf.square(y - output_layer),
reduction_indices=[1]))
train = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # visualize the result
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data, y_data)
plt.ion()
plt.show() with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for i in range(1, 500):
sess.run(train, feed_dict={x: x_data, y: y_data})
# visualize the result
if i%20 == 0:
try:
ax.lines.remove(lines[0])
except Exception:
pass
output = sess.run(output_layer, feed_dict={x: x_data})
lines = ax.plot(x_data, output, 'r-', lw=5)
plt.pause(0.1) plt.pause(100)

使用TenforFlow 搭建BP神经网络拟合二次函数的更多相关文章

  1. BP神经网络拟合给定函数

    近期在准备美赛,因为比赛需要故重新安装了matlab,在里面想尝试一下神将网络工具箱.就找了一个看起来还挺赏心悦目的函数例子练练手: y=1+sin(1+pi*x/4) 针对这个函数,我们首先画出其在 ...

  2. 『TensorFlow』第二弹_线性拟合&神经网络拟合_恰是故人归

    Step1: 目标: 使用线性模拟器模拟指定的直线:y = 0.1*x + 0.3 代码: import tensorflow as tf import numpy as np import matp ...

  3. NO.2:自学tensorflow之路------BP神经网络编程

    引言 在上一篇博客中,介绍了各种Python的第三方库的安装,本周将要使用Tensorflow完成第一个神经网络,BP神经网络的编写.由于之前已经介绍过了BP神经网络的内部结构,本文将直接介绍Tens ...

  4. MATLAB神经网络(2) BP神经网络的非线性系统建模——非线性函数拟合

    2.1 案例背景 在工程应用中经常会遇到一些复杂的非线性系统,这些系统状态方程复杂,难以用数学方法准确建模.在这种情况下,可以建立BP神经网络表达这些非线性系统.该方法把未知系统看成是一个黑箱,首先用 ...

  5. C# + Matlab 实现计件工时基于三层BP神经网络的拟合--真实项目

    工序工时由该工序的工艺参数决定,有了工时后乘以固定因子就是计件工资.一般参考本地小时工资以及同类小时工资并考虑作业的风险等因素给出固定因子 采用的VS2010 , Matlab2015a 64,  开 ...

  6. BP神经网络在python下的自主搭建梳理

    本实验使用mnist数据集完成手写数字识别的测试.识别正确率认为是95% 完整代码如下: #!/usr/bin/env python # coding: utf-8 # In[1]: import n ...

  7. MATLAB神经网络(3) 遗传算法优化BP神经网络——非线性函数拟合

    3.1 案例背景 遗传算法(Genetic Algorithms)是一种模拟自然界遗传机制和生物进化论而形成的一种并行随机搜索最优化方法. 其基本要素包括:染色体编码方法.适应度函数.遗传操作和运行参 ...

  8. tensorflow神经网络拟合非线性函数与操作指南

    本实验通过建立一个含有两个隐含层的BP神经网络,拟合具有二次函数非线性关系的方程,并通过可视化展现学习到的拟合曲线,同时随机给定输入值,输出预测值,最后给出一些关键的提示. 源代码如下: # -*- ...

  9. 机器学习(一):梯度下降、神经网络、BP神经网络

    这几天围绕论文A Neural Probability Language Model 看了一些周边资料,如神经网络.梯度下降算法,然后顺便又延伸温习了一下线性代数.概率论以及求导.总的来说,学到不少知 ...

随机推荐

  1. js里面的垃圾回收

    在公司经常会听到大牛们讨论时说道内存泄露神马的,每每都惊羡不已,最近精力主要用在了Web 开发上,读了一下<JavaScript高级程序设计>(书名很唬人,实际作者写的特别好,由浅入深)了 ...

  2. 细说Handler

    今天来说说Android一个重要类吧:Handler (我写的博客风格不适合新手,因为我讨厌新手教学,我都是直奔主题,不交代前因后果) 大家都知道Handler的用法一般是线程间的通讯,当然,一个线程 ...

  3. Spring和SpringMVC父子的容器之道---[上篇]

    Spring和SpringMVC作为Bean管理容器和MVC层的默认框架,已被众多WEB应用采用,而在实际开发中,由于有了强大的注解功能,很多基于XML的配置方式已经被替代,但在实际项目中,我们经常会 ...

  4. python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。

    本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...

  5. 【NOI复习】树链剖分

    简介 树链剖分通常用来解决一类维护静态树上路径信息的问题, 例如:给定一棵点带权树, 接下来每次操作会修改某条路径上所有点的权值(修改为同一个值或是同加上一个值等) , 以及询问某条路径上所有点的权值 ...

  6. 【html】 a 标签

    摘要 嗷呜,发现好多前端细节,基础不扎实啊,喵了个咪 target 属性 在制定框架中打开 <a href="a.html" target="view_frame& ...

  7. 【css】盒子模型 之 概述

    摘要 一些基本的概念以及常见使用问题 概述 BFC 是css 中布局的核心 - 盒模型,根据块级元素及行级元素可分为块级容器, 行级容器,但容器内部都遵循 BFC BFC 空间布局 备注: IE 的盒 ...

  8. 【mysql】常用操作

    2.mysql service mysql status mysql --version mysql -h 服务器主机地址 -u 用户名 -p 用户密码 exit  退出 mysql -h 主机名 - ...

  9. 权限管理学习 一、ASP.NET Forms身份认证

    说明:本文示例使用的VS2017和MVC5. 系统无论大小.牛逼或屌丝,一般都离不开注册.登录.那么接下来我们就来分析下用户身份认证. 简单实现登录.注销 以前在学习.net的时候不知道什么Forms ...

  10. maven相关的学习资料

    1, maven的settings配置文件详解: http://blog.csdn.net/jinshuaiwang/article/details/23686099 2,maven原理---翡青的博 ...