Mostafa Zayed
Mostafa Zayed

Reputation: 31

what does "fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])" doing?

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)

what does this get.shape().as_list()[0] do?

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

Upvotes: 3

Views: 179

Answers (1)

user11530462
user11530462

Reputation:

For better understanding of get.shape().as_list(), here i am providing simple example, hope this help you.

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)  

Output:

[2, 3]

It meansc has 2 rows and 3 columns. When we print Shape = c.get_shape().as_list()[0] it return 0th element of c in list format (generally it return shape in tuple as (2,3))

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

Output:

2

When we use tf.reshape it returns a new tensor that has the same values as old tensor in the same order, except with a new shape specified by shape.

When we pass shape of [-1], it flatten into 1D.

tf.reshape(c, [-1])

Output:

<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]])

To summarize, here we are flattening (i.e into 1D) weights of conv2 layer (i.e 2D) and extracting 0th element of weights['wd1'].

Upvotes: 1

Related Questions