使用简单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. css动画属性--小球移动

    主体只有一个div <body> <div></div> </body> 样式部分(测试:目前的浏览器还是需要加前缀才能兼容) <style> ...

  2. nyoj_14:会场安排问题

    一道很经典的贪心问题 题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=14 #include<iostream> #inclu ...

  3. MySQL5.6.36 linux rpm包安装配置文档

    一.卸载自带mysql,删除MySQL的lib库,服务文件 [root@localhost ~]#rpm -qa|grep mysql qt-mysql-4.6.2-26.el6_4.x86_64 m ...

  4. SQL Server 数据库表的管理

    上一篇文章简单梳理了一下SQL Server数据库的安装和基本操作,这篇文章主要讲述一下数据库表的管理 一.数据库的创建 有关数据库的创建有两种方式,一种是通过视图创建,第二种就是通过T-SQL语句来 ...

  5. 【逻辑漏洞】基于BurpSuite的越权测试实战教程

    一.什么是越权漏洞?它是如何产生的? 越权漏洞是Web应用程序中一种常见的安全漏洞.它的威胁在于一个账户即可控制全站用户数据.当然这些数据仅限于存在漏洞功能对应的数据.越权漏洞的成因主要是因为开发人员 ...

  6. 【bzoj3772】精神污染

    Description 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位是森林和山地,与拥有关西机场的大阪府比邻而居,是关西地区面积最大的县,是集经济和文化于一体的一大地区, ...

  7. HDU6055 Regular polygon(计算几何)

    Description On a two-dimensional plane, give you n integer points. Your task is to figure out how ma ...

  8. 删除物品[JLOI2013]

    题目描述 箱子再分配问题需要解决如下问题:  (1)一共有N个物品,堆成M堆.  (2)所有物品都是一样的,但是它们有不同的优先级.  (3)你只能够移动某堆中位于顶端的物品.  (4)你可以把任意一 ...

  9. 超超超简单的bfs——POJ-3278

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 89836   Accepted: 28175 ...

  10. Java学生成绩

    import java.util.*; public class guanlixiton { public static void main(String[] args) { Scanner in = ...