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. 转:SQL中 patindex函数的用法

    语法格式:PATINDEX ( '%pattern%' , expression ) 返回pattern字符串在表达式expression里第一次出现的位置,起始值从1开始算. pattern字符串在 ...

  2. 直播内容不合规怎么办?智能AI为您解决审核难题

    背景 近些年来,视频直播快速发展,大量的直播平台如雨后春笋一般出现,但是这同样给直播内容的监管带来了巨大的挑战,一方面国家对于直播内容监管的要求日益严格,另一方面相对于文字内容的审核,多媒体内容的审核 ...

  3. extern “C”的作用详解

    extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码.加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C+ ...

  4. Nmap 使用技巧及其攻略

    Nmap是一款免费开源的网络发现和安全审计工具,支持Windows和Linux平台,有命令行版本和图形化版本.个人建议去学习 nmap 的命令行版本,因为与图形化版本 zenmap 相比,它提供了更多 ...

  5. sql行列转换PIVOT与unPIVOT

    基本语法 select * from Mould pivot ( count(ID)for ProductTypeCode in ( [FC], [RCU], [RCD] )) as PVT; wit ...

  6. 6.Solr4.10.3API使用(CURD)

    转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.在工程中引入solr-solrj-4.10.3.jar <dependency> <gr ...

  7. python第三十课--异常(raise关键字)

    演示: 1.手动抛出异常对象-->raise关键字 2.try-except代码不能解决语法错误 try: print('try...') raise TypeError('类型有误的异常') ...

  8. 基于汇编的 C/C++ 协程 - 背景知识

    近几年来,协程在 C/C++ 服务器中的解决方案开始涌现.本文主要阐述以汇编实现上下文切换的协程方案,并且说明其在异步开发模式中的应用. 本文地址:https://segmentfault.com/a ...

  9. linux,添加新硬盘的方法

    一.物理机添加一块新的硬盘方法(目的是把后加的磁盘直接加在现有的上面,不用再分区挂载)1.首先要确定现有系统在那块盘上  [root@localhost ~]# df -lhFilesystem    ...

  10. Hello Shader之Hello Trangle

    这两天配了一下现代OpenGL的开发环境,同时看了一下基础知识和编程规范 写了一个编译GLSL语言的前端程序和一个Hello trangle的程序 另外,推荐两个资源 1.学习网站Learn Open ...