第一次尝试-
from spektral.datasets import Citation
import numpy as np
import pickle as pkl
import networkx as nx
import scipy.sparse as sp
from scipy.sparse.linalg.eigen.arpack import eigsh
import sys
import scipy.io as scio
from scipy.io import loadmat
from scipy.sparse import coo_matrix
import tensorflow as tf
def parse_index_file(filename):
"""Parse index file."""
index = []
for line in open(filename):
index.append(int(line.strip()))
return index
def sample_mask(idx, l):
"""Create mask."""
mask = np.zeros(l)
mask[idx] = 1
return np.array(mask, dtype=np.bool)
def load_data(dataset_str):
"""
Loads input data from gcn/data directory
ind.dataset_str.x => the feature vectors of the training instances as scipy.sparse.csr.csr_matrix object;
ind.dataset_str.tx => the feature vectors of the test instances as scipy.sparse.csr.csr_matrix object;
ind.dataset_str.allx => the feature vectors of both labeled and unlabeled training instances
(a superset of ind.dataset_str.x) as scipy.sparse.csr.csr_matrix object;
ind.dataset_str.y => the one-hot labels of the labeled training instances as numpy.ndarray object;
ind.dataset_str.ty => the one-hot labels of the test instances as numpy.ndarray object;
ind.dataset_str.ally => the labels for instances in ind.dataset_str.allx as numpy.ndarray object;
ind.dataset_str.graph => a dict in the format {index: [index_of_neighbor_nodes]} as collections.defaultdict
object;
ind.dataset_str.test.index => the indices of test instances in graph, for the inductive setting as list object.
All objects above must be saved using python pickle module.
:param dataset_str: Dataset name
:return: All data input files loaded (as well the training/test data).
"""
names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
objects = []
for i in range(len(names)):
with open("/home/stone/.spektral/datasets/Citation/cora/ind.{}.{}".format(dataset_str, names[i]), 'rb') as f:
if sys.version_info > (3, 0):
objects.append(pkl.load(f, encoding='latin1'))
else:
objects.append(pkl.load(f))
x, y, tx, ty, allx, ally, graph = tuple(objects)
test_idx_reorder = parse_index_file("/home/stone/.spektral/datasets/Citation/cora/ind.{}.test.index".format(dataset_str))
test_idx_range = np.sort(test_idx_reorder)
if dataset_str == 'citeseer':
test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder)+1)
tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1]))
tx_extended[test_idx_range-min(test_idx_range), :] = tx
tx = tx_extended
ty_extended = np.zeros((len(test_idx_range_full), y.shape[1]))
ty_extended[test_idx_range-min(test_idx_range), :] = ty
ty = ty_extended
features = sp.vstack((allx, tx)).tolil()
features[test_idx_reorder, :] = features[test_idx_range, :]
adj = nx.adjacency_matrix(nx.from_dict_of_lists(graph))
labels = np.vstack((ally, ty))
labels[test_idx_reorder, :] = labels[test_idx_range, :]
idx_test = test_idx_range.tolist()
idx_train = range(len(y))
idx_val = range(len(y), len(y)+500)
train_mask = sample_mask(idx_train, labels.shape[0])
val_mask = sample_mask(idx_val, labels.shape[0])
test_mask = sample_mask(idx_test, labels.shape[0])
y_train = np.zeros(labels.shape)
y_val = np.zeros(labels.shape)
y_test = np.zeros(labels.shape)
y_train[train_mask, :] = labels[train_mask, :]
y_val[val_mask, :] = labels[val_mask, :]
y_test[test_mask, :] = labels[test_mask, :]
return adj, features, labels, train_mask, val_mask, test_mask
def normalize_adj(adj):
"""Symmetrically normalize adjacency matrix."""
adj = sp.coo_matrix(adj)
rowsum = np.array(adj.sum(1))
d_inv_sqrt = np.power(rowsum, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
return adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()
def preprocess_adj(adj):
"""Preprocessing of adjacency matrix for simple GCN model and conversion to tuple representation."""
adj_normalized = normalize_adj(adj + sp.eye(adj.shape[0]))
return sparse_to_tuple(adj_normalized)
ALL_W= scio.loadmat('ALL_W.mat')
ALL_X= scio.loadmat('ALL_X.mat')
ALL_Y = scio.loadmat('ALL_Y.mat')
A=ALL_W['ALL_W']
X=ALL_X['ALL_X']
y=ALL_Y['ALL_Y']
A=coo_matrix(A)
num=[198,190,192,188,186,182,196,191,193,191,181,192,184,181,187]
X= X / X.max(1, keepdims=True)
y=y.squeeze()
train_mask = np.zeros(len(y), dtype=np.bool)
val_mask = np.zeros(len(y), dtype=np.bool)
test_mask = np.zeros(len(y), dtype=np.bool)
for i in range(0,15,1):
temp_index=np.where(y==i)
index=temp_index[0]
length=len(index)
Desperation=0.10*length
Des=int(Desperation)
Desperation1=0.5*length
Des1=int(Desperation1)
np.random.shuffle(index)
index2 = index[num[i]::1]
index1=index[num[i]::1]
index = index[0:num[i]:1]
train_mask[index]=True
val_mask[index1] =True
test_mask[index2]=True
y= tf.one_hot(y,15,1,0)
N = A.shape[0]
F = X.shape[-1]
n_classes = y.shape[-1]
print(n_classes)
from spektral.layers import GCNConv
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dropout
A = GCNConv.preprocess(A).astype('f4')
print("\n-----------\n",A)
X_in = Input(shape=(F, ))
A_in = Input((N, ), sparse=True)
X_1 = GCNConv(128, 'relu')([X_in, A_in])
X_1 = Dropout(0.5)(X_1)
X_2 = GCNConv(n_classes, 'softmax')([X_1, A_in])
model = Model(inputs=[X_in, A_in], outputs=X_2)
model.compile(optimizer='adam',
loss='categorical_crossentropy',
weighted_metrics=['acc'])
model.summary()
A = A.astype('f4')
validation_data = ([X, A], y, val_mask)
model.fit([X, A], y,
epochs=200,
sample_weight=train_mask,
validation_data=validation_data,
batch_size=N,
shuffle=False)
eval_results = model.evaluate([X, A],
y,
sample_weight=test_mask,
batch_size=N)
print(eval_results)
第二次尝试
import numpy as np
import tensorflow as tf
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.losses import CategoricalCrossentropy
from tensorflow.keras.optimizers import Adam
from spektral.data.loaders import SingleLoader
from spektral.datasets.citation import Citation
from spektral.layers import GCNConv
from spektral.models.gcn import GCN
from spektral.transforms import AdjToSpTensor, LayerPreprocess
SAVA_PATH = './file/'
X=np.load(SAVA_PATH +'X.npy', allow_pickle=True)
y=np.load(SAVA_PATH +'Y.npy', allow_pickle=True)
A=np.load(SAVA_PATH +'A.npy', allow_pickle=True)
tr_mask=np.load(SAVA_PATH +'mask_tr.npy', allow_pickle=True)
val_mask=np.load(SAVA_PATH +'mask_val.npy', allow_pickle=True)
validation_data = ([X, A], y, val_mask)
learning_rate = 0.001
seed = 0
epochs = 200
patience = 10
tf.random.set_seed(seed=seed)
model = GCN(n_labels=15, n_input_channels=144)
model.compile(
optimizer=Adam(learning_rate),
loss=CategoricalCrossentropy(reduction="sum"),
weighted_metrics=["acc"],
)
model.fit(
[X, A], y,
steps_per_epoch=1,
validation_data=validation_data,
validation_steps=1,
epochs=epochs,
callbacks=[EarlyStopping(patience=patience, restore_best_weights=True)],
)
第三次尝试
from spektral.datasets import Citation
import numpy as np
import pickle as pkl
import networkx as nx
import scipy.sparse as sp
from scipy.sparse.linalg.eigen.arpack import eigsh
import sys
import scipy.io as scio
from scipy.io import loadmat
from scipy.sparse import coo_matrix
import tensorflow as tf
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dropout
from tensorflow.keras import backend as K
from spektral.layers import ops
from spektral.layers.convolutional.conv import Conv
from spektral.utils import gcn_filter
class GCNConv(Conv):
def __init__(
self,
channels,
activation=None,
use_bias=True,
kernel_initializer="glorot_uniform",
bias_initializer="zeros",
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
**kwargs
):
super().__init__(
activation=activation,
use_bias=use_bias,
kernel_initializer=kernel_initializer,
bias_initializer=bias_initializer,
kernel_regularizer=kernel_regularizer,
bias_regularizer=bias_regularizer,
activity_regularizer=activity_regularizer,
kernel_constraint=kernel_constraint,
bias_constraint=bias_constraint,
**kwargs
)
self.channels = channels
def build(self, input_shape):
assert len(input_shape) >= 2
input_dim = input_shape[0][-1]
self.kernel = self.add_weight(
shape=(input_dim, self.channels),
initializer=self.kernel_initializer,
name="kernel",
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint,
)
if self.use_bias:
self.bias = self.add_weight(
shape=(self.channels,),
initializer=self.bias_initializer,
name="bias",
regularizer=self.bias_regularizer,
constraint=self.bias_constraint,
)
self.built = True
def call(self, inputs):
x, a = inputs
output = K.dot(x, self.kernel)
output = ops.modal_dot(a, output)
if self.use_bias:
output = K.bias_add(output, self.bias)
return output
@property
def config(self):
return {
"channels": self.channels}
@staticmethod
def preprocess(a):
return gcn_filter(a)
SAVA_PATH = './file/'
X=np.load(SAVA_PATH +'X.npy', allow_pickle=True)
y=np.load(SAVA_PATH +'Y.npy', allow_pickle=True)
A=np.load(SAVA_PATH +'A.npy', allow_pickle=True)
X= X / X.max(1, keepdims=True)
A=coo_matrix(A)
train_mask=np.load(SAVA_PATH +'mask_tr.npy', allow_pickle=True)
val_mask=np.load(SAVA_PATH +'mask_val.npy', allow_pickle=True)
test_mask=np.load(SAVA_PATH +'mask_test.npy', allow_pickle=True)
N = A.shape[0]
F = X.shape[-1]
n_classes = y.shape[-1]
A = GCNConv.preprocess(A)
X_in = Input(shape=(F, ))
A_in = Input((N, ), sparse=True)
X_1 = tf.keras.layers.BatchNormalization()(X_in)
X_1 = GCNConv(128, 'relu')([X_in, A_in])
X_1= tf.keras.layers.BatchNormalization(axis=1,momentum=0.9)(X_1)
X_1=tf.keras.layers.Activation('relu')(X_1)
X_1= tf.keras.layers.BatchNormalization(axis=1,momentum=0.9)(X_1)
X_2 = GCNConv(n_classes, 'softmax')([X_1, A_in])
X_2=tf.keras.layers.Activation('softmax')(X_2)
model = Model(inputs=[X_in, A_in], outputs=X_2)
adam=Adam(lr=0.001,decay=0.001)
model.compile(optimizer=adam,
loss='categorical_crossentropy',
weighted_metrics=['acc'])
model.summary()
A = A.astype('f4')
validation_data = ([X, A], y, val_mask)
model.fit([X, A], y,
epochs=200,
sample_weight=train_mask,
validation_data=validation_data,
batch_size=N,
shuffle=False)
eval_results = model.evaluate([X, A],y,
sample_weight=test_mask,
batch_size=N)
print(eval_results)