Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk5 min

PyTorch nn.Conv1d: Input Shapes, Output Length, Weights, and Examples

A practical guide to PyTorch Conv1d tensor layout, output-length math, filter weights, groups, and sequence-data examples.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

torch.nn.Conv1d expects a batched input shaped (N, C_in, L_in): batch, channels, then sequence length. Its filters move along the length axis, and the output is (N, C_out, L_out). If your data is stored as (batch, sequence, features), move the feature dimension into the channel position before applying the layer.

What is the input shape for Conv1d?

The current PyTorch 2.14 Conv1d API reference accepts either a batched tensor shaped (N, C_in, L_in) or an unbatched tensor shaped (C_in, L_in).

As an Amazon Associate I earn from qualifying purchases.

Dimension Meaning
N Number of samples in the batch. Omit this dimension for unbatched input.
C_in Input channels, often the number of features at each sequence position.
L_in Length of the ordered one-dimensional signal—the axis the convolution scans.

The output retains the batch dimension when present, replaces the input channel count with out_channels, and uses the calculated output length: (N, C_out, L_out) or, for unbatched input, (C_out, L_out).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Reorder sequence data when features are last

Many datasets arrive as (batch, sequence, features), but Conv1d places channels before length. If the sequence is the axis you want to scan, use x = x.permute(0, 2, 1) to turn (N, L, features) into (N, features, L). Do not permute automatically: first determine which axis is the ordered signal and which represents channels.

A two-dimensional tensor is interpreted as unbatched (channels, length), not batched single-channel (batch, length). Add a channel dimension deliberately when each sample has one channel, for example by shaping it as (N, 1, L).

How do I calculate the Conv1d output shape?

For integer padding, calculate the output length with:

L_out = floor((L_in + 2 * padding - dilation * (kernel_size - 1) - 1) / stride + 1)

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Here, kernel_size is the number of sampled positions per filter window, stride is the distance between window starts, and dilation spaces the sampled positions apart. Compute each layer’s output length before stacking layers so the next layer receives a compatible input.

Worked output-length examples

  • With L_in=50, kernel_size=3, stride=2, padding=0, and dilation=1, the result is floor((50 - 2 - 1) / 2 + 1) = 25. Thus the API example nn.Conv1d(16, 33, 3, stride=2) maps an input of (20, 16, 50) to (20, 33, 25).
  • For a length of 50 with the same kernel and no padding but stride 2, the result is floor((50 - 3) / 2 + 1) = 24. This is the setting used in the following sequence-data example.

The string padding option 'valid' means no padding. 'same' preserves input length only when stride=1; with another stride, use the formula to determine the resulting length.

What does the Conv1d weight shape mean?

The learned weight tensor has shape (out_channels, in_channels / groups, kernel_size). With the default groups=1, that becomes (out_channels, in_channels, kernel_size): every output filter has weights for every input channel at each kernel position. If bias is enabled (the default), its shape is (out_channels,), with one bias value per output channel.

For example, nn.Conv1d(4, 16, kernel_size=3) has a weight shape of (16, 4, 3). Each of its 16 output filters spans 4 input channels and 3 sampled positions. The operation is cross-correlation, as described by the PyTorch API; the weight dimensions describe the parameters, not a promise about what patterns a trained layer will learn.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do groups change channel connections?

The groups argument partitions channel connections. Both in_channels and out_channels must be divisible by groups.

  • groups=1 (default): every input channel can contribute to every output channel.
  • groups=2: the channel connections are split into two groups instead of being fully mixed.
  • groups=in_channels: each input channel is handled independently. When out_channels is an integer multiple of in_channels, PyTorch documents this as depthwise convolution.

Grouping changes the weight tensor’s second dimension to in_channels / groups; it does not change the output-length formula.

Example: apply Conv1d to sequence data

This example starts with features in the last dimension, rearranges the tensor, and then applies a convolution along the sequence:

import torch
from torch import nn

x = torch.randn(8, 50, 4)       # batch, sequence, features
x = x.permute(0, 2, 1)          # (8, 4, 50): batch, channels, sequence
conv = nn.Conv1d(4, 16, kernel_size=3, stride=2)
y = conv(x)                     # (8, 16, 24)

print(conv.weight.shape)        # (16, 4, 3)
print(y.shape)                  # (8, 16, 24)

The output length is 24 from the formula with L_in=50, kernel_size=3, stride=2, and zero padding. The code illustrates the shapes; its output is derived from the documented dimensions and formula.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why do I get a channels mismatch error?

Check the input axes against the layer constructor. The first constructor argument, in_channels, must match the tensor’s channel dimension—not its batch dimension or sequence length.

  • For data shaped (N, L, features) where L is the ordered sequence axis, permute to (N, features, L).
  • For a single unbatched signal, use (C_in, L_in). A two-dimensional tensor will not be treated as a batch of single-channel signals.
  • Check that the input’s channel count equals in_channels and that both channel counts are divisible by groups.

Permuting fixes a layout mismatch only when the axis meanings support it. If rows are independent observations or the features have no meaningful order, treating them as a sequence may be the wrong modeling assumption.

Which Conv1d settings should I choose?

Conv1d is suited to data with meaningful neighboring positions along one ordered axis, such as a sequence or signal. The arguments control different trade-offs rather than offering one universally best configuration:

  • Channel mixing: use groups=1 for full channel connections, or larger groups to restrict which input channels connect to each output. Depthwise convolution keeps input channels separate.
  • Receptive field: increase kernel_size to sample more positions per window. Increase dilation to spread those samples farther apart without increasing the number of kernel positions.
  • Resolution: change stride to move the window more or less densely; the output length changes accordingly.
  • Boundaries: integer padding adds values at both ends. The documented padding modes are zeros, reflect, replicate, and circular.

Before tuning these settings, establish what the length axis means for the task. Convolution assumes neighboring positions have a useful relationship; independent rows or unordered feature vectors do not automatically satisfy that assumption.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does Conv1d behave deterministically on CUDA?

PyTorch notes that CUDA/CuDNN may select nondeterministic algorithms for Conv1d in some circumstances. Setting torch.backends.cudnn.deterministic = True requests deterministic behavior, potentially at a performance cost. Consult the PyTorch 2.14 API reference for the documented details and current caveats.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.