import numpy as np
def softmax(x):
return np.divide(np.exp(x), np.sum(np.exp(x), axis=0, keepdims=True))
def softmax_deriv(x):
return x
#cross entropy loss
def compute_cost(y,o):
return - np.sum(np.sum(y * np.log(o), axis=0, keepdims=True), axis=1)
#init all biases to 0 and all weights to 1
def initialize_model():
#<START> I make model <END>
#windows size 1
# (<START>, I)
# (I, make)
embeddings = np.array([ #embedding size = 7, vocabulary size = 5
[.1, .2, .3, .4, .5, .6, .7], #<START>
[.6, .7, .8, .9, .10, .11, .12], #I
[.11, .12, .13, .14, .15, .16, .17], #make
[.21, .22, .23, .24, .25, .26, .27], #model
[.26, .27, .28, .29, .30, .31, .32] #<END>
], dtype=np.float64)
#no bias since it has same performance with it
W1 = np.array([ #dimensions are vocab_size X embedding_size
[1,2,3,4,5,6,7], #w1, w2, ..., w7
[1,1,4,5,6,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1],
[1,1,1,1,1,1,1]
], dtype=np.float64)
return embeddings, W1
def forward_prop(x, W1):
o = W1.dot(x)
o = softmax(o)
return o
def back_prop(x,y,o,W1):
dZ = softmax_deriv(o)-y
grad_x = W1.T.dot(dZ)
grad_W1 = dZ.dot(x.T)
return grad_x, grad_W1
#for inner words we repeat batches 2 times, since for some inputs
#e.g. "make", we can have as output "I" and "model" - those are
#words that show up around word "make" for window size 1
def get_batch(embeddings):
#<START> I make model <END>
x = np.array([
embeddings[0], #[<START>] I
embeddings[1], #<START> [I]
embeddings[1], #[I] make
embeddings[2], #I [make]
embeddings[2], #[make] model
embeddings[3], #make [model]
embeddings[3], #[model] <END>
embeddings[4] #model [<END>]
], dtype=np.float64)
labels = np.array([
[0,1,0,0,0], #[<START>] I
[1,0,0,0,0], # for word I, before is "<START>"
[0,0,1,0,0], # for word I, after is "make"
[0,1,0,0,0], # for word make, before is I
[0,0,0,1,0], #...
[0,0,1,0,0],
[0,0,0,0,1], #
[0,0,0,1,0], #model [<END>]
], dtype=np.float64)
return x.T, labels.T
def gradient_descent(alpha=0.01):
emb, W1 = initialize_model()
for _ in range(0, 1000): #1000 epochs
x,y = get_batch(emb) #batch is always the same - all input samples
o = forward_prop(x,W1)
cost = compute_cost(y,o)
print(f"cost is {cost}")
grad_emb, grad_W1 = back_prop(x,y,o,W1)
grad_embT = grad_emb.T
#since batch input has same word many times (eg make->I, make->model)
#we need to sum errors for same word input, but different outputs
grad_embT = np.array([
grad_embT[0],
grad_embT[1]+grad_embT[2],
grad_embT[3]+grad_embT[4], # make->I, make->model
grad_embT[5]+grad_embT[6],
grad_embT[7],
], dtype=np.float64)
emb = emb - alpha*grad_embT
W1 = W1 - alpha*grad_W1
return emb,W1
emb,W1 = gradient_descent()
embT,_ = get_batch(emb)
sad = np.array([
embT.T[1], # I
embT.T[3] # make
], dtype=np.float64).T
rezz = forward_prop(sad, W1)
print(rezz.T)
#here we get
# [[4.99451986e-01 4.95151586e-04 4.94055631e-01 1.66231881e-04
# 5.83099999e-03]
# [1.68018351e-06 4.99527827e-01 8.52740862e-04 4.97696671e-01
# 1.92108123e-03]]
# so for "I", 0.5 probs that it is "<START>" and 0.5 prob that it is "make"