鑷劧璇█澶勭悊锛氭枃鏈澶勭悊銆佽瑷€妯″瀷銆丷NN
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鑷劧璇█澶勭悊锛氭枃鏈澶勭悊銆佽瑷€妯″瀷銆丷NN相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/batch' title='batch'>batch
鎵归噺 浣嶇疆 shape red lin char etc 淇濈暀鏂囨湰鏄竴绫诲簭鍒楁暟鎹紝涓€绡囨枃绔犲彲浠ョ湅浣滄槸瀛楃鎴栧崟璇嶇殑搴忓垪锛屾湰鑺傚皢浠嬬粛鏂囨湰鏁版嵁鐨勫父瑙侀澶勭悊姝ラ锛岄澶勭悊閫氬父鍖呮嫭鍥涗釜姝ラ锛?/p>
- 璇诲叆鏂囨湰
- 鍒嗚瘝
- 寤虹珛瀛楀吀锛屽皢姣忎釜璇嶆槧灏勫埌涓€涓敮涓€鐨勭储寮曪紙index锛?/li>
- 灏嗘枃鏈粠璇嶇殑搴忓垪杞崲涓虹储寮曠殑搴忓垪锛屾柟渚胯緭鍏ユā鍨?/li>
鐜版湁鐨勫伐鍏峰彲浠ュ緢濂藉湴杩涜鍒嗚瘝锛?a href="https://spacy.io/" target="_blank">spaCy鍜?a href="https://www.nltk.org/" target="_blank">NLTK銆?/p>
浣跨敤绀轰緥锛?/p>
text = "Mr. Chen doesn鈥榯 agree with my suggestion." ####spaCy: import spacy nlp = spacy.load(鈥?/span>en_core_web_sm鈥?/span>) doc = nlp(text) print([token.text for token in doc]) [鈥?/span>Mr.鈥?/span>, 鈥?/span>Chen鈥?/span>, 鈥?/span>does鈥?/span>, "n鈥榯", 鈥?/span>agree鈥?/span>, 鈥?/span>with鈥?/span>, 鈥?/span>my鈥?/span>, 鈥?/span>suggestion鈥?/span>, 鈥?/span>.鈥?/span>] ####NLTK: from nltk.tokenize import word_tokenize from nltk import data data.path.append(鈥?/span>/home/kesci/input/nltk_data3784/nltk_data鈥?/span>) print(word_tokenize(text)) [鈥?/span>Mr.鈥?/span>, 鈥?/span>Chen鈥?/span>, 鈥?/span>does鈥?/span>, "n鈥榯", 鈥?/span>agree鈥?/span>, 鈥?/span>with鈥?/span>, 鈥?/span>my鈥?/span>, 鈥?/span>suggestion鈥?/span>, 鈥?/span>.鈥?/span>]
璇█妯″瀷
涓€娈佃嚜鐒惰瑷€鏂囨湰鍙互鐪嬩綔鏄竴涓鏁f椂闂村簭鍒楋紝缁欏畾涓€涓暱搴︿负TT鐨勮瘝鐨勫簭鍒?span class="MathJax_Preview">w1,w2,…,wTw1,w2,…,wT锛岃瑷€妯″瀷鐨勭洰鏍囧氨鏄瘎浼拌搴忓垪鏄惁鍚堢悊锛屽嵆璁$畻璇ュ簭鍒楃殑姒傜巼锛?/span>
闅忔満閲囨牱
鍦ㄩ殢鏈洪噰鏍蜂腑锛屾瘡涓牱鏈槸鍘熷搴忓垪涓婁换鎰忔埅鍙栫殑涓€娈靛簭鍒楋紝鐩搁偦鐨勪袱涓殢鏈哄皬鎵归噺鍦ㄥ師濮嬪簭鍒椾笂鐨勪綅缃笉涓€瀹氱浉姣楅偦銆?/p>
import torch import random def data_iter_random(corpus_indices, batch_size, num_steps, device=None): # 鍑?鏄洜涓哄浜庨暱搴︿负n鐨勫簭鍒楋紝X鏈€澶氬彧鏈夊寘鍚叾涓殑鍓峮 - 1涓瓧绗?/span> num_examples = (len(corpus_indices) - 1) // num_steps # 涓嬪彇鏁达紝寰楀埌涓嶉噸鍙犳儏鍐典笅鐨勬牱鏈釜鏁?/span> example_indices = [i * num_steps for i in range(num_examples)] # 姣忎釜鏍锋湰鐨勭涓€涓瓧绗﹀湪corpus_indices涓殑涓嬫爣 random.shuffle(example_indices) def _data(i): # 杩斿洖浠巌寮€濮嬬殑闀夸负num_steps鐨勫簭鍒?/span> return corpus_indices[i: i + num_steps] if device is None: device = torch.device(鈥?/span>cuda鈥?/span> if torch.cuda.is_available() else 鈥?/span>cpu鈥?/span>) for i in range(0, num_examples, batch_size): # 姣忔閫夊嚭batch_size涓殢鏈烘牱鏈?/span> batch_indices = example_indices[i: i + batch_size] # 褰撳墠batch鐨勫悇涓牱鏈殑棣栧瓧绗︾殑涓嬫爣 X = [_data(j) for j in batch_indices] Y = [_data(j + 1) for j in batch_indices] yield torch.tensor(X, device=device), torch.tensor(Y, device=device)
鐩搁偦閲囨牱
鍦ㄧ浉閭婚噰鏍蜂腑锛岀浉閭荤殑涓や釜闅忔満灏忔壒閲忓湪鍘熷搴忓垪涓婄殑浣嶇疆鐩告瘲閭汇€?/p>
def data_iter_consecutive(corpus_indices, batch_size, num_steps, device=None): if device is None: device = torch.device(鈥?/span>cuda鈥?/span> if torch.cuda.is_available() else 鈥?/span>cpu鈥?/span>) corpus_len = len(corpus_indices) // batch_size * batch_size # 淇濈暀涓嬫潵鐨勫簭鍒楃殑闀垮害 corpus_indices = corpus_indices[: corpus_len] # 浠呬繚鐣欏墠corpus_len涓瓧绗?/span> indices = torch.tensor(corpus_indices, device=device) indices = indices.view(batch_size, -1) # resize鎴?batch_size, ) batch_num = (indices.shape[1] - 1) // num_steps for i in range(batch_num): i = i * num_steps X = indices[:, i: i + num_steps] Y = indices[:, i + 1: i + num_steps + 1] yield X, Y
寰幆绁炵粡缃戠粶寮曞叆涓€涓殣钘忓彉閲?span class="MathJax_Preview">HH锛岀敤HtHt琛ㄧずHH鍦ㄦ椂闂存tt鐨勫€笺€?span class="MathJax_Preview">HtHt鐨勮绠楀熀浜?span class="MathJax_Preview">XtXt鍜?span class="MathJax_Preview">Ht−1Ht−1锛屽彲浠ヨ涓?span class="MathJax_Preview">HtHt璁板綍浜嗗埌褰撳墠瀛楃涓烘鐨勫簭鍒椾俊鎭紝鍒╃敤HtHt瀵瑰簭鍒楃殑涓嬩竴涓瓧绗﹁繘琛岄娴嬨€?/span>
def rnn(inputs, state, params):
# inputs鍜宱utputs鐨嗕负num_steps涓舰鐘朵负(batch_size, vocab_size)鐨勭煩闃?/span>
W_xh, W_hh, b_h, W_hq, b_q = params
H, = state
outputs = []
for X in inputs:
H = torch.tanh(torch.matmul(X, W_xh) + torch.matmul(H, W_hh) + b_h)
Y = torch.matmul(H, W_hq) + b_q
outputs.append(Y)
return outputs, (H,)
瑙e喅寰幆绁炵粡缃戠粶姊害鐖嗙偢闂锛氭搴﹁鍓?/p>
瑁佸壀姊害锛坈lip gradient锛夋槸涓€绉嶅簲瀵规搴︾垎鐐哥殑鏂规硶銆傚亣璁炬垜浠妸鎵€鏈夋ā鍨嬪弬鏁扮殑姊害鎷兼帴鎴愪竴涓悜閲?nbsp;g锛屽苟璁捐鍓殑闃堝€兼槸θ銆傝鍓悗鐨勬搴?/span>
瑁佸壀姊害浠g爜锛?/p>
def grad_clipping(params, theta, device): norm = torch.tensor([0.0], device=device) for param in params: norm += (param.grad.data ** 2).sum() norm = norm.sqrt().item() if norm > theta: for param in params: param.grad.data *= (theta / norm)
浣跨敤RNN妯″瀷杩涜棰勬祴锛?/p>
def predict_rnn(prefix, num_chars, rnn, params, init_rnn_state, num_hiddens, vocab_size, device, idx_to_char, char_to_idx): state = init_rnn_state(1, num_hiddens, device) output = [char_to_idx[prefix[0]]] # output璁板綍prefix鍔犱笂棰勬祴鐨刵um_chars涓瓧绗?/span> for t in range(num_chars + len(prefix) - 1): # 灏嗕笂涓€鏃堕棿姝ョ殑杈撳嚭浣滀负褰撳墠鏃堕棿姝ョ殑杈撳叆 X = to_onehot(torch.tensor([[output[-1]]], device=device), vocab_size) # 璁$畻杈撳嚭鍜屾洿鏂伴殣钘忕姸鎬?/span> (Y, state) = rnn(X, state, params) # 涓嬩竴涓椂闂存鐨勮緭鍏ユ槸prefix閲岀殑瀛楃鎴栬€呭綋鍓嶇殑鏈€浣抽娴嬪瓧绗?/span> if t < len(prefix) - 1: output.append(char_to_idx[prefix[t + 1]]) else: output.append(Y[0].argmax(dim=1).item()) return 鈥樷€?/span>.join([idx_to_char[i] for i in output])