平面设计是什么专业学的,西安优化seo托管,网站模块添加,asp.net网站连接mysql初始化#xff1a;初始化方法的参数包括词汇个数 vocab_size 和中间层的神经元个数 hidden_size。首先生成两个权重#xff08;W_in 和 W_out#xff09;#xff0c;并用一些小的随机值初始化这两个权重。设置astype(‘f’)#xff0c;初始化将使用 32 位的浮点数。
生成…
初始化初始化方法的参数包括词汇个数 vocab_size 和中间层的神经元个数 hidden_size。首先生成两个权重W_in 和 W_out并用一些小的随机值初始化这两个权重。设置astype(‘f’)初始化将使用 32 位的浮点数。
生成层生成两个输入侧的 MatMul 层、一个输出侧的 MatMul 层以及一个 Softmax with Loss 层。
保存权重和梯度将该神经网络中使用的权重参数和梯度分别保存在列表类型的成员变量 params 和 grads 中。
正向传播 forward() 函数该函数接收参数 contexts 和 target并返回损失loss。这两个参数结构如下。 contexts 是一个三维 NumPy 数组第 0 维的元素个数是 mini-batch 的数量第 1 维的元素个数是上下文的窗口大小第 2 维表示 one-hot 向量。下面这个代码取出来的是什么 h0 self.in_layer0.forward(contexts[:, 0])h1 self.in_layer1.forward(contexts[:, 1])jym做了一个测试
import sys
sys.path.append(..)
from common.util import preprocess #, create_co_matrix, most_similar
from common.util import create_contexts_target, convert_one_hottext You say goodbye and I say hello.
corpus, word_to_id, id_to_word preprocess(text)
contexts, target create_contexts_target(corpus, window_size1)
#print(contexts)
#print(target)
vocab_size len(word_to_id)
target convert_one_hot(target, vocab_size)
contexts convert_one_hot(contexts, vocab_size)
print(contexts[:, 0])输出然后从输出就知道了取的是不同target的左边的单词。
[[1 0 0 0 0 0 0][0 1 0 0 0 0 0][0 0 1 0 0 0 0][0 0 0 1 0 0 0][0 0 0 0 1 0 0][0 1 0 0 0 0 0]]反向传播 backward()神经网络的反向传播在与正向传播相反的方向上传播梯度。这个反向传播从 1 出发并将其传向 Softmax with Loss 层。然后将 Softmax with Loss 层的反向传播的输出 ds 传到输出侧的 MatMul 层。“×”的反向传播将正向传播时的输入值“交换”后乘以梯度。“”的反向传播将梯度“原样”传播。 这个backward函数里面调用的是之前写好的层的反向传播函数比如loss_layer.backward(dout)因此backward函数用完之后各个权重参数的梯度就保存在了成员变量 grads 中(这是之前写的层里面的反向传播函数来实现的)。先调用 forward() 函数再调用 backward() 函数grads 列表中的梯度被更新。
import sys
sys.path.append(..)
import numpy as np
from common.layers import MatMul, SoftmaxWithLossclass SimpleCBOW:def __init__(self, vocab_size, hidden_size):V, H vocab_size, hidden_size# 初始化权重W_in 0.01 * np.random.randn(V, H).astype(f)W_out 0.01 * np.random.randn(H, V).astype(f)# 生成层self.in_layer0 MatMul(W_in)self.in_layer1 MatMul(W_in)self.out_layer MatMul(W_out)self.loss_layer SoftmaxWithLoss()# 将所有的权重和梯度整理到列表中layers [self.in_layer0, self.in_layer1, self.out_layer]self.params, self.grads [], []for layer in layers:self.params layer.paramsself.grads layer.grads# 将单词的分布式表示设置为成员变量self.word_vecs W_indef forward(self, contexts, target):h0 self.in_layer0.forward(contexts[:, 0])h1 self.in_layer1.forward(contexts[:, 1])h (h0 h1) * 0.5score self.out_layer.forward(h)loss self.loss_layer.forward(score, target)return lossdef backward(self, dout1):ds self.loss_layer.backward(dout)da self.out_layer.backward(ds)da * 0.5self.in_layer1.backward(da)self.in_layer0.backward(da)return None