tensorflow 曲线拟合


Python代码:

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# from tensorflow.examples.tutorials.mnist import input_data # creating data
mu,sigma=0, 0.1
data_size = 300
x_data = np.linspace(-1, 1,data_size)[:, np.newaxis]
# noise = np.random.normal(0,0.05, x_data.shape)
y_data = np.sign(x_data) # mnist data
# mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# x_data, y_data = mnist.train.next_batch(300) # input layer
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1]) # layer function
def layer(data_in, size, func = None):
w = tf.Variable(tf.random_normal(size))
b = tf.Variable(tf.zeros([1, size[1]]))
z = tf.matmul(data_in, w) + b
if(func):
data_out = func(z)
else:
data_out = z
return data_out # hidden layer
output1 = layer(xs, [1, 10], tf.nn.relu)
output2 = layer(output1, [10, 20], tf.nn.softmax)
output3 = layer(output2, [20, 20], tf.nn.relu)
output4 = layer(output3, [20, 10], tf.nn.softmax)
output5 = layer(output4, [10, 10], tf.nn.relu) # output layer
out = layer(output5, [10, 1]) # loss function
# loss = tf.reduce_sum(ys * tf.log(out))
loss = tf.reduce_mean(tf.reduce_sum(tf.square((out - ys)))) # trainning method
# train = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
train = tf.train.AdamOptimizer().minimize(loss) # init all variables
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # print loss value for every 50 times loop
print_step = 50
# loop less than 50 * 1000
loop_max_count = 1000
while True:
print_step -= 1
_,loss_value = sess.run([train,loss],feed_dict={xs:x_data,ys:y_data})
if(print_step == 0):
print(loss_value)
print_step = 50
loop_max_count -= 1
if(loss_value < .00001 or loop_max_count <= 0):
break # print loop times and show the output
print("loop_count = ", (1000 - loop_max_count) * 50)
y_out = sess.run(out, feed_dict={xs:x_data})
plt.plot(x_data, y_out, label="out")
plt.plot(x_data, y_data, label="in")
plt.show()

可以用来看看不同数目的隐含层和不同的激活函数对曲线拟合的训练性能和训练结果有何影响。

tensorflow 曲线拟合的更多相关文章

  1. tensorflow之曲线拟合

    视频链接:https://morvanzhou.github.io/tutorials/machine-learning/ML-intro/ 1.定义层 定义 add_layer() from __f ...

  2. 机器学习&深度学习基础(tensorflow版本实现的算法概述0)

    tensorflow集成和实现了各种机器学习基础的算法,可以直接调用. 代码集:https://github.com/ageron/handson-ml 监督学习 1)决策树(Decision Tre ...

  3. LSTM(长短期记忆网络)及其tensorflow代码应用

     本文主要包括: 一.什么是LSTM 二.LSTM的曲线拟合 三.LSTM的分类问题 四.为什么LSTM有助于消除梯度消失 一.什么是LSTM Long Short Term 网络即为LSTM,是一种 ...

  4. tensorflow之分类学习

    写在前面的话 MNIST教程是tensorflow中文社区的第一课,例程即训练一个 手写数字识别 模型:http://www.tensorfly.cn/tfdoc/tutorials/mnist_be ...

  5. Tensorflow 官方版教程中文版

    2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源,同日,极客学院组织在线TensorFlow中文文档翻译.一个月后,30章文档全部翻译校对完成,上线并提供电子书下载,该 ...

  6. tensorflow学习笔记二:入门基础

    TensorFlow用张量这种数据结构来表示所有的数据.用一阶张量来表示向量,如:v = [1.2, 2.3, 3.5] ,如二阶张量表示矩阵,如:m = [[1, 2, 3], [4, 5, 6], ...

  7. 用Tensorflow让神经网络自动创造音乐

    #————————————————————————本文禁止转载,禁止用于各类讲座及ppt中,违者必究————————————————————————# 前几天看到一个有意思的分享,大意是讲如何用Ten ...

  8. tensorflow 一些好的blog链接和tensorflow gpu版本安装

    pading :SAME,VALID 区别  http://blog.csdn.net/mao_xiao_feng/article/details/53444333 tensorflow实现的各种算法 ...

  9. tensorflow中的基本概念

    本文是在阅读官方文档后的一些个人理解. 官方文档地址:https://www.tensorflow.org/versions/r0.12/get_started/basic_usage.html#ba ...

随机推荐

  1. jquery.validate,错误信息位置

    好长时间没有用jquery.validate.js这个插件了,忘得差不多了.唉,好东西还是要经常拿出来看看的,今天用jquery.validate来做一个小东西,遇到一个问题,就是错误提示信息的位置问 ...

  2. 个人犯的一个golang routine错误

    这个其实不是错误,2个写法没有区别.-2015.11.22 认识golang也不少时间了,也做过几个项目.最近发现之前用golang写的一个服务,内存涨得比较快,一直没找出来原因来.今天把疑惑发到群里 ...

  3. AD用户移除所属组

    AD用户移除所属组: $Membership = Get-ADPrincipalGroupMembership $Users $Membership.distinguishedName Remove- ...

  4. Window10 Linux子系统挂载磁盘

    默认情况下, Linux子系统将当前winodws磁盘的盘全部挂载到/mnt/<disk_label>, 但一些新增的盘就需要手动做下了.. 官方参考文档 挂载磁盘 -- DrvFs 挂载 ...

  5. NIO 学习笔记

    0. 介绍 参考   关于Java IO与NIO知识都在这里   ,在其基础上进行修改与补充. 1. NIO介绍 1.1 NIO 是什么 Java NIO 是 java 1.4, 之后新出的一套IO接 ...

  6. 新人如何进入IT行业

    你遇到了我刚毕业时遇到的问题. 现在需要知道你希望在那里就业,上海和北京就业的待遇差不多,北京能比上海稍微少点(我是指你这类刚毕业的) 说主题好了 应届毕业,找工作都很难的,因为现在很多企业是不愿意找 ...

  7. 《面向对象程序设计》c++第六次作业___calculator SE

    c++第五次作业 Calculator SE 代码 PS:这次作业延迟了很久,人要是迷茫啊----唉------ 新增GUI界面,使用Qt creator编写,纯代码生成控件.写坐标. 感觉Qt cr ...

  8. Fedora29 安装 spring tool suite 4.2

    下载安装包 下载地址:https://spring.io/tools 文件:STS-4.2.0.RELEASE.tar.gz 解压部署软件包 解压文件至 /opt/STS-4.2.0.RELEASE/ ...

  9. Oracle_spatial的常见错误与注意事项

    常见的错误 1.ORA-13226:没有空间索引接口将不被支持 当使用一个空间操作符时,如果没有使用空间索引导致该操作符不能被完成将会返回该错误.这可能会发生在当你使用的列上没有空间索引.或者优化器没 ...

  10. 异步IO的概念

    同步IO是阻塞IO: 异步IO分为两种:1.主动查询是否有数据:2.被动监听是否有数据状态. https://www.cnblogs.com/euphie/p/6376508.html