tensorflow1.0 构建神经网络做非线性归回
"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""
#tensorboard --logdir="./"
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
with tf.name_scope("layer"):
with tf.name_scope("weights"):
Weights = tf.Variable(tf.random_normal([in_size, out_size]),name="W")
with tf.name_scope("biases"):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
with tf.name_scope("Wx_plus_b"):
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs # Make up some real data
x_data = np.linspace(-1 ,1 ,300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise # define placeholder for inputs to network
with tf.name_scope("inputs"):
xs = tf.placeholder(tf.float32, [None, 1],name="x_input")
ys = tf.placeholder(tf.float32, [None, 1],name="y_input")
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.tanh)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None) # the error between prediciton and real data
with tf.name_scope("loss"):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
with tf.name_scope("train"):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # important step
init = tf.initialize_all_variables()
sess = tf.Session()
writer = tf.summary.FileWriter("./",sess.graph)
sess.run(init) fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data,y_data)
plt.ion()
plt.show()
for i in range(1000):
# training
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
# to see the step improvement
print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
try:
ax.lines.remove(lines[0])
except Exception:
prediction_value = sess.run(prediction,feed_dict={xs: x_data, ys: y_data})
lines = ax.plot(x_data,prediction_value,"r-",lw = 5)
plt.pause(0.1)
tensorflow1.0 构建神经网络做非线性归回的更多相关文章
- tensorflow1.0 构建神经网络做图片分类
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...
- tensorflow1.0 构建lstm做图片分类
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #this is data mni ...
- tensorflow1.0 构建卷积神经网络
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import os os.envi ...
- 使用TensorFlow v2.0构建卷积神经网络
使用TensorFlow v2.0构建卷积神经网络. 这个例子使用低级方法来更好地理解构建卷积神经网络和训练过程背后的所有机制. CNN 概述 MNIST 数据集概述 此示例使用手写数字的MNIST数 ...
- 使用TensorFlow v2.0构建多层感知器
使用TensorFlow v2.0构建一个两层隐藏层完全连接的神经网络(多层感知器). 这个例子使用低级方法来更好地理解构建神经网络和训练过程背后的所有机制. 神经网络概述 MNIST 数据集概述 此 ...
- NVIDIA DeepStream 5.0构建智能视频分析应用程序
NVIDIA DeepStream 5.0构建智能视频分析应用程序 无论是要平衡产品分配和优化流量的仓库,工厂流水线检查还是医院管理,要确保员工和护理人员在照顾病人的同时使用个人保护设备(PPE),就 ...
- Vuex2.0+Vue2.0构建备忘录应用实践
一.介绍Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化,适合于构建中大型单页应用. ...
- [转]Theano下用CNN(卷积神经网络)做车牌中文字符OCR
Theano下用CNN(卷积神经网络)做车牌中文字符OCR 原文地址:http://m.blog.csdn.net/article/details?id=50989742 之前时间一直在看 Micha ...
- TFLearn构建神经网络
TFLearn构建神经网络 Building the network TFLearn lets you build the network by defining the layers. Input ...
随机推荐
- 16.如何查找所需的maven的依赖
http://mvnrepository.com/ 1.先打开上面的网址 搜索需要的依赖,点进去 2.选择需要的版本 3.红框中的就是依赖的地址 此外也有其他自动化构建工具所需要的地址
- LLVM 编码规范 - 中文翻译
LLVM 编码规范 导论 语言.库和标准 C++ 标准版本 C++ 标准库 Go 代码准则 机械的代码问题 代码格式化 注释 头文件 类概述 method information 注释格式化 使用Do ...
- javascript实现组合列表框中元素移动效果
应用背景:在页面中有两个列表框,需要把其中一个列表框的元素移动到另一个列表框 . 实现的基本思想: (1)编写init方法对两个列表框进行初始化: (2)为body添加onload事件调用init方 ...
- CodeForces 277A 红娘问题(并查子集)
题目链接 思路如下 这题可以普通的并查集来做,我们把每个人认识的红娘,放到一个同一个集合里面,然后通过 for循环 遍历出现过的编号,看总共有几个集合,当集合的个数大于1的时候,需要的话费rmb的数量 ...
- Celery动态添加定时任务
背景 业务需求:用户可创建多个多人任务,需要在任务截止时间前一天提醒所有参与者 技术选型: Celery:分布式任务队列.实现异步与定时 django-celery-beat:实现动态添加定时任务,即 ...
- 关于C#三层架构增删改查中的“查询”问题
序:问题总是反复出现,可能只是一个小小的问题,但是就像肉中刺. 问题: 关于“姓名”字段的拼接问题 姓名字段的拼接:this.Repeater1.DataSource = db.GetList(&qu ...
- Failed to introspect Class [XXX] from ClassLoader [ParallelWebap报错
今天写了一个Controller,结果刚刚本地跑就给了一个惊喜 org.springframework.beans.factory.BeanCreationException: Error creat ...
- 非参数估计——核密度估计(Parzen窗)
核密度估计,或Parzen窗,是非参数估计概率密度的一种.比如机器学习中还有K近邻法也是非参估计的一种,不过K近邻通常是用来判别样本类别的,就是把样本空间每个点划分为与其最接近的K个训练抽样中,占比最 ...
- go 反射包
一.什么是反射? 反射是用程序检查其所拥有的结构,尤其是类型的一种能力: 二.Printf Printf 的函数声明为: func Printf(format string, args ... int ...
- Java研发技术学习路线
Java研发技术成长路线 作为一名Java研发者,深感Java技术的学习是一个漫长过程,从一名Java菜鸟开始,加之持之以恒的耐心和脚踏实地的精神,不间断理论的学习,不停止技术实践,终成为一名技术佼佼 ...