# How to make the GCN output to be of binary type

**URL:** <https://discuss.pytorch.org/t/how-to-make-the-gcn-output-to-be-of-binary-type/200137>\
**Category:** torch.package / torch::deploy\
**Created:** [April 3, 2024, 4:08am UTC](https://discuss.pytorch.org/t/how-to-make-the-gcn-output-to-be-of-binary-type/200137 "2024-04-03T04:08:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ElenaRz](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/35a633/32.png) [@ElenaRz](https://discuss.pytorch.org/u/ElenaRz)\
**Post date:** [April 3, 2024, 4:08am UTC](https://discuss.pytorch.org/t/how-to-make-the-gcn-output-to-be-of-binary-type/200137/1 "2024-04-03T04:08:00Z")

</div>

Hello. I am very new in PyTorch graph Neural Network. I have multiple graphs and I want to do node classification. I divided the graphs to 80% of them for training and 10% for evaluation and 10% for testing. I tried to use GCN to classify nodes. To do that, I implemented the following code:

class GCN(torch.nn.Module):  
def **init** (self, in\_channels, hidden\_channels, out\_channels):  
super(). **init** ()  
self.conv1 = GCNConv(in\_channels, hidden\_channels)  
self.conv2 = GCNConv(hidden\_channels, out\_channels)  
# Define the linear layer  
self.linear = torch.nn.Linear(out\_channels, out\_channels)

```
def forward(self, x,edge_index,edge_weight):
    # x, edge_index = data.x, data.edge_index

    x = self.conv1(x, edge_index,edge_weight)
    x = F.relu(x)
    x = F.dropout(x, training=self.training)
    x = self.conv2(x, edge_index, edge_weight)
    # Apply the ReLU activation function
    x = F.relu(x)
    # Apply the linear layer
    x = self.linear(x)
    return F.log_softmax(x, dim=1)

```

device = torch.device(‘cuda’ if torch.cuda.is\_available() else ‘cpu’)

in\_channels = gInfo.getNodeNumberOfFeatures() # Number of input features per node

hidden\_channels = gInfo.getNodeNumberOfFeatures()+4 # Number of output features for the first convolutional layer

max\_num\_classes = 2 # Since the target output can be either 0 or 1

# Initialize the GCN model with the maximum number of classes

model = GCN(in\_channels, hidden\_channels, max\_num\_classes).to(device)

# Use Binary Cross Entropy Loss for scenarios where target has only one class

binary\_loss\_op = torch.nn.BCEWithLogitsLoss()

criterion = torch.nn.BCELoss()  
optimizer = torch.optim.Adam(model.parameters(), lr=0.005, weight\_decay=5e-4)

def train():  
model.train()

```
total_loss = 0
for data in train_loader:
    data = data.to(device)
    data.requires_grad_(True)
    optimizer.zero_grad()
    output = model(data.x, data.edge_index, data.edge_weight)
    # Calculate loss
    loss = binary_loss_op(output, data.y.squeeze(dim=1))
    total_loss += loss.item() * data.num_graphs
    loss.backward()
    optimizer.step()
return total_loss / len(train_loader.dataset)

```

Now, here for the loss part, the output has 2 dimensions and the data.y is only 0 or 1. I want to know how can I output to become 0 or 1 so to be able to compare with data.y?
