https://github.com/chenghuige/tensorflow-exp/blob/master/examples/sparse-tensor-classification/
https://github.com/chenghuige/tensorflow-exp/blob/master/examples/sparse-tensor-classification/
tensorflow-exp/example/sparse-tensor-classification/train-validate.py
当你需要train的过程中validate的时候,如果用placeholder来接收输入数据
那么一个compute graph可以完成这个任务。如果你用的是TFRecord的方式
输入嵌入到compute graph,那么对应input(for train), input_1(for validate),就会产生两个compute graph,但是要注意的是validate过程中需要share使用等同于train过程的w_h等变量,如果直接build两次graph就回阐释下面的示意图

这种并没有共享 w_h等数据,因此validate 会有问题(注意Input_1里面对应的w_h_1)
cost, accuracy = build_graph(X, label)
_, accuracy_test = build_graph((index_test, value_test), label_test)
train_op = gen_optimizer(cost, FLAGS.learning_rate)
#train_op_test = gen_optimizer(cost_test, FLAGS.learning_rate)
这里
tf.get_variable_scope().reuse_variables()并不起作用,因为build_graph里面并没有使用ge_variable机制
第一种解决方案
用类 self.w_h
解决此类问题的方法之一就是使用类来创建模块,在需要的地方使用类来小心地管理他们需要的变量. 一个更高明的做法,不用调用类,而是利用TensorFlow 提供了变量作用域 机制,当构建一个视图时,很容易就可以共享命名过的变量.
来自 <http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/variable_scope/index.html>
使用类的方式,共享w_h等变量
class Mlp(object):
def __init__(self):
hidden_size = 200
num_features = NUM_FEATURES
num_classes = NUM_CLASSES
with tf.device('/cpu:0'):
self.w_h = init_weights([num_features, hidden_size], name = 'w_h')
self.b_h = init_bias([hidden_size], name = 'b_h')
self.w_o = init_weights([hidden_size, num_classes], name = 'w_o')
self.b_o = init_bias([num_classes], name = 'b_o')
def model(self, X, w_h, b_h, w_o, b_o):
h = tf.nn.relu(matmul(X, w_h) + b_h)
return tf.matmul(h, w_o) + b_o
def forward(self, X):
py_x = self.model(X, self.w_h, self.b_h, self.w_o, self.b_o)
return py_x
X = (index, value)
algo = Mlp()
cost, accuracy = build_graph(X, label, algo)
cost_test, accuracy_test = build_graph((index_test, value_test), label_test, algo)
train_op = gen_optimizer(cost, FLAGS.learning_rate)

类似这种做法的例子tensorflow/tensorflow/models/embedding/word2vec.py
第二中
变量共享
变量作用域机制在TensorFlow中主要由两部分组成:
- tf.get_variable(<name>, <shape>, <initializer>): 通过所给的名字创建或是返回一个变量.
- tf.variable_scope(<scope_name>): 通过 tf.get_variable()为变量名指定命名空间.
方法 tf.get_variable() 用来获取或创建一个变量,而不是直接调用tf.Variable.它采用的不是像`tf.Variable这样直接获取值来初始化的方法.一个初始化就是一个方法,创建其形状并且为这个形状提供一个张量.这里有一些在TensorFlow中使用的初始化变量:
代码
来自 <http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/variable_scope/index.html>
https://github.com/chenghuige/tensorflow-exp/blob/master/examples/sparse-tensor-classification/的更多相关文章
- https://github.com/tensorflow/models/blob/master/research/slim/datasets/preprocess_imagenet_validation_data.py 改编版
#!/usr/bin/env python # Copyright 2016 Google Inc. All Rights Reserved. # # Licensed under the Apach ...
- iOS - 解决Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named
1 本来cocopods没有问题,最近创建项目,利用cocopods导入第三方库的时候,出现如下错误: [!] Unable to add a source with url `https://gi ...
- Git - could not read Username for 'https://github.com',push报错解决办法
执行git push命令异常,如下: git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sou ...
- fatal: unable to access 'https://github.com/Homebrew/brew/'
最近安装 Homebrew 遇到的坑,总结一下. 我的 Mac 版本是 10.13.6. 首先安装 Homebrew /usr/bin/ruby -e "$(curl -fsSL https ...
- git 解决 error: failed to push some refs to 'https://github.com/xxxx.git'
在github远程创建仓库后, 利用gitbash进行提交本地文件的时候出现如下错误 [root@foundation38 demo]# git push -u origin master Usern ...
- 结对项目https://github.com/bxoing1994/test/blob/master/源代码
所选项目名称:文本替换 结对人:曲承玉 github地址 :https://github.com/bxoing1994/test/blob/master/源代码 结对人github地址:ht ...
- https://github.com/python/cpython/blob/master/Doc/library/contextlib.rst 被同一个线程多次获取的同步基元组件
# -*- coding: utf-8 -*- import time from threading import Lock, RLock from datetime import datetime ...
- https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go
https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go
- https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/connections.py
# Python implementation of the MySQL client-server protocol # http://dev.mysql.com/doc/internals/en/ ...
随机推荐
- UOJ59 WC2013 小Q运动季
题意:给一个模线性方程组,构造解满足尽量多的方程. 直接枚举. 模数两两互质,先排除无解的,然后CRT,然而要高精,上python. 直接高消,有完美解. 2^20暴搜. 分解模数,对每个质数高消,C ...
- iBatis.net 类的继承extends和懒加载
<resultMaps> <resultMap id="FullResultMap" class="t_c_team_member_permission ...
- Codis——分布式Redis服务的解决方案
Codis——分布式Redis服务的解决方案 之前介绍过的 Twemproxy 是一种Redis代理,但它不支持集群的动态伸缩,而codis则支持动态的增减Redis节点:另外,官方的redis 3. ...
- hdu2014青年歌手大赛
Problem Description 青年歌手大奖赛中,评委会给参赛选手打分.选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分. Input 输入数据有多组,每组占 ...
- 改变win7驱动图标
一.背景 自己做的USB设备是HID设备,注册到设备管理器中就是"HID Compliant device",显得很业余,然后想去改变这个图标和名称,也就有了此篇文章 二.正文 还 ...
- ObjC运行时部分概念解析(一)
转型iOS已经许久了,Runtime(运行时)还没有好好了解过.之前没有阅读过源码,紧紧凭借自己的臆测.现在阅读下源码,做一些笔记.方便再次翻阅 SEL SEL是一个关键字,如果没有涉及runtime ...
- Collection小结
Collection:
- bzoj4692: Beautiful Spacing
先二分答案后dp 设\(su[n]\)为\(\sum_{1}^{n}xi[i]\) 设\(f[n]\)为1时表示第n个单次能做某一行的结尾,且之前的空格满足二分出来的答案. 考虑怎样的\(f[i]\) ...
- BZOJ 4668: 冷战
Description 在一个图上,在两个点间连一条边,问这两个点最早在什么时候联通. Sol 并查集+启发式合并. 按秩合并的并查集...我也不知道什么是按秩合并,反正就跟启发式合并差不多,合并的时 ...
- xml读取节点
<?xml version="1.0" encoding="utf-8"?> <tplcd type=" product=" ...