當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。