AttributeError: 'tuple' object has no attribute 'detach'

Dear all,

I run the following code and it works fine. Only the visualisation (out.dteach() command in def visualize(h, color):slight_smile: 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')


from 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,:]

        #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 =, edge_index_temp))

        x_temp = torch.tensor([[counter]], dtype=torch.float)
        x =, x_temp))
        counter = counter+1

        corr_value = LL[i,j]

        edge_attr_temp = torch.tensor([[corr_value]], dtype=torch.float)
        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 =, 0)
#test_mask =, 1)

edge_index  =torch.tensor(edge_index, dtype=torch.long)

edge_index = torch.transpose(edge_index, 0, 1)

x_dummy = torch.tensor([[1]])

#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)),
#data = dataset[0].to(device)

optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

def train():
    model.train() #wo wird das ausgeführt?
    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)
    return loss.item()

def test():
    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))


# 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.scatter(z[:, 0], z[:, 1], s=70, c=color, cmap="Set2")

out = model(data.x, data.edge_index)
visualize(out, color='green')

Look at the DeepGraphInfomax implementation to see, that it returns a tuple. Pick one of the tensors in the returned tuple and visualize that.

Thanks for your reply. I am sorry, I think I do not understand fully what you mean.

Okay, works now. Thanks