hyperlight icon indicating copy to clipboard operation
hyperlight copied to clipboard

About closedloop with the use of Hyperlight.

Open murataryusei opened this issue 1 year ago • 0 comments

I'm considering implementing a HyperNet using Hyperlight in the below code, but I don't know about how to input the previous hidden state during closedloop prediction. What would be the best approach?

import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
import hyperlight as hl

class MyDataset(Dataset):
    def __init__(self):
        super().__init__()
        data = torch.rand(10, 20, 2)
        self.data = data # torch.Size([10, 20, 2])

    def __len__(self):
        return len(self.data)
    
    def __getitem__(self, idx):
        return (self.data[idx])

class HyperRNN(nn.Module):
    def __init__(self, batch_size, hidden_size, hyper_size):
        super().__init__()
        mainnet = nn.RNN(2, hidden_size, 1, batch_first = True)
        module_to_hypernetize = [mainnet]
        self.mainnet = hl.hypernetize(mainnet, modules=module_to_hypernetize)
        
        self.hyperparam_shape = {'h': (batch_size, hyper_size,)}
        self.parameter_shapes = self.mainnet.external_shapes()
        self.hypernet = hl.HyperNet(
            input_shapes=self.hyperparam_shape,
            output_shapes=self.parameter_shapes,
            hidden_sizes=[64,128,256],
        )

    def forward(self, main_input, hyper_input):
        parameters = self.hypernet(h=hyper_input)
        with self.mainnet.using_externals(parameters):
            prediction = self.mainnet(main_input)
        return prediction
    
batch_size=10
hidden_size=16
hyper_size=2

dataset = MyDataset()
dataloader = DataLoader(dataset, batch_size)
model = HyperRNN(batch_size, hidden_size, hyper_size)
print(model)
print(model.parameter_shapes)
print('-------------------------------------------------------------------------------------------')

for data in dataloader:
    input = data # torch.Size([batch_size, 20, 2])
    hyp_input = torch.zeros((batch_size,2,))
    prediction = model(input, hyp_input)[0]
    h_state = model(input, hyp_input)[1]
    print(f'mainnet_input      : {input.shape}')
    print(f'hypnet_input       : {hyp_input.shape}')
    print(f'mainnet_output     : {prediction.shape}')
    print(f'mainnet_hiddenstate: {h_state.shape}')
    print('-------------------------------------------------------------------------------------------')

P.S. "closed loop" means output of previous time step uses as next input during the RNN as shown in the figure below. So, what I want to know is how to input the previous hidden state. closed_loop

murataryusei avatar Jul 09 '23 02:07 murataryusei