text: import torch
  from torch.nn import Parameter
  import torch.nn.functional as F
  from .message_passing import MessagePassing
  from torch_geometric.utils import softmax
  import math
  import numpy as np
  
  def maybe_num_nodes(index, num_nodes=None):
      return index.max().item() + 1 if num_nodes is None else num_nodes
  
  
  def glorot(tensor):
      if tensor is not None:
          stdv = math.sqrt(6.0 / (tensor.size(-2) + tensor.size(-1)))
          tensor.data.uniform_(-stdv, stdv)
  
  
  def zeros(tensor):
      if tensor is not None:
          tensor.data.fill_(0)
  
  
  def add_self_loops(edge_index, edge_type=None, num_nodes=None, max_label=None):
      r"""Adds a self-loop :math:`(i,i) \in \mathcal{E}` to every node
      :math:`i \in \mathcal{V}` in the graph given by :attr:`edge_index`.
      In case the graph is weighted, self-loops will be added with edge weights
      denoted by :obj:`fill_value`.
  
      Args:
          edge_index (LongTensor): The edge indices.
          edge_weight (Tensor, optional): One-dimensional edge weights.
              (default: :obj:`None`)
          fill_value (int, optional): If :obj:`edge_weight` is not :obj:`None`,
              will add self-loops with edge weights of :obj:`fill_value` to the
              graph. (default: :obj:`1`)
          num_nodes (int, optional): The number of nodes, *i.e.*
              :obj:`max_val + 1` of :attr:`edge_index`. (default: :obj:`None`)
  
      :rtype: (:class:`LongTensor`, :class:`Tensor`)
      """
      num_nodes = maybe_num_nodes(edge_index, num_nodes)
  
      loop_index = torch.arange(0, num_nodes, dtype=torch.long,
                                device=edge_index.device)
      loop_index = loop_index.unsqueeze(0).repeat(2, 1)
  
      edge_index = torch.cat([edge_index, loop_index], dim=1)
  
      loop_edge = torch.tensor(np.full(num_nodes, 1), dtype=torch.long).to('cuda')
      #loop_edge = loop_edge.unsqueeze(0)
  
      edge_type = torch.cat([edge_type, loop_edge], dim=0)
  
      return edge_index, edge_type
  
  class GATRConv(MessagePassing):
      r"""The graph attentional operator from the `"Graph Attention Networks"
      <https://arxiv.org/abs/1710.10903>`_ paper
  
      .. math::
          \mathbf{x}^{\prime}_i = \alpha_{i,i}\mathbf{\Theta}\mathbf{x}_{i} +
          \sum_{j \in \mathcal{N}(i)} \alpha_{i,j}\mathbf{\Theta}\mathbf{x}_{j},
  
      where the attention coefficients :math:`\alpha_{i,j}` are computed as
  
      .. math::
          \alpha_{i,j} =
          \frac{
          \exp\left(\mathrm{LeakyReLU}\left(\mathbf{a}^{\top}
          [\mathbf{\Theta}\mathbf{x}_i \, \Vert \, \mathbf{\Theta}\mathbf{x}_j]
          \right)\right)}
          {\sum_{k \in \mathcal{N}(i) \cup \{ i \}}
          \exp\left(\mathrm{LeakyReLU}\left(\mathbf{a}^{\top}
          [\mathbf{\Theta}\mathbf{x}_i \, \Vert \, \mathbf{\Theta}\mathbf{x}_k]
          \right)\right)}.
  
      Args:
          in_channels (int): Size of each input sample.
          out_channels (int): Size of each output sample.
          heads (int, optional): Number of multi-head-attentions.
              (default: :obj:`1`)
          concat (bool, optional): If set to :obj:`False`, the multi-head
              attentions are averaged instead of concatenated.
              (default: :obj:`True`)
          negative_slope (float, optional): LeakyReLU angle of the negative
              slope. (default: :obj:`0.2`)
          dropout (float, optional): Dropout probability of the normalized
              attention coefficients which exposes each node to a stochastically
              sampled neighborhood during training. (default: :obj:`0`)
          bias (bool, optional): If set to :obj:`False`, the layer will not learn
              an additive bias. (default: :obj:`True`)
          **kwargs (optional): Additional arguments of
              :class:`torch_geometric.nn.conv.MessagePassing`.
      """
  
      def __init__(self, in_channels, out_channels, num_relations=None, heads=1, concat=True,
                   negative_slope=0.2, dropout=0, bias=True, **kwargs):
          super(GATRConv, self).__init__(aggr='add', **kwargs)
  
          self.in_channels = in_channels
          self.out_channels = out_channels
          self.heads = heads
          self.concat = concat
          self.negative_slope = negative_slope
          self.dropout = dropout
          self.num_relations = num_relations
  
          self.w = Parameter(torch.Tensor(torch.Tensor(num_relations, in_channels, heads * out_channels)))
  
          self.weight = Parameter(
              torch.Tensor(in_channels, heads * out_channels))
  
          self.att = Parameter(torch.Tensor(1, heads, 2 * out_channels))
  
          if bias and concat:
              self.bias = Parameter(torch.Tensor(heads * out_channels))
          elif bias and not concat:
              self.bias = Parameter(torch.Tensor(out_channels))
          else:
              self.register_parameter('bias', None)
  
          self.reset_parameters()
  
      def reset_parameters(self):
          glorot(self.weight)
          glorot(self.w)
          glorot(self.att)
          zeros(self.bias)
  
      def forward(self, x, edge_index, edge_type=None, size=None):
          return self.propagate(edge_index, size=size, x=x, edge_type=edge_type)
  
      def message(self, edge_index_i, x_i, x_j, size_i, edge_type):
  
          x_i = torch.matmul(x_i, self.weight)
  
          w = self.w
          w = torch.index_select(w, 0, edge_type)
  
          x_j = torch.bmm(x_j.unsqueeze(1), w).squeeze(-2)
  
          # Compute attention coefficients.
          x_j = x_j.view(-1, self.heads, self.out_channels)
          if x_i is None:
              alpha = (x_j * self.att[:, :, self.out_channels:]).sum(dim=-1)
          else:
              x_i = x_i.view(-1, self.heads, self.out_channels)
              alpha = (torch.cat([x_i, x_j], dim=-1) * self.att).sum(dim=-1)
  
          alpha = F.leaky_relu(alpha, self.negative_slope)
          alpha = softmax(alpha, edge_index_i, size_i)
  
          # Sample attention coefficients stochastically.
          alpha = F.dropout(alpha, p=self.dropout, training=self.training)
  
          return x_j * alpha.view(-1, self.heads, 1)
  
      def update(self, aggr_out):
          if self.concat is True:
              aggr_out = aggr_out.view(-1, self.heads * self.out_channels)
          else:
              aggr_out = aggr_out.mean(dim=1)
  
          if self.bias is not None:
              aggr_out = aggr_out + self.bias
          return aggr_out
  
      def __repr__(self):
          return '{}({}, {}, heads={}, relations={}, dropout={})'.format(self.__class__.__name__,
                                               self.in_channels,
                                               self.out_channels, self.heads, self.num_relations,
                                                                                   self.dropout)
  