未加星标

Einops - deep learning operations rethinked

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Einops - deep learning operations rethinked
einops

Flexible and powerful tensor operations for readable and reliable code. Supports numpy, pytorch, tensorflow, and. Examples:


Einops - deep learning operations rethinked
This video in better quality. Tutorial / Documentation

Tutorial is the most convenient way to see einops in action (and right now works as a documentation)

part 1: einops fundamentals part 2: einops for deep learning part 3: TBD Installation

Plain and simple:

pip install einops

einops has no mandatory dependencies (code examples also require jupyter, pillow + backends). To obtain the latest github version

pip install https://github.com/arogozhnikov/einops/archive/master.zip API

einops has minimalistic and powerful API.

Two operations provided (see einops tutorial for examples)

from einops import rearrange, reduce # rearrange elements according to the pattern output_tensor = rearrange(input_tensor, 't b c -> b c t') # combine rearrangement and reduction output_tensor = reduce(input_tensor, 'b c (h h2) (w w2) -> b h w c', 'mean', h2=2, w2=2)

And two corresponding layers ( einops keeps separate version for each framework) with the same API.

from einops.layers.chainer import Rearrange, Reduce from einops.layers.gluon import Rearrange, Reduce from einops.layers.keras import Rearrange, Reduce from einops.layers.torch import Rearrange, Reduce

Layers behave similarly to operations and have same parameters (for the exception of first argument, which is passed during call)

layer = Rearrange(pattern, **axes_lengths) layer = Reduce(pattern, reduction, **axes_lengths) # apply created layer to a tensor / variable x = layer(x)

Example of using layers within a model:

# example given for pytorch, but code in other frameworks is almost identical from torch.nn import Sequential, Conv2d, MaxPool2d, Linear, ReLU from einops.layers.torch import Reduce model = Sequential( Conv2d(3, 6, kernel_size=5), MaxPool2d(kernel_size=2), Conv2d(6, 16, kernel_size=5), MaxPool2d(kernel_size=2), # flattening Rearrange('b c h w -> b (c h w)'), Linear(16*5*5, 120), ReLU(), Linear(120, 10), )

Additionally two auxiliary functions provided

from einops import asnumpy, parse_shape # einops.asnumpy converts tensors of imperative frameworks to numpy numpy_tensor = asnumpy(input_tensor) # einops.parse_shape gives a shape of axes of interest parse_shape(input_tensor, 'batch _ h w') # e.g {'batch': 64, 'h': 128, 'w': 160} Naming and terminology

einops stays for Einstein-Inspired Notation for operations (though "Einstein operations" is more attractive and easier to remember).

Notation was loosely inspired by Einstein summation (in particular by numpy.einsum operation).

Terms tensor and ndarray are equivalently used and refer to multidimensional array Terms axis and dimension are also equivalent Why using einops notation Semantic information (being verbose in expectations) y = x.view(x.shape[0], -1) y = rearrange(x, 'b c h w -> b (c h w)')

while these two lines are doing the same job in some context, second one provides information about input and output. In other words, einops focuses on interface: what is input and output , not how output is computed.

The next operation looks similar:

y = rearrange(x, 'time c h w -> time (c h w)')

But it gives reader a hint: this is not an independent batch of images we are processing, but rather a sequence (video).

Semantic information makes code easier to read and maintain.

More checks

Reconsider the same example:

y = x.view(x.shape[0], -1) # x: (batch, 256, 19, 19) y = rearrange(x, 'b c h w -> b (c h w)')

second line checks that input has four dimensions, but you can also specify particular dimensions. That's opposed to just writing comments about shapes since comments don't work as we know

y = x.view(x.shape[0], -1) # x: (batch, 256, 19, 19) y = rearrange(x, 'b c h w -> b (c h w)', c=256, h=19, w=19) Result is strictly determined

Below we have at least two ways to define depth-to-space operation

# depth-to-space rearrange(x, 'b c (h h2) (w w2) -> b (c h2 w2) h w', h2=2, w2=2) rearrange(x, 'b c (h h2) (w w2) -> b (h2 w2 c) h w', h2=2, w2=2)

there are at least four more ways to do it. Which one is used by the framework?

These details are ignored, since usually it makes no difference, but it can make a big difference (e.g. if you use grouped convolutions on the next stage), and you'd like to specify this in your code.

Uniformity reduce(x, 'b c (x dx) -> b c x', 'max', dx=2) reduce(x, 'b c (x dx) (y dx) -> b c x y', 'max', dx=2, dy=3) reduce(x, 'b c (x dx) (y dx) (z dz)-> b c x y z', 'max', dx=2, dy=3, dz=4)

These examples demonstrated that we don't use separate operations for 1d/2d/3d pooling, those all are defined in a uniform way.

Space-to-depth and depth-to space are defined in many frameworks. But how about width-to-height?

rearrange(x, 'b c h (w w2) -> b c (h w2) w', w2=2) Framework independent behavior

Even simple functions are defined differently by different frameworks

y = x.flatten() # or flatten(x)

Suppose x shape was (3, 4, 5) , then y has shape ...

(60,) (3, 20) Supported frameworks

Einops works with ...

numpy pytorch tensorflow eager cupy chainer gluon tensorflow mxnet (experimental) and keras (experimental) Contributing

Best ways to contribute are

spread the word about einops prepare a guide/post/tutorial for your favorite deep learning framework translating examples in languages other than English is also a good idea use einops notation in your papers to strictly define used operations Supported python versions

einops works with python 3.5 or later.

There is nothing specific to python 3 in the code, we simply need to move further and I decided not to support python 2.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:Einops - deep learning operations rethinked
本站链接:https://www.codesec.net/view/611596.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(67)