在Tensorflow中混合前馈层和复发层?

时间:2016-04-05 15:23:49

标签: python tensorflow recurrent-neural-network gated-recurrent-unit

有没有人能够在Tensorflow中混合前馈层和重复层?

例如: 输入 - > conv-> GRU->直链>输出

我可以想象,可以使用前馈层定义自己的单元格,然后无法使用MultiRNNCell函数堆叠状态,如:

cell = tf.nn.rnn_cell.MultiRNNCell([conv_cell,GRU_cell,linear_cell])

这会让生活变得更轻松......

3 个答案:

答案 0 :(得分:0)

你不能只做以下事情:

rnnouts, _ = rnn(grucell, inputs)
linearout = [tf.matmul(rnnout, weights) + bias for rnnout in rnnouts]

答案 1 :(得分:0)

This tutoria l给出了如何将卷积层与循环层一起使用的示例。例如,最后一个卷积层如下:

...
l_conv4_a = conv_pre(l_pool3, 16, (5, 5), scope="l_conv4_a")
l_pool4 = pool(l_conv3_a, scope="l_pool4")
l_flatten = flatten(l_pool4, scope="flatten")

并定义了RNN单元格:

_, shape_state = tf.nn.dynamic_rnn(cell=shape_cell,
    inputs=tf.expand_dims(batch_norm(x_shape_pl), 2), dtype=tf.float32, scope="shape_rnn")

您可以连接两个输出并将其用作下一层的输入:

features = tf.concat(concat_dim=1, values=[x_margin_pl, shape_state, x_texture_pl, l_flatten], name="features")

或者您可以使用CNN层的输出作为RNN单元的输入:

_, shape_state = tf.nn.dynamic_rnn(cell=shape_cell,
    inputs=l_flatten, dtype=tf.float32, scope="shape_rnn")

答案 2 :(得分:0)

这是我到目前为止所拥有的;欢迎改进:

class LayerCell(rnn_cell_impl.RNNCell):

    def __init__(self, tf_layer, **kwargs):
        ''' :param tf_layer: a tensorflow layer, e.g. tf.layers.Conv2D or 
            tf.keras.layers.Conv2D. NOT tf.layers.conv2d !
            Can pass all other layer params as well, just need to give the 
            parameter name: paramname=param'''
        self.layer_fn = tf_layer(**kwargs)

    def __call__(self, inputs, state, scope=None):
        ''' Every `RNNCell` must implement `call` with
          the signature `(output, next_state) = call(input, state)`.  The optional
          third input argument, `scope`, is allowed for backwards compatibility
          purposes; but should be left off for new subclasses.'''
        return (self.layer_fn(inputs), state)

    def __str__(self):
            return "Cell wrapper of " + str(self.layer_fn)

    def __getattr__(self, attr):
        '''credits to https://stackoverflow.com/questions/1382871/dynamically-attaching-a-method-to-an-existing-python-object-generated-with-swig/1383646#1383646'''
        return getattr(self.layer_fn, attr)

    @property
    def state_size(self):
        """size(s) of state(s) used by this cell.

        It can be represented by an Integer, a TensorShape or a tuple of Integers
        or TensorShapes.
        """
        return  (0,) 

    @property
    def output_size(self):
        """Integer or TensorShape: size of outputs produced by this cell."""
        # use with caution; could be uninitialized
        return self.layer_fn.output_shape

(自然,不要与循环图层一起使用,因为状态保存将被破坏。)

似乎可以使用:tf.layers.Conv2D,tf.keras.layers.Conv2D,tf.keras.layers.Activation,tf.layers.BatchNormalization

不适用于:tf.keras.layers.BatchNormalization。 至少在tf.while循环中使用它对我来说失败了;抱怨合并来自不同框架的变量,类似于here。也许keras使用tf.Variable() instead of tf.get_variable() ...?


用法:

cell0 = tf.contrib.rnn.ConvLSTMCell(conv_ndims=2, input_shape=[40, 40, 3], output_channels=16, kernel_shape=[5, 5])
cell1 = LayerCell(tf.keras.layers.Conv2D, filters=8, kernel_size=[5, 5], strides=(1, 1), padding='same')
cell2 = LayerCell(tf.layers.BatchNormalization, axis=-1)

inputs =  np.random.rand(10, 40, 40, 3).astype(np.float32)
multicell = tf.contrib.rnn.MultiRNNCell([cell0, cell1, cell2])
state = multicell.zero_state(batch_size=10, dtype=tf.float32)

output = multicell(inputs, state)