Skip to content

Instantly share code, notes, and snippets.

@Eligijus112
Eligijus112 / read_embeddings
Created March 9, 2020 18:38
A class to read word embeddings
import numpy as np
class Embeddings():
"""
A class to read the word embedding file and to create the word embedding matrix
"""
def __init__(self, path, vector_dimension):
self.path = path
@Eligijus112
Eligijus112 / text_to_tensor
Created March 11, 2020 21:29
A class to convert text strings to tensors for deep learning
mport numpy as np
from keras.preprocessing.sequence import pad_sequences
class TextToTensor():
def __init__(self, tokenizer, max_len):
self.tokenizer = tokenizer
self.max_len = max_len
@Eligijus112
Eligijus112 / RNN_nlp_model
Created March 14, 2020 12:27
A deep learning model for nlp classification tasks
# Deep learning:
from keras.models import Input, Model
from keras.layers import LSTM, Dense, Embedding, concatenate, Dropout, concatenate
from keras.layers import Bidirectional
class RnnModel():
"""
A recurrent neural network for semantic analysis
"""
@Eligijus112
Eligijus112 / nlp_pipeline
Created March 14, 2020 12:33
An NLP pipeline
import numpy as np
# The main model class
from RNN_model import RnnModel
# Importing the word preprocesing class
from text_preprocessing import TextToTensor, clean_text
# Importing the word embedding class
from embeddings import Embeddings
@Eligijus112
Eligijus112 / fashion-mnist-clf
Last active May 25, 2020 05:43
A very simple classifier for the fashion mnist dataset
# Importing the packages
import tensorflow as tf
# Downloading data
# The default split is 60k images in the training set and 10k in the test set
mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
# Normalizing the pixel values
training_images = training_images / 255.0
@Eligijus112
Eligijus112 / fashion-mnist-clf-conv-pool
Last active May 27, 2020 19:57
An image classifier pipeline using conv and pooling
import tensorflow as tf
# Initiating the class
mnist = tf.keras.datasets.fashion_mnist
# Downloading the data
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
# Reshaping the images for the convolutional input
training_images=training_images.reshape(60000, 28, 28, 1)
@Eligijus112
Eligijus112 / conv_visual
Created May 27, 2020 06:35
Convolution visualizer
# Extracting the trained layers from the model
successive_outputs = [layer.output for layer in model.layers[1:]]
visualization_model = tf.keras.models.Model(inputs = model.input, outputs = successive_outputs)
# Getting an image from the training set
image = training_images[1]
# Intermediate representations for this image
successive_feature_maps = visualization_model.predict(image.reshape(1, 28, 28, 1))
@Eligijus112
Eligijus112 / edge-detection
Created May 27, 2020 19:41
Edge detection using a simple filter
import cv2
import numpy as np
from scipy import misc
import matplotlib.pyplot as plt
# Getting the image of ascent
i = misc.ascent()
# Ploting the original image
plt.grid(False)
@Eligijus112
Eligijus112 / createXY.py
Last active February 3, 2022 12:37
A function to create X and Y training matrices for sequence modeling
def create_X_Y(ts: np.array, lag=1, n_ahead=1, target_index=0) -> tuple:
"""
A method to create X and Y matrix from a time series array for the training of
deep learning models
"""
# Extracting the number of features that are passed from the array
n_features = ts.shape[1]
# Creating placeholder lists
X, Y = [], []
@Eligijus112
Eligijus112 / NNMultistepModel.py
Created December 4, 2020 08:44
Class to create a model object used for sequence modeling
class NNMultistepModel():
def __init__(
self,
X,
Y,
n_outputs,
n_lag,
n_ft,
n_layer,