Пример #1
0
    def save_model(self, net):
        """Save the model.

        This function saves some or all of the following:

          - model parameters;
          - optimizer state;
          - criterion state;
          - training history;
          - custom modules;
          - entire model object.

        """
        kwargs_module, kwargs_other = _check_f_arguments(
            self.__class__.__name__, **self._f_kwargs())

        for key, val in kwargs_module.items():
            if val is None:
                continue

            f = self._format_target(net, val, -1)
            key = key[:-1]  # remove trailing '_'
            self._save_params(f, net, 'f_' + key, key + " state")

        f_history = kwargs_other.get('f_history')
        if f_history is not None:
            f = self.f_history_
            self._save_params(f, net, "f_history", "history")

        f_pickle = kwargs_other.get('f_pickle')
        if f_pickle:
            f_pickle = self._format_target(net, f_pickle, -1)
            with open_file_like(f_pickle, 'wb') as f:
                pickle.dump(net, f)
Пример #2
0
    def save_model(self, net):
        """Save the model.

        This function saves some or all of the following:

          - model parameters;
          - optimizer state;
          - training history;
          - entire model object.
        """
        if self.f_params is not None:
            f = self._format_target(net, self.f_params, -1)
            self._save_params(f, net, "f_params", "model parameters")

        if self.f_optimizer is not None:
            f = self._format_target(net, self.f_optimizer, -1)
            self._save_params(f, net, "f_optimizer", "optimizer state")

        if self.f_history is not None:
            f = self.f_history_
            self._save_params(f, net, "f_history", "history")

        if self.f_pickle:
            f_pickle = self._format_target(net, self.f_pickle, -1)
            with open_file_like(f_pickle, 'wb') as f:
                pickle.dump(net, f)
Пример #3
0
    def save_history(self, f):
        """Saves the history of ``NeuralNet`` as a json file. In order
        to use this feature, the history must only contain JSON encodable
        Python data structures. Numpy and PyTorch types should not
        be in the history.

        Parameters
        ----------
        f : file-like object or str

        Examples
        --------

        >>> before = NeuralNetClassifier(mymodule)
        >>> before.fit(X, y, epoch=2) # Train for 2 epochs
        >>> before.save_params('path/to/params')
        >>> before.save_history('path/to/history.json')
        >>> after = NeuralNetClassifier(mymodule).initialize()
        >>> after.load_params('path/to/params')
        >>> after.load_history('path/to/history.json')
        >>> after.fit(X, y, epoch=2) # Train for another 2 epochs

        """
        with open_file_like(f, 'w') as fp:
            json.dump(self.history.to_list(), fp)
Пример #4
0
    def from_file(cls, f):
        """Load the history of a ``NeuralNet`` from a json file.

        Parameters
        ----------
        f : file-like object or str

        """

        with open_file_like(f, 'r') as fp:
            return cls(json.load(fp))
Пример #5
0
    def load_history(self, f):
        """Load the history of a ``NeuralNet`` from a json file. See
        ``save_history`` for examples.

        Parameters
        ----------
        f : file-like object or str

        """
        with open_file_like(f, 'r') as fp:
            self.history = History(json.load(fp))
Пример #6
0
    def to_file(self, f):
        """Saves the history as a json file. In order to use this feature,
        the history must only contain JSON encodable Python data structures.
        Numpy and PyTorch types should not be in the history.

        Parameters
        ----------
        f : file-like object or str

        """
        with open_file_like(f, 'w') as fp:
            json.dump(self.to_list(), fp)
Пример #7
0
    def save_model(self, net):
        """Save the model.

        This function saves some or all of the following:

          - model parameters;
          - training history;
          - entire model object.
        """
        if self.f_params:
            net.save_params(self._format_target(net, self.f_params))
        if self.f_history:
            net.save_history(self._format_target(net, self.f_history))
        if self.f_pickle:
            f_pickle = self._format_target(net, self.f_pickle)
            with open_file_like(f_pickle, 'wb') as f:
                pickle.dump(net, f)