當前位置: 首頁>>代碼示例>>Python>>正文


Python wrappers.Bidirectional方法代碼示例

本文整理匯總了Python中keras.layers.wrappers.Bidirectional方法的典型用法代碼示例。如果您正苦於以下問題:Python wrappers.Bidirectional方法的具體用法?Python wrappers.Bidirectional怎麽用?Python wrappers.Bidirectional使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在keras.layers.wrappers的用法示例。


在下文中一共展示了wrappers.Bidirectional方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _build_sequence_model

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def _build_sequence_model(self, sequence_input):
        RNN = GRU if self._rnn_type == 'gru' else LSTM

        def rnn():
            rnn = RNN(units=self._rnn_output_size,
                      return_sequences=True,
                      dropout=self._dropout_rate,
                      recurrent_dropout=self._dropout_rate,
                      kernel_regularizer=self._regularizer,
                      kernel_initializer=self._initializer,
                      implementation=2)
            rnn = Bidirectional(rnn) if self._bidirectional_rnn else rnn
            return rnn

        input_ = sequence_input
        for _ in range(self._rnn_layers):
            input_ = BatchNormalization(axis=-1)(input_)
            rnn_out = rnn()(input_)
            input_ = rnn_out
        time_dist_dense = TimeDistributed(Dense(units=self._vocab_size))(rnn_out)

        return time_dist_dense 
開發者ID:danieljl,項目名稱:keras-image-captioning,代碼行數:24,代碼來源:models.py

示例2: change_trainable

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def change_trainable(layer, trainable, verbose=False):
    """ Helper method that fixes some of Keras' issues with wrappers and
        trainability. Freezes or unfreezes a given layer.

    # Arguments:
        layer: Layer to be modified.
        trainable: Whether the layer should be frozen or unfrozen.
        verbose: Verbosity flag.
    """

    layer.trainable = trainable

    if type(layer) == Bidirectional:
        layer.backward_layer.trainable = trainable
        layer.forward_layer.trainable = trainable

    if type(layer) == TimeDistributed:
        layer.backward_layer.trainable = trainable

    if verbose:
        action = 'Unfroze' if trainable else 'Froze'
        print("{} {}".format(action, layer.name)) 
開發者ID:bfelbo,項目名稱:DeepMoji,代碼行數:24,代碼來源:finetuning.py

示例3: create_model

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def create_model(self):
        features_length = self.parameters.pref['features_length']

        inputs = Input(shape=(None, features_length))
        x = inputs
        x = Masking(mask_value=0., input_shape=(None, features_length))(x)
        x = Bidirectional(
            GRU(self.parameters.pref['internal_neurons'], return_sequences=True, dropout=0.0, recurrent_dropout=0.5,
                implementation=1), input_shape=(None, features_length))(x)
        x = Bidirectional(
            GRU(self.parameters.pref['internal_neurons'], return_sequences=True, dropout=0.0, recurrent_dropout=0.5,
                implementation=1), input_shape=(None, features_length))(x)
        x = Dropout(0.5)(x)
        x = TimeDistributed(Dense(self.parameters.pref['output_length'], activation='softmax'))(x)

        self.model = Model(inputs=inputs, outputs=x)

        self.loss = 'categorical_crossentropy'
        self.optimizer = keras.optimizers.Nadam() 
開發者ID:kwrobel-nlp,項目名稱:krnnt,代碼行數:21,代碼來源:keras_models.py

示例4: test_tiny_no_sequence_bidir_random

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def test_tiny_no_sequence_bidir_random(
        self, model_precision=_MLMODEL_FULL_PRECISION
    ):
        np.random.seed(1988)
        input_dim = 1
        input_length = 1
        num_channels = 1
        num_samples = 1

        # Define a model
        model = Sequential()
        model.add(
            Bidirectional(
                LSTM(num_channels, implementation=1, recurrent_activation="sigmoid"),
                input_shape=(input_length, input_dim),
            )
        )

        # Set some random weights
        model.set_weights(
            [np.random.rand(*w.shape) * 0.2 - 0.1 for w in model.get_weights()]
        )

        # Test the keras model
        self._test_model(model, model_precision=model_precision) 
開發者ID:apple,項目名稱:coremltools,代碼行數:27,代碼來源:test_keras2_numeric.py

