pytorch中nn.Embedding理解
給輸入的詞建立詞向量
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
word={'hello':0,'world':1}
print(word)
embeds = nn.Embedding(2, 6)
hello_idx = torch.LongTensor([word['hello']])
hello_idx = Variable(hello_idx)
hello_embed = embeds(hello_idx)
print(hello_embed)
print(hello_embed.shape)
輸出:
{'hello': 0, 'world': 1}
tensor([[-1.0645, -1.1681, 0.6972, -0.5969, -0.4473, -1.0108]],
grad_fn=<EmbeddingBackward>)
torch.Size([1, 6])
首先:
word={'hello':0,'world':1}
是為了讓每個數字來表示一個單詞。
然後,
embeds = nn.Embedding(2, 6)
表示hello 和world兩個詞,6表示6個維度。
hello_idx = torch.LongTensor([word['hello']])
hello_idx = Variable(hello_idx)
這兩行程式碼表示得到一個variable,值是hello的index 既0。
最後print出來的就是hello的詞向量。
相關文章
- Pytorch中stack()方法的總結及理解PyTorch
- 用 Pytorch 理解卷積網路PyTorch卷積
- pytorch dataloader和batch_size大小的理解PyTorchBAT
- Pytorch中的損失函式PyTorch函式
- Pytorch中backward()的思考記錄PyTorch
- 理解pytorch幾個高階選擇函式(如gather)PyTorch函式
- Pytorch入門中 —— 搭建網路模型PyTorch模型
- 實踐Pytorch中的模型剪枝方法PyTorch模型
- 小白學PyTorch 動態圖與靜態圖的淺顯理解PyTorch
- 解說pytorch中的model=model.to(device)PyTorchdev
- 理解 JavaScript 中的 thisJavaScript
- 理解JS中的thisJS
- PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()PyTorch
- win10中pyTorch的GPU模式安裝記錄Win10PyTorchGPU模式
- 簡單介紹pytorch中log_softmax的實現PyTorch
- PyTorch 中 torch.matmul() 函式的文件詳解PyTorch函式
- 取出預訓練模型中間層的輸出(pytorch)模型PyTorch
- 理解 CSS 中的 BFCCSS
- 理解 JavaScript 中的原型JavaScript原型
- Vue中mixin的理解Vue
- 理解 CSS中的BFCCSS
- 全面理解JavaScript中的thisJavaScript
- oracle中的exists理解Oracle
- 理解 PHP 中的 StreamsPHP
- javascript 中{}和[] 的理解JavaScript
- 對HTML中"#"的理解HTML
- 《PyTorch》Part5 PyTorch之遷移學習PyTorch遷移學習
- (pytorch-深度學習系列)pytorch資料操作PyTorch深度學習
- pytorch簡介PyTorch
- PyTorch入門PyTorch
- pytorch 學習PyTorch
- 201204-通過一個A4紙張掃描的例子通俗理解PyTorch中LSTM的引數定義PyTorch
- 【小白學PyTorch】10 pytorch常見運算詳解PyTorch
- 【乾貨理解】理解javascript中實現MVC的原理JavaScriptMVC
- Pytorch中自定義神經網路卷積核權重PyTorch神經網路卷積
- 理解C#中的ValueTaskC#
- React中理解並使用ReduxReactRedux
- JS中this的深入理解JS