File size: 2,828 Bytes
e923d6f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | from torch import nn
import torch
class MemoryUpdater(nn.Module):
def update_memory(self, unique_node_ids, unique_messages, timestamps):
pass
class SequenceMemoryUpdater(MemoryUpdater):
def __init__(self, memory, message_dimension, memory_dimension, device):
super(SequenceMemoryUpdater, self).__init__()
self.memory = memory
self.layer_norm = torch.nn.LayerNorm(memory_dimension)
self.message_dimension = message_dimension
self.device = device
def update_memory(self, unique_node_ids, unique_messages, timestamps):
if len(unique_node_ids) <= 0:
return
assert (self.memory.get_last_update(unique_node_ids) <= timestamps).all().item(), "Trying to " \
"update memory to time in the past"
memory = self.memory.get_memory(unique_node_ids)
self.memory.last_update[unique_node_ids] = timestamps
updated_memory = self.memory_updater(unique_messages, memory)
self.memory.set_memory(unique_node_ids, updated_memory)
def get_updated_memory(self, unique_node_ids, unique_messages, timestamps):
if len(unique_node_ids) <= 0:
return self.memory.memory.data.clone(), self.memory.last_update.data.clone()
assert (self.memory.get_last_update(unique_node_ids) <= timestamps).all().item(), "Trying to " \
"update memory to time in the past"
updated_memory = self.memory.memory.data.clone()
updated_memory[unique_node_ids] = self.memory_updater(unique_messages, updated_memory[unique_node_ids])
updated_last_update = self.memory.last_update.data.clone()
updated_last_update[unique_node_ids] = timestamps
return updated_memory, updated_last_update
class GRUMemoryUpdater(SequenceMemoryUpdater):
def __init__(self, memory, message_dimension, memory_dimension, device):
super(GRUMemoryUpdater, self).__init__(memory, message_dimension, memory_dimension, device)
self.memory_updater = nn.GRUCell(input_size=message_dimension,
hidden_size=memory_dimension)
class RNNMemoryUpdater(SequenceMemoryUpdater):
def __init__(self, memory, message_dimension, memory_dimension, device):
super(RNNMemoryUpdater, self).__init__(memory, message_dimension, memory_dimension, device)
self.memory_updater = nn.RNNCell(input_size=message_dimension,
hidden_size=memory_dimension)
def get_memory_updater(module_type, memory, message_dimension, memory_dimension, device):
if module_type == "gru":
return GRUMemoryUpdater(memory, message_dimension, memory_dimension, device)
elif module_type == "rnn":
return RNNMemoryUpdater(memory, message_dimension, memory_dimension, device)
|