这些功能之间有什么区别?
tf.variable_op_scope(values, name, default_name, initializer=None)返回上下文管理器,用于定义创建变量的op.此上下文管理器验证给定值来自同一图形,确保该图形是默认图形,并推送名称范围和变量范围.
tf.op_scope(values, name, default_name=None)返回定义Python操作时使用的上下文管理器.此上下文管理器验证给定值是否来自同一图,确保该图是默认图,并推送名称范围.
tf.name_scope(name)
Graph.name_scope()使用默认图表的包装器.有关Graph.name_scope()详细信息,请参阅
tf.variable_scope(name_or_scope, reuse=None, initializer=None)返回变量范围的上下文.变量范围允许创建新变量并共享已创建的变量,同时提供检查以避免意外创建或共享.有关详细信息,请参阅变量范围操作方法,此处我们仅提供一些基本示例.
import tensorflow as tf
def activation(e, f, g):
return e + f + g
with tf.Graph().as_default():
a = tf.constant([5, 4, 5], name='a')
b = tf.constant([0, 1, 2], name='b')
c = tf.constant([5, 0, 5], name='c')
res = activation(a, b, c)
init = tf.initialize_all_variables()
with tf.Session() as sess:
# Start running operations on the Graph.
merged = tf.merge_all_summaries()
sess.run(init)
hi = sess.run(res)
print hi
writer = tf.train.SummaryWriter("/tmp/basic", sess.graph_def)
Run Code Online (Sandbox Code Playgroud)
输出错误:
Value Error: Fetch argument <tf.Tensor 'add_1:0' shape=(3,) dtype=int32> of
<tf.Tensor 'add_1:0' shape=(3,) dtype=int32> …Run Code Online (Sandbox Code Playgroud) 根据本文,输出形状是N + H - 1,N输入高度或宽度,H是内核高度或宽度.这是卷积的明显逆过程.本教程给出了计算卷积输出形状的公式(W?F+2P)/S+1,即W- 输入大小,F- 过滤器大小,P- 填充大小,S- 步幅.但是在Tensorflow中,有一些测试用例如:
strides = [1, 2, 2, 1]
# Input, output: [batch, height, width, depth]
x_shape = [2, 6, 4, 3]
y_shape = [2, 12, 8, 2]
# Filter: [kernel_height, kernel_width, output_depth, input_depth]
f_shape = [3, 3, 2, 3]
Run Code Online (Sandbox Code Playgroud)
所以我们使用y_shape,f_shape并x_shape根据公式(W?F+2P)/S+1计算填充大小P.从(12 …
import numpy as np
import tensorflow as tf
class simpleTest(tf.test.TestCase):
def setUp(self):
self.X = np.random.random_sample(size = (2, 3, 2))
def test(self):
a = 4
x = tf.constant(self.X, dtype=tf.float32)
if a % 2 == 0:
y = 2*x
else:
y = 3*x
z = 4*y
with self.test_session():
print y.eval()
print z.eval()
if __name__ == "__main__":
tf.test.main()
Run Code Online (Sandbox Code Playgroud)
这里y是tensorflow变量,并且在if else块中定义,为什么它可以在块外部使用?