首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >以“单元”的方式使用标准的tensorflow层

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

Stack Overflow用户
提问于 2018-07-17 08:14:59
回答 1查看 181关注 0票数 0

我的问题与这个问题*有关。

是否有可能将标准的tensorflow层转换为“cell”,与RNN细胞一起组成递归神经网络?

因此,新的“单元”应该存储参数(权重,.),并且能够在可变输入上调用。就像这样:

代码语言:javascript
运行
复制
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  

这样他们就可以这样使用了:

代码语言:javascript
运行
复制
multi_cell = MultiRNNCell([LSTMCell(...), conv_cell, bn_cell])

或者像这样:

代码语言:javascript
运行
复制
h = ...
conv_h, _ = conv_cell(h, state=None)
normed_h, _ = bn_cell(h, state=None)

我唯一能想到的就是手动为我想要使用的每一层编写这样的“单元格”,并对RNNCell进行子类化。但是,如果在创建过程中不能够传递“input”参数,那么使用Conv2D这样的现有函数似乎就不那么简单了。(将在我管理时发布代码。)

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

EN

回答 1

Stack Overflow用户

发布于 2018-07-20 09:39:15

好吧,到目前为止,我的情况如下:

代码语言:javascript
运行
复制
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循环中使用它时,它失败了;它抱怨将来自不同帧的变量组合在一起,类似于这里。也许克拉斯用变量() .?

使用情况:

代码语言:javascript
运行
复制
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!")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51376396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档