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)

 
 

来自 <http://git.oschina.net/chenghuige/tensorflow-exp/blob/master/example/sparse-tensor-classification/train-validate.py?dir=0&filepath=example%2Fsparse-tensor-classification%2Ftrain-validate.py&oid=04e0aca92d157121cac257125e2c6a66f68c1e4c&sha=b5f3b6b833ddbb99cc2c9ea763a59a3ab5c564b7>

这里
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中使用的初始化变量:

 
 

代码

https://github.com/chenghuige/tensorflow-exp/blob/master/examples/sparse-tensor-classification/train-validate-share.py

 
 

来自 <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/的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. fatal: unable to access 'https://github.com/Homebrew/brew/'

    最近安装 Homebrew 遇到的坑,总结一下. 我的 Mac 版本是 10.13.6. 首先安装 Homebrew /usr/bin/ruby -e "$(curl -fsSL https ...

  5. git 解决 error: failed to push some refs to 'https://github.com/xxxx.git'

    在github远程创建仓库后, 利用gitbash进行提交本地文件的时候出现如下错误 [root@foundation38 demo]# git push -u origin master Usern ...

  6. 结对项目https://github.com/bxoing1994/test/blob/master/源代码

    所选项目名称:文本替换      结对人:曲承玉 github地址 :https://github.com/bxoing1994/test/blob/master/源代码 结对人github地址:ht ...

  7. 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 ...

  8. https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go

    https://github.com/golang/crypto/blob/master/bcrypt/bcrypt.go

  9. 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/ ...

随机推荐

  1. thinkphp 命名空间

    什么是命名空间?从广义上来说,命名空间是一种封装事物的方法.在很多地方都可以见到这种抽象概念.例如,在操作系统中目录用来将相关文件分组,对于目录中的文件来说,它就扮演了命名空间的角色.具体举个例子,文 ...

  2. 深入研究HTTP协议以及部分应用

    引言 工作了一段时间,都是在开发网页,自然和http打交道打得最多了,投身工作之后原来理解的东西又变得模糊,所以有必要深入探讨一下http协议的细节,也借此总结一下学习的成果. HTTP相关认识 对H ...

  3. strrchr 一个获取扩展名的方便的php函数

    上传文件时,$_FILES里面的name是稳健的名称,要获取扩展名就用 strrchr(str,符号)来截取最后一个.后面的扩展名 然后用 trim 去掉特殊字符. 就可以得到扩展名了

  4. canvas变幻曲线

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  5. [Unreal]学习笔记之材质说明

    取消蓝图中的连接线:Alt+鼠标左键 在蓝图中,通过按住1,2,3,4加鼠标左键,可以快速生成1,2,3,4维的向量 材质和材质实例的区别:使用一个母材质,可以创建出多种场景中的材质实例:每次修改母材 ...

  6. 阿里云推送SDK在某些机型(某米为主)下崩溃问题的解决方法

    引言 最近APP上线,遇到一个比较诡异的问题.最后竟然和dex文件有关,也是醉了,看来还得深入底层学习啊. 问题描述 在集成阿里推送SDK时,需要在Application中进行初始化,大多数Andro ...

  7. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  8. C# 深入浅出 异步(八)

    C#异步调用学习链接:从C#5.0说起:再次总结C#异步调用方法发展史

  9. 【bzoj4721】[Noip2016]蚯蚓

    题目描述 本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」=[3.9」=3.蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓.蛐 ...

  10. 对AutoIt中控件和窗口的理解

    经过尝试,对AutoIt中Control和Window有了新的认识,分享一下 1.Control 现在我想对一个WinForm架构的应用程序进行自动化操作,得到控件Advanced Mode属性为[N ...