def __init__(self, args): self.dataset = args.dataset self.device = torch.device(f'cuda:{args.cuda_num}' if args.cuda else 'cpu') if self.dataset in ["Cora", "Citeseer", "Pubmed", 'CoauthorCS']: self.data = load_data(self.dataset) self.loss_fn = torch.nn.functional.nll_loss else: raise Exception(f'the dataset of {self.dataset} has not been implemented') self.miss_rate = args.miss_rate if self.miss_rate > 0.: self.data.x = remove_feature(self.data, self.miss_rate) self.type_model = args.type_model self.epochs = args.epochs self.grad_clip = args.grad_clip self.weight_decay = args.weight_decay if self.type_model == 'GCN': self.model = GCN(args) elif self.type_model == 'simpleGCN': self.model = simpleGCN(args) elif self.type_model == 'GAT': self.model = GAT(args) else: raise Exception(f'the model of {self.type_model} has not been implemented') self.data.to(self.device) self.model.to(self.device) self.optimizer = torch.optim.Adam(self.model.parameters(), lr=args.lr, weight_decay=args.weight_decay) self.seed = args.random_seed self.type_norm = args.type_norm self.skip_weight = args.skip_weight
def run(config, train_dataset, val_dataset): device = 'cpu' if torch.cuda.is_available() else 'cpu' model = GCN(1, 64, 4, 0.01).to(device) print("Training on {}, batch_size is {}, lr is {}".format( device, config['batch_size'], config['lr'])) criterion = torch.nn.MSELoss() optimizer = optim.Adam(model.parameters(), lr=config['lr']) train_loader = DataLoader(train_dataset, batch_size=config['batch_size'], shuffle=True) val_loader = DataLoader(val_dataset, batch_size=config['batch_size'], shuffle=True) trainer = Trainer(model, train_loader, val_loader, criterion, optimizer, config, device) train_acc, train_loss, val_acc, val_loss = trainer.train() return train_acc, train_loss, val_acc, val_loss
def generate_model(opt): assert opt.model_depth in [50, 101, 152] if opt.model_depth == 50: model = torchvision.models.resnet50(pretrained=True) model = I3ResNet(model, opt.sample_duration) elif opt.model_depth == 101: model = torchvision.models.resnet101(pretrained=True) model = I3ResNet(model, opt.sample_duration) elif opt.model_depth == 152: model = torchvision.models.resnet152(pretrained=True) model = I3ResNet(model, opt.sample_duration) if opt.pretrain_path: print('loading pretrained model {}'.format(opt.pretrain_path)) pretrain = torch.load(opt.pretrain_path) assert opt.arch == pretrain['arch'], 'Unmatched model from pretrained path.' state_dict = pretrain['state_dict'] local_state = model.state_dict() for name, param in local_state.items(): if 'fc' not in name: key = 'module.' + name if key in state_dict: input_param = state_dict[key].data param.copy_(input_param) base_model = model model = GCN(opt.n_box_per_frame, opt.step_per_layer, opt.n_classes, opt.basenet_fixed_layers) model.create_architecture(base_model) del base_model if not opt.no_cuda: model = model.cuda() model = nn.DataParallel(model, device_ids=None) parameters = [] arch_parameters = [] for key, value in dict(model.named_parameters()).items(): if value.requires_grad: if 'arch_weights' not in key: parameters += [{'params':[value]}] else: arch_parameters += [{'params':[value]}] return model, parameters, arch_parameters else: parameters = [] arch_parameters = [] for key, value in dict(model.named_parameters()).items(): if value.requires_grad: if 'arch_weights' not in key: parameters += [{'params':[value]}] else: arch_parameters += [{'params':[value]}] return model, parameters, arch_parameters
def __init__(self, nfeat, args): super(FairGNN, self).__init__() nhid = args.num_hidden dropout = args.dropout self.estimator = GCN(nfeat, args.hidden, 1, dropout) self.GNN = get_model(nfeat, args) self.classifier = nn.Linear(nhid, 1) self.adv = nn.Linear(nhid, 1) G_params = list(self.GNN.parameters()) + list( self.classifier.parameters()) + list(self.estimator.parameters()) self.optimizer_G = torch.optim.Adam(G_params, lr=args.lr, weight_decay=args.weight_decay) self.optimizer_A = torch.optim.Adam(self.adv.parameters(), lr=args.lr, weight_decay=args.weight_decay) self.args = args self.criterion = nn.BCEWithLogitsLoss() self.G_loss = 0 self.A_loss = 0
#%% import dgl from utils import feature_norm G = dgl.DGLGraph() G.from_scipy_sparse_matrix(adj) if dataset == "nba": features = feature_norm(features) #%% sens[sens > 0] = 1 if sens_attr: sens[sens > 0] = 1 # Model and optimizer model = GCN(nfeat=features.shape[1], nhid=args.hidden, nclass=1, dropout=args.dropout) optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) if args.cuda: model.cuda() features = features.cuda() # adj = adj.cuda() sens = sens.cuda() # idx_sens_train = idx_sens_train.cuda() # idx_val = idx_val.cuda() idx_test = idx_test.cuda() sens = sens.cuda() idx_sens_train = idx_sens_train.cuda()
args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() np.random.seed(args.seed) torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) # Load data adj, features, labels, idx_train, idx_val, idx_test = load_data() # Model and optimizer if args.model == 'GCN': model = GCN(nfeat=features.shape[1], nhid=args.hidden, nclass=labels.max().item() + 1, dropout=args.dropout) else: model = GAT(nfeat=features.shape[1], nhid=args.hidden, nclass=labels.max().item()+1, dropout=args.dropout, num_head=args.num_head) optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) if args.cuda: model.cuda() features = features.cuda() adj = adj.cuda()
transform=transform) logger.info("Dataset samples: {}".format(len(train_set) + len(val_set))) train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True) val_loader = DataLoader(val_set, batch_size=batch_size, shuffle=True, num_workers=4, pin_memory=True) # Create model model = GCN(in_channels=in_channels, num_class=num_classes, graph_args={ 'layout': 'ntu-rgb+d' }, edge_importance_weighting=True).to(device) # Run the model parallelly if torch.cuda.device_count() > 1: logger.info("Using {} GPUs".format(torch.cuda.device_count())) model = nn.DataParallel(model) # Create loss criterion & optimizer criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=learning_rate) # Start training logger.info("Training Started".center(60, '#')) for epoch in range(epochs): # Train the model train_epoch(model, criterion, optimizer, train_loader, device, epoch,
class FairGNN(nn.Module): def __init__(self, nfeat, args): super(FairGNN, self).__init__() nhid = args.num_hidden dropout = args.dropout self.estimator = GCN(nfeat, args.hidden, 1, dropout) self.GNN = get_model(nfeat, args) self.classifier = nn.Linear(nhid, 1) self.adv = nn.Linear(nhid, 1) G_params = list(self.GNN.parameters()) + list( self.classifier.parameters()) + list(self.estimator.parameters()) self.optimizer_G = torch.optim.Adam(G_params, lr=args.lr, weight_decay=args.weight_decay) self.optimizer_A = torch.optim.Adam(self.adv.parameters(), lr=args.lr, weight_decay=args.weight_decay) self.args = args self.criterion = nn.BCEWithLogitsLoss() self.G_loss = 0 self.A_loss = 0 def forward(self, g, x): s = self.estimator(g, x) z = self.GNN(g, x) y = self.classifier(z) return y, s def optimize(self, g, x, labels, idx_train, sens, idx_sens_train): self.train() ### update E, G self.adv.requires_grad_(False) self.optimizer_G.zero_grad() s = self.estimator(g, x) h = self.GNN(g, x) y = self.classifier(h) s_g = self.adv(h) s_score = torch.sigmoid(s.detach()) # s_score = (s_score > 0.5).float() s_score[idx_sens_train] = sens[idx_sens_train].unsqueeze(1).float() y_score = torch.sigmoid(y) self.cov = torch.abs( torch.mean((s_score - torch.mean(s_score)) * (y_score - torch.mean(y_score)))) self.cls_loss = self.criterion(y[idx_train], labels[idx_train].unsqueeze(1).float()) self.adv_loss = self.criterion(s_g, s_score) self.G_loss = self.cls_loss + self.args.alpha * self.cov - self.args.beta * self.adv_loss self.G_loss.backward() self.optimizer_G.step() ## update Adv self.adv.requires_grad_(True) self.optimizer_A.zero_grad() s_g = self.adv(h.detach()) self.A_loss = self.criterion(s_g, s_score) self.A_loss.backward() self.optimizer_A.step()
def choose_model(conf): if conf['model_name'] == 'GCN': model = GCN(g=G, in_feats=features.shape[1], n_hidden=conf['hidden'], n_classes=labels.max().item() + 1, n_layers=1, activation=F.relu, dropout=conf['dropout']).to(conf['device']) elif conf['model_name'] in ['GAT', 'SGAT']: if conf['model_name'] == 'GAT': num_heads = 8 else: num_heads = 1 num_layers = 1 num_out_heads = 1 heads = ([num_heads] * num_layers) + [num_out_heads] model = GAT( g=G, num_layers=num_layers, in_dim=features.shape[1], num_hidden=8, num_classes=labels.max().item() + 1, heads=heads, activation=F.relu, feat_drop=0.6, attn_drop=0.6, negative_slope=0.2, # negative slope of leaky relu residual=False).to(conf['device']) elif conf['model_name'] == 'GraphSAGE': model = GraphSAGE(in_feats=features.shape[1], n_hidden=conf['embed_dim'], n_classes=labels.max().item() + 1, n_layers=2, activation=F.relu, dropout=0.5, aggregator_type=conf['agg_type']).to(conf['device']) elif conf['model_name'] == 'APPNP': model = APPNP(g=G, in_feats=features.shape[1], hiddens=[64], n_classes=labels.max().item() + 1, activation=F.relu, feat_drop=0.5, edge_drop=0.5, alpha=0.1, k=10).to(conf['device']) elif conf['model_name'] == 'MoNet': model = MoNet(g=G, in_feats=features.shape[1], n_hidden=64, out_feats=labels.max().item() + 1, n_layers=1, dim=2, n_kernels=3, dropout=0.7).to(conf['device']) elif conf['model_name'] == 'SGC': model = SGConv(in_feats=features.shape[1], out_feats=labels.max().item() + 1, k=2, cached=True, bias=False).to(conf['device']) elif conf['model_name'] == 'GCNII': if conf['dataset'] == 'citeseer': conf['layer'] = 32 conf['hidden'] = 256 conf['lamda'] = 0.6 conf['dropout'] = 0.7 elif conf['dataset'] == 'pubmed': conf['hidden'] = 256 conf['lamda'] = 0.4 conf['dropout'] = 0.5 model = GCNII(nfeat=features.shape[1], nlayers=conf['layer'], nhidden=conf['hidden'], nclass=labels.max().item() + 1, dropout=conf['dropout'], lamda=conf['lamda'], alpha=conf['alpha'], variant=False).to(conf['device']) return model
def choose_model(conf, G, features, labels, byte_idx_train, labels_one_hot): if conf['model_name'] == 'GCN': model = GCN(g=G, in_feats=features.shape[1], n_hidden=conf['hidden'], n_classes=labels.max().item() + 1, n_layers=1, activation=F.relu, dropout=conf['dropout']).to(conf['device']) elif conf['model_name'] == 'GAT': num_heads = 8 num_layers = 1 num_out_heads = 1 heads = ([num_heads] * num_layers) + [num_out_heads] model = GAT( g=G, num_layers=num_layers, in_dim=G.ndata['feat'].shape[1], num_hidden=8, num_classes=labels.max().item() + 1, heads=heads, activation=F.relu, feat_drop=0.6, attn_drop=0.6, negative_slope=0.2, # negative slope of leaky relu residual=False).to(conf['device']) elif conf['model_name'] == 'PLP': model = PLP(g=G, num_layers=conf['num_layers'], in_dim=G.ndata['feat'].shape[1], emb_dim=conf['emb_dim'], num_classes=labels.max().item() + 1, activation=F.relu, feat_drop=conf['feat_drop'], attn_drop=conf['attn_drop'], residual=False, byte_idx_train=byte_idx_train, labels_one_hot=labels_one_hot, ptype=conf['ptype'], mlp_layers=conf['mlp_layers']).to(conf['device']) elif conf['model_name'] == 'GraphSAGE': model = GraphSAGE(in_feats=G.ndata['feat'].shape[1], n_hidden=16, n_classes=labels.max().item() + 1, n_layers=1, activation=F.relu, dropout=0.5, aggregator_type=conf['agg_type']).to(conf['device']) elif conf['model_name'] == 'APPNP': model = APPNP(g=G, in_feats=G.ndata['feat'].shape[1], hiddens=[64], n_classes=labels.max().item() + 1, activation=F.relu, feat_drop=0.5, edge_drop=0.5, alpha=0.1, k=10).to(conf['device']) elif conf['model_name'] == 'LogReg': model = MLP(num_layers=1, input_dim=G.ndata['feat'].shape[1], hidden_dim=None, output_dim=labels.max().item() + 1, dropout=0).to(conf['device']) elif conf['model_name'] == 'MLP': model = MLP(num_layers=2, input_dim=G.ndata['feat'].shape[1], hidden_dim=conf['hidden'], output_dim=labels.max().item() + 1, dropout=conf['dropout']).to(conf['device']) else: raise ValueError(f'Undefined Model.') return model
datareader = DataReader(data_dir='./datasets/%s/' % dataset_name.upper(), rnd_state=np.random.RandomState(args.seed), folds=args.n_folds, use_cont_node_attr=False, random_walk=random_walk) for model_name in args.model_list: for i, readout_name in enumerate(args.readout_list): print('-'*25) # Build graph classification model if model_name == 'GCN': model = GCN(n_feat=datareader.data['features_dim'], n_class=datareader.data['n_classes'], n_layer=args.n_agg_layer, agg_hidden=args.agg_hidden, fc_hidden=args.fc_hidden, dropout=args.dropout, readout=readout_name, device=device).to(device) elif model_name == 'GAT': model = GAT(n_feat=datareader.data['features_dim'], n_class=datareader.data['n_classes'], n_layer=args.n_agg_layer, agg_hidden=args.agg_hidden, fc_hidden=args.fc_hidden, dropout=args.dropout, readout=readout_name, device=device).to(device) # elif model_name == 'GraphWaveletNet': # model = GraphWaveletNet(n_feat=datareader.data['features_dim'], # n_class=datareader.data['n_classes'],
def __init__(self, args): self.dataset = args.dataset self.device = torch.device( f"cuda:{args.cuda_num}" if args.cuda else "cpu") if self.dataset in ["Cora", "Citeseer", "Pubmed", "CoauthorCS"]: if args.ptb: self.data = load_perterbued_data(self.dataset, args.ptb_rate, args.ptb_type) self.loss_fn = torch.nn.functional.nll_loss else: self.data = load_data(self.dataset) self.loss_fn = torch.nn.functional.nll_loss elif self.dataset in ["PPI"]: self.data = load_ppi_data() self.loss_fn = torch.nn.BCEWithLogitsLoss() else: raise Exception( f"the dataset of {self.dataset} has not been implemented") self.entropy_loss = torch.nn.functional.binary_cross_entropy_with_logits self.type_model = args.type_model self.epochs = args.epochs self.weight_decay = args.weight_decay self.alpha = args.alpha self.gamma = args.gamma self.beta = args.beta self.lamb = args.lamb self.num_classes = args.num_classes self.ptb_rate = args.ptb_rate self.ptb_type = args.ptb_type self.metric = args.metric self.num_layers = args.num_layers if self.type_model == "GCN": self.model = GCN(args) elif self.type_model == "GAT": self.model = GAT(args) elif self.type_model == "NLGCN": self.model = NLGCN(args) elif self.type_model == "g_U_Net": self.model = gunet(args) elif self.type_model == "JKNet": self.model = JKNetMaxpool(args) elif self.type_model == "SGC": self.model = simpleGCN(args) elif self.type_model == "APPNP": self.model = APPNP(args) else: raise Exception( f"the model of {self.type_model} has not been implemented") if self.dataset in ["Cora", "Citeseer", "Pubmed", "CoauthorCS"]: if args.ptb: self.data.edge_index, self.data.x, self.data.y = utils.preprocess( self.data.edge_index, self.data.x, self.data.y, preprocess_adj=False, sparse=False, device=self.device) else: self.data.to(self.device) self.model.to(self.device) self.optimizer = torch.optim.Adam(self.model.parameters(), lr=args.lr, weight_decay=args.weight_decay) wandb.init(project="Gref", config=args) wandb.watch(self.model)
if os.path.exists(args.preprocessed_data_path): adj, features, labels, idx_train, idx_val, idx_test = load_features(args.preprocessed_data_path) else: # 数据预处理过程 pass labels = torch.LongTensor(labels) adj = torch.Tensor(adj) features = torch.Tensor(features) idx_train = torch.LongTensor(idx_train) idx_test = torch.LongTensor(idx_test) # 数据预处理 # Model and optimizer model = GCN(nfeat=features.shape[1], nhid=args.hidden, nclass=args.n_class, dropout=args.dropout) optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay) # 使用gpu训练 if args.cuda != "-1": model.cuda() features = features.cuda() labels = labels.cuda() idx_train = idx_train.cuda() idx_test = idx_test.cuda() adj = adj.cuda() # train(epoch=args.epoch) hist = (np.inf, 0)