Skip to content

Instantly share code, notes, and snippets.

@rtkclouds
Created February 23, 2024 14:23
Show Gist options
  • Save rtkclouds/404cd69bb344e316eee0377b01f29276 to your computer and use it in GitHub Desktop.
Save rtkclouds/404cd69bb344e316eee0377b01f29276 to your computer and use it in GitHub Desktop.
The idPredict class represents an interesting and potentially innovative approach to time series processing, which may offer specific advantages over Transformers and CNNs depending on the problem at hand.
import torch
import torch.nn as nn
import torch.nn.functional as F
class IdPredictPyTorch(nn.Module):
def __init__(self, input_shape, predict_steps=30, groups=512, layers=20, groups_size=20, no_skip=False):
super(IdPredictPyTorch, self).__init__()
self.predict_steps = predict_steps
self.groups = groups
self.layers = layers
self.groups_size = groups_size
self.no_skip = no_skip
self.n = 0
self.groups_d = nn.ParameterList([nn.Parameter(torch.randn(groups * predict_steps, input_shape[1], 1) * 0.01) for _ in range(layers)])
self.groups_l = nn.ParameterList([nn.Parameter(torch.randn(groups * predict_steps, 1, input_shape[2]) * 0.01) for _ in range(layers)])
self.groups_ff = nn.ParameterList([nn.Parameter(torch.randn(input_shape[1], input_shape[2]) * 0.01) for _ in range(layers)])
self.groups_b = nn.ParameterList([nn.Parameter(torch.rand(1) * 0.2 + 0.2) for _ in range(layers)])
def forward(self, input):
self.n += 1
result = None
for k in range(self.layers):
vd = torch.tanh(self.groups_d[k])
vl = torch.tanh(self.groups_l[k])
b = self.groups_b[k]
r = torch.matmul(vd, vl)
# Adjust the slicing and concatenation logic as per PyTorch's conventions
# This is a simplified conversion; you'll need to adjust it based on your actual input shapes and operations
r2 = input[:, :, self.predict_steps:] + r
if result is not None:
result += r2 * self.groups_ff[k] * b
else:
result = r2 * self.groups_ff[k] * b
return result
# Example usage
# You need to adjust `input_shape` based on your actual input data
input_shape = (batch_size, feature_dim, sequence_length)
model = IdPredictPyTorch(input_shape=input_shape)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment