Tensorflow 理解tensorboard:为什么12个张量发送给优化器?
因此,我制作了一个最简单的模型(感知器/自动编码器),它(除了输入生成)如下所示:Tensorflow 理解tensorboard:为什么12个张量发送给优化器?,tensorflow,tensorboard,Tensorflow,Tensorboard,因此,我制作了一个最简单的模型(感知器/自动编码器),它(除了输入生成)如下所示: N = 64 * 64 * 3 def main(): x = tf.placeholder(tf.float32, shape=(None, 64, 64, 3), name="x") with tf.name_scope("perceptron"): W = tf.Variable(tf.random_normal([N, N], stddev=1), name="W")
N = 64 * 64 * 3
def main():
x = tf.placeholder(tf.float32, shape=(None, 64, 64, 3), name="x")
with tf.name_scope("perceptron"):
W = tf.Variable(tf.random_normal([N, N], stddev=1), name="W")
b = tf.Variable(tf.random_normal([], stddev=1), name="b")
y = tf.add(tf.matmul( tf.reshape(x, [-1,N]), W), b, name="y")
act = tf.nn.sigmoid(y, name="sigmoid")
yhat = tf.reshape(act, [-1, 64, 64, 3], name="yhat")
with tf.name_scope("mse"):
sq_error = tf.reduce_mean(np.square(x - yhat), axis=1)
cost = tf.reduce_mean( sq_error, name="cost" )
tf.summary.scalar("cost", cost)
with tf.name_scope("conv_opt"): #Should just be called 'opt' here
training_op = tf.train.AdamOptimizer(0.005).minimize(cost, name="train_op")
with tf.device("/gpu:0"):
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
sess.run(tf.global_variables_initializer())
logdir = "log_directory"
if os.path.exists(logdir):
shutil.rmtree(logdir)
os.makedirs(logdir)
input_gen = input.input_generator_factory(...)
input_gen.initialize((64,64,3), 512)
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter(logdir, sess.graph)
for i in range(10):
batch = input_gen.next_train_batch()
summary,_ = sess.run([merged, training_op], feed_dict={x : batch})
train_writer.add_summary(summary, i)
print("Iteration %d completed" % (i))
if __name__ == "__main__":
main()
这将产生以下结果。无论如何,我假定从“perception”到“conv_opt”的粗箭头(很抱歉,它应该被称为“opt”)对应于反向传播错误信号,(而?x64x64x3箭头对应于推断)。但为什么是12张量?我不知道这个数字是从哪里来的。我本以为会少一些,实际上只对应于
W
和b
。有人能解释一下发生了什么吗?我认为原因是,当您添加tf.train.AdamOptimizer(0.005).minimize(cost)
op时,隐式假设您优化了所有可训练变量(因为您没有另外指定)。
因此,您需要知道这些变量的值以及参与成本计算的所有中间张量的值,包括梯度(也是张量,隐式添加到计算图中)。现在让我们从感知机中计算变量和张量:
W
b
tf.重塑(x,[-1,N])
tf.matmul(…,W)
tf.add(…,b,name=“y”)
tf.nn.sigmoid(y,name=“sigmoid”)
tf.重塑(动作[1,64,64,3],name=“yhat”)
作为练习,我们可以看到这种类型的会计也解释了数字9在图表中的来源:
x-yhat
np.square(…)
tf.减少平均值(…,轴=1)
tf.减少平均值(sq\u错误,name=“cost”)
我想投赞成票,但现在还不让我——这听起来不错。在我接受任何答复之前,我将等待一段时间。