Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

write a pytorch code for min-max rescaling layer

  1. User

    write a pytorch code for min-max rescaling layer

  2. Assistant

    Here is an example of a PyTorch code for a min-max rescaling layer:

    import torch
    import torch.nn as nn
    
    class MinMaxRescale(nn.Module):
        def __init__(self):
            super(MinMaxRescale, self).__init__()
    
        def forward(self, x):
            min_val = torch.min(x)
            max_val = torch.max(x)
    
            # Rescale the input tensor to the range [0, 1]
            x_rescale = (x - min_val) / (max_val - min_val)
    
            return x_rescale
    

    You can use this MinMaxRescale module as a layer in your PyTorch model. It takes an input tensor and rescales it to the range [0, 1] based on the minimum and maximum values.