def loadmodel(self, nameprefix):
        """ Load a trained model from files.

        Given the prefix of the file paths, load the model from files with name given by the prefix
        followed by "_classlabels.txt", ".json" and ".h5". For shorttext>=0.4.0, a file with
        extension "_config.json" would also be used.

        If this has not been run, or a model was not trained by :func:`~train`,
        a `ModelNotTrainedException` will be raised while performing prediction or saving the model.

        :param nameprefix: prefix of the file path
        :return: None
        :type nameprefix: str
        """
        self.model = kerasio.load_model(nameprefix)
        labelfile = open(nameprefix + '_classlabels.txt', 'r')
        self.classlabels = labelfile.readlines()
        labelfile.close()
        self.classlabels = [s.strip() for s in self.classlabels]

        # check if _config.json exists.
        # This file does not exist if the model was created with shorttext<0.4.0
        if os.path.exists(nameprefix + '_config.json'):
            config = json.load(open(nameprefix + '_config.json', 'r'))
            # these fields are present for release >= 1.0.0
            if 'maxlen' in config:
                self.maxlen = config['maxlen']
            else:
                self.maxlen = 15
            if 'vecsize' in config:
                self.vecsize = config['vecsize']
            else:
                self.vecsize = self.wvmodel.vector_size
            if self.vecsize != self.wvmodel.vector_size:
                warnings.warn(
                    'Record vector size (%i) is not the same as that of the given word-embedding model (%i)! '
                    % (self.vecsize, self.wvmodel.vector_size) +
                    'Setting the vector size to be %i, but there may be run time error!'
                    % (self.wvmodel.vector_size), RuntimeWarning)
                self.vecsize = self.wvmodel.vector_size
        else:
            self.maxlen = 15
            self.vecsize = self.wvmodel.vector_size
            warnings.warn('Model files from old versions.')

        self.with_gensim = False
        self.trained = True
Exemple #2
0
    def loadmodel(self, nameprefix):
        """ Load the model with the given prefix.

        Load the model with the given prefix of their paths. Note that the intermediate
        classifiers are not loaded, and users are required to load them separately.

        :param nameprefix: prefix of the model files
        :return: None
        :type nameprefix: str
        """
        stackedmodeldict = pickle.load(open(nameprefix+'_stackedlogistics.pkl', 'rb'))
        self.register_classlabels(stackedmodeldict['classlabels'])
        self.classifier2idx = stackedmodeldict['classifiers']
        self.idx2classifier = {val: key for key, val in self.classifier2idx.items()}
        self.model = kerasio.load_model(nameprefix+'_stackedlogistics')

        self.trained = True
    def loadmodel(self, nameprefix):
        """ Load a trained model from files.

        Given the prefix of the file paths, load the model from files with name given by the prefix
        followed by "_classlabels.txt", ".json", and ".h5".

        If this has not been run, or a model was not trained by :func:`~train`,
        a `ModelNotTrainedException` will be raised while performing prediction and saving the model.

        :param nameprefix: prefix of the file path
        :return: None
        :type nameprefix: str
        """
        self.model = kerasio.load_model(nameprefix)
        labelfile = open(nameprefix+'_classlabels.txt', 'r')
        self.classlabels = labelfile.readlines()
        labelfile.close()
        self.classlabels = map(lambda s: s.strip(), self.classlabels)
        self.trained = True
    def loadmodel(self, prefix):
        """ Load the model.

        :param prefix: prefix of the model path
        :return: None
        :type prefix: str
        """
        self.dictionary = Dictionary.load(prefix + '_vocabs.gensimdict')
        parameters = json.load(open(prefix + '_config.json', 'r'))
        self.operation = parameters['operation']
        self.alph = parameters['alph']
        self.specialsignals = parameters['special_signals']
        self.binarizer = SCRNNBinarizer(self.alph, self.specialsignals)
        self.concatcharvec_encoder = SpellingToConcatCharVecEncoder(self.alph)
        self.batchsize = parameters['batchsize']
        self.nb_hiddenunits = parameters['nb_hiddenunits']
        self.onehotencoder = OneHotEncoder()
        self.onehotencoder.fit(
            np.arange(len(self.dictionary)).reshape((len(self.dictionary), 1)))
        self.model = kerasio.load_model(prefix)
        self.trained = True
    def loadmodel(self, nameprefix):
        """ Load a trained model from files.

        Given the prefix of the file paths, load the model from files with name given by the prefix
        followed by "_classlabels.txt", ".json" and ".h5". For shorttext>=0.4.0, a file with
        extension "_config.json" would also be used.

        If this has not been run, or a model was not trained by :func:`~train`,
        a `ModelNotTrainedException` will be raised while performing prediction or saving the model.

        :param nameprefix: prefix of the file path
        :return: None
        :type nameprefix: str
        """
        self.model = kerasio.load_model(nameprefix)
        labelfile = open(nameprefix + '_classlabels.txt', 'r')
        self.classlabels = labelfile.readlines()
        labelfile.close()
        self.classlabels = map(lambda s: s.strip(), self.classlabels)
        # check if _config.json exists.
        # This file does not exist if the model was created with shorttext<0.4.0
        self.with_gensim = False
        self.trained = True
Exemple #6
0
    def loadmodel(self, nameprefix):
        """ Load a trained model from files.

        Given the prefix of the file paths, load the model from files with name given by the prefix
        followed by "_classlabels.txt", ".json", ".h5", "_labelidx.pkl", and "_dictionary.dict".

        If this has not been run, or a model was not trained by :func:`~train`,
        a `ModelNotTrainedException` will be raised while performing prediction or saving the model.

        :param nameprefix: prefix of the file path
        :return: None
        :type nameprefix: str
        """
        self.model = kerasio.load_model(nameprefix)

        self.dictionary = Dictionary.load(nameprefix+'_dictionary.dict')

        labelfile = open(nameprefix+'_classlabels.txt', 'r')
        self.classlabels = [s.strip() for s in labelfile.readlines()]
        labelfile.close()

        self.labels2idx = pickle.load(open(nameprefix+'_labelidx.pkl', 'rb'))

        self.trained = True