示例5: test_tiny_no_sequence_bidir_random_gpu

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def test_tiny_no_sequence_bidir_random_gpu(
        self, model_precision=_MLMODEL_FULL_PRECISION
    ):
        np.random.seed(1988)
        input_dim = 1
        input_length = 1
        num_channels = 1
        num_samples = 1

        # Define a model
        model = Sequential()
        model.add(
            Bidirectional(
                LSTM(num_channels, implementation=2, recurrent_activation="sigmoid"),
                input_shape=(input_length, input_dim),
            )
        )

        # Set some random weights
        model.set_weights(
            [np.random.rand(*w.shape) * 0.2 - 0.1 for w in model.get_weights()]
        )

        # Test the keras model
        self._test_model(model, model_precision=model_precision) 
開發者ID:apple,項目名稱:coremltools,代碼行數:27,代碼來源:test_keras2_numeric.py

示例6: test_small_no_sequence_bidir_random

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def test_small_no_sequence_bidir_random(self):
        np.random.seed(1988)
        input_dim = 10
        input_length = 1
        num_channels = 1

        # Define a model
        model = Sequential()
        model.add(
            Bidirectional(
                LSTM(num_channels, implementation=2, recurrent_activation="sigmoid"),
                input_shape=(input_length, input_dim),
            )
        )

        # Set some random weights
        model.set_weights(
            [np.random.rand(*w.shape) * 0.2 - 0.1 for w in model.get_weights()]
        )

        # Test the keras model
        self._test_model(model) 
開發者ID:apple,項目名稱:coremltools,代碼行數:24,代碼來源:test_keras2_numeric.py

示例7: test_medium_no_sequence_bidir_random

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def test_medium_no_sequence_bidir_random(self):
        np.random.seed(1988)
        input_dim = 10
        input_length = 1
        num_channels = 10

        # Define a model
        model = Sequential()
        model.add(
            Bidirectional(
                LSTM(num_channels, implementation=2, recurrent_activation="sigmoid"),
                input_shape=(input_length, input_dim),
            )
        )

        # Set some random weights
        model.set_weights(
            [np.random.rand(*w.shape) * 0.2 - 0.1 for w in model.get_weights()]
        )

        # Test the keras model
        self._test_model(model) 
開發者ID:apple,項目名稱:coremltools,代碼行數:24,代碼來源:test_keras2_numeric.py

示例8: test_Bidirectional_state_reuse

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def test_Bidirectional_state_reuse():
    rnn = layers.LSTM
    samples = 2
    dim = 5
    timesteps = 3
    units = 3

    input1 = Input((timesteps, dim))
    layer = wrappers.Bidirectional(rnn(units, return_state=True, return_sequences=True))
    state = layer(input1)[1:]

    # test passing invalid initial_state: passing a tensor
    input2 = Input((timesteps, dim))
    with pytest.raises(ValueError):
        output = wrappers.Bidirectional(rnn(units))(input2, initial_state=state[0])

    # test valid usage: passing a list
    output = wrappers.Bidirectional(rnn(units))(input2, initial_state=state)
    model = Model([input1, input2], output)
    assert len(model.layers) == 4
    assert isinstance(model.layers[-1].input, list)
    inputs = [np.random.rand(samples, timesteps, dim),
              np.random.rand(samples, timesteps, dim)]
    outputs = model.predict(inputs) 
開發者ID:hello-sea,項目名稱:DeepLearning_Wavelet-LSTM,代碼行數:26,代碼來源:wrappers_test.py

示例9: create

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def create(inputtokens, vocabsize, units=16, dropout=0, embedding=32):

        input_ = Input(shape=(inputtokens,), dtype='int32')

        # Embedding layer
        net = Embedding(input_dim=vocabsize, output_dim=embedding, input_length=inputtokens)(input_)
        net = Dropout(dropout)(net)

        # Bidirectional LSTM layer
        net = BatchNormalization()(net)
        net = Bidirectional(CuDNNLSTM(units))(net)
        net = Dropout(dropout)(net)

        # Output layer
        net = Dense(vocabsize, activation='softmax')(net)
        model = Model(inputs=input_, outputs=net)

        # Make data-parallel
        ngpus = len(get_available_gpus())
        if ngpus > 1:
            model = make_parallel(model, ngpus)

        return model 
開發者ID:albarji,項目名稱:neurowriter,代碼行數:25,代碼來源:models.py

示例10: bilstm_layer

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def bilstm_layer(input_layer, lstm_dims, rnn_layers, dropout):
    lstm = None
    if isinstance(lstm_dims, (list, tuple)):
        lstm_dims = lstm_dims
    else:
        assert isinstance(lstm_dims, int)
        lstm_dims = [lstm_dims] * rnn_layers
    for i in range(rnn_layers):
        if i == 0:
            nested = input_layer
        else:
            nested = lstm
        wrapped = LSTM(
            output_dim=lstm_dims[i], activation='tanh', return_sequences=True,
            dropout_W=dropout, dropout_U=dropout, name='bistm_%d' % i)
        lstm = Bidirectional(wrapped, merge_mode='sum')(nested)
    return lstm 
