Esempio n. 1
0
 def __init__(self,
              embedding_matrix=None,
              backwards=True,
              dropout=0.4,
              optimizer="rmsprop",
              lstm_out_width=20,
              lstm_pool_size=128,
              learn_rate=1.0,
              verbose=0,
              batch_size=32,
              epochs=35,
              shuffle=False,
              class_weight=30.0):
     """Initialize the LSTM pool model."""
     super(LSTMPoolClassifier, self).__init__()
     self.embedding_matrix = embedding_matrix
     self.backwards = backwards
     self.dropout = dropout
     self.optimizer = optimizer
     self.lstm_out_width = lstm_out_width
     self.learn_rate = learn_rate
     self.verbose = verbose
     self.batch_size = batch_size
     self.epochs = epochs
     self.shuffle = shuffle
     self.class_weight = _set_class_weight(class_weight)
     self.lstm_pool_size = lstm_pool_size
     self._model = None
     self.sequence_length = None
Esempio n. 2
0
    def __init__(self, C=1.0, class_weight=1.0, random_state=None, n_jobs=1):
        """Initialize the SKLearn Naive Bayes model.

        Arguments:
        ----------
        C: float
            Parameter inverse to the regularization strength of the model.
        class_weight: float
            Class weight of the inclusions.
        random_state: int, RandomState
            Random state for the model.
        n_jobs: int
            Number of CPU cores used.
        """
        super(LogisticModel, self).__init__()
        self.C = C
        self.class_weight = class_weight
        self.n_jobs = n_jobs

        self._model = LogisticRegression(
            solver="liblinear",
            C=C,
            class_weight=_set_class_weight(class_weight),
            n_jobs=n_jobs,
            random_state=random_state)
        logging.debug(self._model)
Esempio n. 3
0
    def __init__(self,
                 gamma="auto",
                 class_weight=0.249,
                 C=15.4,
                 kernel="linear",
                 random_state=None):
        """Initialize the SKLearn SVM model.

        Arguments
        ---------
        gamma: str
            Gamma parameter of the SVM model.
        class_weight: float
            class_weight of the inclusions.
        C: float
            C parameter of the SVM model.
        kernel: str
            SVM kernel type.
        random_state: int, RandomState
            State of the RNG.
        """
        super(SVMModel, self).__init__()
        self.gamma = gamma
        self.class_weight = class_weight
        self.C = C
        self.kernel = kernel
        self._random_state = random_state

        self._model = SVC(kernel=kernel,
                          C=C,
                          class_weight=_set_class_weight(class_weight),
                          random_state=random_state,
                          gamma=gamma,
                          probability=True)
Esempio n. 4
0
    def fit(self, X, y):

        # check is tensorflow is available
        _check_tensorflow()

        sequence_length = X.shape[1]
        if self._model is None or sequence_length != self.sequence_length:
            self.sequence_length = sequence_length
            keras_model = _create_lstm_base_model(
                embedding_matrix=self.embedding_matrix,
                backwards=self.backwards,
                dropout=self.dropout,
                optimizer=self.optimizer,
                max_sequence_length=self.sequence_length,
                lstm_out_width=self.lstm_out_width,
                dense_width=self.dense_width,
                learn_rate=self.learn_rate,
                verbose=self.verbose)
            print(keras_model)
            self._model = KerasClassifier(keras_model, verbose=self.verbose)

        self._model.fit(
            X,
            y,
            batch_size=self.batch_size,
            epochs=self.epochs,
            shuffle=self.shuffle,
            class_weight=_set_class_weight(self.class_weight),
            verbose=self.verbose)
Esempio n. 5
0
def lstm_fit_defaults(settings, verbose=1):
    """ Set the fit defaults and merge them with custom settings. """

    # arguments to pass to the fit
    fit_kwargs = {}
    fit_kwargs['batch_size'] = 32
    fit_kwargs['epochs'] = 10
    fit_kwargs['verbose'] = verbose
    fit_kwargs['shuffle'] = False
    fit_kwargs['class_weight_inc'] = 30.0

    settings.fit_kwargs = _unsafe_dict_update(fit_kwargs, settings.fit_param)

    _set_class_weight(fit_kwargs.pop('class_weight_inc'), fit_kwargs)

    return settings.fit_kwargs
