简体   繁体   English

在Tensorflow中获取未知张量的形状

[英]Get shape of unknown tensor in Tensorflow

I am trying to implement simple Q-Network for OpenAI gym. 我正在尝试为OpenAI体育馆实现简单的Q网络。 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. 我正在使用(None )除了() )外,因为我想通过批处理来训练网络。

I expected, that state_oh has shape like (None, 16) , but I got <unknown> . 我期望state_oh形状像(None, 16) ,但是我得到了<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 : 这对我来说是个问题,因为我实现了功能来创建完全连接的层,该层使用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. 这不适用于<unknown>形状。

How can I pass batch of Integer to Tensorflow and get it's second dimension (length of one-hot vector)? 如何将一批Integer传递给Tensorflow并获取其第二维(一热向量的长度)? I prefer don't pass input's size to dense() explicitly. 我更喜欢不要将输入的大小显式传递给dense()

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". 正确的形状是[None]而不是(None) ,因为(None)等效于None ,表示“任何形状”。

With correct shape of placeholder, the shape of state_oh will be (?, 16) as an expected. 使用正确的占位符形状, state_oh的形状将是预期的(?, 16)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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