開發者ID:kootenpv,項目名稱:neural_complete,代碼行數:19,代碼來源:rnn_tagger.py

示例11: test_medium_bidir_random_return_seq_false

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def test_medium_bidir_random_return_seq_false(self):
        np.random.seed(1988)
        input_dim = 7
        input_length = 5
        num_channels = 10

        # Define a model
        model = Sequential()
        model.add(
            Bidirectional(
                LSTM(
                    num_channels,
                    return_sequences=False,
                    implementation=2,
                    recurrent_activation="sigmoid",
                ),
                input_shape=(input_length, input_dim),
            )
        )

        # Set some random weights
        model.set_weights(
            [np.random.rand(*w.shape) * 0.2 - 0.1 for w in model.get_weights()]
        )

        # Test the keras model
        self._test_model(model) 
開發者ID:apple,項目名稱:coremltools,代碼行數:29,代碼來源:test_keras2_numeric.py

示例12: test_bilstm_merge_modes

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def test_bilstm_merge_modes(self):
        # issue 157

        def get_model(input_dim, fc_size, rnn_size, output_dim, merge_mode):
            input_data = Input(name="the_input", shape=(None, input_dim))
            x = TimeDistributed(Dense(fc_size, name="fc1", activation="relu",))(
                input_data
            )
            x = Bidirectional(
                LSTM(
                    rnn_size,
                    return_sequences=True,
                    activation="relu",
                    kernel_initializer="he_normal",
                ),
                merge_mode=merge_mode,
            )(x)
            y_pred = TimeDistributed(
                Dense(output_dim, name="y_pred", activation="softmax")
            )(x)
            model = Model([input_data], [y_pred])
            model.set_weights(
                [np.random.rand(*w.shape) * 0.2 - 0.1 for w in model.get_weights()]
            )
            return model

        input_dim = 26
        fc_size = 512
        rnn_size = 512
        output_dim = 29
        for merge_mode in ["concat", "sum", "mul", "ave"]:
            model = get_model(input_dim, fc_size, rnn_size, output_dim, merge_mode)
            self._test_model(model) 
開發者ID:apple,項目名稱:coremltools,代碼行數:35,代碼來源:test_keras2_numeric.py

示例13: test_Bidirectional_dropout

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def test_Bidirectional_dropout(merge_mode):
    rnn = layers.LSTM
    samples = 2
    dim = 5
    timesteps = 3
    units = 3
    X = [np.random.rand(samples, timesteps, dim)]

    inputs = Input((timesteps, dim))
    wrapped = wrappers.Bidirectional(rnn(units, dropout=0.2, recurrent_dropout=0.2),
                                     merge_mode=merge_mode)
    outputs = _to_list(wrapped(inputs, training=True))
    assert all(not getattr(x, '_uses_learning_phase') for x in outputs)

    inputs = Input((timesteps, dim))
    wrapped = wrappers.Bidirectional(rnn(units, dropout=0.2, return_state=True),
                                     merge_mode=merge_mode)
    outputs = _to_list(wrapped(inputs))
    assert all(x._uses_learning_phase for x in outputs)

    model = Model(inputs, outputs)
    assert model.uses_learning_phase
    y1 = _to_list(model.predict(X))
    y2 = _to_list(model.predict(X))
    for x1, x2 in zip(y1, y2):
        assert_allclose(x1, x2, atol=1e-5) 
開發者ID:hello-sea,項目名稱:DeepLearning_Wavelet-LSTM,代碼行數:28,代碼來源:wrappers_test.py

示例14: test_Bidirectional_trainable

# 需要導入模塊: from keras.layers import wrappers [as 別名]
# 或者: from keras.layers.wrappers import Bidirectional [as 別名]
def test_Bidirectional_trainable():
    # test layers that need learning_phase to be set
    x = Input(shape=(3, 2))
    layer = wrappers.Bidirectional(layers.SimpleRNN(3))
    _ = layer(x)
    assert len(layer.trainable_weights) == 6
    layer.trainable = False
    assert len(layer.trainable_weights) == 0
    layer.trainable = True
    assert len(layer.trainable_weights) == 6 
開發者ID:hello-sea,項目名稱:DeepLearning_Wavelet-LSTM,代碼行數:12,代碼來源:wrappers_test.py


注:本文中的keras.layers.wrappers.Bidirectional方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。