This file contains 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 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: |
This file contains 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
// 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 |
This file contains 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
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) |
This file contains 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 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') |
This file contains 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 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) |
This file contains 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
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:]] |
This file contains 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 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 |
This file contains 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 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)) |
This file contains 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 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)) |
This file contains 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 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)) |
OlderNewer