This repository has been archived by the owner on Apr 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Continuing to implement the Convolutional Seq2Seq.
- Loading branch information
Showing
5 changed files
with
58 additions
and
15 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
examples/applications/generation/conv_seq2seq_generation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from torchtext.data import BPTTIterator, Field | ||
|
||
from textformer.datasets.generative import GenerativeDataset | ||
from textformer.models import ConvSeq2Seq | ||
|
||
# Defines the device which should be used, e.g., `cpu` or `cuda` | ||
device = 'cpu' | ||
|
||
# Defines the input file | ||
file_path = 'data/generative/chapter1_harry.txt' | ||
|
||
# Defines a datatype for further tensor conversion | ||
source = Field(lower=True) | ||
|
||
# Creates the GenerativeDataset | ||
dataset = GenerativeDataset(file_path, source) | ||
|
||
# Builds the vocabulary | ||
source.build_vocab(dataset, min_freq=1) | ||
|
||
# Creates an iterator that backpropagates through time | ||
train_iterator = BPTTIterator(dataset, batch_size=16, bptt_len=10, device=device) | ||
|
||
# Creating the ConvSeq2Seq model | ||
conv_seq2seq = ConvSeq2Seq(n_input=len(source.vocab), n_output=len(source.vocab), | ||
n_hidden=512, n_embedding=256, n_layers=1, kernel_size=3, | ||
ignore_token=None, init_weights=None, device=device) | ||
|
||
# Training the model | ||
conv_seq2seq.fit(train_iterator, epochs=10) | ||
|
||
# Generating artificial text | ||
text = conv_seq2seq.generate_text( | ||
'Mr. Dursley', source, length=100, temperature=0.5) | ||
|
||
print(' '.join(text)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters