我正在尝试使用SSU隐藏单元制作RBM,我必须更新标准偏差。
我像这样定义我的变量:
def _build_model(self):
with tf.device('/gpu:0'):
with self.graph.as_default():
...
with tf.variable_scope("visible_layer"):
self.v_clamp = tf.placeholder(name = "v_in", dtype = tf.float32, shape=[self.batch_size, self.n_visibles])
self.bv = tf.get_variable(name = "b_v", dtype = tf.float32, shape=[self.n_visibles], initializer=tf.random_uniform_initializer(maxval=0.01,minval=-0.01))
self.stddev = tf.get_variable(name = "stddev", dtype = tf.float32, shape = [1], initializer = tf.constant_initializer(float(self.stddev_)))
...
with tf.variable_scope("update_weights"):
self.optimizer = self.update_weights()
....
其中stddev_具有初始值。
我的更新功能如下:
def update_weights(self):
with self.graph.as_default():
with tf.device('/gpu:0'):
...
with tf.variable_scope("calc_deltas"):
...
##UPDATE STDDEV
delta_stddev = tf.multiply((2)/(self.stddev**3),
tf.subtract(tf.reduce_sum(tf.pow(tf.subtract(self.v_clamp,self.bv),2)),
tf.reduce_sum(tf.pow(tf.subtract(v_free,self.bv),2))))
#self.stddev.assing_add(delta_stddev)
self.stddev.assign_add(tf.constant(0.1,shape=[1]))
return self.stddev
评论的行是我尝试过的。
我像这样训练:
def train_model(self):
with tf.Session(graph=self.graph) as session:
session.run(tf.global_variables_initializer())#Now all variables should be initialized.
print("Uninitialized variables: ", session.run(tf.report_uninitialized_variables())) #Just to check, should print nothing
print("Training for ", self.n_steps)
for step in range(self.n_steps):
feed_train = self._create_feed_dict(self.X_train,step)
feed_test = self._create_feed_dict(self.X_test,step)
print(session.run(self.optimizer, feed_dict = {self.v_clamp: feed_train}))
问题在于其他变量,即矢量(如self.bv)是否正确更新,但这一个(stddev)总是等于初始值。
我不知道,我做错了什么
答案 0 :(得分:2)
这是因为当您调用assign_add
方法时,您无法运行在TensorFlow图中定义的tf.assign_add
操作。
import tensorflow as tf
v = tf.get_variable('t', shape=[], initializer=tf.constant_initializer(0.))
op = tf.assign_add(v, 1)
with tf.Session() as session:
session.run(tf.global_variables_initializer())
print(session.run(v)) # print 0.
print(session.run(op)) # print 1. as you just ran the `assign_add` operation
print(session.run(v)) # print 1. as `v` has been incremented.
编辑:
在您的情况下,您可以做的是:
def update_weights(self):
...
return self.stddev.assing_add(delta_stddev)
这样,您的方法将返回实际更新op
变量的self.stdv
。