使用简单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. FineReport单元格扩展与父子格设置

    1.描述 在讲述报表设计之前,首先介绍一下FineReport报表制作的几个基本概念,本章节介绍FineReport报表赖以生存的单元格扩展. 扩展,顾名思义,就是由一变多,那么单元格扩展就是指在we ...

  2. nopCommerce 3.9 大波浪系列 之 IWebHelper

    接口:Nop.Core.IWebHelper 实现:Nop.Core.WebHelper 测试:Nop.Core.Tests.WebHelperTests 简介:Web辅助类 功能:获取客户端IP地址 ...

  3. xdu_1077:循环节长度

    题意很简单,就是给出p,q,求p/q的循环节长度. 由循环小数的循环部分的值等于等比数列求和的值S,列公式得到最简分数分母的值.最终得10^x%q==1(其中q为经过modify之后的值).搞清这些之 ...

  4. 一个Web 持续集成工作实践

    一个web的持续基础实践: https://mp.weixin.qq.com/src=3&timestamp=1494325174&ver=1&signature=wFVC0E ...

  5. js实现日期显示的一些操作

    1.js获取当前日期(yyyy-mm-dd) 以下代码是获取到的当前日期: var myDate = new Date(); var year = myDate.getFullYear(); //获取 ...

  6. iOS开发实战-上架AppStore 通过内购和广告获得收益

    写在前面 由于一些原因需要离职,准备重回大上海 忽然发现手头上也没什么独立App,那就随便写个放到AppStore上吧,凑个数吧.哈哈哈. 这个App是无聊找配色的时候看到的一套图 正好春节在家没什么 ...

  7. Mac下安装 MongoDB

    Mac 下安装 MongoDB 一般有两种方法,一种是通过源码安装,一种是直接使用 homebrew ,个人推荐使用 homebrew ,简单粗暴. 1.安装 homebrew : /usr/bin/ ...

  8. HTML细节

    link   设置body中超链接默认颜色 : alink  设置body中超链接点击时候的颜色: vlink  设置body中超链接访问过后的颜色 字体的大小  size       大小 取值范围 ...

  9. 朋友的发展--->对自己深深地激励。

    从4月5号来厦门开始实习到现在,也断断续续的跟着大佬开始实现需求了,就记录下自己这一段时间的想法吧,可能未来的自己看来会觉得挺可笑的,这个春招,说实话,自己挺失败的,为了求稳,来厦门这边面试美团,以至 ...

  10. 46. leetcode 500. Keyboard Row

    500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabe ...