import torch
import torch.nn as nn
from torch.autograd import Variable word2id = {'hello': 0, 'world': 1}
# you have 2 words, and then need 5 dim each word
embeds = nn.Embedding(2, 5)
# we need variable, because we need use element of nn.Embedding
hello_idx = torch.LongTensor([word2id['hello']])
print(hello_idx)
hello_idx = Variable(hello_idx)
print(hello_idx)
# achieve the initial word_embedding of 'hello'
hello_embed = embeds(hello_idx)
print(hello_embed)
'''
0
[torch.LongTensor of size 1] Variable containing:
0
[torch.LongTensor of size 1] Variable containing:
1.1842 0.6819 -0.8768 -1.5130 0.7650
[torch.FloatTensor of size 1x5]
'''

PyTorch学习笔记之初识word_embedding的更多相关文章

  1. Storm学习笔记 - Storm初识

    Storm学习笔记 - Storm初识 1. Strom是什么? Storm是一个开源免费的分布式计算框架,可以实时处理大量的数据流. 2. Storm的特点 高性能,低延迟. 分布式:可解决数据量大 ...

  2. LevelDB学习笔记 (1):初识LevelDB

    LevelDB学习笔记 (1):初识LevelDB 1. 写在前面 1.1 什么是levelDB LevelDB就是一个由Google开源的高效的单机Key/Value存储系统,该存储系统提供了Key ...

  3. Java学习笔记心得——初识Java

    初识Java 拿到这本厚厚的<Java学习笔记>,翻开目录:Java平台概论.从JDK到TDE.认识对象.封装.继承与多态...看着这些似懂非懂的术语名词,心里怀着些好奇与担忧,就这样我开 ...

  4. Pytorch学习笔记(二)---- 神经网络搭建

    记录如何用Pytorch搭建LeNet-5,大体步骤包括:网络的搭建->前向传播->定义Loss和Optimizer->训练 # -*- coding: utf-8 -*- # Al ...

  5. Pytorch学习笔记(一)---- 基础语法

    书上内容太多太杂,看完容易忘记,特此记录方便日后查看,所有基础语法以代码形式呈现,代码和注释均来源与书本和案例的整理. # -*- coding: utf-8 -*- # All codes and ...

  6. 【pytorch】pytorch学习笔记(一)

    原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...

  7. 【深度学习】Pytorch 学习笔记

    目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...

  8. Pytorch学习笔记(一)——简介

    一.Tensor Tensor是Pytorch中重要的数据结构,可以认为是一个高维数组.Tensor可以是一个标量.一维数组(向量).二维数组(矩阵)或者高维数组等.Tensor和numpy的ndar ...

  9. [PyTorch 学习笔记] 1.3 张量操作与线性回归

    本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/linear_regression.py 张量的操作 拼 ...

随机推荐

  1. The 2018 ACM-ICPC Chinese Collegiate Programming Contest Caesar Cipher

    #include <iostream> #include <cstdio> #include <cstring> #include <string> # ...

  2. Leetcode 81. 搜索旋转排序数组 II

    题目链接 https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/description/ 题目描述 假设按照升序排序的数 ...

  3. Linux学习-编译前的任务:认识核心与取得核心原始码

    什么是核心 (Kernel) Kernel 其实核心就是系统上面的一个文件而已, 这个文件包含了驱动主机各项硬 件的侦测程序与驱动模块. 核心文件通常被放置成 /boot/vmlinuz-xxx ,不 ...

  4. TTL与COMS的区别

    1.电平的上限和下限定义不一样,CMOS具有更大的抗噪区域. 同是5伏供电的话,ttl一般是1.7V和3.5V的样子,CMOS一般是  2.2V,2.9V的样子,不准确,仅供参考. 2.电流驱动能力不 ...

  5. Freemarker的循环通过assign指令引入计数变量

    这里是一个jeecms框架的前台的一个内容列表集,因为不是每个内容子项符合要求,而且需要统计符合要求的子项个数,仿照java的for循环,需要在循环前声明一个计数变量,这就需要使用Freemaker的 ...

  6. luogu3381 【模板】最小费用最大流

    每次选代价最小的流增广 #include <iostream> #include <cstring> #include <cstdio> #include < ...

  7. MacOS常用软件推荐

    1.效率提升神器Alfred 可以搜索文件.应用.web搜索.词典等等 链接:https://pan.baidu.com/s/1igv4tuXkuMFOPT9E6Cc5Jg 密码:3o51 软件解压密 ...

  8. day04_07 while循环01

    while循环结构: #while 条件: print("any") print("any") 死循环案例 num = 1 while num<=10 : ...

  9. python 时间、日期、时间戳的转换

    在实际开发中经常遇到时间格式的转换,例如: 前端传递的时间格式是字符串格式,我们需要将其转换为时间戳,或者前台传递的时间格式和我们数据库中的格式不对应,我们需要对其进行转换才能与数据库的时间进行匹配等 ...

  10. [git 学习篇]版本回退

    再次修改readme.txt ,并将其提交成功 $ git add readme.txt $ git commit -m "append GPL" [master ] append ...