DrQA基于维基百科数据的开放域问答机器人实战教程

DrQA基于维基百科数据的开放域问答机器人实战教程

DrQA 是一个基于维基百科数据的开放域问答系统,它由检索器和阅读器组成。其中检索器用于从海量的文本(例如维基百科)中获得相关的文章;阅读器用于从文章中获得相应的答案。

官方介绍:
DrQA是一个应用于开放域问答的阅读理解系统。特别是,DrQA的目标是“大规模机读”(MRS)。在这个设定中,我们在可能非常大的非结构化文档集中搜索问题的答案。因此,系统必须将文档检索(查找相关文档)的挑战与机器对文本的理解(从这些文档中识别答案)的挑战相结合。

我们使用DrQA的实验侧重于回答factoid问题,同时使用Wikipedia作为文档的独特知识源。维基百科是一个非常适合大规模,丰富,详细信息的来源。为了回答任何问题,必须首先在超过500万个文章中检索可能相关的文章,然后仔细扫描它们以确定答案。

请注意,DrQA将Wikipedia视为一个通用的文章集合,并不依赖于其内部知识结构。因此,DrQA可以直接应用于任何文档集合。

数据集:维基百科

框架:PyTorch

版本:PyTorch torch-0.3.0

论文:Reading Wikipedia to Answer Open-Domain Questions

项目:https://github.com/facebookresearch/DrQA

系统架构:

实战:
交互模式下提问:

where is stanford university

可以看到检索到的文档是 Stanford University,问题的答案是:浅绿色标注的部分,答案非常的精准。

交互模式下提问:

where is Barack Hussein Obama from

交互模式下提问:
回答不是很精准,我其实想问的是奥巴马来自哪里。答案返回的是奥巴马在哪里,奥巴马在华盛顿,不过也还是相当不错。

who is Donald Trump
这个问题并没有找到准确的答案,虽然文档是相关的。

PyTorch 60 分钟入门教程:PyTorch 深度学习官方入门中文教程

什么是 PyTorch?

PyTorch 是一个基于 Python 的科学计算包,主要定位两类人群:

  • NumPy 的替代品,可以利用 GPU 的性能进行计算。
  • 深度学习研究平台拥有足够的灵活性和速度

开始学习

Tensors (张量)

Tensors 类似于 NumPy 的 ndarrays ,同时  Tensors 可以使用 GPU 进行计算。

from __future__ import print_function
import torch

构造一个5×3矩阵,不初始化。

x = torch.empty(5, 3)
print(x)

输出:

tensor(1.00000e-04 *
       [[-0.0000,  0.0000,  1.5135],
        [ 0.0000,  0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000]])

 

构造一个随机初始化的矩阵:

x = torch.rand(5, 3)
print(x)

输出:

tensor([[ 0.6291,  0.2581,  0.6414],
        [ 0.9739,  0.8243,  0.2276],
        [ 0.4184,  0.1815,  0.5131],
        [ 0.5533,  0.5440,  0.0718],
        [ 0.2908,  0.1850,  0.5297]])

 

构造一个矩阵全为 0,而且数据类型是 long.

Construct a matrix filled zeros and of dtype long:

x = torch.zeros(5, 3, dtype=torch.long)
print(x)

输出:

tensor([[ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0]])

构造一个张量,直接使用数据:

x = torch.tensor([5.5, 3])
print(x)

输出:

tensor([ 5.5000,  3.0000])

创建一个 tensor 基于已经存在的 tensor。

x = x.new_ones(5, 3, dtype=torch.double)      
# new_* methods take in sizes
print(x)

x = torch.randn_like(x, dtype=torch.float)    
# override dtype!
print(x)                                      
# result has the same size

输出:

tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]], dtype=torch.float64)
tensor([[-0.2183,  0.4477, -0.4053],
        [ 1.7353, -0.0048,  1.2177],
        [-1.1111,  1.0878,  0.9722],
        [-0.7771, -0.2174,  0.0412],
        [-2.1750,  1.3609, -0.3322]])

获取它的维度信息:

print(x.size())

输出:

torch.Size([5, 3])

注意

torch.Size  是一个元组,所以它支持左右的元组操作。

操作

在接下来的例子中,我们将会看到加法操作。

加法: 方式 1

y = torch.rand(5, 3)
print(x + y)

Out:

tensor([[-0.1859,  1.3970,  0.5236],
        [ 2.3854,  0.0707,  2.1970],
        [-0.3587,  1.2359,  1.8951],
        [-0.1189, -0.1376,  0.4647],
        [-1.8968,  2.0164,  0.1092]])

