Skip to content

Instantly share code, notes, and snippets.

View mcleonard's full-sized avatar

Mat Leonard mcleonard

View GitHub Profile
@mcleonard
mcleonard / attention.py
Last active April 9, 2021 23:59
Example of implementing a super basic attention layer in numpy
import numpy as np
def softmax(x, axis=0):
""" Calculate softmax function for an array x
axis=0 calculates softmax across rows which means each column sums to 1
axis=1 calculates softmax across columns which means each row sums to 1
"""
return np.exp(x) / np.expand_dims(np.sum(np.exp(x), axis=axis), axis)
@mcleonard
mcleonard / react-markdown-mathjax.js
Last active October 7, 2023 18:32
Render math expressions in Markdown with Mathjax as a React component
import ReactMarkdown from 'react-markdown';
import MathJax from 'react-mathjax';
import RemarkMathPlugin from 'remark-math';
function MarkdownRender(props) {
const newProps = {
...props,
plugins: [
RemarkMathPlugin,
],
@mcleonard
mcleonard / batch_examples
Created September 13, 2017 20:56
RNN batch examples
seq = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
###
## For get_batches(seq, 2, 2)
# batch 1
x = array([[1, 2],
[7, 8]]),
y = array([[2, 3],
[8, 9]]))
@mcleonard
mcleonard / process_notebook.sh
Created February 1, 2016 23:00
A shell script for processing a Jupyter notebook Markdown file for use with Pelican
#!/bin/bash
FILE=$1
FILENAME="${FILE%%.*}"
FILE_DIR="${FILENAME}_files"
if [ -d $FILE_DIR ]; then
# When converting a notebook to markdown, all the images in the markdown file link to the files
# folder created when converting. We need to replace the file folder with
# the static images folder used by Pelican.
@mcleonard
mcleonard / pip_update_all
Last active July 14, 2017 10:11
Update all Python packages
pip list --outdated | grep -Eo '^[^ ]+' | xargs pip install -U
@mcleonard
mcleonard / vector.py
Last active October 22, 2024 12:31
A vector class in pure python.
import math
class Vector(object):
def __init__(self, *args):
""" Create a vector, example: v = Vector(1,2) """
if len(args)==0: self.values = (0,0)
else: self.values = args
def norm(self):
""" Returns the norm (length, magnitude) of the vector """