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

PyTorch torch.nn.GRU function

PyTorch torch.nn.GRU function

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


torch.nn.GRU is the gated recurrent unit module in PyTorch.

GRU is a simplified version of LSTM with fewer parameters, faster computation, and similar performance.

Function definition



torch.nn.GRU(input_size, hidden_size, num_layers=1, bias=True, batch_first=True, dropout=0, bidirectional=False)


Usage examples

Example 1: Basic usage

Example

import

torch

import

torch.

nn

as

nn

GRU: input 256 dimensions, hidden 256 dimensions, 2 layers

gru

=

nn.

GRU

(

input_size

=

256

,

hidden_size

=

256

,

num_layers

=

2

,

batch_first

=

True

)

Input: batch=4, sequence=10, features=256

x

=

torch.

randn

(

4

,

10

,

256

)

output

,

hidden

=

gru

(

x

)

print

(

"Input shape:"

,

x.

shape

)

print

(

"Output shape:"

,

output.

shape

)

print

(

"Hidden state shape:"

,

hidden.

shape

)

Example 2: Comparing with LSTM

Example

import

torch

import

torch.

nn

as

nn

import

time

LSTM and GRU with the same configuration

lstm

=

nn.

LSTM

(

256

,

256

,

1

,

batch_first

=

True

)

gru

=

nn.

GRU

(

256

,

256

,

1

,

batch_first

=

True

)

x

=

torch.

randn

(

32

,

100

,

256

)

Performance Comparison

for

model

,

name

in

[

(

lstm

,

"LSTM"

)

,

(

gru

,

"GRU"

)

]

:

    start

=

time

.

time

(

)

   

for

_

in

range

(

100

)

:

        _

=

model

(

x

)

   

print

(

f

"{name} time: {time.time()-start:.3f}s"

)

Example 3: Classification task

Example

import

torch

import

torch.

nn

as

nn

class

GRUClassifier

(

nn.

Module

)

:

   

def

init

(

self

,

vocab_size

,

embed_dim

,

hidden_dim

,

num_classes

)

:

       

super

(

GRUClassifier

,

self

)

.

init

(

)

       

self

.

embedding

=

nn.

Embedding

(

vocab_size

,

embed_dim

)

       

self

.

gru

=

nn.

GRU

(

embed_dim

,

hidden_dim

,

batch_first

=

True

,

bidirectional

=

True

)

       

self

.

fc

=

nn.

Linear

(

hidden_dim *

2

,

num_classes

)

   

def

forward

(

self

,

x

)

:

        embedded

=

self

.

embedding

(

x

)

        _

,

hidden

=

self

.

gru

(

embedded

)

       

Concatenate the final hidden states of both directions

        hidden

=

torch.

cat

(

[

hidden

[

2

]

,

hidden

[

1

]

]

,

dim

=

1

)

       

return

self

.

fc

(

hidden

)

model

=

GRUClassifier

(

10000

,

128

,

128

,

2

)

x

=

torch.

randint

(

0

,

10000

,

(

8

,

50

)

)

output

=

model

(

x

)

print

(

"Input:"

,

x.

shape

,

"-> Output:"

,

output.

shape

)


LSTM vs GRU

Aspect LSTM GRU
Number of parameters Relatively Many less
Gates 3 gates 2 gates
Compute Slower Faster

Use Cases


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