看完cs224有段时间了,回头做下作业总结下。
官网:http://web.stanford.edu/class/cs224n/
里面有相关的资料可以下载。
1 Softmax (10 points)
一个证明和一个实现:
我的实现:
np.exp(x) / np.sum(np.exp(x))
github上:
git上答案:
def softmax(x):
"""
Arguments:
x -- A N dimensional vector or M x N dimensional numpy matrix.
Return:
x -- You are allowed to modify x in-place
"""
orig_shape = x.shape
if len(x.shape) > 1:
# Matrix
### YOUR CODE HERE
exp_minmax = lambda x: np.exp(x - np.max(x))
denom = lambda x: 1.0 / np.sum(x)
x = np.apply_along_axis(exp_minmax, 1, x)
denominator = np.apply_along_axis(denom, 1, x)
if len(denominator.shape) == 1:
denominator = denominator.reshape((denominator.shape[0], 1))
x = x * denominator
### END YOUR CODE
else:
# Vector
### YOUR CODE HERE
x_max = np.max(x)
x = x - x_max
numerator = np.exp(x)
denominator = 1.0 / np.sum(numerator)
x = numerator.dot(denominator)
### END YOUR CODE
assert x.shape == orig_shape
return x
看了下这个帖子:https://www.aliyun.com/jiaocheng/524269.html,大致明白了。
apply_along_axis是执行一个函数,第一个参数是其函数,0/1对应行列,x即输入数据。
2 Neural Network Basics (30 points)
part a: 对sigmod函数求导
原函数:
求导:
part b: 求出softmax函数的交叉熵损失关于theta的导数。
softmax:
交叉熵:
求导:
这块标记下,面试必考,手推导。贴个链接:
https://blog.csdn.net/qian99/article/details/78046329
我也要手推导下去。
part c:单隐层神经网络关于参数的导数。
这块比较难了,重要的是一种思想,即前向过程的计算值,作为后向计算的中间值。建议看solid,吴恩达的视频这块将的非常好,一步一步推导的。包括矩阵形式的计算。
我记得一开始是看的国外一个知名的博客,Implementing a Neural Network from Scratch in Python – An Introduction。
https://blog.csdn.net/luoganttcc/article/details/63251234这个推导很详细。
part d:计算神经网络的参数个数
这里的1是偏执项。
part e: 完成sigmod激活函数以及其梯度:
sigmod function :
return s = 1.0 / (1 + np.exp(-x))
sigmod gradient:
return s * (1 - s)
part f:完成一个梯度检查器:
# First implement a gradient checker by filling in the following functions
def gradcheck_naive(f, x):
""" Gradient check for a function f.
Arguments:
f -- a function that takes a single argument and outputs the
cost and its gradients
x -- the point (numpy array) to check the gradient at
"""
rndstate = random.getstate()
random.setstate(rndstate)
fx, grad = f(x) # Evaluate function value at original point
h = 1e-4 # Do not change this!
# Iterate over all indexes in x
it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
ix = it.multi_index
# Try modifying x[ix] with h defined above to compute
# numerical gradients. Make sure you call random.setstate(rndstate)
# before calling f(x) each time. This will make it possible
# to test cost functions with built in randomness later.
### YOUR CODE HERE:
x[ix] += h
random.setstate(rndstate)
new_f1 = f(x)[0]
x[ix] -= 2*h
random.setstate(rndstate)
new_f2 = f(x)[0]
x[ix] += h
numgrad = (new_f1 - new_f2) / (2 * h)
### END YOUR CODE
# Compare gradients
reldiff = abs(numgrad - grad[ix]) / max(1, abs(numgrad), abs(grad[ix]))
if reldiff > 1e-5:
print("Gradient check failed.")
print("First gradient error found at index %s" % str(ix))
print("Your gradient: %f \t Numerical gradient: %f" % (grad[ix], numgrad))
return
it.iternext() # Step to next dimension
print("Gradient check passed!")
即(f(x+h)-f(x-h))/2h来计算梯度,然后与计算的梯度值进行对比。
np.nditer():numpy迭代器
part g: 写出只有一个隐层且激活函数为sigmoid的神经网络前向和后向传播代码
def forward_backward_prop(data, labels, params, dimensions):
"""
Forward and backward propagation for a two-layer sigmoidal network
Compute the forward propagation and for the cross entropy cost,
and backward propagation for the gradients for all parameters.
Arguments:
data -- M x Dx matrix, where each row is a training example.
labels -- M x Dy matrix, where each row is a one-hot vector.
params -- Model parameters, these are unpacked for you.
dimensions -- A tuple of input dimension, number of hidden units
and output dimension
"""
### Unpack network parameters (do not modify)
ofs = 0
Dx, H, Dy = (dimensions[0], dimensions[1], dimensions[2])
W1 = np.reshape(params[ofs:ofs+ Dx * H], (Dx, H))
ofs += Dx * H
b1 = np.reshape(params[ofs:ofs + H], (1, H))
ofs += H
W2 = np.reshape(params[ofs:ofs + H * Dy], (H, Dy))
ofs += H * Dy
b2 = np.reshape(params[ofs:ofs + Dy], (1, Dy))
### YOUR CODE HERE: forward propagation
h = sigmoid(np.dot(data,W1) + b1)
yhat = softmax(np.dot(h,W2) + b2)
### END YOUR CODE
### YOUR CODE HERE: backward propagation
cost = np.sum(-np.log(yhat[labels==1])) / data.shape[0]
d3 = (yhat - labels) / data.shape[0]
gradW2 = np.dot(h.T, d3)
gradb2 = np.sum(d3,0,keepdims=True)
dh = np.dot(d3,W2.T)
grad_h = sigmoid_grad(h) * dh
gradW1 = np.dot(data.T,grad_h)
gradb1 = np.sum(grad_h,0)
### END YOUR CODE
### Stack gradients (do not modify)
grad = np.concatenate((gradW1.flatten(), gradb1.flatten(),
gradW2.flatten(), gradb2.flatten()))
return cost, grad
3 word2vec (40 points + 2 bonus)
part a: 求解损失函数对Uc的梯度值。
softmax在word2vec中的函数为:
该word2vec使用skip-gram模型,即由中心词预测上下文词,vc为中心词的词向量,uo为上下文词的词向量。uw为输出词向量中每个单词对应的词向量。
损失函数为:
其中U = [u1,u2,…, uW]是所有词向量构成的矩阵。
其梯度为:
part b: 计算损失函数对输出词向量的梯度。
part c: 使用为预测的向量υc使用负采样损失的计算方式,并且设定期望输出词为o。假设获得了K个负样例(词),并且被记为1,…,K,分别作为这些样例的标签(o∉1,…,K)。那么,对于一个给定的词o,将其输出向量记作μo。这里,负采样损失函数如下:
分别计算其梯度值:
这里是重点,代码中比较难理解的部分,即negSamplingCostAndGradient()函数的实现。负采样的速度是非常快的,每次计算是只有k个值。
part d:计算所有梯度
part e: 完成q3 word2vec.py
#!/usr/bin/env python
import numpy as np
import random
from q1_softmax import softmax
from q2_gradcheck import gradcheck_naive
from q2_sigmoid import sigmoid, sigmoid_grad
def normalizeRows(x):
""" Row normalization function
Implement a function that normalizes each row of a matrix to have
unit length.
"""
### YOUR CODE HERE
denom = np.apply_along_axis(lambda x: np.sqrt(x.T.dot(x)), 1, x)
x /= denom[:, None]
### END YOUR CODE
return x
def test_normalize_rows():
print("Testing normalizeRows...")
x = normalizeRows(np.array([[3.0, 4.0], [1, 2]]))
print(x)
ans = np.array([[0.6, 0.8], [0.4472136, 0.89442719]])
assert np.allclose(x, ans, rtol=1e-05, atol=1e-06)
print("")
def softmaxCostAndGradient(predicted, target, outputVectors, dataset):
""" Softmax cost function for word2vec models
Implement the cost and gradients for one predicted word vector
and one target word vector as a building block for word2vec
models, assuming the softmax prediction function and cross
entropy loss.
Arguments:
predicted -- numpy ndarray, predicted word vector (\hat{v} in
the written component)
target -- integer, the index of the target word
outputVectors -- "output" vectors (as rows) for all tokens
dataset -- needed for negative sampling, unused here.
Return:
cost -- cross entropy cost for the softmax word prediction
gradPred -- the gradient with respect to the predicted word
vector
grad -- the gradient with respect to all the other word
vectors
We will not provide starter code for this function, but feel
free to reference the code you previously wrote for this
assignment!
"""
### YOUR CODE HERE
## Gradient for $\hat{\bm{v}}$:
# Calculate the predictions:
vhat = predicted
z = np.dot(outputVectors, vhat)
preds = softmax(z)
# Calculate the cost:
cost = -np.log(preds[target])
# Gradients
z = preds.copy()
z[target] -= 1.0
grad = np.outer(z, vhat)
gradPred = np.dot(outputVectors.T, z)
### END YOUR CODE
return cost, gradPred, grad
def getNegativeSamples(target, dataset, K):
""" Samples K indexes which are not the target """
indices = [None] * K
for k in range(K):
newidx = dataset.sampleTokenIdx()
while newidx == target:
newidx = dataset.sampleTokenIdx()
indices[k] = newidx
return indices
def negSamplingCostAndGradient(predicted, target, outputVectors, dataset,
K=10):
""" Negative sampling cost function for word2vec models
Implement the cost and gradients for one predicted word vector
and one target word vector as a building block for word2vec
models, using the negative sampling technique. K is the sample
size.
Note: See test_word2vec below for dataset's initialization.
Arguments/Return Specifications: same as softmaxCostAndGradient
"""
# Sampling of indices is done for you. Do not modify this if you
# wish to match the autograder and receive points!
indices = [target]
indices.extend(getNegativeSamples(target, dataset, K))
### YOUR CODE HERE
grad = np.zeros(outputVectors.shape)
gradPred = np.zeros(predicted.shape)
cost = 0
z = sigmoid(np.dot(outputVectors[target], predicted))
cost -= np.log(z)
grad[target] += predicted * (z - 1.0)
gradPred += outputVectors[target] * (z - 1.0)
for k in range(K):
samp = indices[k + 1]
z = sigmoid(np.dot(outputVectors[samp], predicted))
cost -= np.log(1.0 - z)
grad[samp] += predicted * z
gradPred += outputVectors[samp] * z
### END YOUR CODE
return cost, gradPred, grad
def skipgram(currentWord, C, contextWords, tokens, inputVectors, outputVectors,
dataset, word2vecCostAndGradient=softmaxCostAndGradient):
""" Skip-gram model in word2vec
Implement the skip-gram model in this function.
Arguments:
currrentWord -- a string of the current center word
C -- integer, context size
contextWords -- list of no more than 2*C strings, the context words
tokens -- a dictionary that maps words to their indices in
the word vector list
inputVectors -- "input" word vectors (as rows) for all tokens
outputVectors -- "output" word vectors (as rows) for all tokens
word2vecCostAndGradient -- the cost and gradient function for
a prediction vector given the target
word vectors, could be one of the two
cost functions you implemented above.
Return:
cost -- the cost function value for the skip-gram model
grad -- the gradient with respect to the word vectors
"""
cost = 0.0
gradIn = np.zeros(inputVectors.shape)
gradOut = np.zeros(outputVectors.shape)
### YOUR CODE HERE
cword_idx = tokens[currentWord]
vhat = inputVectors[cword_idx]
for j in contextWords:
u_idx = tokens[j]
c_cost, c_grad_in, c_grad_out = word2vecCostAndGradient(vhat, u_idx, outputVectors, dataset)
cost += c_cost
gradIn[cword_idx] += c_grad_in
gradOut += c_grad_out
### END YOUR CODE
return cost, gradIn, gradOut
def cbow(currentWord, C, contextWords, tokens, inputVectors, outputVectors,
dataset, word2vecCostAndGradient=softmaxCostAndGradient):
"""CBOW model in word2vec
Implement the continuous bag-of-words model in this function.
Arguments/Return specifications: same as the skip-gram model
Extra credit: Implementing CBOW is optional, but the gradient
derivations are not. If you decide not to implement CBOW, remove
the NotImplementedError.
"""
cost = 0.0
gradIn = np.zeros(inputVectors.shape)
gradOut = np.zeros(outputVectors.shape)
### YOUR CODE HERE
predicted_indices = [tokens[word] for word in contextWords]
predicted_vectors = inputVectors[predicted_indices]
predicted = np.sum(predicted_vectors, axis=0)
target = tokens[currentWord]
cost, gradIn_predicted, gradOut = word2vecCostAndGradient(predicted, target, outputVectors, dataset)
for i in predicted_indices:
gradIn[i] += gradIn_predicted
### END YOUR CODE
return cost, gradIn, gradOut
#############################################
# Testing functions below. DO NOT MODIFY! #
#############################################
def word2vec_sgd_wrapper(word2vecModel, tokens, wordVectors, dataset, C,
word2vecCostAndGradient=softmaxCostAndGradient):
batchsize = 50
cost = 0.0
grad = np.zeros(wordVectors.shape)
N = wordVectors.shape[0]
inputVectors = wordVectors[:N // 2, :]
outputVectors = wordVectors[N // 2:, :]
for i in range(batchsize):
C1 = random.randint(1, C)
centerword, context = dataset.getRandomContext(C1)
if word2vecModel == skipgram:
denom = 1
else:
denom = 1
c, gin, gout = word2vecModel(
centerword, C1, context, tokens, inputVectors, outputVectors,
dataset, word2vecCostAndGradient)
cost += c / batchsize / denom
grad[:N // 2, :] += gin / batchsize / denom
grad[N // 2:, :] += gout / batchsize / denom
return cost, grad
def test_word2vec():
""" Interface to the dataset for negative sampling """
dataset = type('dummy', (), {})()
def dummySampleTokenIdx():
return random.randint(0, 4)
def getRandomContext(C):
tokens = ["a", "b", "c", "d", "e"]
return tokens[random.randint(0, 4)], \
[tokens[random.randint(0, 4)] for i in range(2 * C)]
dataset.sampleTokenIdx = dummySampleTokenIdx
dataset.getRandomContext = getRandomContext
random.seed(31415)
np.random.seed(9265)
dummy_vectors = normalizeRows(np.random.randn(10, 3))
dummy_tokens = dict([("a", 0), ("b", 1), ("c", 2), ("d", 3), ("e", 4)])
print("==== Gradient check for skip-gram ====")
gradcheck_naive(lambda vec: word2vec_sgd_wrapper(
skipgram, dummy_tokens, vec, dataset, 5, softmaxCostAndGradient),
dummy_vectors)
gradcheck_naive(lambda vec: word2vec_sgd_wrapper(
skipgram, dummy_tokens, vec, dataset, 5, negSamplingCostAndGradient),
dummy_vectors)
print("\n==== Gradient check for CBOW ====")
gradcheck_naive(lambda vec: word2vec_sgd_wrapper(
cbow, dummy_tokens, vec, dataset, 5, softmaxCostAndGradient),
dummy_vectors)
gradcheck_naive(lambda vec: word2vec_sgd_wrapper(
cbow, dummy_tokens, vec, dataset, 5, negSamplingCostAndGradient),
dummy_vectors)
print("\n=== Results ===")
print(skipgram("c", 3, ["a", "b", "e", "d", "b", "c"],
dummy_tokens, dummy_vectors[:5, :], dummy_vectors[5:, :], dataset))
print(skipgram("c", 1, ["a", "b"],
dummy_tokens, dummy_vectors[:5, :], dummy_vectors[5:, :], dataset,
negSamplingCostAndGradient))
print(cbow("a", 2, ["a", "b", "c", "a"],
dummy_tokens, dummy_vectors[:5, :], dummy_vectors[5:, :], dataset))
print(cbow("a", 2, ["a", "b", "a", "c"],
dummy_tokens, dummy_vectors[:5, :], dummy_vectors[5:, :], dataset,
negSamplingCostAndGradient))
if __name__ == "__main__":
test_normalize_rows()
#test_word2vec()
part f: 完成q3 sgd.py。
#!/usr/bin/env python
# Save parameters every a few SGD iterations as fail-safe
SAVE_PARAMS_EVERY = 5000
import glob
import random
import numpy as np
import os.path as op
import pickle as pickle
def load_saved_params():
"""
A helper function that loads previously saved parameters and resets
iteration start.
"""
st = 0
for f in glob.glob("saved_params_*.npy"):
iter = int(op.splitext(op.basename(f))[0].split("_")[2])
if (iter > st):
st = iter
if st > 0:
with open("saved_params_%d.npy" % st, "rb") as f:
params = pickle.load(f)
state = pickle.load(f)
return st, params, state
else:
return st, None, None
def save_params(iter, params):
with open("saved_params_%d.npy" % iter, "wb+", ) as f:
pickle.dump(params, f)
pickle.dump(random.getstate(), f)
def sgd(f, x0, step, iterations, postprocessing=None, useSaved=False,
PRINT_EVERY=10):
""" Stochastic Gradient Descent
Implement the stochastic gradient descent method in this function.
Arguments:
f -- the function to optimize, it should take a single
argument and yield two outputs, a cost and the gradient
with respect to the arguments
x0 -- the initial point to start SGD from
step -- the step size for SGD
iterations -- total iterations to run SGD for
postprocessing -- postprocessing function for the parameters
if necessary. In the case of word2vec we will need to
normalize the word vectors to have unit length.
PRINT_EVERY -- specifies how many iterations to output loss
Return:
x -- the parameter value after SGD finishes
"""
# Anneal learning rate every several iterations
ANNEAL_EVERY = 20000
if useSaved:
start_iter, oldx, state = load_saved_params()
if start_iter > 0:
x0 = oldx
step *= 0.5 ** (start_iter / ANNEAL_EVERY)
if state:
random.setstate(state)
else:
start_iter = 0
x = x0
if not postprocessing:
postprocessing = lambda x: x
expcost = None
for iter in range(start_iter + 1, iterations + 1):
# Don't forget to apply the postprocessing after every iteration!
# You might want to print the progress every few iterations.
cost = None
### YOUR CODE HERE
cost, grad = f(x)
x -= step * grad
postprocessing(x)
### END YOUR CODE
if iter % PRINT_EVERY == 0:
if not expcost:
expcost = cost
else:
expcost = .95 * expcost + .05 * cost
print("iter %d: %f" % (iter, expcost))
if iter % SAVE_PARAMS_EVERY == 0 and useSaved:
save_params(iter, x)
if iter % ANNEAL_EVERY == 0:
step *= 0.5
return x
def sanity_check():
quad = lambda x: (np.sum(x ** 2), x * 2)
print("Running sanity checks...")
t1 = sgd(quad, 0.5, 0.01, 1000, PRINT_EVERY=100)
print("test 1 result:", t1)
assert abs(t1) <= 1e-6
t2 = sgd(quad, 0.0, 0.01, 1000, PRINT_EVERY=100)
print("test 2 result:", t2)
assert abs(t2) <= 1e-6
t3 = sgd(quad, -1.5, 0.01, 1000, PRINT_EVERY=100)
print("test 3 result:", t3)
assert abs(t3) <= 1e-6
print("")
def your_sanity_checks():
"""
Use this space add any additional sanity checks by running:
python q3_sgd.py
This function will not be called by the autograder, nor will
your additional tests be graded.
"""
print("Running your sanity checks...")
### YOUR CODE HERE
# raise NotImplementedError
### END YOUR CODE
if __name__ == "__main__":
sanity_check()
#your_sanity_checks()
print("------------------------->it's over!!")
part g:完成q3 run.py。
#!/usr/bin/env python
import random
import numpy as np
from utils.treebank import StanfordSentiment
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import time
from q3_word2vec import *
from q3_sgd import *
# Reset the random seed to make sure that everyone gets the same results
random.seed(314)
dataset = StanfordSentiment()
tokens = dataset.tokens()
nWords = len(tokens)
# We are going to train 10-dimensional vectors for this assignment
dimVectors = 10
# Context size
C = 5
# Reset the random seed to make sure that everyone gets the same results
random.seed(31415)
np.random.seed(9265)
startTime=time.time()
wordVectors = np.concatenate(
((np.random.rand(nWords, dimVectors) - 0.5) /
dimVectors, np.zeros((nWords, dimVectors))),
axis=0)
wordVectors = sgd(
lambda vec: word2vec_sgd_wrapper(skipgram, tokens, vec, dataset, C,
negSamplingCostAndGradient),
wordVectors, 0.3, 100000, None, True, PRINT_EVERY=100)
# Note that normalization is not called here. This is not a bug,
# normalizing during training loses the notion of length.
print("sanity check: cost at convergence should be around or below 10")
print("training took %d seconds" % (time.time() - startTime))
# concatenate the input and output word vectors
wordVectors = np.concatenate(
(wordVectors[:nWords,:], wordVectors[nWords:,:]),
axis=0)
# wordVectors = wordVectors[:nWords,:] + wordVectors[nWords:,:]
visualizeWords = [
"the", "a", "an", ",", ".", "?", "!", "``", "''", "--",
"good", "great", "cool", "brilliant", "wonderful", "well", "amazing",
"worth", "sweet", "enjoyable", "boring", "bad", "waste", "dumb",
"annoying"]
visualizeIdx = [tokens[word] for word in visualizeWords]
visualizeVecs = wordVectors[visualizeIdx, :]
temp = (visualizeVecs - np.mean(visualizeVecs, axis=0))
covariance = 1.0 / len(visualizeIdx) * temp.T.dot(temp)
U,S,V = np.linalg.svd(covariance)
coord = temp.dot(U[:,0:2])
for i in range(len(visualizeWords)):
plt.text(coord[i,0], coord[i,1], visualizeWords[i],
bbox=dict(facecolor='green', alpha=0.1))
plt.xlim((np.min(coord[:,0]), np.max(coord[:,0])))
plt.ylim((np.min(coord[:,1]), np.max(coord[:,1])))
plt.savefig('q3_word_vectors.png')
part h:完成cbow模型。
整个代码理解起来还好,关键是理解skip-gram模型。看完这篇文章大致界理解了。
http://mccormickml.com/2016/04/19/word2vec-tutorial-the-skip-gram-model/
有些文档中将该模型分为3层,有些4层,有点糊涂。
代码中可以理解为4层,输入层、input hide layer、output hide layer、输出层。
注意这块:
wordVectors = np.concatenate(
(wordVectors[:nWords,:], wordVectors[nWords:,:]),
axis=0)
将word的数量作为V-dim,词向量的维度为10,即D-dim。这里是拼接。
梯度计算那部分对应的就是part c,不过需要自己分析下,将一部分值替换,分别对应c、o、w。然后就会发现就是part c中的公式。
sigmoid(-x) = 1 – sigmoid(x)。这里需要推导下,替换入公式内。
负采样的代码在提供的接口里。原理搞懂了,不过这个代码还是没理解,真是惭愧。
拮下来试了下是这样的:
import numpy as np
vec_dict = {0:10, 1:1, 2:5, 3:1, 4:1, 5:20, 6:3, 7:100, 8:6, 9:9, 10:2, 11:1, 12:2, 13:2, 14:15, 15:7, 16:8, 17:1, 18:34, 19:3}
samplingFreq = np.zeros((len(vec_dict),))
for i in range(len(vec_dict)):
if i in vec_dict.keys():
freq = vec_dict[i]
freq = freq ** 0.75
samplingFreq[i] = freq
samplingFreq /= np.sum(samplingFreq)
#print(samplingFreq)
samplingFreq = np.cumsum(samplingFreq) * 15
re_table = [0] * 15
j = 0
for i in range(15):
while i > samplingFreq[j]:
j += 1
re_table[i] = j
print(re_table)
[0, 2, 5, 5, 7, 7, 7, 7, 8, 9, 13, 14, 16, 18, 18]
没关系,这个就是负采样的baseline了。
参考:
git@github.com:zzb5233/CS224n.git
https://blog.csdn.net/han_xiaoyang/article/details/51760923
https://blog.csdn.net/u012416045/article/details/78237060
https://www.aliyun.com/jiaocheng/524269.html