加法: 方式2

print(torch.add(x, y))

Out:

tensor([[-0.1859,  1.3970,  0.5236],
        [ 2.3854,  0.0707,  2.1970],
        [-0.3587,  1.2359,  1.8951],
        [-0.1189, -0.1376,  0.4647],
        [-1.8968,  2.0164,  0.1092]])

加法: 提供一个输出 tensor 作为参数

result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)

Out:

tensor([[-0.1859,  1.3970,  0.5236],
        [ 2.3854,  0.0707,  2.1970],
        [-0.3587,  1.2359,  1.8951],
        [-0.1189, -0.1376,  0.4647],
        [-1.8968,  2.0164,  0.1092]])

加法: in-place

# adds x to y
y.add_(x)
print(y)

Out:

tensor([[-0.1859,  1.3970,  0.5236],
        [ 2.3854,  0.0707,  2.1970],
        [-0.3587,  1.2359,  1.8951],
        [-0.1189, -0.1376,  0.4647],
        [-1.8968,  2.0164,  0.1092]])

Note

注意

任何使张量会发生变化的操作都有一个前缀 ‘_’。例如:x.copy_(y)x.t_(), 将会改变 x.

你可以使用标准的  NumPy 类似的索引操作

print(x[:, 1])

Out:

tensor([ 0.4477, -0.0048,  1.0878, -0.2174,  1.3609])

改变大小:如果你想改变一个 tensor 的大小或者形状,你可以使用 torch.view:

x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)  # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())

Out:

torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

如果你有一个元素 tensor ,使用 .item() 来获得这个 value 。

x = torch.randn(1)
print(x)
print(x.item())

Out:

tensor([ 0.9422])
0.9422121644020081

Caffe2 + PyTorch = PyTorch 1.0

作者:贾扬清
链接:https://zhuanlan.zhihu.com/p/36353529
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Announcing PyTorch 1.0 for both research and production

The path for taking AI development from research to production has historically involved multiple steps and tools, making it time-intensive and complicated to test new approaches, deploy them, and iterate to improve accuracy and performance. To help accelerate and optimize this process, we’re introducing PyTorch 1.0, the next version of our open source AI framework.

PyTorch 1.0 takes the modular, production-oriented capabilities from Caffe2 and ONNX and combines them with PyTorch’s existing flexible, research-focused design to provide a fast, seamless path from research prototyping to production deployment for a broad range of AI projects. With PyTorch 1.0, AI developers can both experiment rapidly and optimize performance through a hybrid front end that seamlessly transitions between imperative and declarative execution modes. The technology in PyTorch 1.0 has already powered many Facebook products and services at scale, including performing 6 billion text translations per day.

PyTorch 1.0 will be available in beta within the next few months, and will include a family of tools, libraries, pre-trained models, and datasets for each stage of development, enabling the community to quickly create and deploy new AI innovations at scale.

The path from research to production

PyTorch’s imperative front end allows for more rapid prototyping and experimentation through its flexible and productive programming model. The first version of PyTorch launched a little over a year ago, and its speed, productivity, and ability to support cutting-edge AI models such as dynamic graphs quickly made it a popular and important development tool for AI researchers. It has more than 1.1 million downloads and is the second-most cited deep learning framework on arxiv over the last month. For example, UC Berkeley computer scientists put PyTorch’s dynamic graph capabilities to use for their noteworthy CycleGAN image-to-image transform work.

Although the current version of PyTorch has provided great flexibility for AI research and development, performance at production-scale is sometimes a challenge, given its tight coupling to Python. We often need to translate the research code — either training script or trained model — to a graph mode representation in Caffe2 to run at production scale. Caffe2’s graph-based executor allows developers to take advantage of state-of-the-art optimizations like graph transformations, efficient memory reuse, and tight hardware interface integration. The Caffe2 project was launched two years ago to standardize our production AI tooling, and is now running neural networks across Facebook servers and on more than 1 billion phones around the world, spanning eight generations of iPhones and six generations of Android CPU architectures. Today, Caffe2 delivers more than 200 trillion predictions per day across all models, small and large, with optimized production performance.

The migration from PyTorch to Caffe2 to ship to production used to be a manual process, time-intensive, and error-prone. To solve this problem, we partnered with major hardware and software companies to create ONNX (Open Neural Network Exchange), an open format for representing deep learning models. With ONNX, developers can share models among different frameworks, for example, by exporting models built in PyTorch and importing them to Caffe2. At Facebook, this enabled us to have smoother AI research, training and inference with large-scale server and mobile deployment.

