Python keras.metrics.get() Examples

The following are 15 code examples of keras.metrics.get(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module keras.metrics , or try the search function .
Example #1
Source Project: dts   Author: albertogaspar   File: FFNN.py    License: MIT License 6 votes vote down vote up
def evaluate(self, inputs, fn_inverse=None, fn_plot=None):
        try:
            X, y = inputs
            inputs = X
        except:
            X, conditions, y = inputs
            inputs = [X, conditions]

        y_hat = self.predict(inputs)

        if fn_inverse is not None:
            y_hat = fn_inverse(y_hat)
            y = fn_inverse(y)

        if fn_plot is not None:
            fn_plot([y, y_hat])

        results = []
        for m in self.model.metrics:
            if isinstance(m, str):
                results.append(K.eval(K.mean(get(m)(y, y_hat))))
            else:
                results.append(K.eval(K.mean(m(y, y_hat))))
        return results 
Example #2
Source Project: dts   Author: albertogaspar   File: TCN.py    License: MIT License 6 votes vote down vote up
def evaluate(self, data, fn_inverse=None, fn_plot=None):
        try:
            X, y = data
            y_hat = self.predict(X)
        except:
            X, X_ex, y = data
            y_hat = self.predict([X, X_ex])

        if fn_inverse is not None:
            y = fn_inverse(y)
            y_hat = fn_inverse(y_hat)

        y = np.float32(y)
        y_hat = np.float32(y_hat)

        if fn_plot is not None:
            fn_plot([y, y_hat])

        results = []
        for m in self.model.metrics:
            if isinstance(m, str):
                results.append(K.eval(K.mean(get(m)(y, y_hat))))
            else:
                results.append(K.eval(K.mean(m(y, y_hat))))
        return results 
Example #3
Source Project: dts   Author: albertogaspar   File: FFNN.py    License: MIT License 5 votes vote down vote up
def __init__(self,
                 layers,
                 activation=None,
                 use_bias=True,
                 kernel_initializer='glorot_uniform',
                 bias_initializer='zeros',
                 kernel_regularizer=None,
                 bias_regularizer=None,
                 activity_regularizer=None,
                 kernel_constraint=None,
                 bias_constraint=None,
                 dropout=0.0,
                 recursive_forecast=False,
                 ):
        """
        Base FeedForward Neural Network.

        :param layers: list of integers. The i-th elem. of the list is the number of units of the i-th layer.
        :param dropout: An integer or tuple/list of a single integer, specifying the length
            of the 1D convolution window.
        :param recursive_forecast: an integer or tuple/list of a single integer, specifying the dilation rate
            to use for dilated convolution.
            Usually dilation rate increases exponentially with the depth of the network.
        :param for all the other parameters see keras.layers.Dense
        """
        self.layers = layers
        self.dropout = dropout
        self.activation = activations.get(activation)
        self.use_bias = use_bias
        self.kernel_initializer = initializers.get(kernel_initializer)
        self.bias_initializer = initializers.get(bias_initializer)
        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)
        self.kernel_constraint = constraints.get(kernel_constraint)
        self.bias_constraint = constraints.get(bias_constraint)
        self.recursive_forecast = recursive_forecast
        self.model = None
        self.horizon = None 
Example #4
Source Project: dts   Author: albertogaspar   File: Recurrent.py    License: MIT License 5 votes vote down vote up
def _eval(self, y, y_hat):
        results = []
        for m in self.model.metrics:
            if isinstance(m, str):
                results.append(K.eval(K.mean(get(m)(y, y_hat))))
            else:
                results.append(K.eval(K.mean(m(y, y_hat))))
        return results 
Example #5
Source Project: dts   Author: albertogaspar   File: Seq2Seq.py    License: MIT License 5 votes vote down vote up
def _build_encoder(self):
        """
        Build the encoder multilayer RNN (stacked RNN)
        """
        # Create a list of RNN Cells, these get stacked one after the other in the RNN,
        # implementing an efficient stacked RNN
        encoder_cells = []
        for n_hidden_neurons in self.encoder_layers:
            encoder_cells.append(self.cell(units=n_hidden_neurons,
                                           dropout=self.dropout,
                                           kernel_regularizer=l2(self.l2),
                                           recurrent_regularizer=l2(self.l2)))

        self.encoder = RNN(encoder_cells, return_state=True, name='encoder') 
