Tensorflow细节-P89-collection的使用
知识总结
(1)再次注意summary的使用
(2)x = rdm.rand(dataset_size, 2) y_ = [[x1**2 + x2**2] for (x1, x2) in x]这里的问题要注意
(3)注意batch时,全部先按照一套W进行前向传播,这时候在进行正则化时,加的是同一套W,然后反向传播改变W值,进行下一轮前向传播
代码如下
import tensorflow as tf
import numpy as np
from numpy.random import RandomState
rdm = RandomState(1)
dataset_size = 128
x = rdm.rand(dataset_size, 2)
y_ = [[x1**2 + x2**2] for (x1, x2) in x]
def get_weight(shape, alpha, name):
with tf.variable_scope("get_variable" + name):
var = tf.get_variable(name, shape, tf.float32, initializer=tf.truncated_normal_initializer(0.01))
tf.add_to_collection("losses", tf.contrib.layers.l2_regularizer(alpha)(var))
return var
with tf.name_scope("generate_value"):
xs = tf.placeholder(tf.float32, [None, 2], name="x_input")
ys = tf.placeholder(tf.float32, [None, 1], name="y_output")
batch_size = 8
layers_dimension = [2 ,10, 10, 10 ,1]
n_layers = len(layers_dimension)
in_dimension = layers_dimension[0]
cur_layer = xs
for i in range(1, n_layers):
out_dimension = layers_dimension[i]
with tf.variable_scope("layer%d" % i):
weights = get_weight([in_dimension, out_dimension], 0.001, "layers")
biases = tf.get_variable("biases", [out_dimension], tf.float32, tf.constant_initializer(0.0))
cur_layer = tf.matmul(cur_layer, weights) + biases
cur_layer = tf.nn.relu(cur_layer)
in_dimension = layers_dimension[i]
with tf.name_scope("loss_op"):
mse_loss = tf.reduce_mean(tf.square(ys - cur_layer))
tf.add_to_collection("losses", mse_loss)
loss = tf.add_n(tf.get_collection("losses"))
tf.summary.scalar("loss", loss)
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
merged = tf.summary.merge_all()
init = tf.global_variables_initializer()
with tf.Session() as sess:
writer = tf.summary.FileWriter("path/", tf.get_default_graph())
sess.run(init)
for i in range(5000):
start = i*batch_size % dataset_size
end = min(start+batch_size, dataset_size)
if i % 50 == 0:
result = sess.run(merged, feed_dict={xs: x, ys: y_})
writer.add_summary(result, global_step=i)
if i % 500 ==0:
loss_op = sess.run(loss, feed_dict={xs: x, ys: y_})
print("After %d training, loss is %g" % (i, loss_op))
_ = sess.run(train_op, feed_dict={xs: x[start:end], ys: y_[start:end]})
writer.close()


Tensorflow细节-P89-collection的使用的更多相关文章
- 【原创】【Android】揭秘 ART 细节 ---- Garbage collection
背景 Dalvik :http://zh.wikipedia.org/wiki/Dalvik%E8%99%9A%E6%8B%9F%E6%9C%BA ART :http://source.andro ...
- Tensorflow细节-P312-PROJECTOR
首先进行数据预处理,需要生成.tsv..jpg文件 import matplotlib.pyplot as plt import numpy as np import os from tensorfl ...
- Tensorflow细节-P319-使用GPU基本的操作
如果什么都不加,直接运行装了GPU的Tensorflow,结果是这样子的 import tensorflow as tf a = tf.constant([1.0, 2.0, 3.0], shape= ...
- Tensorflow细节-P309-高维向量可视化
import matplotlib.pyplot as plt import tensorflow as tf import numpy as np import os from tensorflow ...
- Tensorflow细节-P309-监控指标可视化
注意下面一个点就ok了 with tf.name_scope('input_reshape'): # 注意看这里,图片的生成 image_shaped_input = tf.reshape(x, [- ...
- Tensorflow细节-P290-命名空间与tensorboard上的节点
讲解几个重点知识 1.对于tf.get_variable()中的reuse,意思是,如果有名字一模一样的变量,则对这个变量继续使用,如果没有名字一模一样的变量,则创建这个变量 2.options=ru ...
- Tensorflow细节-Tensorboard可视化-简介
先搞点基础的 注意注意注意,这里虽然很基础,但是代码应注意: 1.从writer开始后边就错开了 2.writer后可以直接接writer.close,也就是说可以: writer = tf.summ ...
- Tensorflow细节-P202-数据集的高层操作
本节是对上节的补充 import tempfile import tensorflow as tf # 输入数据使用本章第一节(1. TFRecord样例程序.ipynb)生成的训练和测试数据. tr ...
- Tensorflow细节-P199-数据集
数据集的基本使用方法 import tempfile import tensorflow as tf input_data = [1, 2, 3, 5, 8] # 这不是列表吗,为什么书里叫数组 da ...
- Tensorflow细节-P196-输入数据处理框架
要点 1.filename_queue = tf.train.string_input_producer(files, shuffle=False)表示创建一个队列来维护列表 2.min_after_ ...
随机推荐
- 什么是REST 、RESTful 、RESTful API?
介绍 自从Roy Fielding博士在2000年他的博士论文中提出Rest(Representational State Transfer)风格的软件架构模式后,REST就基本上迅速取代了复杂而笨重 ...
- jQuery的基础总结
**本篇只列出零碎的jQuery基础知识点,个人记录自己的学习进度,无序排列,谨慎查看.** 1.jQuery入口函数的四种写法2.jQuery与JS遍历数组的区别3.jQuery符号冲突问题4.jQ ...
- SpringCloudConfig相关配置简介、使用、整合Eureka
目的: 1.SpringCloud Config简介 2.Config Server基本使用 3.Config Client基本使用 4.Config整合Eureka 5.Config配置搜索路径 S ...
- 最简单 无返回值 无参数 sql server存储过程
CREATE proc Upadte_stateas update Table_1 set [state]=2 where id in (select id from Table_1 where st ...
- ASP.NET MVC+Entity Framework code first 迁移
再来一张,选择 MVC 模版,其他的没选过,不会用 =_=!! 身份验证用个人用户账户,这个是为了偷懒,话说 ASP.NET Identity 还是很给力的,不用白不用 ^_^~ 点击确定之后,会看 ...
- head引入样式
引入CSS(base基础样式,index页面样式): <link rel="stylesheet" type="text/css" href=" ...
- ubuntu安装mysql数据库方法
ubuntu基于linux的免费开源桌面PC操作系统,十分契合英特尔的超极本定位,支持x86.64位和ppc架构.一个比较流行的Linux操作系统,不仅简单易用,而且和Windows相容性非常好.那么 ...
- 服务接口,选择rpc还是http?
从通信内容/功能上看 http应用于web环境,rpc应用于分布式调度从功能上看没有太大区别,很多情况下rpc与消息中间件结合通信实现分布式调度 从用法上看两者都是c/s结构,无太大区别 从实现上看类 ...
- react-router-dom下的BrowserRouter和HashRouter
奇思妙想的
- iview carousel 图片不显示;iview 轮播图 图片无法显示(转载)
转载来源:https://segmentfault.com/q/1010000016778108 相关代码 <Carousel autoplay v-model="value2&quo ...