This file contains hidden or 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 MyClass: | |
def __init__(self, x): | |
self.x = x | |
def double_value(self): | |
return 2 * self.x | |
value_list = [MyClass(i) for i in range(10)] | |
# These are the same |
This file contains hidden or 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 find_equal_partitions(L, r, left=0, k=0, partition=[], partition_list=[]): | |
if len(L) == 0: | |
return partition_list + [[tuple(partition[start:start+r]) for start in range(0, len(partition), r)]] | |
else: | |
if k == 0: | |
left = 0 | |
for idx, elem in enumerate(L[left:len(L) - (r - 1 - k)]): | |
remove_idx = left + idx | |
partition_list = find_equal_partitions(L[:remove_idx] + L[remove_idx + 1:], r, remove_idx, (k + 1) % r, partition + [elem], partition_list) |
This file contains hidden or 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 loop(lengths, count=0): | |
if len(lengths) == 0: | |
return count + 1 | |
else: | |
head, *tail = lengths | |
for _ in range(head): | |
count = loop(tail, count) | |
return count | |
lengths = [2, 3, 8, 6] |
This file contains hidden or 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 numpy as np | |
num_examples = 100000 | |
num_features = 50 | |
data = np.random.random(size=(num_examples, num_features)) | |
batch_size = 1000 | |
num_batches = num_examples // batch_size |
This file contains hidden or 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 numpy as np | |
def get_hann_window(M): | |
# M+1 evenly spaced numbers between -pi and pi | |
fac = np.linspace(-np.pi, np.pi, M + 1, dtype=np.float32) | |
w = 0.5 * (np.ones(M + 1, dtype=np.float32) + np.cos(fac)) | |
return w[:-1] | |
This file contains hidden or 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 numpy as np | |
def real_forward_fft(x): | |
N = len(x) # length of input signal | |
K = N // 2 + 1 # length of resulting FFT | |
result = np.zeros(K, dtype=np.complex64) | |
for k in range(K): | |
val = 0.0 |
This file contains hidden or 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 numpy as np | |
from numpy.fft import fft, ifft | |
def czt(x): | |
n = len(x) | |
N2 = int(2 ** np.ceil(np.log2(2 * n - 1))) # next power of 2 | |
chirp = np.zeros(2 * n - 1, dtype=np.complex64) | |
inv_chirp = np.zeros(N2, dtype=np.complex64) |
This file contains hidden or 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 pathlib import Path | |
import json | |
with Path('resources/tokens.json').open() as f: | |
tokens = json.load(f) | |
def decode(predictions): | |
# 'predictions' is 2D array, shape (num_frames, 999) | |
# For each frame, the index in range [0-998] with the largest value tells | |
# you the predicted token. |
This file contains hidden or 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 torch import nn | |
from einops.layers.torch import Reduce | |
model = nn.Sequential( | |
nn.Conv2d(3, 64, 3, 2), | |
nn.ReLU(inplace=True), | |
nn.Dropout(0.2), | |
nn.BatchNorm2d(64), | |
nn.Conv2d(64, 128, 3, 2), |
This file contains hidden or 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 einops import repeat, parse_shape | |
def make_broadcastable(array_list, pattern_list, result_pattern): | |
shape_list = [] | |
all_keys = set() | |
for array, pattern in zip(array_list, pattern_list): | |
shape_list.append(parse_shape(array, pattern)) | |
all_keys |= set(shape_list[-1].keys()) |