tensorflow 曲线拟合
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 曲线拟合的更多相关文章
- tensorflow之曲线拟合
视频链接:https://morvanzhou.github.io/tutorials/machine-learning/ML-intro/ 1.定义层 定义 add_layer() from __f ...
- 机器学习&深度学习基础(tensorflow版本实现的算法概述0)
tensorflow集成和实现了各种机器学习基础的算法,可以直接调用. 代码集:https://github.com/ageron/handson-ml 监督学习 1)决策树(Decision Tre ...
- LSTM(长短期记忆网络)及其tensorflow代码应用
本文主要包括: 一.什么是LSTM 二.LSTM的曲线拟合 三.LSTM的分类问题 四.为什么LSTM有助于消除梯度消失 一.什么是LSTM Long Short Term 网络即为LSTM,是一种 ...
- tensorflow之分类学习
写在前面的话 MNIST教程是tensorflow中文社区的第一课,例程即训练一个 手写数字识别 模型:http://www.tensorfly.cn/tfdoc/tutorials/mnist_be ...
- Tensorflow 官方版教程中文版
2015年11月9日,Google发布人工智能系统TensorFlow并宣布开源,同日,极客学院组织在线TensorFlow中文文档翻译.一个月后,30章文档全部翻译校对完成,上线并提供电子书下载,该 ...
- tensorflow学习笔记二:入门基础
TensorFlow用张量这种数据结构来表示所有的数据.用一阶张量来表示向量,如:v = [1.2, 2.3, 3.5] ,如二阶张量表示矩阵,如:m = [[1, 2, 3], [4, 5, 6], ...
- 用Tensorflow让神经网络自动创造音乐
#————————————————————————本文禁止转载,禁止用于各类讲座及ppt中,违者必究————————————————————————# 前几天看到一个有意思的分享,大意是讲如何用Ten ...
- tensorflow 一些好的blog链接和tensorflow gpu版本安装
pading :SAME,VALID 区别 http://blog.csdn.net/mao_xiao_feng/article/details/53444333 tensorflow实现的各种算法 ...
- tensorflow中的基本概念
本文是在阅读官方文档后的一些个人理解. 官方文档地址:https://www.tensorflow.org/versions/r0.12/get_started/basic_usage.html#ba ...
随机推荐
- JSP中文乱码问题终极解决方案
在介绍方法之前我们首先应该清楚具体的问题有哪些,笔者在本博客当中论述的JSP中文乱码问题有如下几个方面:页面乱码.参数乱码.表单乱码.源文件乱码.下面来逐一解决其中的乱码问题. 一.JSP页面中文乱码 ...
- spring-bean 版本的问题(报错:org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 75;)
当XML中配置的xsd是4.0,而引用的包是4以下的spring-bean.jar时,当服务器能连网时没问题,不能连网时,就报以下类似错误: org.xml.sax.SAXParseException ...
- windows环境下lib和dll的区别和联系详细
https://blog.csdn.net/ghevinn/article/details/43759655 c++中共有两种库:1.LIB包含了函数所在的DLL文件和文件中函数位置的信息(入口),代 ...
- PyQt5--EventHandler
# -*- coding:utf-8 -*- ''' Created on Sep 14, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...
- Python在Win10系统的安装和使用配置
Python是一种计算机程序设计语言.你可能已经听说过很多种流行的编程语言,比如非常难学的C语言,非常流行的Java语言,适合初学者的Basic语言,适合网页编程的JavaScript语言等等. 下载 ...
- SGU刷题之路开启
VJ小组:SGU---48h/题 每道做出的题目写成题解,将传送门更新在这里. SGU:101 - 200 SGU - 107 水题 解题报告 SGU - 105 找规律水题 解题报告 SGU - 1 ...
- Flume学习之路 (一)Flume的基础介绍
一.背景 Hadoop业务的整体开发流程: 从Hadoop的业务开发流程图中可以看出,在大数据的业务处理过程中,对于数据的采集是十分重要的一步,也是不可避免的一步. 许多公司的平台每天会产生大量的日志 ...
- 20145236《网络对抗》Exp1 逆向及Bof基础
20145236<网络对抗>Exp 1逆向及Bof基础 一.实践目标 运行原本不可访问的代码片段 强行修改程序执行流 以及注入运行任意代码. 二.基础知识及实践准备 理解EIP寄存器及其功 ...
- 【转载】python中利用smtplib发送邮件的3中方式 普通/ssl/tls
#!/usr/bin/python # coding:utf- import smtplib from email.MIMEText import MIMEText from email.Utils ...
- Python2.7-collections
collections 模块主要提供了五种特殊类型容器,此外还提供了许多抽象基类用于检查类的接口 1.Counter 对象,主要用于统计出现次数,是dict的一个子类,用法与形式和 dict 很类似 ...