Created
August 10, 2023 13:19
-
-
Save pythonlessons/e45c0bd807604ae61193908d24f42f3d to your computer and use it in GitHub Desktop.
transformers_introduction
This file contains hidden or 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
| import numpy as np | |
| import tensorflow as tf | |
| for gpu in tf.config.experimental.list_physical_devices('GPU'): | |
| tf.config.experimental.set_memory_growth(gpu, True) | |
| def positional_encoding(length: int, depth: int): | |
| """ | |
| Generates a positional encoding for a given length and depth. | |
| Args: | |
| length (int): The length of the input sequence. | |
| depth (int): The depth that represents the dimensionality of the encoding. | |
| Returns: | |
| tf.Tensor: The positional encoding of shape (length, depth). | |
| """ | |
| depth = depth / 2 | |
| positions = np.arange(length)[:, np.newaxis] # (seq, 1) | |
| depths = np.arange(depth)[np.newaxis, :]/depth # (1, depth) | |
| angle_rates = 1 / (10000**depths) # (1, depth) | |
| angle_rads = positions * angle_rates # (pos, depth) | |
| pos_encoding = np.concatenate([np.sin(angle_rads), np.cos(angle_rads)], axis=-1) | |
| return tf.cast(pos_encoding, dtype=tf.float32) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment