Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions week1/LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2017 Thomas Kipf

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Binary file added week1/code/__pycache__/models.cpython-310.pyc
Binary file not shown.
35 changes: 35 additions & 0 deletions week1/code/log_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import datetime

# open
def open_log_file(file_name = None):
file = open('./log_results/' + file_name, 'w', encoding='utf-8')
return file

# close
def close_log_file(file = None):
file.close()

# print
def log(msg = '', file = None, print_msg = True, end = '\n'):
"""
msg 表示打印信息, file 为日志文件对象,
print_msg 表示是否在控制台打印 msg 信息,
end 为写入文件默认结尾
"""
if print_msg:
print(msg)

now = datetime.datetime.now()
t = str(now.year) + '/' + str(now.month) + '/' + str(now.day) + ' ' \
+ str(now.hour).zfill(2) + ':' + str(now.minute).zfill(2) + ':' + str(now.second).zfill(2)

if isinstance(msg, str):
lines = msg.split('\n')
else:
lines = [msg]

for line in lines:
if line == lines[-1]:
file.write('[' + t + ']' + str(line) + end)
else:
file.write('[' + t + ']' + str(line))
21 changes: 21 additions & 0 deletions week1/code/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import torch.nn as nn
import torch.nn.functional as F
# from layers import GraphConvolution
from torch_geometric.nn import GCNConv

class GCN(nn.Module):
def __init__(self, nfeat, nhid, nclass, dropout):
super(GCN, self).__init__()

# self.gc1 = GraphConvolution(nfeat, nhid)
# self.gc2 = GraphConvolution(nhid, nclass)
self.gc1 = GCNConv(nfeat, nhid, normalize=True)
self.gc2 = GCNConv(nhid, nclass, normalize=True)
self.dropout = dropout


def forward(self, x, adj):
x = F.relu(self.gc1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = self.gc2(x, adj)
return F.log_softmax(x, dim=1)
151 changes: 151 additions & 0 deletions week1/code/train_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
from __future__ import division
from __future__ import print_function

import time
import argparse
import numpy as np

import torch
import torch.nn.functional as F
import torch.optim as optim

from utils import load_data, accuracy
from models import GCN

# log
choice = int(input('请选择所使用数据集\n1 为 Cora, 2 为 Citeseer, 3 为 Pumbed:'))
path_name = None
dataset_name = None
import sys
sys.path.append('D:/Kuang/Code/Data/')
from log_file import *
if choice == 1:
file_name = 'Cora_Dataset_Log.txt'
path_name = '/tmp/Cora'
dataset_name = 'Cora'
elif choice == 2:
file_name = 'Citeseer_Dataset_Log.txt'
path_name = '/tmp/Citeseer'
dataset_name = 'Citeseer'
elif choice == 3:
file_name = 'Pumbed_Dataset_Log.txt'
path_name = '/tmp/Pubmed'
dataset_name = 'Pubmed'
else: exit(114514)
file = open_log_file(file_name)


# Training settings
parser = argparse.ArgumentParser()
parser.add_argument('--no-cuda', action='store_true', default=False,
help='Disables CUDA training.')
parser.add_argument('--fastmode', action='store_true', default=False,
help='Validate during training pass.')
parser.add_argument('--seed', type=int, default=42, help='Random seed.')
parser.add_argument('--epochs', type=int, default=100,
help='Number of epochs to train.')
parser.add_argument('--lr', type=float, default=0.01,
help='Initial learning rate.')
parser.add_argument('--weight_decay', type=float, default=5e-4,
help='Weight decay (L2 loss on parameters).')
parser.add_argument('--hidden', type=int, default=16,
help='Number of hidden units.')
parser.add_argument('--dropout', type=float, default=0.5,
help='Dropout rate (1 - keep probability).')

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(path_name, dataset_name)
# from torch.utils.data import DataLoader
from torch_geometric.datasets import Planetoid
dataset = Planetoid(root=path_name, name=dataset_name)
data = dataset[0] # ? just for a test
adj, features, labels = data.edge_index, data.x, data.y
idx_train = range(140)
idx_val = range(200, 500)
idx_test = range(500, 1500)
idx_train = torch.LongTensor(idx_train)
idx_val = torch.LongTensor(idx_val)
idx_test = torch.LongTensor(idx_test)

# Model and optimizer
model = GCN(nfeat=features.shape[1],
nhid=args.hidden,
nclass=labels.max().item() + 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()
labels = labels.cuda()
idx_train = idx_train.cuda()
idx_val = idx_val.cuda()
idx_test = idx_test.cuda()


def train(epoch):
t = time.time()
model.train()
optimizer.zero_grad()
output = model(features, adj)
loss_train = F.nll_loss(output[idx_train], labels[idx_train])
acc_train = accuracy(output[idx_train], labels[idx_train])
loss_train.backward()
optimizer.step()

if not args.fastmode:
# Evaluate validation set performance separately,
# deactivates dropout during validation run.
model.eval()
output = model(features, adj)

loss_val = F.nll_loss(output[idx_val], labels[idx_val])
acc_val = accuracy(output[idx_val], labels[idx_val])
# print('Epoch: {:04d}'.format(epoch+1),
# 'loss_train: {:.4f}'.format(loss_train.item()),
# 'acc_train: {:.4f}'.format(acc_train.item()),
# 'loss_val: {:.4f}'.format(loss_val.item()),
# 'acc_val: {:.4f}'.format(acc_val.item()),
# 'time: {:.4f}s'.format(time.time() - t))
log("[EPOCH: {:3d}/{:d}]".format(epoch + 1, args.epochs) + \
"训练损失为: [{:.4f}]".format(loss_train.item()) + \
"训练精度为: [{:.4f}]".format(acc_train) + \
"验证损失为: [{:.4f}]".format(loss_val.item()) + \
"验证精度为: [{:.4f}]".format(acc_val), file, True)


def test():
model.eval()
output = model(features, adj)
loss_test = F.nll_loss(output[idx_test], labels[idx_test])
acc_test = accuracy(output[idx_test], labels[idx_test])
# print("Test set results:",
# "loss= {:.4f}".format(loss_test.item()),
# "accuracy= {:.4f}".format(acc_test.item()))
log("\n\n测试损失为: [{:.4f}]".format(loss_test.item()) + \
"测试精度为: [{:.4f}]".format(acc_test), file, True)


# Train model
t_total = time.time()
for epoch in range(args.epochs):
train(epoch)
print("Optimization Finished!")
print("Total time elapsed: {:.4f}s".format(time.time() - t_total))

# Testing
test()

torch.save(model, './Model/Cora_Model.pth')
close_log_file(file)
144 changes: 144 additions & 0 deletions week1/code/train_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
from __future__ import division
from __future__ import print_function

import time
import argparse
import numpy as np

import torch
import torch.nn.functional as F
import torch.optim as optim

# from utils import load_data, accuracy
from models import GCN

# log
choice = int(input('请选择所使用数据集\n1 为 Cora, 2 为 Citeseer, 3 为 Pumbed:'))
path_name = None
dataset_name = None
import sys
sys.path.append('D:/Kuang/Code/Data/')
from log_file import *
if choice == 1:
file_name = 'Cora_Dataset_Log.txt'
path_name = '/tmp/Cora'
dataset_name = 'Cora'
elif choice == 2:
file_name = 'Citeseer_Dataset_Log.txt'
path_name = './tmp/Citeseer/'
dataset_name = 'Citeseer'
elif choice == 3:
file_name = 'Pumbed_Dataset_Log.txt'
path_name = '/tmp/Pubmed'
dataset_name = 'Pubmed'
else: exit(114514)
file = open_log_file(file_name)

# utils
def accuracy(output, labels):
preds = output.max(1)[1].type_as(labels)
correct = preds.eq(labels).double()
correct = correct.sum()
return correct / len(labels)

# Training settings
parser = argparse.ArgumentParser()
parser.add_argument('--no-cuda', action='store_true', default=False,
help='Disables CUDA training.')
parser.add_argument('--fastmode', action='store_true', default=False,
help='Validate during training pass.')
parser.add_argument('--seed', type=int, default=42, help='Random seed.')
parser.add_argument('--epochs', type=int, default=100,
help='Number of epochs to train.')
parser.add_argument('--lr', type=float, default=0.01,
help='Initial learning rate.')
parser.add_argument('--weight_decay', type=float, default=5e-4,
help='Weight decay (L2 loss on parameters).')
parser.add_argument('--hidden', type=int, default=32,
help='Number of hidden units.')
parser.add_argument('--dropout', type=float, default=0.5,
help='Dropout rate (1 - keep probability).')

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(path_name, dataset_name)
# from torch.utils.data import DataLoader
from torch_geometric.datasets import Planetoid
import torch_geometric.transforms as T
dataset = Planetoid(root=path_name, name=dataset_name, transform=T.NormalizeFeatures())
data = dataset[0]
adj, features, labels = data.edge_index, data.x, data.y


# Model and optimizer
model = GCN(nfeat=features.shape[1],
nhid=args.hidden,
nclass=labels.max().item() + 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()
labels = labels.cuda()
# idx_ train = idx_train.cuda()
# idx_val = idx_val.cuda()
# idx_test = idx_test.cuda()


def train(epoch):
t = time.time()
model.train()
optimizer.zero_grad()
output = model(features, adj)
loss_train = F.nll_loss(output[data.train_mask], labels[data.train_mask])
acc_train = accuracy(output[data.train_mask], labels[data.train_mask])
loss_train.backward()
optimizer.step()

if not args.fastmode:
# Evaluate validation set performance separately,
# deactivates dropout during validation run.
model.eval()
output = model(features, adj)

loss_val = F.nll_loss(output[data.val_mask], labels[data.val_mask])
acc_val = accuracy(output[data.val_mask], labels[data.val_mask])
log("[EPOCH: {:3d}/{:d}]".format(epoch + 1, args.epochs) + \
"训练损失为: [{:.4f}]".format(loss_train.item()) + \
"训练精度为: [{:.4f}]".format(acc_train) + \
"验证损失为: [{:.4f}]".format(loss_val.item()) + \
"验证精度为: [{:.4f}]".format(acc_val), file, True)


def test():
model = torch.load('./Model/Cora_Model.pth')
model.eval()
output = model(features, adj)
loss_test = F.nll_loss(output[data.test_mask], labels[data.test_mask])
acc_test = accuracy(output[data.test_mask], labels[data.test_mask])
log("\n\n测试损失为: [{:.4f}]".format(loss_test.item()) + \
"测试精度为: [{:.4f}]".format(acc_test), file, True)


# Train model
t_total = time.time()
for epoch in range(args.epochs):
train(epoch)
print("Optimization Finished!")
print("Total time elapsed: {:.4f}s".format(time.time() - t_total))

# Testing
test()

close_log_file(file)
Loading