We’ve used these tools (PyTorch, Caffe2, and ONNX) to build and deploy Translate, the tool that now runs at scale to power translations for the 48 most commonly used languages on Facebook. In VR, these tools have been critical in deploying new research from Oculus into production to make avatars move more realistically.

However, while this combination of three different tools has been effective, there are still manual steps that are complicated and time-consuming. As such, it didn’t allow us to bring new AI research innovation to production as seamlessly as we would have liked.

Unifying research and production capabilities in one framework

PyTorch 1.0 fuses together immediate and graph execution modes, providing both flexibility for research and performance optimization for production. More specifically, rather than force developers to do an entire code rewrite to optimize or migrate from Python, PyTorch 1.0 provides a hybrid front end enabling you to seamlessly share the majority of code between immediate mode for prototyping and graph execution mode for production.

In addition, ONNX is natively woven into PyTorch 1.0 as the model export format, making models from PyTorch 1.0 interoperable with other AI frameworks. ONNX also serves as the integration interface for accelerated runtimes or hardware-specific libraries. This gives developers full freedom to mix and match the best AI frameworks and tools without having to take on resource-intensive custom engineering. Facebook is committed to supporting new features and functionalities for ONNX, which continues to be a powerful open format as well as an important part of developing with PyTorch 1.0.

Building an end-to-end deep learning system

Along with PyTorch 1.0, we’ll also open-source many of the AI tools we are using at scale today. These include Translate — a PyTorch Language Library — for fast, flexible neural machine translation, as well as the next generation of ELF, a comprehensive game platform for AI reasoning applications. Developers can also take advantage of tools like Glow, a machine learning compiler that accelerates framework performance on different hardware platforms, and Tensor Comprehensions, a tool that automatically generates efficient GPU code from high-level mathematical operations. We have also open-sourced other libraries, such as Detectron, which supports object-detection research, covering both bounding box and object instance segmentation outputs. Visit our AI developer site at facebook.ai/developers for the full list, and learn more about PyTorch on the PyTorch and Caffe2 blogs.

Over the coming months, we’re going to refactor and unify the codebases of both the Caffe2 and PyTorch 0.4 frameworks to deduplicate components and share abstractions. The result will be a unified framework that supports efficient graph-mode execution with profiling, mobile deployment, extensive vendor integrations, and more. As with other open AI initiatives like ONNX, we’re also partnering with other companies and the community to give more developers these accelerated research to production capabilities. To start, Microsoft plans to support PyTorch 1.0 in their Azure cloud and developer offerings, including Azure Machine Learning services and Data Science Virtual Machines, and Amazon Web Services currently supports the latest version of PyTorch, optimized for P3 GPU instances, and plans to make PyTorch 1.0 available shortly after release in their cloud offerings, including its Deep Learning AMI (Amazon Machine Image).

This is just the beginning, as we look to create and share better AI programming models, interfaces and automatic optimizations. AI is a foundational technology at Facebook today, making existing products better and powering entirely new experiences. By opening up our work via papers, code, and models, we can work with all AI researchers and practitioners to advance the state of the art faster and to help apply these techniques in new ways.

PyTorch 1.0现身(Logo也换了),围棋AI开源

F8开发者大会第二天,Facebook亮出了一系列AI大动作。

Caffe2和PyTorch合体事件有了清晰的发展方向,同样服务于深度学习开发者的ONNX也宣布支持更多的框架。

另外,Facebook开源了视频理解、自然语言处理的模型,开源了围棋AI ELF OpenGo,还展示了一个打星际的AI。

PyTorch

v0.4.0正式版发布没几天,Facebook在F8开发者大会第二天宣布将发布PyTorch 1.0,还提前展示了这款新框架的特性。

这个框架,还换了LOGO:

深度学习框架Caffe2的作者贾扬清,在知乎上将这一版本的发布总结为Caffe2 + PyTorch = PyTorch 1.0。

也就是将原本两款框架面向研究的和面向生产的特性结合了起来。

Facebook介绍说,PyTorch 1.0结合了Caffe2和ONNX模块化、面向生产的特性,和PyTorch自身灵活、面向研究的特性结合起来,为广泛的AI项目提供了一个从科研原型到生产部署的快速、无缝途径,让用户可以快速实验,通过一个能在强制执行模式和声明执行模式之间无缝切花的混合前端优化性能。

