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 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 |
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
# 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: |
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 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], |
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
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.
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
# 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' |
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 get_features(model, image): | |
layers = { | |
'0' : 'conv1_1', | |
'5' : 'conv2_1', | |
'10': 'conv3_1', | |
'19': 'conv4_1', | |
'21': 'conv4_2', | |
'28': 'conv5_1' | |
} |
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 gramian(tensor): | |
t = tensor.view(tensor.shape[1], -1) | |
return t @ t.T |
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 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) |
OlderNewer