"""
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 构建神经网络做非线性归回的更多相关文章

  1. tensorflow1.0 构建神经网络做图片分类

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...

  2. tensorflow1.0 构建lstm做图片分类

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #this is data mni ...

  3. tensorflow1.0 构建卷积神经网络

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import os os.envi ...

  4. 使用TensorFlow v2.0构建卷积神经网络

    使用TensorFlow v2.0构建卷积神经网络. 这个例子使用低级方法来更好地理解构建卷积神经网络和训练过程背后的所有机制. CNN 概述 MNIST 数据集概述 此示例使用手写数字的MNIST数 ...

  5. 使用TensorFlow v2.0构建多层感知器

    使用TensorFlow v2.0构建一个两层隐藏层完全连接的神经网络(多层感知器). 这个例子使用低级方法来更好地理解构建神经网络和训练过程背后的所有机制. 神经网络概述 MNIST 数据集概述 此 ...

  6. NVIDIA DeepStream 5.0构建智能视频分析应用程序

    NVIDIA DeepStream 5.0构建智能视频分析应用程序 无论是要平衡产品分配和优化流量的仓库,工厂流水线检查还是医院管理,要确保员工和护理人员在照顾病人的同时使用个人保护设备(PPE),就 ...

  7. Vuex2.0+Vue2.0构建备忘录应用实践

    一.介绍Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化,适合于构建中大型单页应用. ...

  8. [转]Theano下用CNN(卷积神经网络)做车牌中文字符OCR

    Theano下用CNN(卷积神经网络)做车牌中文字符OCR 原文地址:http://m.blog.csdn.net/article/details?id=50989742 之前时间一直在看 Micha ...

  9. TFLearn构建神经网络

    TFLearn构建神经网络 Building the network TFLearn lets you build the network by defining the layers. Input ...

随机推荐

  1. vue动态定义图片路径

     当我在html模块或者css中引入图片的时候用相对路径,例: <div> <img src="../../assets/img/policeImg/tt.png" ...

  2. nodejs 模块加载顺序

    nodejs 模块加载顺序 一.当引入模块的形式是 require('lt') 时(1).先找当前文件夹下的node_modules文件夹下的lt文件夹下的package.json 文件指定的main ...

  3. 从零开始的JSON库

    从零开始的JSON库 项目地址 jsoncpp ,此项目受到 leptjson 启发,实现了最基本的功能,仅作学习使用. 提供简单的 parse() 和 generate() 方法将 JSON 文本解 ...

  4. 深入理解NIO(三)—— NIO原理及部分源码的解析

    深入理解NIO(三)—— NIO原理及部分源码的解析 欢迎回到淦™的源码看爆系列 在看完前面两个系列之后,相信大家对NIO也有了一定的理解,接下来我们就来深入源码去解读它,我这里的是OpenJDK-8 ...

  5. Java IO流的写入和写出操作 FileInputStream和FileOutputStream

    今天学习了Java的IO流,关于文件的读入和写出,主要是FileInputStream和FileOutputStream来实现,这两个流是字节流.还有字符流(FileReader和FileWriter ...

  6. D - Expanding Rods POJ - 1905(二分)

    D - Expanding Rods POJ - 1905 When a thin rod of length L is heated n degrees, it expands to a new l ...

  7. hello world: 我的博客写作思路

    1. 本人计算机专业,研究生刚毕业,即将入职金融科技领域,决定借博客园平台写自己的博客,原因如下: 从小白到大白,离不开各大学习平台和技术博客的指导和分享,是时候回馈了. 借此机会整理自己从本科.研究 ...

  8. PTA数据结构与算法题目集(中文) 7-9

    PTA数据结构与算法题目集(中文)  7-9 7-9 旅游规划 (25 分)   有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游 ...

  9. MTK Android Driver :Memory

    型号配置: 1.CUSTOM_MEMORY_HDR(需要确认是否是MTK认证的flash ic) mediatek\custom\$(PROJECT)\preloader\inc\custom_Mem ...

  10. go For-range结构

    For结构: for 初始化;条件语句;修饰语句{ 输出 } 一.For-range结构是可以怎么用? 这种构建方法可以应用于数组和切片: for ix, value := range slice1 ...