【问题标题】:what does "fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])" doing?“fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])”在做什么?
【发布时间】:2020-11-08 15:07:55
【问题描述】:
def conv_net(x, weights, biases, dropout):
    # Layer 1 - 28*28*1 to 14*14*32
    conv1 = conv2d(x, weights['wc1'], biases['bc1'])
    conv1 = maxpool2d(conv1, k=2)

    # Layer 2 - 14*14*32 to 7*7*64
    conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
    conv2 = maxpool2d(conv2, k=2)

get.shape().as_list()[0] 是做什么的?

fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])

【问题讨论】:

  • 获取weights['wd1']的当前形状的第0个元素。
  • 如果你想了解更多细节,你可以仔细阅读this。这是一个更复杂的话题。

标签: python tensorflow2.0


【解决方案1】:

为了更好地理解get.shape().as_list(),这里我提供一个简单的例子,希望对你有所帮助。

import tensorflow as tf
c = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
Shape = c.get_shape().as_list()
print(Shape)  

输出:

[2, 3]

这意味着c2 rows and 3 columns。 当我们打印Shape = c.get_shape().as_list()[0] 时,它以列表格式返回c0th 元素(通常它返回元组中的形状为(2,3))

Shape = c.get_shape().as_list()[0] 

输出:

2

当我们使用tf.reshape 时,它会返回一个新的张量,该张量的值与旧张量的值相同,顺序相同,但形状指定的新形状除外。

当我们通过shape of [-1] 时,它会变平为一维。

tf.reshape(c, [-1])

输出:

<tf.Tensor: shape=(6,), dtype=float32, numpy=array([1., 2., 3., 4., 5., 6.], dtype=float32)>

fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])

总而言之,这里我们将conv2 层的(即1D) 权重(即2D) 并提取weights['wd1']0th 元素。

【讨论】:

    猜你喜欢
    • 2022-07-04
    • 2022-01-25
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    相关资源
    最近更新 更多