Example #6
Source Project: dts   Author: albertogaspar   File: Seq2Seq.py    License: MIT License 5 votes vote down vote up
def evaluate(self, data, fn_inverse=None, horizon=1, fn_plot=None):
        """
        Evaluate model
        :return:
        """
        encoder_input_data, decoder_input_exog, y = data

        y_hat = self.predict(encoder_inputs=encoder_input_data,
                             pred_steps=horizon,
                             decoder_input_exog=decoder_input_exog)

        if fn_inverse is not None:
            y = fn_inverse(y)
            y_hat = fn_inverse(y_hat)

        y = np.float32(y)
        y_hat = np.float32(y_hat)

        if fn_plot is not None:
            fn_plot([y,y_hat])

        results = []
        for m in self.model.metrics:
            if isinstance(m, str):
                results.append(K.eval(K.mean(get(m)(y, y_hat))))
            else:
                results.append(K.eval(K.mean(m(y, y_hat))))
        return results 
Example #7
Source Project: DeepLearning_Wavelet-LSTM   Author: hello-sea   File: metrics_test.py    License: MIT License 5 votes vote down vote up
def test_invalid_get():

    with pytest.raises(ValueError):
        metrics.get(5) 
Example #8
Source Project: DeepLearning_Wavelet-LSTM   Author: hello-sea   File: metrics_test.py    License: MIT License 5 votes vote down vote up
def test_invalid_get():

    with pytest.raises(ValueError):
        metrics.get(5) 
Example #9
Source Project: DeepLearning_Wavelet-LSTM   Author: hello-sea   File: metrics_test.py    License: MIT License 5 votes vote down vote up
def test_invalid_get():

    with pytest.raises(ValueError):
        metrics.get(5) 
Example #10
Source Project: DeepLearning_Wavelet-LSTM   Author: hello-sea   File: metrics_test.py    License: MIT License 5 votes vote down vote up
def test_invalid_get():

    with pytest.raises(ValueError):
        metrics.get(5) 
Example #11
Source Project: DeepLearning_Wavelet-LSTM   Author: hello-sea   File: metrics_test.py    License: MIT License 5 votes vote down vote up
def test_invalid_get():

    with pytest.raises(ValueError):
        metrics.get(5) 
Example #12
Source Project: DeepLearning_Wavelet-LSTM   Author: hello-sea   File: metrics_test.py    License: MIT License 5 votes vote down vote up
def test_invalid_get():

    with pytest.raises(ValueError):
        metrics.get(5) 
Example #13
Source Project: DeepLearning_Wavelet-LSTM   Author: hello-sea   File: metrics_test.py    License: MIT License 5 votes vote down vote up
def test_invalid_get():

    with pytest.raises(ValueError):
        metrics.get(5) 
Example #14
Source Project: Benchmarks   Author: ECP-CANDLE   File: p1b3_baseline_keras2.py    License: MIT License 5 votes vote down vote up
def evaluate_keras_metric(y_true, y_pred, metric):
    objective_function = metrics.get(metric)
    objective = objective_function(y_true, y_pred)
    return K.eval(objective) 
Example #15
Source Project: Benchmarks   Author: ECP-CANDLE   File: p1b3_baseline_keras2.py    License: MIT License 5 votes vote down vote up
def on_epoch_end(self, batch, logs={}):
        val_loss, val_acc, y_true, y_pred, y_true_class, y_pred_class = evaluate_model(self.model, self.val_gen, self.val_steps, self.metric, self.category_cutoffs)
        test_loss, test_acc, _, _, _, _ = evaluate_model(self.model, self.test_gen, self.test_steps, self.metric, self.category_cutoffs)
        self.progbar.append_extra_log_values([('val_acc', val_acc), ('test_loss', test_loss), ('test_acc', test_acc)])
        if float(logs.get('val_loss', 0)) < self.best_val_loss:
            plot_error(y_true, y_pred, batch, self.ext, self.pre)
        self.best_val_loss = min(float(logs.get('val_loss', 0)), self.best_val_loss)
        self.best_val_acc = max(float(logs.get('val_acc', 0)), self.best_val_acc)