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. Shuffle UVA - 12174 尺取法

    题目:题目链接 思路:见紫书,对具体操作方式还不是很理解,代码是从一个题解里看的,以后多回顾下,需要理解 代码: #include <iostream> #include <cstr ...

  2. Python虚拟机之异常控制流(四)

    Python虚拟机中的异常控制流 先前,我们分别介绍了Python虚拟机之if控制流(一).Python虚拟机之for循环控制流(二)和Python虚拟机之while循环控制结构(三).这一章,我们来 ...

  3. org.hibernate.AnnotationException: No identifier specified for entity: com.example1.demo1.Entity.User错误

    最近在公司带人,他们问我的问题在这里也顺便总结下. 此项目为SpringDataJpa项目. 出现的错误如下: Caused by: org.hibernate.AnnotationException ...

  4. python - 自动化测试框架 - 测试报告

    testSuitr.py: # -*- coding:utf-8 -*- '''@project: Voctest@author: Jimmy@file: testSuite.py@ide: PyCh ...

  5. css中可继承和不可继承属性

    一.无继承性的属性 1.display:规定元素应该生成的框的类型 2.文本属性: vertical-align:垂直文本对齐 text-decoration:规定添加到文本的装饰 text-shad ...

  6. 第五部分 linux 软件安装RPM SRPM与YUM

    第五部分  linux  软件安装RPM    SRPM与YUM   软件管理员简介 RPM与DPKG两大主流 rpm: redhat       centos     suse    命令:yum ...

  7. [python工具][3]sublime常用配置 与操作指南

    https://github.com/jikeytang/sublime-text http://zh.lucida.me/blog/sublime-text-complete-guide/

  8. java 8:I / O 基础

    原文地址:https://docs.oracle.com/javase/tutorial/essential/io/index.html 说明:每一个点都有一篇详细的文章与之对应,每翻译完一篇文章会更 ...

  9. linux shell脚本监控进程是否存在

    用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货:    #!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ...

  10. C/C++、Java、Python谁是编译型语言,谁是解释型语言?

    最近各大互联网公司线上笔试,编程题目里的编译器只支持C/C++.Java,甚至有的支持javaScrpit和Pascal,就是不支持Python.让一直以来用惯了Python的我直吐血,于是今天痛定思 ...