def __init__(self, model_type, filename, labels=None, feature_extractor=None, model=None): if model_type is None: model_type = SPARSE_PERCEPTRON self.model_type = model_type self.filename = filename if feature_extractor is not None and model is not None: self.feature_extractor = feature_extractor self.model = model return if model_type == SPARSE_PERCEPTRON: from features.sparse_features import SparseFeatureExtractor from linear.sparse_perceptron import SparsePerceptron self.feature_extractor = SparseFeatureExtractor() self.model = SparsePerceptron(filename, labels) elif model_type == DENSE_PERCEPTRON: from features.embedding import FeatureEmbedding from linear.dense_perceptron import DensePerceptron self.feature_extractor = self.dense_features_wrapper( FeatureEmbedding) self.model = DensePerceptron( filename, labels, num_features=self.feature_extractor.num_features()) elif model_type == MLP_NN: from features.enumerator import FeatureEnumerator from nn.feedforward import MLP self.feature_extractor = self.dense_features_wrapper( FeatureEnumerator) self.model = MLP(filename, labels, input_params=self.feature_extractor.params) elif model_type == BILSTM_NN: from features.enumerator import FeatureEnumerator from features.indexer import FeatureIndexer from nn.bilstm import BiLSTM self.feature_extractor = FeatureIndexer( self.dense_features_wrapper(FeatureEnumerator)) self.model = BiLSTM(filename, labels, input_params=self.feature_extractor.params) else: raise ValueError("Invalid model type: '%s'" % model_type)
def create_model(model_type, labels): if model_type == "sparse": from classifiers.sparse_perceptron import SparsePerceptron from features.sparse_features import SparseFeatureExtractor features = SparseFeatureExtractor() model = SparsePerceptron(labels, min_update=Config().min_update) elif model_type == "dense": from features.embedding import FeatureEmbedding from classifiers.dense_perceptron import DensePerceptron features = dense_features_wrapper(FeatureEmbedding) model = DensePerceptron(labels, num_features=features.num_features()) elif model_type == "nn": from features.indexer import FeatureIndexer from classifiers.neural_network import NeuralNetwork features = dense_features_wrapper(FeatureIndexer) model = NeuralNetwork(labels, inputs=features.feature_types) else: raise ValueError("Invalid model type: '%s'" % model_type) return features, model