Skip to content

Instantly share code, notes, and snippets.

View wcneill's full-sized avatar

Wesley wcneill

View GitHub Profile
@wcneill
wcneill / writetofile.py
Created September 3, 2019 22:42
Parses a Vanity Fair URL, prints article content to file
from bs4 import BeautifulSoup
import requests as req
import textwrap as tr
# Iterates through all paragraph tags containing article content
# and formats content such that paragraphs are separated and all
# lines are no more than 72 characters in width. The .strings
# attribute of BS4 Tags returns a generator containing all text
# children of the given tag. In order to format the code
# using string.join, the generator needed to be converted to a
@wcneill
wcneill / readfile.py
Created September 4, 2019 19:51
functions for reading txt files with two different formats and counting instances of certain strings in the files
# reads a file with a single name on each line and counts the
# number of instances of that name in the file
def getcount(path):
with open(path, 'r') as open_file:
name_count = {}
for line in open_file:
if line in name_count.keys():
name_count[line] += 1
else:
@wcneill
wcneill / tic-tac-to-win-checker.py
Created September 5, 2019 20:23
given a 3x3 grid, determine if three identical elements can be found in any given row, column or diagonal.
def all_same(lst):
return all(item == lst[0] for item in lst)
# given an a 3x3 board representing a tic tac to game between player 1
# and player 2, determine if any players have won by checking for
# three in a row in any column, row or diagonal.
if __name__ == '__main__':
grid = [[0, 2, 1],
[2, 1, 2],
@wcneill
wcneill / tic-tac-toe.py
Created September 7, 2019 20:58
A game of tic tac toe
grid = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
print(grid)
win = False
turns = 0
player = 1
while not win:
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# Define pre-trained model
vgg = models.vgg19(pretrained=True).features
# Freeze all model weights
for param in vgg.parameters():
param.requires_grad_(False)
# Move model to GPU, if available.
device = torch.device(
'cuda' if torch.cuda.is_available() else 'cpu'
def get_features(model, image):
layers = {
'0' : 'conv1_1',
'5' : 'conv2_1',
'10': 'conv3_1',
'19': 'conv4_1',
'21': 'conv4_2',
'28': 'conv5_1'
}
def gramian(tensor):
t = tensor.view(tensor.shape[1], -1)
return t @ t.T
def content_loss(c_features, t_features):
"""
Compute mean squared content loss of all feature maps.
"""
loss = 0.5 * (t_features['conv4_2'] - c_features['conv4_2']) ** 2
return torch.mean(loss)