以类似“单元”的方式使用标准的tensorflow层

时间:2018-07-17 08:14:59

标签: python tensorflow lstm rnn language-features

我的问题与this question *有关。

是否可以将标准张量流层转换为“细胞”,并与RNN cells一起使用以构成递归神经网络?

因此,新的“单元格”应存储参数(权重,...),并可以在各种输入上调用。像这样:

from tf.nn import batch_normalization, conv2d
from tf.contrib.rnn import MultiRNNCell, LSTMCell

bn_cell = cell_creation_fun(batch_normalization, otherparams) # batch norm cell
conv_cell = cell_creation_fun(conv2d, otherparams )           # non-rnn conv cell
# or `conv_cell = cell_creation_fun(tf.layers.Conv2D, otherparams )` # using tf.layers  

以便可以像这样使用它们:

multi_cell = MultiRNNCell([LSTMCell(...), conv_cell, bn_cell])

或者这样:

h = ...
conv_h, _ = conv_cell(h, state=None)
normed_h, _ = bn_cell(h, state=None)

我唯一想到的就是为我要使用的每一层手动编写这样的“单元格”,并将其RNNCell子类化。但是,使用现有功能(例如Conv2D)在创建过程中不能传递“ input”参数似乎并不简单。 (当我管理时,将发布邮政编码。)


*也许以更有针对性的方式提问有可能得到答案。

1 个答案:

答案 0 :(得分: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 !'''
        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)
print("Yippee!")