Skip to content

Instantly share code, notes, and snippets.

@eileen-code4fun
eileen-code4fun / autograd_add_mul.go
Created April 27, 2023 03:44
Autograd Incorrect add and mul in Golang
func (v *Value) add(v1 *Value) *Value {
nv := newValue(v.data+v1.data, nil /* incorrect, should be []*Value{v, v1} */)
nv.backward = func() {
v.grad = /* incorrect, should be += */ nv.grad
v1.grad = /* incorrect, should be += */ nv.grad
}
return nv
}
func (v *Value) mul(v1 *Value) *Value {
@eileen-code4fun
eileen-code4fun / autograd.go
Created April 27, 2023 03:41
Autograd in Golang
package main
import "fmt"
type Value struct {
data, grad float64
backward func()
children []*Value
}
@eileen-code4fun
eileen-code4fun / sum.go
Created April 27, 2023 03:39
Sum in Golang
package main
import "fmt"
func sum(a, b int) int {
return a + b
}
func main() {
fmt.Printf("%d\n", sum(1, 2))
@eileen-code4fun
eileen-code4fun / sql_translation_model.py
Created February 4, 2022 02:24
SQL Translation Model
class Nl2SqlTranslator(tf.keras.Model):
def __init__(self, nl_text_processor, sql_text_processor, fixed_embedding, unit=128):
super().__init__()
# Natural language
self.nl_text_processor = nl_text_processor
self.nl_voba_size = len(nl_text_processor.get_vocabulary())
self.nl_embedding = tf.keras.layers.Embedding(
self.nl_voba_size,
output_dim=unit,
mask_zero=True)
@eileen-code4fun
eileen-code4fun / filling.py
Created February 3, 2022 23:09
Filling Placeholders
import names
def populate(in_filename, out_filename):
out = []
with open(in_filename) as f:
for l in f.readlines():
parts = l.split('|||')
nli_template = parts[0].strip().split()
nli = []
target = []
@eileen-code4fun
eileen-code4fun / load_embedding.py
Created February 3, 2022 15:26
Load Embedding
# wget http://nlp.stanford.edu/data/glove.6B.zip
# unzip glove.6B.zip
import numpy as np
import tensorflow as tf
embeddings_index = {}
with open('glove.6B.100d.txt') as f:
for line in f:
word, coefs = line.split(maxsplit=1)
@eileen-code4fun
eileen-code4fun / translation_prepro.py
Created January 21, 2022 06:03
Translation Preprocessing
def standardize(text):
# Split accecented characters.
text = tf_text.normalize_utf8(text, 'NFKD')
text = tf.strings.lower(text)
# Keep space, a to z, and select punctuation.
text = tf.strings.regex_replace(text, '[^ a-z.?!,¿]', '')
# Add spaces around punctuation.
text = tf.strings.regex_replace(text, '[.?!,¿]', r' \0 ')
# Strip whitespace.
text = tf.strings.strip(text)
@eileen-code4fun
eileen-code4fun / translation_load.py
Created January 21, 2022 06:01
Translation Load
import tensorflow as tf
def split(text):
parts = tf.strings.split(text, sep='\t')
return parts[0], parts[1]
dataset = tf.data.TextLineDataset(['spa.txt']).map(split)
eng_dataset = dataset.map(lambda eng, spa : eng)
spa_dataset = dataset.map(lambda eng, spa : spa)
@eileen-code4fun
eileen-code4fun / translation_model.py
Created January 21, 2022 06:00
Translation Model
class Spa2EngTranslator(tf.keras.Model):
def __init__(self, eng_text_processor, spa_text_processor, unit=512):
pass
def call(self, eng_text, spa_text):
pass
@eileen-code4fun
eileen-code4fun / translation_init.py
Created January 21, 2022 05:59
Translation Init
class Spa2EngTranslator(tf.keras.Model):
def __init__(self, eng_text_processor, spa_text_processor, unit=512):
super().__init__()
# Spanish
self.spa_text_processor = spa_text_processor
self.spa_voba_size = len(spa_text_processor.get_vocabulary())
self.spa_embedding = tf.keras.layers.Embedding(
self.spa_voba_size,
output_dim=unit,
mask_zero=True)