PROBLEMS WORKING WITH OWN DATASET (GCn) #5519
Replies: 7 comments 9 replies
-
@rusty1s Can u help me out with issues |
Beta Was this translation helpful? Give feedback.
-
x_h = torch.tensor(x_h.values, dtype=torch.float)
|
Beta Was this translation helpful? Give feedback.
-
more errors sir:KeyError Traceback (most recent call last) 4 frames During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) AttributeError: 'GlobalStorage' object has no attribute 'train_mask' ANY SOLUTIONS? |
Beta Was this translation helpful? Give feedback.
-
hello sir @EdisonLeeeee |
Beta Was this translation helpful? Give feedback.
-
import pandas as pd Load the datax = pd.read_csv('tempdataset.csv', index_col=0) Drop unnecessary columns from xx = x.drop(columns=['ID', 'Name', 'Screen Name', 'Location']) Convert data to numpy arraysx = x.to_numpy() Encode edge_index columns as integersle = LabelEncoder() Convert data to tensorsx = torch.tensor(x, dtype=torch.float) Create a PyTorch Geometric Data objectdata = Data(x=x, edge_index=edge_index, y=y) Split the data into train, val, and test setsdata = T.RandomNodeSplit(num_val=0.2, num_test=0.4)(data) Set devicedevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu') Define the GCN modelclass GCN(torch.nn.Module):
Move model and data to the designated devicemodel = GCN(data.x.size(-1), 64, data.y.max().item()+1) Set up the optimizeroptimizer = torch.optim.Adam(model.parameters(), lr=0.01) Define the training and evaluation functionsdef train(): @torch.no_grad()
Train and evaluate the modelfor epoch in range(1, 101): this is my code |
Beta Was this translation helpful? Give feedback.
-
import pandas as pd Load the datax = pd.read_csv('tempdataset.csv', index_col=0) Drop unnecessary columns from xx = x.drop(columns=['ID', 'Name', 'Screen Name', 'Location']) Convert data to numpy arraysx = x.to_numpy() Encode edge_index columns as integersle = LabelEncoder() Convert data to tensorsx = torch.tensor(x, dtype=torch.float) Create a PyTorch Geometric Data objectdata = Data(x=x, edge_index=edge_index, y=y) Split the data into train, val, and test setsdata = T.RandomNodeSplit(num_val=0.2, num_test=0.4)(data) Set devicedevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu') Define the GCN modelclass GCN(torch.nn.Module):
Move model and data to the designated devicemodel = GCN(data.x.size(-1), 64, data.y.max().item()+1) Set up the optimizeroptimizer = torch.optim.Adam(model.parameters(), lr=0.01) Define the training and evaluation functionsdef train(): @torch.no_grad()
Train and evaluate the modelfor epoch in range(1, 101): this is the entire code ihave been using . actually i am trying to perform node classification using GCN. i am using a custom dataset. i made a twitter dataset, and my goal is bot detection. the three data files which are given as input in the code are : x, edge_index, y. x has features of users like Name, Screen Name, Friends , Followers, Favorites, Location, Tweets, ID. 'edge_index' describes the link or to be precise edges. finally 'y' is label which specifies whether the user is human or bot ( 1 for bot and 0 for human). this is all about my dataset. for some reason i am not able to execute the code. ERROR: RuntimeError: index 114 is out of bounds for dimension 0 with size 114 |
Beta Was this translation helpful? Give feedback.
-
Hi @AnotherAvenger, I have this dataset file and I want to train a GCN with 1 hidden layer only, Can you help me out how to load the data and give it to this current model here |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
e_x
source target
0 0.0 1.0
1 0.0 2.0
2 0.0 3.0
3 0.0 4.0
4 0.0 5.0
5 0.0 6.0
6 0.0 7.0
7 0.0 8.0
8 0.0 8.5
9 0.0 9.0
e_x=torch.tensor(e_x.values,dtype=torch.long)
e_x
tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 2, 3, 4, 5, 6, 7, 8, 8, 9]])
x_h=pd.DataFrame(([0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0174, 0.0000, 0.0248,
0.0000], [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0174, 0.0000, 0.0248,
0.0000] , [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0174, 0.0000, 0.0248,
0.0000] , [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0174, 0.0000, 0.0248,
0.0000] , [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0174, 0.0000, 0.0248,
0.0000], [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0174, 0.0000, 0.0248,
0.0000] , [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0174, 0.0000, 0.0248,
0.0000], [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0174, 0.0000, 0.0248,
0.0000] , [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0174, 0.0000, 0.0248,
0.0000], [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0174, 0.0000, 0.0248,
0.0000] ))
x_h= torch.tensor(x_h.values, dtype=torch.long)
from torch_geometric.data import Data
data = Data(x=x_h, edge_index=e_x)
import torch
from torch.nn import Linear
from torch_geometric.nn import GCNConv
class GCN(torch.nn.Module):
def init(self):
super().init()
torch.manual_seed(1234)
self.conv1 = GCNConv(10, 4)
self.conv2 = GCNConv(4, 4)
self.conv3 = GCNConv(4, 2)
self.classifier = Linear(2, 2)
model = GCN()
print(model)
GCN(
(conv1): GCNConv(10, 4)
(conv2): GCNConv(4, 4)
(conv3): GCNConv(4, 2)
(classifier): Linear(in_features=2, out_features=2, bias=True)
)
from IPython.display import Javascript # Restrict height of output cell.
display(Javascript('''google.colab.output.setIframeHeight(0, true, {maxHeight: 300})'''))
model = GCN()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
criterion = torch.nn.CrossEntropyLoss()
def train():
model.train()
optimizer.zero_grad() # Clear gradients.
out = model(data.x, data.edge_index) # Perform a single forward pass.
loss = criterion(out[data.train_mask], data.y) # Compute the loss solely based on the training nodes.
loss.backward() # Derive gradients.
optimizer.step() # Update parameters based on gradients.
return loss
def test():
model.eval()
out = model(data.x, data.edge_index)
pred = out.argmax(dim=1) # Use the class with highest probability.
test_correct = pred[data.test_mask] == data.y[data.test_mask] # Check against ground-truth labels.
test_acc = int(test_correct.sum()) / int(data.test_mask.sum()) # Derive ratio of correct predictions.
return test_acc
for epoch in range(1, 101):
loss = train()
print(f'Epoch: {epoch:03d}, Loss: {loss:.4f}')
AND THE ERRORS I GET ARE:
RuntimeError Traceback (most recent call last)
in
25
26 for epoch in range(1, 101):
---> 27 loss = train()
28 print(f'Epoch: {epoch:03d}, Loss: {loss:.4f}')
6 frames
in train()
9 model.train()
10 optimizer.zero_grad() # Clear gradients.
---> 11 out = model(data.x, data.edge_index) # Perform a single forward pass.
12 loss = criterion(out[data.train_mask], data.y) # Compute the loss solely based on the training nodes.
13 loss.backward() # Derive gradients.
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []
in forward(self, x, edge_index)
14
15 def forward(self, x, edge_index):
---> 16 h = self.conv1(x, edge_index)
17 h = h.tanh()
18 h = self.conv2(h, edge_index)
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []
/usr/local/lib/python3.7/dist-packages/torch_geometric/nn/conv/gcn_conv.py in forward(self, x, edge_index, edge_weight)
192 edge_index = cache
193
--> 194 x = self.lin(x)
195
196 # propagate_type: (x: Tensor, edge_weight: OptTensor)
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []
/usr/local/lib/python3.7/dist-packages/torch_geometric/nn/dense/linear.py in forward(self, x)
116 x (Tensor): The features.
117 """
--> 118 return F.linear(x, self.weight, self.bias)
119
120 @torch.no_grad()
RuntimeError: expected scalar type Long but found Float
I AM COMPLETELY STRUCK HERE. AND MY COMMUNITY PLEASE HELP ME OUT WITH SOLUTION
Beta Was this translation helpful? Give feedback.
All reactions