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
positional_encoding = PositionalEncoding(50, 512) | |
positional_encoding_values = positional_encoding.get_positional_encoding() | |
print("Positional Encoding Example:") | |
print("-----------") | |
print(positional_encoding_values) | |
print("-----------") |
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
class PositionalEncoding(object): | |
def __init__(self, position, d): | |
angle_rads = self._get_angles(np.arange(position)[:, np.newaxis], np.arange(d)[np.newaxis, :], d) | |
sines = np.sin(angle_rads[:, 0::2]) | |
cosines = np.cos(angle_rads[:, 1::2]) | |
self._encoding = np.concatenate([sines, cosines], axis=-1) | |
self._encoding = self._encoding[np.newaxis, ...] | |
def _get_angles(self, position, i, d): |
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
tfds.core.DatasetInfo( | |
name='ted_hrlr_translate', | |
version=0.0.1, | |
description='Data sets derived from TED talk transcripts for comparing similar language pairs | |
where one is high resource and the other is low resource. | |
', | |
urls=['https://github.com/neulab/word-embeddings-for-nmt'], | |
features=Translation({ | |
'en': Text(shape=(), dtype=tf.string), | |
'ru': Text(shape=(), dtype=tf.string), |
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 tensorflow_datasets as tfds | |
import tensorflow as tf | |
from tensorflow.keras.layers import Layer, Dense, LayerNormalization, Embedding, Dropout | |
from tensorflow.keras.models import Sequential, Model | |
from tensorflow.keras.optimizers.schedules import LearningRateSchedule | |
from tensorflow.keras.optimizers import Adam | |
from tensorflow.keras.losses import SparseCategoricalCrossentropy | |
from tensorflow.keras.metrics import Mean, SparseCategoricalAccuracy | |
from tqdm import tqdm |
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
class DataHandler(object): | |
def __init__(self, word_max_length = 30, batch_size = 64, buffer_size = 20000): | |
train_data, test_data = self._load_data() | |
self.tokenizer_ru = tfds.features.text.SubwordTextEncoder.build_from_corpus((ru.numpy() for ru, en in train_data), target_vocab_size=2**13) | |
self.tokenizer_en = tfds.features.text.SubwordTextEncoder.build_from_corpus((en.numpy() for ru, en in train_data), target_vocab_size=2**13) | |
self.train_data = self._prepare_training_data(train_data, word_max_length, batch_size, buffer_size) | |
self.test_data = self._prepare_testing_data(test_data, word_max_length, batch_size) |
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
enviroment.reset() | |
frames = [] | |
for _ in range(NUMBER_OF_FRAMES): | |
enviroment.step(enviroment.action_space.sample()) | |
frame = img_processor.resize_and_grayscale(enviroment.ale.getScreenRGB()) | |
frames.append(frame) | |
img_processor.plot_frames(frames, gray=True) |
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
total_epochs, total_penalties = 0, 0 | |
num_of_episodes = 10 | |
enviroment.reset() | |
counter = 0 | |
for e in range(num_of_episodes): | |
state = enviroment.reset() | |
state = img_processor.process_env_state(state) | |
epochs = 0 |
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
for e in tqdm(range(0, num_of_episodes)): | |
# Reset the enviroment | |
state = enviroment.reset() | |
# Initialize variables | |
reward = 0 | |
terminated = False | |
for timestep in range(timesteps_per_episode): | |
state = img_processor.process_env_state(state) |
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
optimizer = Adam(learning_rate=0.01) | |
state = enviroment.reset() | |
agent = Agent(enviroment, optimizer, state.shape) | |
batch_size = 32 | |
num_of_episodes = 1000 | |
timesteps_per_episode = 1000 | |
agent.q_network.summary() |
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
def retrain(self, batch_size): | |
minibatch = random.sample(self.expirience_replay, batch_size) | |
for state, action, reward, next_state, terminated in minibatch: | |
state = np.expand_dims(np.asarray(state).astype(np.float64), axis=0) | |
next_state = np.expand_dims(np.asarray(next_state).astype(np.float64), axis=0) | |
target = self.q_network.predict(state) |