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/ ...
随机推荐
- Html中列表标签<ul>的使用
借助Html中的列标标签<ul>在某列文字前面添加标注 <html> <head> <title>文字上方添加标记说明</title> &l ...
- [.net core]简介(转)
原文地址:https://docs.microsoft.com/zh-cn/dotnet/articles/core/ NET Core 是一个通用开发平台,由 Microsoft 和 GitHub ...
- asp.net三层架构 及其中使用泛型获取实体数据介绍
asp.net中使用泛型获取实体数据可以发挥更高的效率,代码简洁方便,本例采用三层架构.首先在model层中定义StuInfo实体,然后在 DAL层的SQLHelper数据操作类中定义list< ...
- 关于break语句如何结束多重循环的嵌套
在Java中的break语句功能大体上同c语言, 用于循环语句中,表示结束当前循环. 但是有时候在循环嵌套语句中,仅仅靠一 个break语句想实现是不够的. 例: 如果想使sum在501时就直接输出, ...
- Dota2 demo手游项目历程
最近其实是打算认真研究c++ primer和设计模式的原著,然后写一些读书笔记的,不过设计模式原著里生词太多,大多都看的不是很明白,因此暂时放弃阅读设计模式,打算用这些时间做一个类似我叫mt2的手游d ...
- JavaScript - 正则表达式
正则表达式的大致匹配过程是:依次拿出表达式和文本中的字符比较,如果每一个字符都能匹配,则匹配成功:一旦有匹配不成功的字符则匹配失败. 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默 ...
- laravel强大功能路由初探(二)
目标当然是先输出helloworld 配置hosts文件和apache下的httpd-vhosts.conf, hosts:127.0.0.1 www.blog.com httpd-vhosts.c ...
- Maven的Missing artifact问题解决
Maven的Missing artifact问题解决 今天在创建一个新的Maven项目时,在其中添加了很多依赖.刚开始为了避免错误就每添加一次,保存一下,Eclipse就会下载相应的包.最后为了 ...
- calendar的一些操作
一.通过分析日期函数,根据日期进行一系列操作,例如:我们需要知道2个时间段中所有的日期等等. 由于Calendar 类是一个抽象类,因此我们不能通过new来获取该对象的实例.我们可以通过其类方法 ge ...
- HTTP的客户端识别与cookie机制
本文是<HTTP权威指南>的读书笔记 Web服务器可能同时在与数千个客户端同时进行会话,服务器需要记录下它们在与谁交谈,而不是认为所有的请求都来自于匿名客户端.在HTTP中可以有以下几种方 ...