Esempio n. 6
0
    def __init__(self, n_estimators=100, max_features=10, class_weight=1.0,
                 random_state=None):
        """Initialize the SKLearn Random Forest model.

        Arguments
        ---------
        n_estimators: int
            Number of estimators.
        max_features: int
            Number of features in the model.
        class_weight: float
            Class weight of the inclusions.
        random_state: int, RandomState
            Set the random state of the RNG.
        """
        super(RFModel, self).__init__()
        self.n_estimators = int(n_estimators)
        self.max_features = int(max_features)
        self.class_weight = class_weight
        self.random_state = random_state

        self._model = RandomForestClassifier(
            n_estimators=self.n_estimators, max_features=self.max_features,
            class_weight=_set_class_weight(class_weight),
            random_state=random_state)
    def __init__(self,
                 embedding_matrix=None,
                 backwards=True,
                 dropout=0.4,
                 optimizer="rmsprop",
                 lstm_out_width=20,
                 lstm_pool_size=128,
                 learn_rate=1.0,
                 verbose=0,
                 batch_size=32,
                 epochs=35,
                 shuffle=False,
                 class_weight=30.0):
        """Initialize the LSTM pool model.

        Arguments
        ---------
        embedding_matrix: np.array
            Embedding matrix to use with LSTM model.
        backwards: bool
            Whether to have a forward or backward LSTM.
        dropout: float
            Value in [0, 1.0) that gives the dropout and recurrent
            dropout rate for the LSTM model.
        optimizer: str
            Optimizer to use.
        lstm_out_width: int
            Output width of the LSTM.
        lstm_pool_size: int
            Size of the pool, must be a divisor of max_sequence_length.
        learn_rate: float
            Learn rate multiplier of default learning rate.
        verbose: int
            Verbosity.
        batch_size: int
            Size of the batch size for the LSTM model.
        epochs: int
            Number of epochs to train the LSTM model.
        shuffle: bool
            Whether to shuffle the data before starting to train.
        class_weight: float
            Class weight for the included papers.
        """
        super(LSTMPoolModel, self).__init__()
        self.embedding_matrix = embedding_matrix
        self.backwards = backwards
        self.dropout = dropout
        self.optimizer = optimizer
        self.lstm_out_width = lstm_out_width
        self.learn_rate = learn_rate
        self.verbose = verbose
        self.batch_size = batch_size
        self.epochs = epochs
        self.shuffle = shuffle
        self.class_weight = _set_class_weight(class_weight)
        self.lstm_pool_size = lstm_pool_size
        self._model = None
        self.sequence_length = None
    def fit(self, X, y):
        if scipy.sparse.issparse(X):
            X = X.toarray()
        if self._model is None or X.shape[1] != self.input_dim:
            self.input_dim = X.shape[1]
            keras_model = _create_dense_nn_model(
                self.input_dim, self.dense_width, self.optimizer,
                self.learn_rate, self.regularization, self.verbose)
            self._model = KerasClassifier(keras_model, verbose=self.verbose)

        self._model.fit(X, y, batch_size=self.batch_size, epochs=self.epochs,
                        shuffle=self.shuffle, verbose=self.verbose,
                        class_weight=_set_class_weight(self.class_weight))
Esempio n. 9
0
    def __init__(self, C=1.0, class_weight=1.0, random_state=None, n_jobs=1):

        super(LogisticClassifier, self).__init__()
        self.C = C
        self.class_weight = class_weight
        self.n_jobs = n_jobs

        self._model = LogisticRegression(
            solver="liblinear",
            C=C,
            class_weight=_set_class_weight(class_weight),
            n_jobs=n_jobs,
            random_state=random_state)
        logging.debug(self._model)
Esempio n. 10
0
    def __init__(self,
                 n_estimators=100,
                 max_features=10,
                 class_weight=1.0,
                 random_state=None):

        super(RandomForestClassifier, self).__init__()
        self.n_estimators = int(n_estimators)
        self.max_features = int(max_features)
        self.class_weight = class_weight
        self._random_state = random_state

        self._model = SKRandomForestClassifier(
            n_estimators=self.n_estimators,
            max_features=self.max_features,
            class_weight=_set_class_weight(class_weight),
            random_state=random_state)
Esempio n. 11
0
    def __init__(self, C=1.0, class_weight=1.0, n_jobs=1):
        """Initialize the SKLearn Naive Bayes model.

        Arguments:
        ----------
        alpha: float
            Parameter to set the regularization strength of the model.
        """
        super(LogisticModel, self).__init__()
        self.C = C
        self.class_weight = class_weight
        self.n_jobs = n_jobs

        self._model = LogisticRegression(
            solver="liblinear", C=C,
            class_weight=_set_class_weight(class_weight),
            n_jobs=n_jobs)
        logging.debug(self._model)
Esempio n. 12
0
    def __init__(self,
                 gamma="auto",
                 class_weight=0.249,
                 C=15.4,
                 kernel="linear",
                 random_state=None):
        super(SVMClassifier, self).__init__()
        self.gamma = gamma
        self.class_weight = class_weight
        self.C = C
        self.kernel = kernel
        self._random_state = random_state

        self._model = SVC(kernel=kernel,
                          C=C,
                          class_weight=_set_class_weight(class_weight),
                          random_state=random_state,
                          gamma=gamma,
                          probability=True)
Esempio n. 13
0
 def fit_kwargs(self):
     fit_kwargs = self.fit_param()
     class_weight_inc = fit_kwargs.pop('class_weight_inc', None)
     if class_weight_inc is not None:
         fit_kwargs['class_weight'] = _set_class_weight(class_weight_inc)
     return fit_kwargs