Dear all,
I run the following code and it works fine. Only the visualisation (out.dteach() command in def visualize(h, color) does not work.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 18 14:14:00 2021
@author: neurolab
"""
import os.path as osp
import torch
import torch.nn as nn
from torch_geometric.datasets import Planetoid
from torch_geometric.nn import GCNConv, DeepGraphInfomax
from torch_geometric.transforms import NormalizeFeatures
import numpy as np
LL = np.load('/home/neurolab2/GCN/sub-20Corr_LL.npy')
np.shape(LL)
from torch_geometric.data import Data
torch.Size([3352*3352, 2])
edge_index = torch.empty([0, 2])
x = torch.empty([0, 1])
edge_attr = torch.empty([0, 1])
counter = 1
for i in range (1,10):
for j in range (1,10):
#row = LL[i,:]
#column = LL[1,:]
print(i)
print(j)
#a = np.array([i, j])
#c = np.concatenate((c[i,:], a[i,:]), axis=1)
edge_index_temp = torch.tensor([[i, j]], dtype=torch.long)
edge_index = torch.cat((edge_index, edge_index_temp))
x_temp = torch.tensor([[counter]], dtype=torch.float)
x = torch.cat((x, x_temp))
counter = counter+1
corr_value = LL[i,j]
edge_attr_temp = torch.tensor([[corr_value]], dtype=torch.float)
edge_attr = torch.cat((edge_attr, edge_attr_temp))
#y = torch.tensor([0, 1, 1, 1, 1, 1, 1, 1, 1], dtype=torch.long)
y = (torch.rand(size=(1,81)) < 0.25).long().squeeze(0)
#y_bool = (torch.rand(size=(1,81)) < 0.25).boolean()
#train_mask = (torch.rand(size=(0,81)) < 0.25).long()
train_mask = (torch.rand(size=(1,81)) < 0.25).long().squeeze(0)
test_mask = (torch.rand(size=(1,81)) < 0.25).long().squeeze(0)
#train_mask = torch.gt(train_mask, 0)
#test_mask = torch.gt(y, 1)
edge_index =torch.tensor(edge_index, dtype=torch.long)
edge_index = torch.transpose(edge_index, 0, 1)
x_dummy = torch.tensor([[1]])
#x = torch.cat((x, x_temp)) ##need one more entry in x than in edge_index
data_HGG = Data(x=x, y=y, train_mask=train_mask, test_mask=test_mask, edge_attr=edge_attr, edge_index=edge_index)#, train_mask=2, test_mask=2, val_mask=2)
data = data_HGG
class Encoder(nn.Module):
def __init__(self, in_channels, hidden_channels):
super(Encoder, self).__init__()
self.conv = GCNConv(in_channels, hidden_channels, cached=True)
self.prelu = nn.PReLU(hidden_channels)
def forward(self, x, edge_index):
x = self.conv(x, edge_index)
x = self.prelu(x)
return x
def corruption(x, edge_index):
return x[torch.randperm(x.size(0))], edge_index
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = DeepGraphInfomax(
hidden_channels=81, encoder=Encoder(1, 81),
#hidden_channels=512, encoder=Encoder(dataset.num_features, 512),
summary=lambda z, *args, **kwargs: torch.sigmoid(z.mean(dim=0)),
corruption=corruption).to(device)
#data = dataset[0].to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
def train():
model.train() #wo wird das ausgeführt?
optimizer.zero_grad()
pos_z, neg_z, summary = model(data.x, data.edge_index) ###where does model go with data.x,..
loss = model.loss(pos_z, neg_z, summary)
loss.backward()
optimizer.step()
return loss.item()
def test():
model.eval()
z, _, _ = model(data.x, data.edge_index)
acc = model.test(z[data.train_mask], data.y,
z[data.test_mask], data.y, max_iter=150)
return acc
for epoch in range(1, 301):
loss = train()
print('Epoch: {:03d}, Loss: {:.4f}'.format(epoch, loss))
acc = test()
print('Accuracy: {:.4f}'.format(acc))
#model.eval()
# Helper function for visualization.
#%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
def visualize(h, color):
z = TSNE(n_components=2).fit_transform(*out.detach()*.cpu().numpy())
plt.figure(figsize=(10,10))
plt.xticks([])
plt.yticks([])
plt.scatter(z[:, 0], z[:, 1], s=70, c=color, cmap="Set2")
plt.show()
out = model(data.x, data.edge_index)
visualize(out, color='green')