def __init__(self, hparams): super(Model, self).__init__() self.hparams = hparams self.model = load_network(hparams) self.train_acc = Accuracy() self.val_acc = Accuracy() self.criterion = nn.BCEWithLogitsLoss()
def __init__(self, input_size: int = 784, lin1_size: int = 256, lin2_size: int = 256, lin3_size: int = 256, output_size: int = 10, lr: float = 0.001, weight_decay: float = 0.0005, **kwargs): super().__init__() # this line ensures params passed to LightningModule will be saved to ckpt # it also allows to access params with 'self.hparams' attribute self.save_hyperparameters() self.model = SimpleDenseNet(hparams=self.hparams) # loss function self.criterion = torch.nn.CrossEntropyLoss() # use separate metric instance for train, val and test step # to ensure a proper reduction over the epoch self.train_accuracy = Accuracy() self.val_accuracy = Accuracy() self.test_accuracy = Accuracy() self.metric_hist = { "train/acc": [], "val/acc": [], "train/loss": [], "val/loss": [], }
def __init__(self, arch, num_classes, dims, resnet_variant='alpha', optimizer='adam', lr=0.001, beta1=0.9, beta2=0.999, weight_decay=0.0001, momentum=0.9, schedule='none', steps=[100, 150], step_factor=0.1, k=5): super(ComplexModel, self).__init__() self.save_hyperparameters() self.encoder, self.processor, self.decoder = load_complex_network( arch, num_classes, resnet_variant, ) self.real_to_complex = RealToComplex() self.complex_to_real = ComplexToReal() size = get_encoder_output_size(self.encoder, dims) self.discriminator = Discriminator(size=size) self.train_acc = Accuracy() self.val_acc = Accuracy() print(self.hparams)
def __init__( self, arch, num_classes, additional_layers=False, resnet_variant='alpha', noisy=False, gamma=1.0, optimizer='sgd', lr=0.1, beta1=0.9, beta2=0.999, weight_decay=0.0001, momentum=0.9, schedule='none', steps=[100, 150], step_factor=0.1, ): super(BaselineModel, self).__init__() self.save_hyperparameters() self.encoder, self.processor, self.decoder = load_baseline_network( arch, num_classes, resnet_variant, additional_layers) self.train_acc = Accuracy() self.val_acc = Accuracy() print(self.hparams)
def prepare(self): (self.train_transforms, self.infer_transforms) = self._build_transforms( **self.config.transforms) ( self._train_dataloader, self._val_dataloader, self._test_dataloader, ) = self._build_dataloaders(**self.config.data) num_slots = len( self.train_transforms["utterance&slots"][0].vocab.idx.keys()) num_intents = len(self.train_transforms["intent"][0].vocab.idx.keys()) add_feat_len = 0 if self.config.model.use_intent: add_feat_len += num_intents self.joint_model = build_intent_joint_model( **self.config.model, num_slots=num_slots, num_intents=num_intents, vocab=self.train_transforms["utterance"][1].vocab, add_feat_len=add_feat_len, ) self.slot_accuracy_metric = Accuracy(num_classes=num_slots) self.doc_accuracy_metric = Accuracy(num_classes=num_intents) self.precision_metric = AveragePrecision() self.slot_f1_metric = F1(num_classes=num_slots) self.doc_f1_metric = F1(num_classes=num_intents) self.optimizer = FairSeqAdam(self.joint_model.parameters(), **self.config.optimizer)
def __init__( self, encoder_weights, inversion_net_weights, dims, num_classes, arch='resnet56', resnet_variant='alpha', additional_layers=False, optimizer='sgd', lr=0.1, beta1=0.9, beta2=0.999, weight_decay=0.0001, momentum=0.9, schedule='none', steps=[100, 150], step_factor=0.1, complex=False, angle_dis_weights=None, ): super(InferenceAttack3Model, self).__init__() self.save_hyperparameters() # Load pretrained models if complex: # Load complex encoder self.protype_encoder = ComplexEncoder(encoder_weights) else: self.protype_encoder = BaselineEncoder(encoder_weights) self.protype_encoder.freeze() # Feature inversion network in_size = get_encoder_output_size(self.protype_encoder, dims) self.inversion_network = InversionNetwork(inversion_net_weights, in_size[-3]) self.inversion_network.freeze() if complex: # Angle discriminator self.angle_discriminator = AngleDiscriminator( angle_dis_weights, in_size) self.angle_discriminator.freeze() self.complex_to_real = ComplexToReal() # Load attack classification model self.encoder, self.processor, self.decoder = load_baseline_network( arch, num_classes, resnet_variant, additional_layers=False) self.train_acc = Accuracy() self.val_acc = Accuracy() print(self.hparams)
def __init__(self, hidden_dim=128, learning_rate=1e-3): super().__init__() self.save_hyperparameters() # mnist images are (1, 28, 28) (channels, width, height) self.layer_1 = nn.Linear(28 * 28, self.hparams.hidden_dim) self.layer_2 = nn.Linear(self.hparams.hidden_dim, self.hparams.hidden_dim * 2) self.layer_3 = nn.Linear(self.hparams.hidden_dim * 2, 10) self.train_accuracy = Accuracy() self.val_accuracy = Accuracy() self.test_accuracy = Accuracy()
def __init__( self, architecture: str = "GraphSAGE", aggregation_method: str = "concat", num_node_features: int = 1, add_pos_to_features: bool = False, num_conv_layers: int = 3, conv_size: int = 128, lin_size: int = 128, output_size: int = 10, lr: float = 0.001, weight_decay: float = 0, **kwargs ): super().__init__() self.save_hyperparameters() self.add_pos_to_features = add_pos_to_features # init network architecture if self.hparams.architecture == "GraphSAGE": self.model = graph_sage.GraphSage(hparams=self.hparams) elif self.hparams.architecture == "GAT": self.model = gat.GAT(hparams=self.hparams) elif self.hparams.architecture == "JumpingKnowledge": self.model = jumping_knowledge.JK(hparams=self.hparams) elif self.hparams.architecture == "GCN": self.model = gcn.GCN(hparams=self.hparams) elif self.hparams.architecture == "VectorSAGE": self.model = vector_sage_module.VectorSAGEModule(hparams=self.hparams) else: raise Exception("Incorrect architecture name!") # loss function self.criterion = torch.nn.CrossEntropyLoss() # use separate metric instance for train, val and test step # to ensure a proper reduction over the epoch self.train_accuracy = Accuracy() self.val_accuracy = Accuracy() self.test_accuracy = Accuracy() self.metric_hist = { "train/acc": [], "val/acc": [], "train/loss": [], "val/loss": [], }
def __init__(self, HPARAMS): super().__init__() # HPARAMS self.save_hyperparameters() self.model = Wav2VecLSTM(HPARAMS['model_hidden_size']) self.classification_criterion = MSE() self.regression_criterion = MSE() self.mae_criterion = MAE() self.rmse_criterion = RMSELoss() self.accuracy = Accuracy() self.alpha = HPARAMS['model_alpha'] self.beta = HPARAMS['model_beta'] self.gamma = HPARAMS['model_gamma'] self.lr = HPARAMS['training_lr'] self.csv_path = HPARAMS['speaker_csv_path'] self.df = pd.read_csv(self.csv_path, sep=' ') self.h_mean = self.df['Height'].mean() self.h_std = self.df['Height'].std() self.a_mean = self.df['Age'].mean() self.a_std = self.df['Age'].std() print(f"Model Details: #Params = {self.count_total_parameters()}\t#Trainable Params = {self.count_trainable_parameters()}")
def __init__(self): super().__init__() self.metrics_singleclass = nn.ModuleDict({ 'loss': BCEWithLogitsLoss(), 'acc': SoftMaxWrapper(Accuracy(), multiclass=False), 'auc': AUROC(), }) self.metrics_multiclass = nn.ModuleDict({ 'loss': CrossEntropyLoss(), 'acc': SoftMaxWrapper(Accuracy(), multiclass=True), })
def test_accuracy(num_classes): acc = Accuracy(num_classes=num_classes) assert acc.name == 'accuracy' result = acc(pred=torch.tensor([[0, 1, 1], [1, 0, 1]]), target=torch.tensor([[0, 0, 1], [1, 0, 1]])) assert isinstance(result, torch.Tensor)
def __init__(self, HPARAMS): super().__init__() # HPARAMS self.save_hyperparameters() self.models = { 'wav2vecLSTMAttn': Wav2VecLSTM, 'spectralCNNLSTM': SpectralCNNLSTM, 'MultiScale': SpectralMultiScale, } self.model = self.models[HPARAMS['model_type']](HPARAMS['hidden_size']) self.classification_criterion = MSE() self.regression_criterion = MSE() self.mae_criterion = MAE() self.rmse_criterion = RMSELoss() self.accuracy = Accuracy() self.alpha = HPARAMS['alpha'] self.beta = HPARAMS['beta'] self.gamma = HPARAMS['gamma'] self.lr = HPARAMS['lr'] self.csv_path = HPARAMS['speaker_csv_path'] self.df = pd.read_csv(self.csv_path) self.h_mean = self.df['height'].mean() self.h_std = self.df['height'].std() self.a_mean = self.df['age'].mean() self.a_std = self.df['age'].std() print( f"Model Details: #Params = {self.count_total_parameters()}\t#Trainable Params = {self.count_trainable_parameters()}" )
def __init__(self, model: torch.nn.Module, train_data: str, val_data: str, test_data: str, batch_size: int, num_classes: int = 2, num_workers: int = 1): super().__init__() self.num_workers = num_workers self.train_data = train_data self.val_data = val_data self.test_data = test_data self.batch_size = batch_size self.model = model self.criterion = torch.nn.BCELoss() self.collate_fn = PaddingCollateFn(150) self.train_metrics = [] self.val_metrics = [ Accuracy(num_classes=num_classes), F1(num_classes=num_classes), Precision(num_classes=num_classes), Recall(num_classes=num_classes) ]
def __init__( self, num_classes: int, backbone: str = "prajjwal1/bert-tiny", optimizer: Type[torch.optim.Optimizer] = torch.optim.Adam, metrics: Union[Callable, Mapping, Sequence, None] = [Accuracy()], learning_rate: float = 1e-3, ): self.save_hyperparameters() os.environ["TOKENIZERS_PARALLELISM"] = "TRUE" # disable HF thousand warnings warnings.simplefilter("ignore") # set os environ variable for multiprocesses os.environ["PYTHONWARNINGS"] = "ignore" super().__init__( model=None, loss_fn=None, optimizer=optimizer, metrics=metrics, learning_rate=learning_rate, ) self.model = BertForSequenceClassification.from_pretrained( backbone, num_labels=num_classes)
def __init__(self, model, criterion, config: dict, loaders: dict): super().__init__() self.model = model self.criterion = criterion self.config = config self.loaders = loaders self.metrics = {'acc': Accuracy(), 'f1': F1()}
def __init__(self, out_classes: int = 3, lr_bert: float = 1e-5, lr_class: float = 1e-4, weight_decay: float = 1e-2, freeze_base: bool = False, train_steps: int = 100): super(SentBert, self).__init__() self.lr_bert = lr_bert self.lr_class = lr_class self.weight_decay = weight_decay self.train_steps = train_steps self.save_hyperparameters() self.bert = BertForSequenceClassification.from_pretrained( 'bert-base-uncased', num_labels=out_classes, return_dict=True) if freeze_base: for param in self.bert.base_model.parameters(): param.requires_grad = False self.f1 = F1(num_classes=out_classes, average='macro') self.train_acc = Accuracy() self.val_acc = Accuracy()
def __init__(self, *args, **kwargs): super().__init__() self.save_hyperparameters() self.loss = BCELoss(reduction='mean') self.accuracy = Accuracy() # Initialize model architecture self.architecture = XLMREncoder(hparams=self.hparams)
def __init__(self, config, trial=None): super(LitPSD, self).__init__() if trial: self.trial = trial else: self.trial = None self.pylog = logging.getLogger(__name__) logging.getLogger("lightning").setLevel(self.pylog.level) self.config = config if hasattr(config.system_config, "half_precision"): self.needs_float = not config.system_config.half_precision else: self.needs_float = True self.hparams = DictionaryUtility.to_dict(config) self.n_type = config.system_config.n_type self.lr = config.optimize_config.lr self.modules = ModuleUtility(config.net_config.imports + config.dataset_config.imports + config.optimize_config.imports) self.model_class = self.modules.retrieve_class( config.net_config.net_class) # self.data_module = PSDDataModule(config,self.device) self.model = self.model_class(config) self.criterion_class = self.modules.retrieve_class( config.net_config.criterion_class) self.criterion = self.criterion_class( *config.net_config.criterion_params) self.softmax = LogSoftmax(dim=1) self.accuracy = Accuracy() self.confusion = ConfusionMatrix(num_classes=self.n_type) if hasattr(self.config.dataset_config, "calgroup"): calgroup = self.config.dataset_config.calgroup else: calgroup = None if self.config.dataset_config.dataset_class == "PulseDatasetDet": self.evaluator = PhysEvaluator( self.config.system_config.type_names, self.logger, device=self.device) elif self.config.dataset_config.dataset_class == "PulseDatasetWaveformNorm": self.evaluator = TensorEvaluator(self.logger, calgroup=calgroup, target_has_phys=False, target_index=None, metric_name="accuracy", metric_unit="") else: if hasattr(self.config.dataset_config, "calgroup"): self.evaluator = PSDEvaluator( self.config.system_config.type_names, self.logger, device=self.device, calgroup=self.config.dataset_config.calgroup) else: self.evaluator = PSDEvaluator( self.config.system_config.type_names, self.logger, device=self.device)
def __init__(self, hparams): super(YourModel, self).__init__() """ Initialise custom objects here """ self.hparams = hparams self.model = ImageNet_Pretrained_Model() self.accuracy = pl.metrics.Accuracy() self.acc = Accuracy()
def __init__(self, hparams): super().__init__() self.hparams = hparams self.train_batch_size = hparams.train_batch_size self.test_batch_size = hparams.test_batch_size self.input_size = ((3, ) if hparams.pseudo3d else (1, )) + tuple(hparams.resize) self.classifier = Encoder(num_convolutions=hparams.num_convolutions, filters=hparams.filters, input_size=self.input_size, latent_dim=1, use_weight_norm=hparams.use_weight_norm, dropout_rate=hparams.dropout_rate) self.classifier.fc = nn.Linear(np.prod( self.classifier.intermediate_shape), 1, bias=True) self.train_acc = Accuracy() self.val_acc = Accuracy() self.test_acc = Accuracy() if hparams.validate: torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False torch.autograd.set_detect_anomaly(self.hparams.validate) resize = None if self.hparams.resize == (0, 0) else self.hparams.resize train_crop_type = self.hparams.train_crop_type if hasattr( self.hparams, 'train_crop_type') else 'random' crop_size = self.hparams.crop_size if hasattr( self.hparams, 'crop_size') else (224, 224) self.calabresi_train = CalabresiDataset(self.hparams.train_csv, crop_size=crop_size, crop_type=train_crop_type, resize=resize) # noqa: E501 self.calabresi_val = CalabresiDataset(self.hparams.valid_csv, crop_size=crop_size, crop_type='center', resize=resize) self.calabresi_test = CalabresiDataset(self.hparams.test_csv, crop_size=crop_size, crop_type='center', resize=resize)
def __init__(self, config): super().__init__() self.train_config = config self.roberta = RobertaForMaskedLM.from_pretrained('roberta-base') _ = self.roberta.eval() for param in self.roberta.parameters(): param.requires_grad = False self.pred_model = self.roberta.roberta self.enc_model = self.pred_model.embeddings.word_embeddings self.proj_head = DVProjectionHead_EmbActi() self.lossfunc = nn.BCEWithLogitsLoss() self.acc = Accuracy(threshold=0.0) self.f1 = F1(threshold=0.0)
def __init__(self, HPARAMS): super().__init__() # HPARAMS self.save_hyperparameters() lstm_inp=512 lstm_h = HPARAMS['model_hidden_size'] alpha = HPARAMS['model_alpha'] beta = HPARAMS['model_beta'] gamma = HPARAMS['model_gamma'] csv_path = HPARAMS['speaker_csv_path'] self.encoder = Wav2Vec(pretrained=True) for param in self.encoder.parameters(): param.requires_grad = False for param in self.encoder.feature_extractor.conv_layers[5:].parameters(): param.requires_grad = True self.lstm = nn.LSTM(lstm_inp, lstm_h, batch_first=True) self.attention = Attention(lstm_h) self.height_regressor = nn.Linear(lstm_h, 1) self.age_regressor = nn.Linear(lstm_h, 1) self.gender_classifier = nn.Linear(lstm_h, 1) self.classification_criterion = MSE() self.regression_criterion = MSE() self.mae_criterion = MAE() self.rmse_criterion = RMSELoss() self.accuracy = Accuracy() self.alpha = alpha self.beta = beta self.gamma = gamma self.lr = HPARAMS['training_lr'] self.csv_path = csv_path self.df = pd.read_csv(self.csv_path, sep=' ') self.h_mean = self.df['Height'].mean() self.h_std = self.df['Height'].std() self.a_mean = self.df['Age'].mean() self.a_std = self.df['Age'].std() print(f"Model Details: #Params = {self.count_total_parameters()}\t#Trainable Params = {self.count_trainable_parameters()}")
def __init__(self, classifier_weights, encoder_weights, inversion_net_weights, dims, complex=False, angle_dis_weights=None, verbose=False): super(InferenceAttack1Model, self).__init__() self.save_hyperparameters() # Load classification network self.classifier = BaselineNetwork(classifier_weights) self.classifier.freeze() if complex: # Load complex encoder self.encoder = ComplexEncoder(encoder_weights) else: self.encoder = BaselineEncoder(encoder_weights) self.encoder.freeze() # Load feature inversion network in_size = get_encoder_output_size(self.encoder, dims) self.inversion_network = InversionNetwork(inversion_net_weights, in_size[-3]) self.inversion_network.freeze() if complex: # Load angle discriminator self.angle_discriminator = AngleDiscriminator( angle_dis_weights, in_size) self.angle_discriminator.freeze() self.complex_to_real = ComplexToReal() self.acc = Accuracy() if verbose: print(self.hparams)
def accuracy_score(labels, predictions): predictions = torch.Tensor(predictions) labels = torch.Tensor(labels) accuracy = Accuracy(threshold=cfg['training']['threshold']) return accuracy(predictions, labels).item()
str(commit)[2:-3], shell=True) return tag if __name__ == '__main__': with open("params.yaml", 'r') as fd: params = yaml.safe_load(fd) MODEL_PATH = params['test']['model_path'] module = TrainingModule.load_from_checkpoint(MODEL_PATH).to('cuda') module.eval() acc = Accuracy() f1 = F1() precision = Precision() recall = Recall() mlflow.set_tracking_uri('file-plugin:/content/NLP_Emotions/mlruns') if get_closest_gittag() == 'v2.0': mlflow.set_experiment('SGD') mlflow.set_tag('Version', 'SGD') mlflow.set_tag('Stage', 'test') mlflow.set_tag('Commit', get_commit()) mlflow.set_tag('Time', get_commit_time()) mlflow.set_tag('Model', module.model_name) mlflow.log_params({ 'batch_size': module.hparams.batch_size, 'epochs': module.hparams.epochs,
download_data("https://pl-flash-data.s3.amazonaws.com/titanic.zip", 'data/') # 2. Load the data datamodule = TabularData.from_csv( "./data/titanic/titanic.csv", test_csv="./data/titanic/test.csv", categorical_input=[ "Sex", "Age", "SibSp", "Parch", "Ticket", "Cabin", "Embarked" ], numerical_input=["Fare"], target="Survived", val_size=0.25, ) # 3. Build the model model = TabularClassifier.from_data( datamodule, metrics=[Accuracy(), Precision(), Recall()]) # 4. Create the trainer. Run 10 times on data trainer = flash.Trainer(max_epochs=10) # 5. Train the model trainer.fit(model, datamodule=datamodule) # 6. Test model trainer.test() # 7. Save it! trainer.save_checkpoint("tabular_classification_model.pt")
def get_acc(self, batch, infer): pred = self.get_pred(infer) targ = infer[1] flat_pred = pred.reshape(-1) flat_targ = targ.reshape(-1) return Accuracy()(flat_pred, flat_targ)
def __init__(self, *args, **kwargs): super(LanguageModel, self).__init__(*args, **kwargs) self.lossf = nn.CrossEntropyLoss() self.metric = Accuracy(self.outclasses)
if __name__ == "__main__": # 1. Download the data download_data("https://pl-flash-data.s3.amazonaws.com/titanic.zip", 'data/') # 2. Load the data datamodule = TabularData.from_csv( "./data/titanic/titanic.csv", test_csv="./data/titanic/test.csv", categorical_input=["Sex", "Age", "SibSp", "Parch", "Ticket", "Cabin", "Embarked"], numerical_input=["Fare"], target="Survived", val_size=0.25, ) # 3. Build the model model = TabularClassifier.from_data(datamodule, metrics=[Accuracy(), Precision(), Recall()]) # 4. Create the trainer. Run 10 times on data trainer = flash.Trainer(max_epochs=10) # 5. Train the model trainer.fit(model, datamodule=datamodule) # 6. Test model trainer.test() # 7. Save it! trainer.save_checkpoint("tabular_classification_model.pt")