Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.nn.Conv1d function

PyTorch torch.nn.Conv1d function

PyTorch torch.nn Reference Manual PyTorch torch.nn Reference Manual


torch.nn.Conv1d is the one-dimensional convolution module in PyTorch.

It is mainly used to process sequential data, such as text, audio, and time-series signals.

Function definition



torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

Parameter Description


Usage examples

Example 1: Basic usage

Example

import

torch

import

torch.

nn

as

nn

One-dimensional convolution: input channels=3, output channels=64, kernel size=3

conv1d

=

nn.

Conv1d

(

in_channels

=

3

,

out_channels

=

64

,

kernel_size

=

3

,

padding

=

1

)

Input: batch=4, channels=3, sequence length=100

x

=

torch.

randn

(

4

,

3

,

100

)

output

=

conv1d

(

x

)

print

(

"Input shape:"

,

x.

shape

)

print

(

"Output shape:"

,

output.

shape

)

Example 2: Text classification

Example

import

torch

import

torch.

nn

as

nn

class

TextCNN

(

nn.

Module

)

:

   

def

init

(

self

,

vocab_size

,

embed_dim

,

num_classes

)

:

       

super

(

TextCNN

,

self

)

.

init

(

)

       

Embedding layer: (batch, seq) -> (batch, embed, seq)

       

self

.

embedding

=

nn.

Embedding

(

vocab_size

,

embed_dim

)

       

Multiple convolution kernels of different sizes

       

self

.

convs

=

nn.

ModuleList

(

[

            nn.

Conv1d

(

embed_dim

,

128

,

kernel_size

=

k

)

           

for

k

in

[

2

,

3

,

4

,

5

]

       

]

)

       

self

.

fc

=

nn.

Linear

(

128

4

,

num_classes

)

   

def

forward

(

self

,

x

)

:

       

x: (batch, seq)

        x

=

self

.

embedding

(

x

)

 

(batch, seq, embed)

        x

=

x.

permute

(

0

,

2

,

1

)

 

(batch, embed, seq)

       

Convolution + ReLU + GlobalMaxPool

        pooled

=

[

]

       

for

conv

in

self

.

convs

:

            c

=

conv

(

x

)

 

(batch, 128, seq')

            c

=

nn.

functional

.

relu

(

c

)

            c

=

c.

max

(

dim

=

2

)

[

0

]

 

Global max pooling

            pooled.

append

(

c

)

        x

=

torch.

cat

(

pooled

,

dim

=

1

)

       

return

self

.

fc

(

x

)

Testing

model

=

TextCNN

(

vocab_size

=

10000

,

embed_dim

=

128

,

num_classes

=

2

)

x

=

torch.

randint

(

0

,

10000

,

(

4

,

50

)

)

 

batch=4, seq=50

output

=

model

(

x

)

print

(

"Input shape:"

,

x.

shape

)

print

(

"Output shape:"

,

output.

shape

)

Example 3: Audio processing

Example

import

torch

import

torch.

nn

as

nn

Audio feature extraction

conv_audio

=

nn.

Conv1d

(

in_channels

=

1

,

out_channels

=

32

,

kernel_size

=

5

,

stride

=

2

)

Single-channel audio: batch=4, channels=1, samples=16000

audio

=

torch.

randn

(

4

,

1

,

16000

)

output

=

conv_audio

(

audio

)

print

(

"Input shape:"

,

audio.

shape

)

print

(

"Output shape:"

,

output.

shape

)

print

(

"Output length:"

,

output.

shape

[

2

]

)


Use Cases

Note: The input tensor has shape (batch, channels, length).


PyTorch torch.nn Reference Manual PyTorch torch.nn Reference Manual