除了将研究和生产特性结合起来,PyTorch 1.0还将ONNX(开放神经网络交换)包含进来。ONNX是Facebook去年联合多家软硬件公司发布的神经网络模型转换协议,现在,它新增了对苹果的Core ML、百度PaddlePaddle、高通SNPE的支持,再加上原本支持的MXNet、Caffe2、PyTorch、TensorFlow、CNTK等框架,实现了神经网络模型在各种主流框架之间的转换。

PyTorch 1.0 beta版将在今年夏天和用户见面。

不过,Facebook内部已经用上了。官方称,Facebook多款产品和服务都在大规模应用这个新框架,它每天要处理60亿次文本翻译任务。

PyTorch最初亮相于1年多以前,Facebook的另一款深度学习框架Caffe2,则在去年的F8大会上正式发布。

不过今年4月,Caffe2已经宣布全部代码并入PyTorch。接下来的几个月里,两款框架原本的组件将深度结合,成为一个单独的软件包。

就在上周,PyTorch发布了v0.4.0版本,将Tensors(张量)和Variables(变量)合并,新增了零维张量,还开始了对Windows系统的官方支持。

展示PyTorch 1.0的同时,Facebook还开源了一部分研究成果。比如用于视频理解的ResNext3D模型将于6月发布,视频行为识别模型Res 2+1今天就已经开源,PyTorch中的自然语言理解库Translate也将开源。

发布了这么多资源和工具,去哪找呢?Facebook还为旗下所有的AI资源推出了一个网站:

https://facebook.ai/developers

围棋AI开源下载

在F8大会上,还开源了一个围棋AI:ELF OpenGo。

这个AI是Facebook团队对DeepMind技术的一个重现,最近他们选择与四名排名世界前30的人类高手对战,取得了14-0的胜利。

和AlphaGo一样,这个AI的重点也并不只是下围棋,而是想要更好的解决问题。现在ELF OpenGo已经可以开源下载。

对此,田渊栋在知乎上有更详细的解答:

我们最近改进了ELF框架,并且在上面实现了DeepMind的AlphaGoZero及AlphaZero的算法。用两千块GPU训练约两到三周后得到的围棋AI,基本上超过了强职业的水平。我们和韩国棋院合作进行了一次测试,给这个AI单卡每步50秒搜索时间(每步搜索8万个局面),给人类棋手任意长时间思考,结果AI以14比0完胜。参与测试的棋手包括金志锡,申真谞,朴永训及崔哲瀚,在这里我们非常感谢他们的合作,大家都很尽力,一些棋局下了三四个小时极其精彩。应棋手们的要求,这14局棋谱中的12局不久将公开。

另外我们也和现在著名的LeelaZero比较了下。我们采用了LeelaZero除ponder外的缺省配置(约一分钟一步),及4月25日的公开权重(192×15, 158603eb),结果我们的AI以200比0获胜。在此我们非常感谢Leela团队的工作,对于他们的开源精神,我们表示由衷的敬意。

这次我们将训练代码,测试代码及训练出来的模型(224×20)全部公开,首要目的是贯彻我们一直以来坚持的开源方针,让AI为全世界服务。其次是对于AlphaGoZero及AlphaZero这样非常优秀的算法,我们想要提供一个可重复的参考实现,让全球的研究者们能在这上面继续改进,充分发挥自己的创造力。最后是借此机会推广一下我们的ELF平台和PyTorch深度学习框架,希望更多的人能使用和完善它。

感谢大家的支持!

田渊栋,龚渠成&马子嫯(Jerry Ma), Shubho Sengupta, 陈卓远,Larry Zitnick

ELF OpenGo代码及模型的地址:

https://github.com/pytorch/ELF

其他

在F8大会上,还展示了一个可以打《星际争霸》的AI,Facebook也计划随后开源这一项目。星际争霸和围棋一直也都是Facebook团队研究的方向。

还有一项突破研究。基于35亿张用户已打标签(17000个)的公开图像,Facebook成功训练了一个图像识别系统,这比之前只能用手动打标签的5000万张图片训练相比,提高了系统的识别能力,在ImageNet上获得了创纪录的高分(准确率85.4%)。

更多信息,可以参考这个页面:

https://code.facebook.com/posts/1700437286678763/

此外,F8大会上还展示了AR和VR方面的进步。

Facebook已经创建了一个原型系统,可以生成效果惊人的三围重建画面。下面这个视频,展示了正常的视频与3D重建画面的比较,几乎难以分辨左右哪个画面为真。(友情提示:左边露出操作员脚部的是真实世界)

原文链接:http://www.sohu.com/a/230230002_610300