首页 技术 正文
技术 2022年11月21日
0 收藏 426 点赞 3,386 浏览 1809 个字
# 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 sigmoidx_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()
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,027
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857