Exemple #1
0
    def savemodel(self, nameprefix):
        """ Save the trained model into files.

        Given the prefix of the file paths, save the model into files, with name given by the prefix.
        There will be give files produced, one name ending with "_classlabels.txt", one with ".json",
        one with ".h5", one with "_labelidx.pkl", and one with "_dictionary.dict".

        If there is no trained model, a `ModelNotTrainedException` will be thrown.

        :param nameprefix: prefix of the file path
        :return: None
        :type nameprefix: str
        :raise: ModelNotTrainedException
        """
        if not self.trained:
            raise e.ModelNotTrainedException()

        kerasio.save_model(nameprefix, self.model)

        self.dictionary.save(nameprefix+'_dictionary.dict')

        labelfile = open(nameprefix+'_classlabels.txt', 'w')
        labelfile.write('\n'.join(self.classlabels))
        labelfile.close()

        pickle.dump(self.labels2idx, open(nameprefix+'_labelidx.pkl', 'wb'))
    def savemodel(self, nameprefix):
        """ Save the trained model into files.

        Given the prefix of the file paths, save the model into files, with name given by the prefix.
        There will be three files produced, one name ending with "_classlabels.txt", one name
        ending with ".json", and one name ending with ".h5". For shorttext>=0.4.0, another file
        with extension "_config.json" would be created.

        If there is no trained model, a `ModelNotTrainedException` will be thrown.

        :param nameprefix: prefix of the file path
        :return: None
        :type nameprefix: str
        :raise: ModelNotTrainedException
        """
        if not self.trained:
            raise e.ModelNotTrainedException()
        kerasio.save_model(nameprefix, self.model)
        labelfile = open(nameprefix + '_classlabels.txt', 'w')
        labelfile.write('\n'.join(self.classlabels))
        labelfile.close()
        json.dump(
            {
                'with_gensim': False,
                'maxlen': self.maxlen,
                'vecsize': self.vecsize
            }, open(nameprefix + '_config.json', 'w'))
    def savemodel(self, prefix):
        """ Save the model.

        :param prefix: prefix of the model path
        :return: None
        :type prefix: str
        """
        if not self.trained:
            raise ce.ModelNotTrainedException()
        kerasio.save_model(prefix, self.model)
        self.dictionary.save(prefix + '_vocabs.gensimdict')
        parameters = {
            'alph': self.alph,
            'special_signals': self.specialsignals,
            'operation': self.operation,
            'batchsize': self.batchsize,
            'nb_hiddenunits': self.nb_hiddenunits
        }
        json.dump(parameters, open(prefix + '_config.json', 'w'))
Exemple #4
0
    def savemodel(self, nameprefix):
        """ Save the logistic stacked model into files.

        Save the stacked model into files. Note that the intermediate classifiers
        are not saved. Users are advised to save those classifiers separately.

        If neither :func:`~train` nor :func:`~loadmodel` was run, it will raise `ModelNotTrainedException`.

        :param nameprefix: prefix of the files
        :return: None
        :raise: ModelNotTrainedException
        :type nameprefix: str
        """
        if not self.trained:
            raise e.ModelNotTrainedException()

        stackedmodeldict = {'classifiers': self.classifier2idx,
                            'classlabels': self.classlabels}
        pickle.dump(stackedmodeldict, open(nameprefix+'_stackedlogistics.pkl', 'wb'))
        kerasio.save_model(nameprefix+'_stackedlogistics', self.model)
    def savemodel(self, nameprefix, save_complete_autoencoder=True):
        """ Save the model with names according to the prefix.

        Given the prefix of the file paths, save the model into files, with name given by the prefix.
        There are files with names ending with "_encoder.json" and "_encoder.h5", which are
        the JSON and HDF5 files for the encoder respectively. They also include a gensim dictionary (.gensimdict).

        If `save_complete_autoencoder` is True,
        then there are also files with names ending with "_decoder.json" and "_decoder.h5".

        If neither :func:`~train` nor :func:`~loadmodel` was run, it will raise `ModelNotTrainedException`.

        :param nameprefix: prefix of the paths of the file
        :param save_complete_autoencoder: whether to store the decoder and the complete autoencoder (Default: True; but False for version <= 0.2.1)
        :return: None
        :type nameprefix: str
        :type save_complete_autoencoder: bool
        """
        if not self.trained:
            raise e.ModelNotTrainedException()

        parameters = {}
        parameters['nb_topics'] = self.nb_topics
        parameters['classlabels'] = self.classlabels
        json.dump(parameters, open(nameprefix + '.json', 'wb'))

        self.dictionary.save(nameprefix + '.gensimdict')
        kerasio.save_model(nameprefix + '_encoder', self.encoder)
        if save_complete_autoencoder:
            kerasio.save_model(nameprefix + '_decoder', self.decoder)
            kerasio.save_model(nameprefix + '_autoencoder', self.autoencoder)
        pickle.dump(self.classtopicvecs,
                    open(nameprefix + '_classtopicvecs.pkl', 'w'))