Skip to content

Instantly share code, notes, and snippets.

@nbertagnolli
nbertagnolli / rc_interview_dfs.py
Last active December 21, 2017 05:48
Interview question for recurse center. Depth First Search in Python.
import string
def dfs(tree, element):
"""An iterative implementation of depth first search. It returns the element in question
if it is present and none otherwise.
"""
visited = set()
stack = [tree]
while stack:
@nbertagnolli
nbertagnolli / ResizeImage.swift
Created February 23, 2020 18:20
Resize Image in Swift3
// Create a variable describing the desired dimensions of our image
private let trainedImageSize = CGSize(width: 64, height: 64)
// Taken from:
// https://stackoverflow.com/questions/31314412/how-to-resize-image-in-swift
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
@nbertagnolli
nbertagnolli / UpdateClassificationImageSize.swift
Created February 23, 2020 18:27
Update image size in Xcode Model
func updateClassifications(for image: UIImage) {
classificationLabel.text = "Classifying..."
// Add this line before orientation to update the image size using the function in ResizeImage.swift
let resizedImage = resizeImage(image: image, targetSize: trainedImageSize)
// Previous:
// let orientation = CGImagePropertyOrientation(image.imageOrientation)
// The new line just has image changed to resizedImage
let orientation = CGImagePropertyOrientation(resizedImage.imageOrientation)
@nbertagnolli
nbertagnolli / keras_to_coreml.py
Created February 23, 2020 18:32
Convert Keras Model to CoreML
import coremltools
class_labels = ['airplane', 'automobile' ,'bird ','cat ','deer ','dog ','frog ','horse ','ship ','truck']
// model is just a trained keras model.
coreml_model = coremltools.converters.keras.convert(model,
input_names=['image'],
image_input_names='image',
class_labels=class_labels)
coreml_model.save('CIFAR.mlmodel')
@nbertagnolli
nbertagnolli / fine_tune_gpt2.py
Last active March 25, 2020 19:58
Use gpt2_simple to fine tune on a new dataset
import gpt_2_simple as gpt2
# The name of the pretrained GPT2 model we want to use it can be 117M, 124M, or 355M
# 124M is about as big as I can fit on my 1080Ti.
model_name = "124M"
# Download the model if it is not present
if not os.path.isdir(os.path.join("models", model_name)):
print(f"Downloading {model_name} model...")
gpt2.download_gpt2(model_name=model_name)
@nbertagnolli
nbertagnolli / create_binary_list_from_positive_int.py
Last active August 4, 2020 18:21
Converts a positive integer to its unsigned bianry representation as a list
from typing import List
def create_binary_list_from_int(number: int) -> List[int]:
if number < 0 or type(number) is not int:
raise ValueError("Only Positive integers are allowed")
return [int(x) for x in list(bin(number))[2:]]
@nbertagnolli
nbertagnolli / generate_even_data.py
Last active March 1, 2020 22:49
Generates a tuple containing a list of ones and a list of binary even numbers
def generate_even_data(max_int: int, batch_size: int=16) -> Tuple[List[int], List[List[int]]]:
# Get the number of binary places needed to represent the maximum number
max_length = int(math.log(max_int, 2))
# Sample batch_size number of integers in range 0-max_int
sampled_integers = np.random.randint(0, int(max_int / 2), batch_size)
# create a list of labels all ones because all numbers are even
labels = [1] * batch_size
@nbertagnolli
nbertagnolli / even_gan.py
Created March 1, 2020 22:58
Even number GAN generator
class Generator(nn.Module):
def __init__(self, input_length: int):
super(Generator, self).__init__()
self.dense_layer = nn.Linear(int(input_length), int(input_length))
self.activation = nn.Sigmoid()
def forward(self, x):
return self.activation(self.dense_layer(x))
@nbertagnolli
nbertagnolli / even_discriminator.py
Created March 1, 2020 23:08
A basic discriminator for a simple GAN
class Discriminator(nn.Module):
def __init__(self, input_length: int):
super(Discriminator, self).__init__()
self.dense = nn.Linear(int(input_length), 1);
self.activation = nn.Sigmoid()
def forward(self, x):
return self.activation(self.dense(x))
@nbertagnolli
nbertagnolli / even_discriminator.py
Created March 1, 2020 23:08
A basic discriminator for a simple GAN
class Discriminator(nn.Module):
def __init__(self, input_length: int):
super(Discriminator, self).__init__()
self.dense = nn.Linear(int(input_length), 1);
self.activation = nn.Sigmoid()
def forward(self, x):
return self.activation(self.dense(x))