danche
danche

Reputation: 1815

Get the layer shape when construct Graph in TensoFlow?

when I used

    concat = tf.concat([query_rep, title_rep, cos_similarity], axis=1)
    print(concat.shape[1].value)
    # query_rep + title_rep + cos_similarity
    hidden_size = concat.shape[1]

I found I can't get the concat shape, it will return None. I has to specifically assign a value to hidden_size, e.g. hidden_size=201. How can I do to get the shape automatically?

In addition, for my CNN networks, I want to padding the input sequence in each batch rather than in whole dataset. so I have to make the max_len a placeholder, but then I find that a placeholder can not serve as another placeholder's parameters. e.g. following codes do not work

    self.max_len = tf.placeholder(int32)
    self.query_holder = tf.placeholder(tf.int32, shape=[None, self.max_len])

how can achieve this?

Upvotes: 0

Views: 180

Answers (1)

kafman
kafman

Reputation: 2860

There are two "kinds" of shapes: the static shape that can be inferred at compile time and the dynamic shape which is only known during runtime. To get the static shape you can call my_tensor.get_shape() on a tensor, to access the dynamic shape you can call tf.shape(my_tensor). If get_shape() returns None then the shape can only be known dynamically. If you have additional information about the shape you can set the shape using my_tensor.set_shape().

For your second question, why don't you use

self.query_holder = tf.placeholder(tf.int32, shape=[None, None])

This way both dimensions are variable.

Upvotes: 1

Related Questions