Best Python code snippet using lemoncheesecake
torch_model.py
Source:torch_model.py  
...4from torch.nn import AvgPool1d, Conv1d, Conv2d, ConvTranspose1d5from torch.nn.utils import remove_weight_norm, spectral_norm, weight_norm6# from utils import init_weights, get_padding7LRELU_SLOPE = 0.18def get_padding(kernel_size, dilation=1):9    return int((kernel_size * dilation - dilation) / 2)10def init_weights(m, mean=0.0, std=0.01):11    classname = m.__class__.__name__12    if classname.find("Conv") != -1:13        m.weight.data.normal_(mean, std)14class ResBlock1(torch.nn.Module):15    def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5)):16        super(ResBlock1, self).__init__()17        self.h = h18        self.convs1 = nn.ModuleList(19            [20                weight_norm(21                    Conv1d(22                        channels,23                        channels,24                        kernel_size,25                        1,26                        dilation=dilation[0],27                        padding=get_padding(kernel_size, dilation[0]),28                    )29                ),30                weight_norm(31                    Conv1d(32                        channels,33                        channels,34                        kernel_size,35                        1,36                        dilation=dilation[1],37                        padding=get_padding(kernel_size, dilation[1]),38                    )39                ),40                weight_norm(41                    Conv1d(42                        channels,43                        channels,44                        kernel_size,45                        1,46                        dilation=dilation[2],47                        padding=get_padding(kernel_size, dilation[2]),48                    )49                ),50            ]51        )52        self.convs1.apply(init_weights)53        self.convs2 = nn.ModuleList(54            [55                weight_norm(56                    Conv1d(57                        channels,58                        channels,59                        kernel_size,60                        1,61                        dilation=1,62                        padding=get_padding(kernel_size, 1),63                    )64                ),65                weight_norm(66                    Conv1d(67                        channels,68                        channels,69                        kernel_size,70                        1,71                        dilation=1,72                        padding=get_padding(kernel_size, 1),73                    )74                ),75                weight_norm(76                    Conv1d(77                        channels,78                        channels,79                        kernel_size,80                        1,81                        dilation=1,82                        padding=get_padding(kernel_size, 1),83                    )84                ),85            ]86        )87        self.convs2.apply(init_weights)88    def forward(self, x):89        for c1, c2 in zip(self.convs1, self.convs2):90            xt = F.leaky_relu(x, LRELU_SLOPE)91            xt = c1(xt)92            xt = F.leaky_relu(xt, LRELU_SLOPE)93            xt = c2(xt)94            x = xt + x95        return x96    def remove_weight_norm(self):97        for l in self.convs1:98            remove_weight_norm(l)99        for l in self.convs2:100            remove_weight_norm(l)101class ResBlock2(torch.nn.Module):102    def __init__(self, h, channels, kernel_size=3, dilation=(1, 3)):103        super(ResBlock2, self).__init__()104        self.h = h105        self.convs = nn.ModuleList(106            [107                weight_norm(108                    Conv1d(109                        channels,110                        channels,111                        kernel_size,112                        1,113                        dilation=dilation[0],114                        padding=get_padding(kernel_size, dilation[0]),115                    )116                ),117                weight_norm(118                    Conv1d(119                        channels,120                        channels,121                        kernel_size,122                        1,123                        dilation=dilation[1],124                        padding=get_padding(kernel_size, dilation[1]),125                    )126                ),127            ]128        )129        self.convs.apply(init_weights)130    def forward(self, x):131        for c in self.convs:132            xt = F.leaky_relu(x, LRELU_SLOPE)133            xt = c(xt)134            x = xt + x135        return x136    def remove_weight_norm(self):137        for l in self.convs:138            remove_weight_norm(l)139class Generator(torch.nn.Module):140    def __init__(self, h):141        super(Generator, self).__init__()142        self.h = h143        self.num_kernels = len(h.resblock_kernel_sizes)144        self.num_upsamples = len(h.upsample_rates)145        self.conv_pre = weight_norm(146            Conv1d(80, h.upsample_initial_channel, 7, 1, padding=3)147        )148        resblock = ResBlock1 if h.resblock == "1" else ResBlock2149        self.ups = nn.ModuleList()150        for i, (u, k) in enumerate(zip(h.upsample_rates, h.upsample_kernel_sizes)):151            self.ups.append(152                weight_norm(153                    ConvTranspose1d(154                        h.upsample_initial_channel // (2**i),155                        h.upsample_initial_channel // (2 ** (i + 1)),156                        k,157                        u,158                        padding=(k - u) // 2,159                    )160                )161            )162        self.resblocks = nn.ModuleList()163        for i in range(len(self.ups)):164            ch = h.upsample_initial_channel // (2 ** (i + 1))165            for j, (k, d) in enumerate(166                zip(h.resblock_kernel_sizes, h.resblock_dilation_sizes)167            ):168                self.resblocks.append(resblock(h, ch, k, d))169        self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3))170        self.ups.apply(init_weights)171        self.conv_post.apply(init_weights)172    def forward(self, x):173        x = self.conv_pre(x)174        for i in range(self.num_upsamples):175            x = F.leaky_relu(x, LRELU_SLOPE)176            x = self.ups[i](x)177            xs = None178            for j in range(self.num_kernels):179                if xs is None:180                    xs = self.resblocks[i * self.num_kernels + j](x)181                else:182                    xs += self.resblocks[i * self.num_kernels + j](x)183            x = xs / self.num_kernels184        x = F.leaky_relu(x)185        x = self.conv_post(x)186        x = torch.tanh(x)187        return x188    def remove_weight_norm(self):189        print("Removing weight norm...")190        for l in self.ups:191            remove_weight_norm(l)192        for l in self.resblocks:193            l.remove_weight_norm()194        remove_weight_norm(self.conv_pre)195        remove_weight_norm(self.conv_post)196class DiscriminatorP(torch.nn.Module):197    def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):198        super(DiscriminatorP, self).__init__()199        self.period = period200        norm_f = weight_norm if use_spectral_norm == False else spectral_norm201        self.convs = nn.ModuleList(202            [203                norm_f(204                    Conv2d(205                        1,206                        32,207                        (kernel_size, 1),208                        (stride, 1),209                        padding=(get_padding(5, 1), 0),210                    )211                ),212                norm_f(213                    Conv2d(214                        32,215                        128,216                        (kernel_size, 1),217                        (stride, 1),218                        padding=(get_padding(5, 1), 0),219                    )220                ),221                norm_f(222                    Conv2d(223                        128,224                        512,225                        (kernel_size, 1),226                        (stride, 1),227                        padding=(get_padding(5, 1), 0),228                    )229                ),230                norm_f(231                    Conv2d(232                        512,233                        1024,234                        (kernel_size, 1),235                        (stride, 1),236                        padding=(get_padding(5, 1), 0),237                    )238                ),239                norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(2, 0))),240            ]241        )242        self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))243    def forward(self, x):244        fmap = []245        # 1d to 2d246        b, c, t = x.shape247        if t % self.period != 0:  # pad first248            n_pad = self.period - (t % self.period)249            x = F.pad(x, (0, n_pad), "reflect")250            t = t + n_pad...spec2vec.py
Source:spec2vec.py  
...8        self.conv1 = nn.Conv2d(9            2,10            96,11            kernel_size=(1, 7),12            padding=self.get_padding((1, 7), (1, 1)),13            dilation=(1, 1),14        )15        self.conv2 = nn.Conv2d(16            96,17            96,18            kernel_size=(7, 1),19            padding=self.get_padding((7, 1), (1, 1)),20            dilation=(1, 1),21        )22        self.conv3 = nn.Conv2d(23            96,24            96,25            kernel_size=(5, 5),26            padding=self.get_padding((5, 5), (1, 1)),27            dilation=(1, 1),28        )29        self.conv4 = nn.Conv2d(30            96,31            96,32            kernel_size=(5, 5),33            padding=self.get_padding((5, 5), (2, 1)),34            dilation=(2, 1),35        )36        self.conv5 = nn.Conv2d(37            96,38            96,39            kernel_size=(5, 5),40            padding=self.get_padding((5, 5), (4, 1)),41            dilation=(4, 1),42        )43        self.conv6 = nn.Conv2d(44            96,45            96,46            kernel_size=(5, 5),47            padding=self.get_padding((5, 5), (8, 1)),48            dilation=(8, 1),49        )50        self.conv7 = nn.Conv2d(51            96,52            96,53            kernel_size=(5, 5),54            padding=self.get_padding((5, 5), (16, 1)),55            dilation=(16, 1),56        )57        self.conv8 = nn.Conv2d(58            96,59            96,60            kernel_size=(5, 5),61            padding=self.get_padding((5, 5), (32, 1)),62            dilation=(32, 1),63        )64        self.conv9 = nn.Conv2d(65            96,66            96,67            kernel_size=(5, 5),68            padding=self.get_padding((5, 5), (1, 1)),69            dilation=(1, 1),70        )71        self.conv10 = nn.Conv2d(72            96,73            96,74            kernel_size=(5, 5),75            padding=self.get_padding((5, 5), (2, 2)),76            dilation=(2, 2),77        )78        self.conv11 = nn.Conv2d(79            96,80            96,81            kernel_size=(5, 5),82            padding=self.get_padding((5, 5), (4, 4)),83            dilation=(4, 4),84        )85        self.conv12 = nn.Conv2d(86            96,87            96,88            kernel_size=(5, 5),89            padding=self.get_padding((5, 5), (8, 8)),90            dilation=(8, 8),91        )92        self.conv13 = nn.Conv2d(93            96,94            96,95            kernel_size=(5, 5),96            padding=self.get_padding((5, 5), (16, 16)),97            dilation=(16, 16),98        )99        self.conv14 = nn.Conv2d(100            96,101            96,102            kernel_size=(5, 5),103            padding=self.get_padding((5, 5), (32, 32)),104            dilation=(32, 32),105        )106        self.conv15 = nn.Conv2d(107            96,108            last_shape,109            kernel_size=(1, 1),110            padding=self.get_padding((1, 1), (1, 1)),111            dilation=(1, 1),112        )113        # Batch normalization layers114        self.batch_norm1 = nn.BatchNorm2d(96)115        self.batch_norm2 = nn.BatchNorm2d(96)116        self.batch_norm3 = nn.BatchNorm2d(96)117        self.batch_norm4 = nn.BatchNorm2d(96)118        self.batch_norm5 = nn.BatchNorm2d(96)119        self.batch_norm6 = nn.BatchNorm2d(96)120        self.batch_norm7 = nn.BatchNorm2d(96)121        self.batch_norm8 = nn.BatchNorm2d(96)122        self.batch_norm9 = nn.BatchNorm2d(96)123        self.batch_norm10 = nn.BatchNorm2d(96)124        self.batch_norm11 = nn.BatchNorm2d(96)125        self.batch_norm11 = nn.BatchNorm2d(96)126        self.batch_norm12 = nn.BatchNorm2d(96)127        self.batch_norm13 = nn.BatchNorm2d(96)128        self.batch_norm14 = nn.BatchNorm2d(96)129        self.batch_norm15 = nn.BatchNorm2d(last_shape)130    def get_padding(self, kernel_size, dilation):131        padding = (132            ((dilation[0]) * (kernel_size[0] - 1)) // 2,133            ((dilation[1]) * (kernel_size[1] - 1)) // 2,134        )135        return padding136    def forward(self, input_audio):137        # input audio will be (2,256,256)138        output_layer = F.leaky_relu(self.batch_norm1(self.conv1(input_audio)), negative_slope=0.1)139        output_layer = F.leaky_relu(self.batch_norm2(self.conv2(output_layer)), negative_slope=0.1)140        output_layer = F.leaky_relu(self.batch_norm3(self.conv3(output_layer)), negative_slope=0.1)141        output_layer = F.leaky_relu(self.batch_norm4(self.conv4(output_layer)), negative_slope=0.1)142        output_layer = F.leaky_relu(self.batch_norm5(self.conv5(output_layer)), negative_slope=0.1)143        output_layer = F.leaky_relu(self.batch_norm6(self.conv6(output_layer)), negative_slope=0.1)144        output_layer = F.leaky_relu(self.batch_norm7(self.conv7(output_layer)), negative_slope=0.1)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
