ngaggion's picture
First push
e87a462
raw
history blame
2.57 kB
from torch_geometric.nn.conv import MessagePassing
from torch_geometric.nn.conv.cheb_conv import ChebConv
from torch_geometric.nn.inits import zeros, normal
# We change the default initialization from zeros to a normal distribution
class ChebConv(ChebConv):
def reset_parameters(self):
for lin in self.lins:
normal(lin, mean = 0, std = 0.1)
#lin.reset_parameters()
normal(self.bias, mean = 0, std = 0.1)
#zeros(self.bias)
# Pooling from COMA: https://github.com/pixelite1201/pytorch_coma/blob/master/layers.py
class Pool(MessagePassing):
def __init__(self):
# source_to_target is the default value for flow, but is specified here for explicitness
super(Pool, self).__init__(flow='source_to_target')
def forward(self, x, pool_mat, dtype=None):
pool_mat = pool_mat.transpose(0, 1)
out = self.propagate(edge_index=pool_mat._indices(), x=x, norm=pool_mat._values(), size=pool_mat.size())
return out
def message(self, x_j, norm):
return norm.view(1, -1, 1) * x_j
import torch.nn as nn
import torch.nn.functional as F
class residualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1):
"""
Args:
in_channels (int): Number of input channels.
out_channels (int): Number of output channels.
stride (int): Controls the stride.
"""
super(residualBlock, self).__init__()
self.skip = nn.Sequential()
if stride != 1 or in_channels != out_channels:
self.skip = nn.Sequential(
nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_channels, track_running_stats=False))
else:
self.skip = None
self.block = nn.Sequential(nn.BatchNorm2d(in_channels, track_running_stats=False),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels, out_channels, 3, padding=1),
nn.BatchNorm2d(out_channels, track_running_stats=False),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, 3, padding=1)
)
def forward(self, x):
identity = x
out = self.block(x)
if self.skip is not None:
identity = self.skip(x)
out += identity
out = F.relu(out)
return out