Tensorflow 疑惑解答

本文希望以最精简的语言记录并解释tensorflow使用中遇到的疑惑。与API学习不同的是,这篇用于记录tensorflow特有的机制,如:会话机制,variable_scope用法等系列问题。

tensorflow 广播测试

a = tf.constant([[[1, 1, 1], [2, 2, 2]],
                 [[3, 3, 3], [4, 4, 4]],
                 [[5, 5, 5], [6, 6, 6]]])
b = tf.constant([[[1, 1, 1], [1, 1, 1]]])
c = a - b

sess = tf.Session()
a_shape, b_shape, c_shape = sess.run([tf.shape(a), tf.shape(b), tf.shape(c)])

print(sess.run(c))
print("a.shape: ", a_shape)
print("b.shape: ", b_shape)
print("c.shape: ", c_shape)

结果如下:
[[[0 0 0]
  [1 1 1]]

 [[2 2 2]
  [3 3 3]]

 [[4 4 4]
  [5 5 5]]]

a.shape:  [3 2 3]
b.shape:  [1 2 3]
c.shape:  [3 2 3]

如代码所示,tensorflow能完成正常的广播功能,类似于numpy。即一个[3,2,3]维的变量和一个[1,2,3]维的变量做运算操作后,是不会报错的,而且得到结果的形状为[3,2,3],我们就说tensorflow具备了广播功能。

tf.Graph().as_default()

a = tf.constant(value=1)
print("a.graph: ", a.graph)    # 打印此时a所在的图
print("a.name: ", a.name)      # 打印a此时的名字
print("default graph: ", tf.get_default_graph())    # 打印默认图的名字
print("="*50)

graph1 = tf.Graph()    # 定义了一个新的图graph1
print("graph1: ", graph1)
b = tf.constant(value=2)
print("b.graph: ", b.graph)    # 测试此时b所在的图
print("b.name: ", b.name)    # 测试此时b的名字
with graph1.as_default():
    c1 = tf.constant(value=3)    # 在graph1中定义变量c1
    print("c1.graph: ", c1.graph)    # 测试此时c1所在的图
    print("c1.name: ", c1.name)    # 测试此时c1的名字
    print("-" * 50)
    c2 = tf.constant(value=3)    # 在graph1中定义变量c2
    print("c2.graph: ", c2.graph)    # 测试此时c2所在的图
    print("c2.name: ", c2.name)    # 测试此时c2的名字
    print("-" * 50)
print("c1.graph: ", c1.graph)    # 测试在with语句外,c1的名字
print("c1.name: ", c1.name)    # 测试在with语句外,c1所在的图
print("="*50)

graph2 = tf.Graph()
print("graph2: ", graph2)
graph2.as_default()
d = tf.constant(value=4)
print("d.graph: ", d.graph)
print("d.name: ", d.name)

结果如下:
a.graph:  <tensorflow.python.framework.ops.Graph object at 0x00000215B7072FD0>
a.name:  Const:0
default graph:  <tensorflow.python.framework.ops.Graph object at 0x00000215B7072FD0>
==================================================
graph1:  <tensorflow.python.framework.ops.Graph object at 0x00000215BD368CC0>
b.graph:  <tensorflow.python.framework.ops.Graph object at 0x00000215B7072FD0>
b.name:  Const_1:0
c1.graph:  <tensorflow.python.framework.ops.Graph object at 0x00000215BD368CC0>
c1.name:  Const:0
--------------------------------------------------
c2.graph:  <tensorflow.python.framework.ops.Graph object at 0x00000215BD368CC0>
c2.name:  Const_1:0
--------------------------------------------------
c1.graph:  <tensorflow.python.framework.ops.Graph object at 0x00000215BD368CC0>
c1.name:  Const:0
==================================================
graph2:  <tensorflow.python.framework.ops.Graph object at 0x00000215BD368FD0>
d.graph:  <tensorflow.python.framework.ops.Graph object at 0x00000215B7072FD0>
d.name:  Const_2:0

通过上述代码的测试,我们了解到以下几点:

  • tensorflow会默认创建一个图,所有命名的变量若是没有指定图,就会创建在默认图中
  • tf.Graph()可以定义一个新的图,搭配with, as_default()使用的话,新创建的变量会创建在新的图中
  • 若是使用tf.Graph()定义了新图,但没有搭配with使用,那么新创建的变量仍然存在于默认的图中
  • tensorflow中constant的命名方式:Const, Const_1, Const_2,如下类推
  • 即使不在with下,在with中定义的变量名,也会具备with图的属性。即一旦定义完,变量所在的图就固定了

tf.nn.conv2d_transpose()

大部分做图像生成的朋友们都会用到反卷积,那么反卷积图像的输出大小到底如何计算呢?反卷积到底经历了什么过程呢?

# 定义一个输入
x = tf.ones(shape=[1, 3, 3, 3], dtype=tf.float32)
# 定义反卷积的filter
weight = tf.ones(shape=[3, 3, 6, 3], dtype=tf.float32)
# 进行反卷积操作
x_ = tf.nn.conv2d_transpose(x, weight, [1, 6, 6, 6], [1,2,2,1], padding='SAME')

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    _x = sess.run(x_)

print("_x.shape: ", _x.shape)

输出结果:
_x.shape:  (1, 6, 6, 6)

在padding='VALID'情况下,反卷积的输出大小就是卷积的逆运算。我们今天着重强调padding='SAME'的情况,并讲解一下反卷积的几个重点:

  • 反卷积操作本质还是卷积,tensorflow根据你输入:反卷积filter,stride, padding,首先可以计算出输出的大小;
  • 当padding='SAME'时,output_shape = input_shape * stride = 3 * 2 = 6,和你在conv2d_transpose()中指定的大小一致,tensorflow就不报错,程序继续往下走;
  • 为什么说反卷积操作本质还是卷积,当tensorflow匹配完形状没问题之后,会给根据输出的形状给输入加padding,当padding='SAME'的模式下,padding_shape = output_shape * stride = input_shape * stride * stride,然后在这个padding_shape的中间变量上做卷积操作得到最后的输出output;
  • 大家可借助博客:https://blog.csdn.net/sinat_29957455/article/details/85558870来详细理解反卷积的原理

备注:本文为作者原创,转载需注明出处!

猜你喜欢

转载自blog.csdn.net/GodWriter/article/details/85251969