tensroflow建立一个完整的单层网络
来源:互联网 发布:linux配置hadoop环境 编辑:程序博客网 时间:2024/05/22 00:40
传送门
学习这一部分代码还是收获了很多的,虽然还是有的地方不是很理解,但是之后看的代码多了就会理解了。首先,写一下我的心得体会。
1、add_layer()函数是作者定义的函数,很方便,可以参考
2、变量定义的方式也很厉害,是numpy部分内容。可以定义x_data.shape大小的正态分布noise。
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
其中是啥意思,不是很理解,可以看看numpy手册
x_data = np.linspace(-1,1,300)[:, np.newaxis]
3、placeholder定义占位符,可以用none的形式定义随机长度的输入
xs = tf.placeholder(tf.float32, [None, 1])
4、定义训练,设置训练步长
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
5、对于有placeholder的输入,session的run要加上feed_dict,同时,读取一个loss变量值也要加上这一个东西
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}))
完整代码如下:
# View more python learning tutorial on my Youtube and Youku channel!!!# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg# Youku video tutorial: http://i.youku.com/pythontutorial"""Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly."""from __future__ import print_functionimport tensorflow as tfimport numpy as npdef add_layer(inputs, in_size, out_size, activation_function=None): # add one more layer and return the output of this layer Weights = tf.Variable(tf.random_normal([in_size, out_size])) biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) 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 datax_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 networkxs = tf.placeholder(tf.float32, [None, 1])ys = tf.placeholder(tf.float32, [None, 1])# add hidden layerl1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)# add output layerprediction = add_layer(l1, 10, 1, activation_function=None)# the error between prediction and real dataloss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)# important step# tf.initialize_all_variables() no long valid from# 2017-03-02 if using tensorflow >= 0.12if int((tf.__version__).split('.')[1]) < 12: init = tf.initialize_all_variables()else: init = tf.global_variables_initializer()sess = tf.Session()sess.run(init)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}))
0 0
- tensroflow建立一个完整的单层网络
- Tensorflow学习:单层神经网络的建立
- 如何建立一个完整的游戏AI
- 在FreeBSD上建立一个功能完整的邮件服务器
- 简单的建立一个网络电视直播软件
- zigbee 建立一个新的网络
- 建立一个程序员自己的全国性营销网络
- 建立一个程序员自己的全国性营销网络
- 理解分布式tensroflow必看的几个文档
- 在FreeBSD上建立一个功能完整的邮件服务器(Postfix)
- 在rhas3.0上建立一个完整的邮件系统(内含四部分)修正版 V
- 一个建立完整游戏AI的事例:第一部分(作者:Geoff Howland)
- 一个建立完整游戏AI的事例:第二部分(作者:Geoff Howland)
- tensroflow:tensor
- 一个完整的类
- 一个完整的项目
- 单层网络模型下对无监督特征学习算法的分析
- Deep learning:二十(无监督特征学习中关于单层网络的分析)
- 作业3:我的博客主页
- 剑指offer:替换空格
- bean的实例化时机
- Json,List,Map的数据格式详解。
- 反转数字
- tensroflow建立一个完整的单层网络
- 从0开始学习 GitHub 系列之「初识 GitHub」
- 智能卡 APTU命令
- ssh 免密码登录
- Qt+OpenCV在不同ubuntu系统移植时SIGSEGV问题解决
- 面试常见问题总结
- robot framework调整Text Edit调整字体颜色
- htm和html有什么区别
- Android中关于使用空格对齐文字