简体   繁体   中英

Get shape of unknown tensor in Tensorflow

I am trying to implement simple Q-Network for OpenAI gym. I've got placehoder for state. State is represented as integer. I want a one-hot vector. So, I do this:

input_state = tf.placeholder(tf.int64, shape=(None))
state_oh = tf.one_hot(input_state, env.observation_space.n)

I am using (None ) except () becouse I want to pass batch to train network.

I expected, that state_oh has shape like (None, 16) , but I got <unknown> . That is a problem for me, becouse I implement function to create fully-connected layer, which determine input tensor's shape using tensor.shape :

def dense(x, output_size, activation, name=None):  
with tf.name_scope(name, "dense", [x]):       

    w = tf.Variable(tf.random_normal([input_size, output_size]), name="w")
    b = tf.Variable(tf.random_normal([1, output_size]), name="b")
    layer = tf.matmul(x, w) + b
    layer_act = activation(layer)

    return layer_act

This isn't work with <unknown> shape.

How can I pass batch of Integer to Tensorflow and get it's second dimension (length of one-hot vector)? I prefer don't pass input's size to dense() explicitly.

I found out, that if I define my placeholder like this:

input_state = tf.placeholder(tf.int64, shape=[None], name="input_state")

I made a very silly mistake. Correct shape is [None] instead (None) , becouse (None) is equivalent to None , which means "any shape".

With correct shape of placeholder, the shape of state_oh will be (?, 16) as an expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM