My model demonstrates exceptional performance when trained on subsets of up to 10…
- User
My model demonstrates exceptional performance when trained on subsets of up to 10 features but struggles to generalize effectively when utilizing the full dataset of 414 features. Would it be feasible and methodologically sound to design a wrapper framework that trains multiple smaller models, each using 10 features, to collectively capture the data's complexity? Additionally, is this a recommended approach in such scenarios?
Weight normzlizatioin 2223, Layer 2425. Chomp1d removes extra time steps from the end of the sequence class Chomp1d(nn.Module): def init(self, chomp_size): super(Chomp1d, self).init() self.chomp_size = chomp_size def forward(self, x): if self.chomp_size > 0: return x[:, :, :-self.chomp_size] else: return x
Residual Block with causal convolutions and Chomp1d class ResidualBlock(nn.Module): def init(self, in_channels, out_channels, kernel_size, dilation, dropout): super(ResidualBlock, self).init() padding = (kernel_size - 1) * dilation
self.conv1 = nn.utils.weight_norm( nn.Conv1d(in_channels, out_channels, kernel_size, padding=padding, dilation=dilation) ) self.chomp1 = Chomp1d(padding) self.relu1 = nn.ReLU() self.dropout1 = nn.Dropout(dropout) self.conv2 = nn.utils.weight_norm( nn.Conv1d(out_channels, out_channels, kernel_size, padding=padding, dilation=dilation) ) self.chomp2 = Chomp1d(padding) self.relu2 = nn.ReLU() self.dropout2 = nn.Dropout(dropout)
If the input and output channels are not the same, adjust with a 1x1 convolution self.downsample = nn.Conv1d(in_channels, out_channels, kernel_size=1) if in_channels != out_channels else None
self.relu = nn.ReLU() self.init_weights() content_copy Use code with caution.
def init_weights(self): nn.init.normal_(self.conv1.weight, std=0.01) nn.init.normal_(self.conv2.weight, std=0.01) if self.downsample is not None: nn.init.normal_(self.downsample.weight, std=0.01)
def forward(self, x): residual = x # Save the input for the residual connection out = self.conv1(x) out = self.chomp1(out) # Remove extra padding out = self.relu1(out) out = self.dropout1(out) out = self.conv2(out) out = self.chomp2(out) # Remove extra padding out = self.relu2(out) out = self.dropout2(out) if self.downsample is not None: residual = self.downsample(residual)
return self.relu(out + residual) # Residual connection with addition content_copy Use code with caution.
Updated TCN Encoder with Residual Blocks 0.4 class TCNEncoder(nn.Module): def init(self, input_size, hidden_size, num_layers, kernel_size, dropout): super(TCNEncoder, self).init() layers = [] in_channels = input_size
for i in range(num_layers): dilation = 2 ** i out_channels = hidden_size
layers.append(ResidualBlock(in_channels, out_channels, kernel_size, dilation, dropout))
in_channels = out_channels # For next layer self.network = nn.Sequential(*layers) content_copy Use code with caution.
def forward(self, x):
x shape: (batch_size, seq_length, input_size) x = x.transpose(1, 2) # Change to (batch_size, input_size, seq_length) x = self.network(x) x = x.transpose(1, 2) # Back to (batch_size, seq_length, hidden_size) return x Transformer Decoder with masked multi-head attention class TransformerDecoder(nn.Module): def init(self, input_size, hidden_size, num_layers, num_heads, dropout): super(TransformerDecoder, self).init() self.embedding = nn.Linear(input_size, hidden_size) self.positional_encoding = PositionalEncoding(hidden_size, dropout) decoder_layer = nn.TransformerDecoderLayer(d_model=hidden_size, nhead=num_heads, dropout=dropout) self.transformer_decoder = nn.TransformerDecoder(decoder_layer, num_layers=num_layers) self.fc_out = nn.Linear(hidden_size, input_size)
- Assistant