Skip to content

Instantly share code, notes, and snippets.

View firobeid's full-sized avatar
🎯
Focused

Firas Obeid firobeid

🎯
Focused
View GitHub Profile

Chapter 1: The Entrance (Embeddings & Positional Encodings) Our story begins with raw text—the sentence "The car is blue"—entering the machine. To be understood by the model, these words must shed their human form. They are converted into Embeddings, transforming them into vector representations of meaning. However, meaning isn't enough; order matters. So, Positional Encodings are added to these vectors, giving each word a unique address in the sequence so the model knows that "The" comes before "car."

Chapter 2: The Three Personalities (Queries, Keys, and Values) As the vectors move deeper (0:00-0:02), they undergo a linear transformation. Each input vector splits into three distinct roles, known as the Query, the Key, and the Value.

  • The Query is the distinct representation of the word asking questions (e.g., "What am I related to?").
  • The Key is the label or identifier for every other word.
  • The Value is the actual content or substance of the word.

Chapter 3: The Great Conversation (Self-Attent

path = '%s/%s'% (path_dir, file_name)
if file_name not in os.listdir(path_dir):
writer = pd.ExcelWriter(path=path, engine= "openpyxl")
fp = open(path, 'w', encoding='utf-8')
else:
writer = pd.ExcelWriter(path=path, engine= "openpyxl", mode = 'a', if_sheet_exists = 'replace')
def save_sheet(df, metric_name, exits = True):
try:
# Read the csv and convert it into a list of dictionaries
with open(file_to_load) as financial_data:
reader = csv.reader(financial_data)
# Read the header row
header = next(reader)
# Extract first row to avoid appending to net_change_list
first_row = next(reader)
total_months = total_months + 1
def direction_model():
model = Sequential(name = 'RNNStocks')
model.add(Embedding(input_dim = 185, output_dim = 256,batch_input_shape=[None, None],
mask_zero = True, name ='EmbedLayer'))
model.add(Bidirectional(LSTM(1024,
return_sequences=False,stateful=False,
recurrent_initializer='glorot_uniform'), merge_mode ='concat',name = 'BiLSTM'))
#final state encodes full representation of a single passed headine
model.add(BatchNormalization(name='BatchNormal')) #After RNN(S-shape activation-f(x) / Before ReLU(Non-Gaussian))
# model.add(tf.keras.layers.Masking(mask_value=0))
@firobeid
firobeid / gpu.py
Last active August 31, 2023 03:33
gpu_devices = tf.config.list_physical_devices('GPU')
if gpu_devices:
print('Using GPU')
for gpu in gpu_devices[0:2]:
tf.config.experimental.set_memory_growth(gpu, True)
else:
print('Using CPU')
tf.config.optimizer.set_jit(True)
print('used: {}% free: {:.2f}GB'.format(psutil.virtual_memory().percent, float(psutil.virtual_memory().free)/1024**3))#@
def option_pricer(n, So, k, r, sigma, t):
import numpy as np
N = n
St = So * np.exp((r - 0.5*sigma**2)*t + sigma*np.sqrt(t)*np.random.normal(size = N))
print(np.exp((r - 0.5*sigma**2)*t + sigma*np.sqrt(t)))
print(St)
pv_call = np.exp(-r*t) * np.maximum(St - k,0)
print(np.exp(-r*t))
print(pv_call)
c = np.mean(pv_call)
@firobeid
firobeid / NPV-Cost Benefit Calculation
Created October 26, 2018 02:46
Coded function that calculated the NPV, IRR, price index and payback period of a project with foretasted cash flows. The cash flows are displayed in pandas dataframe.
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 21 18:12:58 2018
@author: Firas Obeid
Email: feras.obeid@lau.edu
All rights reserved.
"""
# This assignement has taught me 10,000 ways how not to let my code work with pandas. Make it simple it comes out elegant.
import pandas as pd
import numpy as np