Added dcrnn_cell
This commit is contained in:
parent
e80c47390d
commit
b65df994e4
|
|
@ -1,17 +1,14 @@
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from torch import Tensor
|
|
||||||
|
|
||||||
from lib import utils
|
from lib import utils
|
||||||
|
|
||||||
|
|
||||||
class LayerParams:
|
class LayerParams:
|
||||||
def __init__(self, rnn_network: torch.nn.RNN, type: str):
|
def __init__(self, rnn_network: torch.nn.Module, layer_type: str):
|
||||||
self._rnn_network = rnn_network
|
self._rnn_network = rnn_network
|
||||||
self._params_dict = {}
|
self._params_dict = {}
|
||||||
self._biases_dict = {}
|
self._biases_dict = {}
|
||||||
self._type = type
|
self._type = layer_type
|
||||||
|
|
||||||
def get_weights(self, shape):
|
def get_weights(self, shape):
|
||||||
if shape not in self._params_dict:
|
if shape not in self._params_dict:
|
||||||
|
|
@ -31,32 +28,24 @@ class LayerParams:
|
||||||
return self._biases_dict[length]
|
return self._biases_dict[length]
|
||||||
|
|
||||||
|
|
||||||
class DCGRUCell(torch.nn.RNN):
|
class DCGRUCell(torch.nn.Module):
|
||||||
def __init__(self, num_units, adj_mx, max_diffusion_step, num_nodes, input_size: int,
|
def __init__(self, num_units, adj_mx, max_diffusion_step, num_nodes, nonlinearity='tanh',
|
||||||
hidden_size: int,
|
filter_type="laplacian", use_gc_for_ru=True):
|
||||||
num_layers: int = 1,
|
|
||||||
num_proj=None,
|
|
||||||
nonlinearity='tanh', filter_type="laplacian", use_gc_for_ru=True):
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
:param num_units:
|
:param num_units:
|
||||||
:param adj_mx:
|
:param adj_mx:
|
||||||
:param max_diffusion_step:
|
:param max_diffusion_step:
|
||||||
:param num_nodes:
|
:param num_nodes:
|
||||||
:param input_size:
|
|
||||||
:param num_proj:
|
|
||||||
:param nonlinearity:
|
:param nonlinearity:
|
||||||
:param filter_type: "laplacian", "random_walk", "dual_random_walk".
|
:param filter_type: "laplacian", "random_walk", "dual_random_walk".
|
||||||
:param use_gc_for_ru: whether to use Graph convolution to calculate the reset and update gates.
|
:param use_gc_for_ru: whether to use Graph convolution to calculate the reset and update gates.
|
||||||
"""
|
"""
|
||||||
super(DCGRUCell, self).__init__(input_size, hidden_size, bias=True,
|
|
||||||
# bias param does not exist in tf code?
|
super().__init__()
|
||||||
num_layers=num_layers,
|
|
||||||
nonlinearity=nonlinearity)
|
|
||||||
self._activation = torch.tanh if nonlinearity == 'tanh' else torch.relu
|
self._activation = torch.tanh if nonlinearity == 'tanh' else torch.relu
|
||||||
# support other nonlinearities up here?
|
# support other nonlinearities up here?
|
||||||
self._num_nodes = num_nodes
|
self._num_nodes = num_nodes
|
||||||
self._num_proj = num_proj
|
|
||||||
self._num_units = num_units
|
self._num_units = num_units
|
||||||
self._max_diffusion_step = max_diffusion_step
|
self._max_diffusion_step = max_diffusion_step
|
||||||
self._supports = []
|
self._supports = []
|
||||||
|
|
@ -73,8 +62,6 @@ class DCGRUCell(torch.nn.RNN):
|
||||||
supports.append(utils.calculate_scaled_laplacian(adj_mx))
|
supports.append(utils.calculate_scaled_laplacian(adj_mx))
|
||||||
for support in supports:
|
for support in supports:
|
||||||
self._supports.append(self._build_sparse_matrix(support))
|
self._supports.append(self._build_sparse_matrix(support))
|
||||||
|
|
||||||
self._proj_weights = torch.nn.Parameter(torch.randn(self._num_units, self._num_proj))
|
|
||||||
self._fc_params = LayerParams(self, 'fc')
|
self._fc_params = LayerParams(self, 'fc')
|
||||||
self._gconv_params = LayerParams(self, 'gconv')
|
self._gconv_params = LayerParams(self, 'gconv')
|
||||||
|
|
||||||
|
|
@ -89,7 +76,7 @@ class DCGRUCell(torch.nn.RNN):
|
||||||
output_size = self._num_nodes * self._num_proj
|
output_size = self._num_nodes * self._num_proj
|
||||||
return output_size
|
return output_size
|
||||||
|
|
||||||
def forward(self, input: Tensor, hx: Optional[Tensor] = ...):
|
def forward(self, inputs, hx):
|
||||||
"""Gated recurrent unit (GRU) with Graph Convolution.
|
"""Gated recurrent unit (GRU) with Graph Convolution.
|
||||||
:param input: (B, num_nodes * input_dim)
|
:param input: (B, num_nodes * input_dim)
|
||||||
|
|
||||||
|
|
@ -104,23 +91,18 @@ class DCGRUCell(torch.nn.RNN):
|
||||||
fn = self._gconv
|
fn = self._gconv
|
||||||
else:
|
else:
|
||||||
fn = self._fc
|
fn = self._fc
|
||||||
value = torch.sigmoid(fn(input, hx, output_size, bias_start=1.0))
|
value = torch.sigmoid(fn(inputs, hx, output_size, bias_start=1.0))
|
||||||
value = torch.reshape(value, (-1, self._num_nodes, output_size))
|
value = torch.reshape(value, (-1, self._num_nodes, output_size))
|
||||||
r, u = torch.split(tensor=value, split_size_or_sections=2, dim=-1)
|
r, u = torch.split(tensor=value, split_size_or_sections=2, dim=-1)
|
||||||
r = torch.reshape(r, (-1, self._num_nodes * self._num_units))
|
r = torch.reshape(r, (-1, self._num_nodes * self._num_units))
|
||||||
u = torch.reshape(u, (-1, self._num_nodes * self._num_units))
|
u = torch.reshape(u, (-1, self._num_nodes * self._num_units))
|
||||||
|
|
||||||
c = self._gconv(input, r * hx, self._num_units)
|
c = self._gconv(inputs, r * hx, self._num_units)
|
||||||
if self._activation is not None:
|
if self._activation is not None:
|
||||||
c = self._activation(c)
|
c = self._activation(c)
|
||||||
|
|
||||||
output = new_state = u * hx + (1 - u) * c
|
new_state = u * hx + (1.0 - u) * c
|
||||||
if self._num_proj is not None:
|
return new_state
|
||||||
batch_size = input.shape[0]
|
|
||||||
output = torch.reshape(new_state, shape=(-1, self._num_units))
|
|
||||||
output = torch.reshape(torch.matmul(output, self._proj_weights),
|
|
||||||
shape=(batch_size, self.output_size))
|
|
||||||
return output, new_state
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _concat(x, x_):
|
def _concat(x, x_):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue