# coding:utf-8
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = ''
from Sigmoid import sigmoid x_data = np.arange(-2*np.pi,2*np.pi,0.1).reshape(-1,1)
y_data = np.sin(x_data).reshape(-1,1)
# x_data = sigmoid(x_data)
# y_data = sigmoid(y_data)
print(x_data.shape,y_data.shape) # 建立tensorflow模型
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
# 首层
w = tf.Variable(tf.random_normal([1,10]))
b = tf.Variable(tf.zeros([1,10]))
# 中间层
w1 = tf.Variable(tf.random_normal([10,20]))
b1 = tf.Variable(tf.zeros([1,1]))
# 输出层
w2 = tf.Variable(tf.random_normal([20,1]))
b2 = tf.Variable(tf.zeros([1,1])) y_pred = tf.matmul(x,w)+b
# 激活函数
y_pred_1 = tf.nn.tanh(y_pred)
yy = tf.matmul(y_pred_1,w1)+b1
y_pred_ = tf.nn.tanh(yy)
y1 = tf.matmul(y_pred_,w2)+b2
y2 = tf.nn.tanh(y1)
#二次代价函数
loss = tf.reduce_mean(tf.square(y-y2))
# 训练方法:梯度下降法
train_model = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y2,1))#argmax返回一维张量中最大的值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
# 初始化变量
inint = tf.global_variables_initializer()
# 开始训练
with tf.Session() as sess:
sess.run(inint)
for i in range(10000):
sess.run(train_model,feed_dict={x:x_data,y:y_data})
if i%1000==0:
auc = sess.run(accuracy,feed_dict={x:x_data,y:y_data})
print('迭代次数:%d'%i,'auc:%d'%auc,' 损失函数(loss):',sess.run(loss,feed_dict={x:x_data,y:y_data}))
y_ = sess.run(y2,feed_dict={x:x_data})
sess.close() plt.figure('tensorflow',figsize=(12,6))
plt.scatter(x_data, y_data,label='sin(x)的值')
plt.plot(x_data,y_,'r',linewidth=1,label='tensorflow拟合值')
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置字体为SimHei显示中文
plt.rcParams['axes.unicode_minus'] = False # 设置正常显示符号
plt.title('tensorflow实现y=sin(x)拟合')
plt.xlabel('x-values',{'size':15})
plt.ylabel('y-values-sin(x)',{'size':15})
plt.legend(loc='upper right')
plt.show()

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

  1. matlab 曲线拟合

    曲线拟合(转载:http://blog.sina.com.cn/s/blog_8e1548b80101c9iu.html) 补:拟合多项式输出为str 1.poly2str([p],'x') 2. f ...

  2. python scipy学习-曲线拟合

    根据某地每月的平均温度[17, 19, 21, 28, 33, 38, 37, 37, 31, 23, 19, 18]拟合温度函数. import numpy as np import matplot ...

  3. excel曲线拟合怎么弄

    在做社会调研或科学实验时常常需要把得到的实验数据拟合成曲线图,这样可以使结果形象易懂.下面将介绍怎么用excel来快速地进行曲线拟合.包括添加平滑曲线,线性,指数,幂,多项式(如二次曲线,三次曲线.. ...

  4. Matlab的曲线拟合工具箱CFtool使用简介

    http://phylab.fudan.edu.cn/doku.php?id=howtos:matlab:mt1-5 一. 单一变量的曲线逼近Matlab有一个功能强大的曲线拟合工具箱 cftool ...

  5. Matlab单一变量曲线拟合-cftool

    2.启动曲线拟合工具箱>cftool 3.进入曲线拟合工具箱界面“Curve Fitting tool”(1)点击“Data”按钮,弹出“Data”窗口:(2)利用X data和Y data的下 ...

  6. Apache Commons Math3学习笔记(2) - 多项式曲线拟合(转)

    多项式曲线拟合:org.apache.commons.math3.fitting.PolynomialCurveFitter类. 用法示例代码: // ... 创建并初始化输入数据: double[] ...

  7. Matlab: 白噪声与曲线拟合

    在信号处理中常常需要用到曲线拟合,这里介绍一下利用最小二乘拟合一般曲线的方法,并对滤掉信号中白噪声的方法作些介绍. 为了测试拟合算法的好坏,先模拟出一个信号作为检验算法的例子: 用白噪声产生模拟信号: ...

  8. 关于java实现自定义曲线拟合的研究

    项目需要拟合曲线,使用java实现.采用了apache-commons-math3实现自定义的曲线. 作为apache开源的搞数学计算的超强的库,一直不受市场重视.为啥呢?经过研究,使用java这个强 ...

  9. matlab 曲线拟合小记

    在matlab中经常需要对数据进行曲线拟合,如最常见的多项式拟合,一般可以通过cftool调用曲线拟合工具(curve fit tool),通过图形界面可以很方便的进行曲线拟合,但是有些时候也会遇到不 ...

随机推荐

  1. jmeter☞文件目录(一)

    Jmeter的文件目录如下图: 1.bin:可执行文件目录 a.jmeter.bat:Windows环境下的启动文件 b.jmeter.log:日志文件 c.jmeter.sh:Linux环境下的启动 ...

  2. drf 缓存扩展

    drf缓存给了一个非常方便的扩展,使用起来相当方便 1-   安装 pip install drf-extensions 2-配置 在settings里面增加两项配置 # drf扩展REST_FRAM ...

  3. CentOS安装Harbor

    CentOS版本:7.4 Harbor版本:1.5.0 Docker版本:1.12.6 Docker Compose版本:1.21.2 一.安装Harbor(http方式,80端口) 1.安装Dock ...

  4. Python时间获取及转换知识汇总

    时间处理是我们日常开发中最最常见的需求,例如:获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个datetime的时间 ...

  5. iterator 的设计原则和traits

    iterator我前面写过是作为algorithm和container之间的一个桥梁,algorithm进程操作的时候向iterator进行提问,iterator并对提问进行了回答,其中主要就是回答5 ...

  6. tar 加密压缩和解密解压

    加密压缩 tar -czvf - file | openssl des3 -salt -k password -out /path/to/file.tar.gz 解密解压 openssl des3 - ...

  7. Java 对象及其内存控制

    作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 更新:其实这里有好多的变戏法,只要你理解了他们在JVM的中的实现机制,就豁然开朗了.有时间我会把这些变戏法的东西 ...

  8. maven release版本重复上传error

    A couple things I can think of: user credentials are wrong url to server is wrong user does not have ...

  9. JavaScript页面跳转

    <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding=& ...

  10. 【bzoj3488】[ONTAK2010]Highways DFS序+树上倍增+树状数组

    题目描述 一棵n个点的树,给定m条路径,q次询问包含一条路径的给定路径的个数+1 输入 The first line of input contains a single integer N(1< ...