|
| 1 | +import numpy as np |
| 2 | +import torch |
| 3 | +import torch.nn as nn |
| 4 | +import torch.nn.functional as F |
| 5 | +from scipy.stats import loguniform |
| 6 | +from skopt.space import Categorical |
| 7 | +from skopt.space import Real |
| 8 | + |
| 9 | + |
| 10 | +class Encoder(nn.Module): |
| 11 | + """ |
| 12 | + Deterministic encoder for conditional neural process model. |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + input_dim, |
| 18 | + output_dim, |
| 19 | + hidden_dim, |
| 20 | + latent_dim, |
| 21 | + hidden_layers_enc, |
| 22 | + activation, |
| 23 | + context_mask=None, |
| 24 | + ): |
| 25 | + super().__init__() |
| 26 | + layers = [nn.Linear(input_dim + output_dim, hidden_dim), activation()] |
| 27 | + for _ in range(hidden_layers_enc): |
| 28 | + layers.extend([nn.Linear(hidden_dim, hidden_dim), activation()]) |
| 29 | + layers.append(nn.Linear(hidden_dim, latent_dim)) |
| 30 | + self.net = nn.Sequential(*layers) |
| 31 | + |
| 32 | + self.x_encoder = nn.Linear(input_dim, latent_dim) |
| 33 | + |
| 34 | + self.crossattn = nn.MultiheadAttention( |
| 35 | + embed_dim=latent_dim, num_heads=4, batch_first=True |
| 36 | + ) |
| 37 | + |
| 38 | + def forward(self, x_context, y_context, x_target, context_mask=None): |
| 39 | + """ |
| 40 | + Encode context |
| 41 | +
|
| 42 | + Parameters |
| 43 | + ---------- |
| 44 | + x_context: (batch_size, n_context_points, input_dim) |
| 45 | + y_context: (batch_size, n_context_points, output_dim) |
| 46 | + context_mask: (batch_size, n_context_points) |
| 47 | +
|
| 48 | + Returns |
| 49 | + ------- |
| 50 | + r: (batch_size, n_points, latent_dim) |
| 51 | + """ |
| 52 | + # context self attention |
| 53 | + x = torch.cat([x_context, y_context], dim=-1) |
| 54 | + r = self.net(x) |
| 55 | + # q, k, v |
| 56 | + x_target_enc = self.x_encoder(x_target) |
| 57 | + x_context_enc = self.x_encoder(x_context) |
| 58 | + if context_mask is not None: |
| 59 | + r, _ = self.crossattn( |
| 60 | + x_target_enc, |
| 61 | + x_context_enc, |
| 62 | + r, |
| 63 | + need_weights=False, |
| 64 | + key_padding_mask=context_mask, |
| 65 | + ) |
| 66 | + else: |
| 67 | + r, _ = self.crossattn(x_target_enc, x_context_enc, r, need_weights=False) |
| 68 | + return r |
| 69 | + |
| 70 | + |
| 71 | +class Decoder(nn.Module): |
| 72 | + def __init__( |
| 73 | + self, |
| 74 | + input_dim, |
| 75 | + latent_dim, |
| 76 | + hidden_dim, |
| 77 | + output_dim, |
| 78 | + hidden_layers_dec, |
| 79 | + activation, |
| 80 | + ): |
| 81 | + super().__init__() |
| 82 | + layers = [nn.Linear(latent_dim + input_dim, hidden_dim), activation()] |
| 83 | + for _ in range(hidden_layers_dec): |
| 84 | + layers.extend([nn.Linear(hidden_dim, hidden_dim), activation()]) |
| 85 | + self.net = nn.Sequential(*layers) |
| 86 | + self.mean_head = nn.Linear(hidden_dim, output_dim) |
| 87 | + self.logvar_head = nn.Linear(hidden_dim, output_dim) |
| 88 | + |
| 89 | + def forward(self, r, x_target): |
| 90 | + """ |
| 91 | + Decode using representation r and target points x_target |
| 92 | +
|
| 93 | + Parameters |
| 94 | + ---------- |
| 95 | + r: (batch_size, n_points, latent_dim) |
| 96 | + x_target: (batch_size, n_points, input_dim) |
| 97 | +
|
| 98 | + Returns |
| 99 | + ------- |
| 100 | + mean: (batch_size, n_points, output_dim) |
| 101 | + logvar: (batch_size, n_points, output_dim) |
| 102 | + """ |
| 103 | + x = torch.cat([r, x_target], dim=-1) |
| 104 | + hidden = self.net(x) |
| 105 | + mean = self.mean_head(hidden) |
| 106 | + logvar = self.logvar_head(hidden) |
| 107 | + |
| 108 | + return mean, logvar |
| 109 | + |
| 110 | + |
| 111 | +class AttnCNPModule(nn.Module): |
| 112 | + def __init__( |
| 113 | + self, |
| 114 | + input_dim, |
| 115 | + output_dim, |
| 116 | + hidden_dim, |
| 117 | + latent_dim, |
| 118 | + hidden_layers_enc, |
| 119 | + hidden_layers_dec, |
| 120 | + activation=nn.ReLU, |
| 121 | + ): |
| 122 | + super().__init__() |
| 123 | + self.encoder = Encoder( |
| 124 | + input_dim, output_dim, hidden_dim, latent_dim, hidden_layers_enc, activation |
| 125 | + ) |
| 126 | + self.decoder = Decoder( |
| 127 | + input_dim, latent_dim, hidden_dim, output_dim, hidden_layers_dec, activation |
| 128 | + ) |
| 129 | + |
| 130 | + def forward(self, X_context, y_context, X_target=None, context_mask=None): |
| 131 | + """ |
| 132 | +
|
| 133 | + Parameters |
| 134 | + ---------- |
| 135 | + X_context: (batch_size, n_context_points, input_dim) |
| 136 | + y_context: (batch_size, n_context_points, output_dim) |
| 137 | + X_target: (batch_size, n_target_points, input_dim) |
| 138 | + context_mask: (batch_size, n_context_points), currently unused, |
| 139 | + as we pad with 0's and don't have attention, layernorm yet. |
| 140 | +
|
| 141 | + Returns |
| 142 | + ------- |
| 143 | + mean: (batch_size, n_points, output_dim) |
| 144 | + logvar: (batch_size, n_points, output_dim) |
| 145 | + """ |
| 146 | + # inverse context_mask |
| 147 | + if context_mask is not None: |
| 148 | + context_mask = ~context_mask |
| 149 | + r = self.encoder(X_context, y_context, X_target) |
| 150 | + mean, logvar = self.decoder(r, X_target) |
| 151 | + return mean, logvar |
0 commit comments