Skip to content

Commit a6acf47

Browse files
committed
Merge branch 'develop'
2 parents 12d7ea9 + c260b39 commit a6acf47

File tree

24 files changed

+1367
-35
lines changed

24 files changed

+1367
-35
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ dist
1010
!*.md
1111

1212
.idea
13-
.python-version
13+
.python-version
14+
15+
test

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## [1.1.0] - 2022-08-28
2+
### Changed
3+
- Changed `mltu.transformers.SpectrogramPadding` object, to pad spectrogram end with zeros instead of start
4+
5+
### Added
6+
- Created `Tutorials/09_translation_transformer` tutorial, that shows how to train translation transformer model
7+
- Created `mltu.tensorflow.tokenizers` module, that contains `CustomTokenizer` for text data
8+
- Created `mltu.tensorflow.transformer.attention` module, that contains `BaseAttention`, `CrossAttention`, `GlobalSelfAttention` and `CausalSelfAttention` layers
9+
- Created `mltu.tensorflow.transformer.layers` module, that contains `positional_encoding` function, `PositionalEmbedding`, `FeedForward`, `EncoderLayer`, `DecoderLayer`, `Encoder`, `Decoder` layers and `Transformer` model
10+
- Created `mltu.tensorflow.transformer.callbacks` module, that contains `EncDecSplitCallback` callback, to split Transformer model into separate encoder and decoder models
11+
- Created `mltu.tensorflow.transformer.utils` module, that contains `MaskedLoss` loss and `MaskedAccuracy` metric, used for training Transformer models
12+
113
## [1.0.15] - 2022-07-15
214
### Changed
315
- Fixed bug in `mltu.dataProvider.DataProvider` to work with `batch_postprocessors`.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ Each tutorial has its own requirements.txt file for a specific mltu version. As
2323
5. [Introduction to speech recognition with TensorFlow](https://pylessons.com/speech-recognition), code in ```Tutorials\05_speech_recognition``` folder;
2424
6. [Introduction to PyTorch in a practical way](https://pylessons.com/pytorch-introduction), code in ```Tutorials\06_pytorch_introduction``` folder;
2525
7. [Using custom wrapper to simplify PyTorch models training pipeline](https://pylessons.com/pytorch-introduction), code in ```Tutorials\07_pytorch_wrapper``` folder;
26-
8. [Handwriting words recognition with PyTorch](https://pylessons.com/handwriting-recognition-pytorch), code in ```Tutorials\08_handwriting_recognition_torch``` folder;
26+
8. [Handwriting words recognition with PyTorch](https://pylessons.com/handwriting-recognition-pytorch), code in ```Tutorials\08_handwriting_recognition_torch``` folder;
27+
9. [Transformer training with TensorFlow for Translation task](https://pylessons.com/transformers-training), code in ```Tutorials\09_translation_transformer``` folder;

Tutorials/05_sound_to_text/inferenceModel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def predict(self, data: np.ndarray):
3636
spectrogram = WavReader.get_spectrogram(wav_path, frame_length=configs.frame_length, frame_step=configs.frame_step, fft_length=configs.fft_length)
3737
# WavReader.plot_raw_audio(wav_path, label)
3838

39-
padded_spectrogram = np.pad(spectrogram, ((configs.max_spectrogram_length - spectrogram.shape[0], 0),(0,0)), mode="constant", constant_values=0)
39+
padded_spectrogram = np.pad(spectrogram, ((0, configs.max_spectrogram_length - spectrogram.shape[0]),(0,0)), mode="constant", constant_values=0)
4040

4141
# WavReader.plot_spectrogram(spectrogram, label)
4242

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Training TensorFlow Transformer model for Spanish to English translation task
2+
### In this tutorial, I'll walk through a practical example of Transformer Training for Language Translation tasks from Spanish to the English language
3+
4+
<br><br>
5+
# **Detailed tutorial**:
6+
### [Transformer training with TensorFlow for Translation task](https://pylessons.com/transformers-training)
7+
8+
<p align="center">
9+
<img src="https://pylessons.com/media/Tutorials/transformers/transformers-training/transformers-training.png">
10+
</p>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
from datetime import datetime
3+
4+
from mltu.configs import BaseModelConfigs
5+
6+
7+
class ModelConfigs(BaseModelConfigs):
8+
def __init__(self):
9+
super().__init__()
10+
self.model_path = os.path.join(
11+
"Models/09_translation_transformer",
12+
datetime.strftime(datetime.now(), "%Y%m%d%H%M"),
13+
)
14+
self.num_layers = 4
15+
self.d_model = 128
16+
self.num_heads = 8
17+
self.dff = 512
18+
self.dropout_rate = 0.1
19+
self.batch_size = 16
20+
self.train_epochs = 50
21+
# CustomSchedule parameters
22+
self.init_lr = 0.00001
23+
self.lr_after_warmup = 0.0005
24+
self.final_lr = 0.0001
25+
self.warmup_epochs = 2
26+
self.decay_epochs = 18
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
import os
3+
import requests
4+
from tqdm import tqdm
5+
from bs4 import BeautifulSoup
6+
7+
# URL to the directory containing the files to be downloaded
8+
language = "en-es"
9+
url = f"https://data.statmt.org/opus-100-corpus/v1.0/supervised/{language}/"
10+
save_directory = f"./Datasets/{language}"
11+
12+
# Create the save directory if it doesn't exist
13+
os.makedirs(save_directory, exist_ok=True)
14+
15+
# Send a GET request to the URL
16+
response = requests.get(url)
17+
18+
# Parse the HTML response
19+
soup = BeautifulSoup(response.content, 'html.parser')
20+
21+
# Find all the anchor tags in the HTML
22+
links = soup.find_all('a')
23+
24+
# Extract the href attribute from each anchor tag
25+
file_links = [link['href'] for link in links if '.' in link['href']]
26+
27+
# Download each file
28+
for file_link in tqdm(file_links):
29+
file_url = url + file_link
30+
save_path = os.path.join(save_directory, file_link)
31+
32+
print(f"Downloading {file_url}")
33+
34+
# Send a GET request for the file
35+
file_response = requests.get(file_url)
36+
if file_response.status_code == 404:
37+
print(f"Could not download {file_url}")
38+
continue
39+
40+
# Save the file to the specified directory
41+
with open(save_path, 'wb') as file:
42+
file.write(file_response.content)
43+
44+
print(f"Saved {file_link}")
45+
46+
print("All files have been downloaded.")
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import tensorflow as tf
2+
3+
from mltu.tensorflow.transformer.layers import Encoder, Decoder
4+
5+
def Transformer(
6+
input_vocab_size: int,
7+
target_vocab_size: int,
8+
encoder_input_size: int = None,
9+
decoder_input_size: int = None,
10+
num_layers: int=6,
11+
d_model: int=512,
12+
num_heads: int=8,
13+
dff: int=2048,
14+
dropout_rate: float=0.1,
15+
) -> tf.keras.Model:
16+
"""
17+
A custom TensorFlow model that implements the Transformer architecture.
18+
19+
Args:
20+
input_vocab_size (int): The size of the input vocabulary.
21+
target_vocab_size (int): The size of the target vocabulary.
22+
encoder_input_size (int): The size of the encoder input sequence.
23+
decoder_input_size (int): The size of the decoder input sequence.
24+
num_layers (int): The number of layers in the encoder and decoder.
25+
d_model (int): The dimensionality of the model.
26+
num_heads (int): The number of heads in the multi-head attention layer.
27+
dff (int): The dimensionality of the feed-forward layer.
28+
dropout_rate (float): The dropout rate.
29+
30+
Returns:
31+
A TensorFlow Keras model.
32+
"""
33+
inputs = [
34+
tf.keras.layers.Input(shape=(encoder_input_size,), dtype=tf.int64),
35+
tf.keras.layers.Input(shape=(decoder_input_size,), dtype=tf.int64)
36+
]
37+
38+
encoder_input, decoder_input = inputs
39+
40+
encoder = Encoder(num_layers=num_layers, d_model=d_model, num_heads=num_heads, dff=dff, vocab_size=input_vocab_size, dropout_rate=dropout_rate)(encoder_input)
41+
decoder = Decoder(num_layers=num_layers, d_model=d_model, num_heads=num_heads, dff=dff, vocab_size=target_vocab_size, dropout_rate=dropout_rate)(decoder_input, encoder)
42+
43+
output = tf.keras.layers.Dense(target_vocab_size)(decoder)
44+
45+
return tf.keras.Model(inputs=inputs, outputs=output)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
beautifulsoup4

0 commit comments

Comments
 (0)