Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Binary file added large/Indian_pines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added large/__pycache__/build_graph.cpython-39.pyc
Binary file not shown.
Binary file added large/__pycache__/data_utils.cpython-39.pyc
Binary file not shown.
Binary file added large/__pycache__/dataset.cpython-39.pyc
Binary file not shown.
Binary file added large/__pycache__/eval.cpython-39.pyc
Binary file not shown.
Binary file added large/__pycache__/gnns.cpython-39.pyc
Binary file not shown.
Binary file added large/__pycache__/logger.cpython-39.pyc
Binary file not shown.
Binary file added large/__pycache__/ours.cpython-39.pyc
Binary file not shown.
Binary file added large/__pycache__/parse.cpython-39.pyc
Binary file not shown.
7 changes: 7 additions & 0 deletions large/arxiv.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@echo off
echo Running ogbn-arxiv...
python main.py --method sgformer --dataset ogbn-arxiv --metric acc --lr 0.001 --hidden_channels 256 --use_graph --graph_weight 0.5 ^
--gnn_num_layers 3 --gnn_dropout 0.5 --gnn_weight_decay 0.0 --gnn_use_residual --gnn_use_weight --gnn_use_bn --gnn_use_act ^
--trans_num_layers 1 --trans_dropout 0.5 --trans_weight_decay 0.0 --trans_use_residual --trans_use_weight --trans_use_bn ^
--seed 123 --runs 5 --epochs 1000 --eval_step 9 --device 0 --data_dir D:\ogb_dataset\ogb\arxiv
pause
Binary file added large/best_model.pt
Binary file not shown.
126 changes: 126 additions & 0 deletions large/build_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import numpy as np
import torch
from torch import long
from sklearn.metrics import pairwise_distances
from sklearn.neighbors import NearestNeighbors
from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.preprocessing import StandardScaler


def build_graph_by_pos(height: long, width: long, node_features=None):
"""
简单的八向建图 8*height*width
"""
edge_index = []
for i in range(height):
for j in range(width):
node_idx = i * width + j
if i > 0: # up
edge_index.append([node_idx, node_idx - width])
if i < height - 1: # down
edge_index.append([node_idx, node_idx + width])
if j > 0: # left
edge_index.append([node_idx, node_idx - 1])
if j < width - 1: # right
edge_index.append([node_idx, node_idx + 1])
if i > 0 and j > 0: # left and up
edge_index.append([node_idx, node_idx - width - 1])
if i > 0 and j < width - 1: # right and up
edge_index.append([node_idx, node_idx - width + 1])
if i < height - 1 and j > 0: # left and down
edge_index.append([node_idx, node_idx + width - 1])
if i < height - 1 and j < width - 1: # right and down
edge_index.append([node_idx, node_idx + width + 1])
edge_index = torch.tensor(edge_index, dtype=torch.long).t()
return edge_index

def build_graph_by_Knn(height: long, width: long, node_features=None, k=10):
num_nodes = height * width
edge_index = []
nbrs = NearestNeighbors(n_neighbors=k, algorithm='auto').fit(node_features)
distances, indices = nbrs.kneighbors(node_features)

# 遍历每个节点的近邻
for i in range(num_nodes):
for j in range(1, k): # 从1开始,跳过自己
neighbor_index = indices[i][j]
edge_index.append((i, neighbor_index))

# 将边列表转换为 NumPy 数组
edge_index = torch.tensor(edge_index, dtype=torch.long).T # 转置为 [2, num_edges]

return edge_index


def get_weight(node_features, edge_index, sigma=10):
"""根据节点特征和边索引计算边权重,并返回张量形式的权重"""
num_edges = edge_index.shape[1] # 边的数量
edge_weights = torch.zeros(num_edges, dtype=torch.float)

def gaussian_kernel(x, y, sig=1.0):
return torch.exp(-torch.norm(x - y) ** 2 / (sig ** 2))

# 遍历每条边
for i in range(num_edges):
start_node = edge_index[0, i] # 从节点
end_node = edge_index[1, i] # 到节点

# 提取节点特征
x_i = node_features[start_node] # 从节点特征
x_j = node_features[end_node] # 到节点特征

# 计算高斯核权重
edge_weights[i] = gaussian_kernel(x_i, x_j, sigma)

return edge_weights


def build_graph_by_fix(node_fea, ground_truth, train_idx, row, col):
"""
先对数据进行LDA降维之后 对降维光谱特征+位置特征进行knn建图
主要是加入pos 但是单纯的加入pos对于欧式距离的衡量改变不大,所以先降维成channel再增加pos信息
这样pos信息所占比重就能上升,以此查看与单纯的220个波段的knn之间的区别
"""
edge_index = []
k = 15 # 邻居数量
weight_factor = 15 # 权重系数

x = node_fea[train_idx]
y = ground_truth[train_idx]
lda = LinearDiscriminantAnalysis()
# 调用fit方法 对训练集像素点的特征和标签进行拟合
lda.fit(x, y - 1)
x_new = lda.transform(node_fea)

all_indices = np.arange(x_new.shape[0]) # 计算 x_new 中所有节点的索引
x_coords = all_indices // col # 计算 x 坐标
y_coords = all_indices % col # 计算 y 坐标

# 将坐标信息添加到 x_new 中
coords = np.column_stack((x_coords, y_coords)) # 合并 x 和 y 坐标
x_new_with_coords = np.hstack((x_new, coords)) # 将坐标与 x_new 合并

# 进行最小-最大标准化
x_min = np.min(x_new_with_coords, axis=0) # 每列的最小值
x_max = np.max(x_new_with_coords, axis=0) # 每列的最大值

# 标准化
x_normalized = (x_new_with_coords - x_min) / (x_max - x_min)

x_weighted = x_normalized.copy()
x_weighted[:, -2:] *= weight_factor # 增加最后两个维度的权重

knn = NearestNeighbors(n_neighbors=k, algorithm='auto').fit(x_weighted)
distances, indices = knn.kneighbors(x_weighted)

# 遍历每个节点的近邻
for i in range(row * col):
for j in range(1, k): # 从1开始,跳过自己
neighbor_index = indices[i][j]
edge_index.append((i, neighbor_index))

# 将边列表转换为 NumPy 数组
edge_index = torch.tensor(edge_index, dtype=torch.long).T # 转置为 [2, num_edges]

return edge_index
Loading