【问题标题】:How to use `tf.nn.dynamic_rnn` with non rnn component如何将 `tf.nn.dynamic_rnn` 与非 rnn 组件一起使用
【发布时间】:2018-11-20 22:52:28
【问题描述】:

我有一个在输入 RNN 之前使用编码器的架构。编码器输入形状为[batch, height, width, channels],RNN 输入形状为[batch, time, height, width, channels]。我想将编码器的输出直接提供给 RNN,但这会带来内存问题。我必须一次将batch*time ~= 3*100(通过重塑)图像输入编码器。我知道tf.nn.dynamic_rnn 可以利用swap_memory,我也想在编码器中利用它。这是一些精简的代码:

#image inputs [batch, time, height, width, channels]
inputs = tf.placeholder(tf.float32, [batch, time, in_sh[0], in_sh[1], in_sh[2]])

#This is where the trouble starts
#merge batch and time
inputs = tf.reshape(inputs, [batch*time, in_sh[0], in_sh[1], in_sh[2]])
#build the encoder (and get shape of output)
enc, enc_sh = build_encoder(inputs)
#change back to time format
enc = tf.reshape(enc, [batch, time, enc_sh[0], enc_sh[1], enc_sh[2]])

#build rnn and get initial state (zero_state)
rnn, initial_state = build_rnn()
#use dynamic unrolling
rnn_outputs, rnn_state = tf.nn.dynamic_rnn(
        rnn, enc,
        initial_state=initial_state,
        swap_memory=True,
        time_major=False)

我目前使用的方法是先验地在我的所有图像上运行编码器(并保存到磁盘),但我想执行数据集扩充(到图像),这在提取特征后是不可能的。

【问题讨论】:

  • 为什么投反对票? (真的很想知道,这样我就可以改进我未来的问题)。

标签: python tensorflow recurrent-neural-network


【解决方案1】:

对于遇到此问题的其他人。我制作了一个源自RNNCell 的包装器,可以满足我的需要。 model_fn 是一个使用输入构建子图并返回输出张量的函数。不幸的是,必须知道输出形状(至少我不能让它工作)。

class WrapperCell(tf.nn.rnn_cell.RNNCell):
    """A Wrapper for a non recurrent component that feeds into an RNN."""

    def __init__(self, model_fn, out_shape, reuse=None):
        super(WrapperCell, self).__init__(_reuse=reuse)
        self.out_shape = out_shape
        self.model_fn = model_fn

    @property
    def state_size(self):
        return tf.TensorShape([1])

    @property
    def output_size(self):
        return tf.TensorShape(self.out_shape)

    def call(self, x, h):
        mod = self.model_fn(x)
        return mod, tf.zeros_like(h)

【讨论】:

    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 2023-03-17
    • 2017-01-26
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多