当前位置: 首页>>代码示例>>Python>>正文


Python cudnn_rnn_ops.CudnnGRU方法代码示例

本文整理汇总了Python中tensorflow.contrib.cudnn_rnn.python.ops.cudnn_rnn_ops.CudnnGRU方法的典型用法代码示例。如果您正苦于以下问题:Python cudnn_rnn_ops.CudnnGRU方法的具体用法?Python cudnn_rnn_ops.CudnnGRU怎么用?Python cudnn_rnn_ops.CudnnGRU使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tensorflow.contrib.cudnn_rnn.python.ops.cudnn_rnn_ops的用法示例。


在下文中一共展示了cudnn_rnn_ops.CudnnGRU方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: create_symbol

# 需要导入模块: from tensorflow.contrib.cudnn_rnn.python.ops import cudnn_rnn_ops [as 别名]
# 或者: from tensorflow.contrib.cudnn_rnn.python.ops.cudnn_rnn_ops import CudnnGRU [as 别名]
def create_symbol(X, num_classes=0, is_training=False, CUDNN=False, 
                  maxf=30000, edim=125, nhid=100, batchs=64):
    word_vectors = tf.contrib.layers.embed_sequence(X, vocab_size=maxf, embed_dim=edim)
    

    word_list = tf.unstack(word_vectors, axis=1)
    
    if not CUDNN:
        cell1 = tf.contrib.rnn.LSTMCell(nhid)
        cell2 = tf.contrib.rnn.GRUCell(nhid)
        stacked_cell = tf.nn.rnn_cell.MultiRNNCell([cell1, cell2])
        outputs, states = tf.nn.static_rnn(stacked_cell, word_list, dtype=tf.float32)
        logits = tf.layers.dense(outputs[-1], 2, activation=None, name='output')
    else:
        # Using cuDNN since vanilla RNN
        from tensorflow.contrib.cudnn_rnn.python.ops import cudnn_rnn_ops
        cudnn_cell = cudnn_rnn_ops.CudnnGRU(num_layers=1, 
                                            num_units=nhid, 
                                            input_size=edim, 
                                            input_mode='linear_input')
        params_size_t = cudnn_cell.params_size()
        params = tf.Variable(tf.random_uniform([params_size_t], -0.1, 0.1), validate_shape=False)   
        input_h = tf.Variable(tf.zeros([1, batchs, nhid]))
        outputs, states = cudnn_cell(input_data=word_list,
                                     input_h=input_h,
                                     params=params)
        logits = tf.layers.dense(outputs[-1], 2, activation=None, name='output')
    
    return logits, logits 
开发者ID:microsoft,项目名称:MMdnn,代码行数:31,代码来源:test_rnn.py

示例2: build

# 需要导入模块: from tensorflow.contrib.cudnn_rnn.python.ops import cudnn_rnn_ops [as 别名]
# 或者: from tensorflow.contrib.cudnn_rnn.python.ops.cudnn_rnn_ops import CudnnGRU [as 别名]
def build(self, input_shape):
        super(CuDNNGRU, self).build(input_shape)
        if isinstance(input_shape, list):
            input_shape = input_shape[0]
        input_dim = input_shape[-1]

        from tensorflow.contrib.cudnn_rnn.python.ops import cudnn_rnn_ops
        self._cudnn_gru = cudnn_rnn_ops.CudnnGRU(
            num_layers=1,
            num_units=self.units,
            input_size=input_dim,
            input_mode='linear_input')

        self.kernel = self.add_weight(shape=(input_dim, self.units * 3),
                                      name='kernel',
                                      initializer=self.kernel_initializer,
                                      regularizer=self.kernel_regularizer,
                                      constraint=self.kernel_constraint)
        self.recurrent_kernel = self.add_weight(
            shape=(self.units, self.units * 3),
            name='recurrent_kernel',
            initializer=self.recurrent_initializer,
            regularizer=self.recurrent_regularizer,
            constraint=self.recurrent_constraint)

        self.bias = self.add_weight(shape=(self.units * 6,),
                                    name='bias',
                                    initializer=self.bias_initializer,
                                    regularizer=self.bias_regularizer,
                                    constraint=self.bias_constraint)

        self.kernel_z = self.kernel[:, :self.units]
        self.recurrent_kernel_z = self.recurrent_kernel[:, :self.units]
        self.kernel_r = self.kernel[:, self.units: self.units * 2]
        self.recurrent_kernel_r = self.recurrent_kernel[:,
                                                        self.units:
                                                        self.units * 2]
        self.kernel_h = self.kernel[:, self.units * 2:]
        self.recurrent_kernel_h = self.recurrent_kernel[:, self.units * 2:]

        self.bias_z_i = self.bias[:self.units]
        self.bias_r_i = self.bias[self.units: self.units * 2]
        self.bias_h_i = self.bias[self.units * 2: self.units * 3]
        self.bias_z = self.bias[self.units * 3: self.units * 4]
        self.bias_r = self.bias[self.units * 4: self.units * 5]
        self.bias_h = self.bias[self.units * 5:]

        self.built = True 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:50,代码来源:cudnn_recurrent.py


注:本文中的tensorflow.contrib.cudnn_rnn.python.ops.cudnn_rnn_ops.CudnnGRU方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。