首页 技术 正文
技术 2022年11月12日
0 收藏 576 点赞 4,926 浏览 2510 个字

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就回阐释下面的示意图

Tensorflow 变量的共享

这种并没有共享 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/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-shared.py

来自 <http://wiki.jikexueyuan.com/project/tensorflow-zh/how_tos/variable_scope/index.html>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,285