Convolutional 1D subsampling (to 1/2 length). Args: idim (int): Input dimension. odim (int): Output dimension. dropout_rate (float): Dropout rate. pos_enc (torch.nn.Module): Custom position encoding layer.
| 330 | |
| 331 | |
| 332 | class Conv1dSubsampling(torch.nn.Module): |
| 333 | """Convolutional 1D subsampling (to 1/2 length). |
| 334 | |
| 335 | Args: |
| 336 | idim (int): Input dimension. |
| 337 | odim (int): Output dimension. |
| 338 | dropout_rate (float): Dropout rate. |
| 339 | pos_enc (torch.nn.Module): Custom position encoding layer. |
| 340 | |
| 341 | """ |
| 342 | |
| 343 | def __init__( |
| 344 | self, |
| 345 | idim, |
| 346 | odim, |
| 347 | kernel_size, |
| 348 | stride, |
| 349 | pad, |
| 350 | tf2torch_tensor_name_prefix_torch: str = "stride_conv", |
| 351 | tf2torch_tensor_name_prefix_tf: str = "seq2seq/proj_encoder/downsampling", |
| 352 | ): |
| 353 | """Initialize Conv1dSubsampling. |
| 354 | |
| 355 | Args: |
| 356 | idim: TODO. |
| 357 | odim: TODO. |
| 358 | kernel_size: Size/dimension parameter. |
| 359 | stride: TODO. |
| 360 | pad: TODO. |
| 361 | tf2torch_tensor_name_prefix_torch: TODO. |
| 362 | tf2torch_tensor_name_prefix_tf: TODO. |
| 363 | """ |
| 364 | super(Conv1dSubsampling, self).__init__() |
| 365 | self.conv = torch.nn.Conv1d(idim, odim, kernel_size, stride) |
| 366 | self.pad_fn = torch.nn.ConstantPad1d(pad, 0.0) |
| 367 | self.stride = stride |
| 368 | self.odim = odim |
| 369 | self.tf2torch_tensor_name_prefix_torch = tf2torch_tensor_name_prefix_torch |
| 370 | self.tf2torch_tensor_name_prefix_tf = tf2torch_tensor_name_prefix_tf |
| 371 | |
| 372 | def output_size(self) -> int: |
| 373 | """Output size.""" |
| 374 | return self.odim |
| 375 | |
| 376 | def forward(self, x, x_len): |
| 377 | """Subsample x.""" |
| 378 | x = x.transpose(1, 2) # (b, d ,t) |
| 379 | x = self.pad_fn(x) |
| 380 | # x = F.relu(self.conv(x)) |
| 381 | x = F.leaky_relu(self.conv(x), negative_slope=0.0) |
| 382 | x = x.transpose(1, 2) # (b, t ,d) |
| 383 | |
| 384 | if x_len is None: |
| 385 | |
| 386 | return x, None |
| 387 | x_len = (x_len - 1) // self.stride + 1 |
| 388 | return x, x_len |
| 389 |
no outgoing calls
no test coverage detected
searching dependent graphs…