Skip to content

Instantly share code, notes, and snippets.

import numpy as np
import math
np.random.seed(423)
# code for the environment
class CartPoleEnv:
def __init__(self):
# Environment parameters
self.gravity = 9.8
self.masscart = 1.0
# inspired by micrograd and tinygrad
import numpy as np
class Tensor:
def __init__(self, data, name=''):
self.data = np.array(data)
self.grad = np.zeros_like(self.data)
self.parents = []
self.op = ''
@apoorvnandan
apoorvnandan / claude_3.5_sonnet_artifacts.xml
Created July 25, 2024 08:40 — forked from dedlim/claude_3.5_sonnet_artifacts.xml
Claude 3.5 Sonnet, Full Artifacts System Prompt
<artifacts_info>
The assistant can create and reference artifacts during conversations. Artifacts are for substantial, self-contained content that users might modify or reuse, displayed in a separate UI window for clarity.
# Good artifacts are...
- Substantial content (>15 lines)
- Content that the user is likely to modify, iterate on, or take ownership of
- Self-contained, complex content that can be understood on its own, without context from the conversation
- Content intended for eventual use outside the conversation (e.g., reports, emails, presentations)
- Content likely to be referenced or reused multiple times
@apoorvnandan
apoorvnandan / app.js
Created December 12, 2023 03:03
Basic contenteditable text editor
document.getElementById("editor").addEventListener("keydown", function (event) {
// Check if Enter is pressed without the Ctrl key
if (event.key === "Enter" && !event.ctrlKey) {
event.preventDefault();
}
// Check if both Ctrl and Enter are pressed
if (event.ctrlKey && event.key === "Enter") {
event.preventDefault();
@apoorvnandan
apoorvnandan / minimal_progress_bar.py
Created August 27, 2020 07:03
Extremely simple progress bar in python
class ProgressBar:
def __init__(self, total, desc='', width=10):
self.total = total
self.width = width
self.header = f"{desc}: " if len(desc) > 0 else ''
self.i = 0
self.__print_progress()
def __print_progress(self):
p = round((self.i*self.width) / self.total)
@apoorvnandan
apoorvnandan / convert_nb_script.py
Last active January 20, 2020 10:53
Convert jupyter notebook to python script (inspired by nbdev)
##
# usage:
# 1. Add '#export' as the first line of the cells you want to export.
# This script will ignore all other cells.
# 2. $python convert_nb_script.py -n sample.ipynb > sample_script.py
#
# You can use other tags to selectively export other cells
# eg. Add tag #test to the cells containing unit tests
# python convert_nb_script.py -n sample.ipynb -t test > unit_tests.py
##
def prefix_beam_search(ctc,
alphabet,
blank_token,
end_token,
space_token,
lm,
k=25,
alpha=0.30,
beta=5,
prune=0.001):
def create_spectrogram(signals):
stfts = tf.signal.stft(signals, fft_length=256)
spectrograms = tf.math.pow(tf.abs(stfts), 0.5)
return spectrograms
class ASR(tf.keras.Model):
def __init__(self, filters, kernel_size, conv_stride, conv_border, n_lstm_units, n_dense_units):
super(ASR, self).__init__()
self.conv_layer = tf.keras.layers.Conv1D(filters,
kernel_size,
strides=conv_stride,
padding=conv_border,
activation='relu')
self.lstm_layer = tf.keras.layers.LSTM(n_lstm_units,
return_sequences=True,
@apoorvnandan
apoorvnandan / prefix_beam_search_complete.py
Last active August 19, 2019 09:16
Prefix Beam Search
class Cache:
'''
class to cache the probability values while performing prefix beam search
'''
def __init__(self):
self.data = {}
def add(self, key1, key2, value):
if key1 not in self.data:
self.data[key1] = {}