Skip to content

Instantly share code, notes, and snippets.

View airalcorn2's full-sized avatar

Michael A. Alcorn airalcorn2

View GitHub Profile
@airalcorn2
airalcorn2 / atan2.py
Created September 14, 2018 20:06
Testing the (cos(theta), sin(theta)) encoding of an angle discussed here --> https://stats.stackexchange.com/questions/218407/encoding-angle-data-for-neural-networ.
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.utils.data
import torch.nn as nn
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class Net(nn.Module):
@airalcorn2
airalcorn2 / tensorflow_vs_pytorch_scatter_nd.py
Created August 14, 2018 16:03
Replicating TensorFlow's scatter_nd function in PyTorch.
# See: https://www.tensorflow.org/api_docs/python/tf/scatter_nd.
# TensorFlow
import tensorflow as tf
sess = tf.InteractiveSession()
indices = tf.constant([[0, 1], [2, 3]])
updates = tf.constant([[5, 5, 5, 5],
[6, 6, 6, 6]])
shape = tf.constant([4, 4, 4])
@airalcorn2
airalcorn2 / rtw_fatalities.py
Created July 3, 2018 16:36
Plotting fatality rates for states that had a change in right-to-work laws from this paper --> https://oem.bmj.com/content/early/2018/06/13/oemed-2017-104747.
# Michael A. Alcorn
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("repdata.tab", sep = "\t", index_col = False)
no_law = set(df[df["RTW"] == 0]["state"])
yes_law = set(df[df["RTW"] == 1]["state"])
states_that_changed = list(no_law & yes_law)
states_that_changed.sort()
@airalcorn2
airalcorn2 / attention_grok.py
Last active March 21, 2020 14:10
A barebones PyTorch implementation of a seq2seq model with attention.
# Michael A. Alcorn
import torch
import torch.autograd as autograd
import torch.nn as nn
def create_lstm(params):
"""Create a LSTM from a dictionary of parameters.
# Michael A. Alcorn
library(ggplot2)
library(plyr)
data <- read.csv("species.csv")
data$Date <- as.Date(data$Date, "%Y-%m-%d")
ggplot(data, aes(Date, Total)) + geom_line() + scale_x_date(date_breaks = "1 month")
@airalcorn2
airalcorn2 / clade_counts.py
Created April 13, 2018 18:54
A script for counting the total species in each clade in a Newick formatted file.
# Michael A. Alcorn
from Bio import Phylo
tree = Phylo.read("species_newick.txt", "newick")
clade_counts = {}
for clade in tree.find_clades():
if not clade.is_terminal():
clade_counts[str(clade)] = clade.count_terminals()
@airalcorn2
airalcorn2 / species_plots.py
Last active April 13, 2018 17:40
Python code to generate the plots used to make this GIF --> http://imgur.com/BjgN6UA.
# Michael A. Alcorn
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import multiprocessing
import numpy as np
import pandas as pd
import seaborn as sns
import time
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from keras import backend
from keras.layers import Input, Dense
from keras.models import Model
# Autoencoder.
kernels = model.layers[0].get_weights()[0].T
(rows, cols) = (8, 4)
plt.figure(figsize = (16, 24))
gs = gridspec.GridSpec(rows, cols, wspace = 0.1, hspace = 0.1)
for i in range(kernels.shape[0]):
row = i // cols
col = i % cols
ax = plt.subplot(gs[row, col])
# Michael A. Alcorn ([email protected])
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import random
fixed_rates = {"p_1": 0.3, "p_2": 0.5}
find_rates = {"biased": {"fixed": 0.7, "non_fixed": 0.9},