Esempio n. 1
0
 def __init__(self, cached_features):
     base_model.BaseModel.__init__(self, cached_features)
     self.weights_file = "working/models/lstm_model.pickle.weights"
     self.n_epochs = 4
     self.padding_size = 256
     self.max_features = 43980
     self.batch_size = 32
     self.model = self.SetupModel()
     self.X_train = None
     self.y_train = numpy.array([])
     self.feature_extractor = CachedFeatureExtractor("working/models/sequence_train_batches")
     self.test_feature_extractor = CachedFeatureExtractor("working/models/sequence_test_batches")
Esempio n. 2
0
class BaseModel(object):

    """
    This is the Base class for all models
    There are two ways of training any of the derived models:
    1) Online: override the _fit_internal() method that trains on a batch
    2) Complete: override the fit() method that trains on entire training set
    """
    def __init__(self, cached_features=True):
        self.model = None
        self.classes = numpy.array([0, 1])
        self.cached_features = cached_features
        self.feature_extractor = CachedFeatureExtractor("working/models/train_batches")
        self.test_feature_extractor = CachedFeatureExtractor("working/models/test_batches")
        

    def fit(self):
        print "   Fitting ", self.__class__.__name__, "..."
        counter = 0
        for (X_train, y_train) in self.feature_extractor.nextBatch():
            self._fit_internal(X_train, y_train)
            
    def _fit_internal(self, X_train, y_train):
        self.model.partial_fit(X_train, y_train, classes=self.classes)


    def predict_proba(self):
        print "   Predicting ", self.__class__.__name__, "..."
        counter = 0
        y_ans = numpy.array([])

        for (X_test, y_test_ignored) in self.test_feature_extractor.nextBatch():
            y_ans = numpy.append(y_ans, self._predict_internal(X_test))
        return y_ans
   

    def _predict_internal(self, X_test):
        pass


    def predict_and_dump(self, submission_file):
        pass


    def dump(self, filename):
        print "Dumping model to", filename, "..."
        with open(filename, "wb") as f:
            cPickle.dump(self, f)
Esempio n. 3
0
class LSTMModel(base_model.BaseModel):

    def __init__(self, cached_features):
        base_model.BaseModel.__init__(self, cached_features)
        self.weights_file = "working/models/lstm_model.pickle.weights"
        self.n_epochs = 4
        self.padding_size = 256
        self.max_features = 43980
        self.batch_size = 32
        self.model = self.SetupModel()
        self.X_train = None
        self.y_train = numpy.array([])
        self.feature_extractor = CachedFeatureExtractor("working/models/sequence_train_batches")
        self.test_feature_extractor = CachedFeatureExtractor("working/models/sequence_test_batches")

    def SetupModel(self):
        model = Sequential()
        model.add(Embedding(self.max_features, 128))
        model.add(LSTM(128, 128))
        model.add(Dropout(0.5))
        model.add(Dense(128, 1))
        model.add(Activation('sigmoid'))

        # try using different optimizers and different optimizer configs
        model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")
        return model


    def _fit_internal(self, X_train, y_train):
        if self.X_train is None:
            self.X_train = sequence.pad_sequences(X_train, maxlen=self.padding_size)
        else:
            self.X_train = numpy.vstack((self.X_train, sequence.pad_sequences(X_train, maxlen=self.padding_size)))
        self.y_train = numpy.append(self.y_train, y_train)
        print self.X_train.shape, self.y_train.shape

    def fit(self):
        print "Fitting ", self.__class__.__name__, "..."
        counter = 0
        for (X_train, y_train) in self.feature_extractor.nextBatch():
            print "   Adding batch ", counter, "...",
            self._fit_internal(X_train, y_train)
            counter += 1
        self.model.fit(self.X_train, self.y_train, batch_size=self.batch_size, nb_epoch=self.n_epochs, show_accuracy=True)

    def predict_proba(self):
        print "   Predicting ", self.__class__.__name__, "..."
        print "Creating model using SetupModel..."
        self.model = self.SetupModel()
        print "Loading model weights from ", self.weights_file, "..."
        self.model.load_weights(self.weights_file)
        counter = 0
        y_ans = numpy.array([])
        #self.test_feature_extractor = CachedFeatureExtractor("working/models/sequence_test_batches")
        for (X_test, y_test_ignored) in self.test_feature_extractor.nextBatch():
            y_ans = numpy.append(y_ans, self._predict_internal(X_test))
            counter += 1
        return y_ans


    def _predict_internal(self, X_test):
        X_new = sequence.pad_sequences(X_test, maxlen=self.padding_size)
        return self.model.predict(X_new, batch_size=self.batch_size)[:, 0]


    def dump(self, filename):
        print "saving weights to: ", self.weights_file
        self.model.save_weights(self.weights_file, overwrite=True)
        self.model = None
        print "Dumping model to ", filename, "..."
        with open(filename, "wb") as f:
            cPickle.dump(self, f)
Esempio n. 4
0
 def __init__(self, cached_features=True):
     self.model = None
     self.classes = numpy.array([0, 1])
     self.cached_features = cached_features
     self.feature_extractor = CachedFeatureExtractor("working/models/train_batches")
     self.test_feature_extractor = CachedFeatureExtractor("working/models/test_batches")