2017-05-19 31 views
0

我想创建输入到具有一个RNN小区中确定需要创建一个变量,其形状是仅在运行时

  1. 输入张量尺寸batch_size x time_step x n_classes
  2. 初始隐藏状态张量hx尺寸batch_size x hidden_state_size

现在的问题是,我不确定如何使用tf.get_variable(...)创建h使得其具有的的形状其中?是当前批量大小。

x = tf.nn.embedding_lookup(self.pretrained_embeddings, self.input_placeholder) 
x = tf.reshape(x, (-1, self.max_length, Config.n_features * Config.embed_size)) 

所以batch_size自动从tf.reshape

推断所以x.get_shape()会给我(?, max_length, n_features * embed_size)

,因为我能够定义x喜欢,所以我就没有这个问题x

我必须创建的唯一解决方法h如下:

# x_ is of shape (max_length, batch_size, n_features * embed_size) 
x_ = tf.unstack(x, axis = 1) 

# h is of shape (batch_size, n_features) 
h = tf.get_variable("h", tf.shape(x_[0]), initializer = 
tf.constant_initializer(0.0)) 

然后h.get_shape()会得到期望的(?, n_features * embed_size)这偶然等于我hidden_state_size。此解决方法的问题是,只有在hidden_state_size等于n_features * embed_size时才有效,但并非总是如此。

有没有一种办法,这样我可以定义隐藏张量h,以便它可以有(?, hidden_state_size)的形状没有错误:

ValueError: Shape of a new variable (pred/h) must be fully defined, but instead was (?, 300)

回答

0

愚蠢的我,我可能只是做类似:

h = tf.zeros(shape = (tf.shape(x)[0], n_features * embed_size))