Theano 中文文档 0.9 - 7.2.6 Theano如何处理形状信息

来源:互联网 发布:sql查询成绩相同语句 编辑:程序博客网 时间:2024/05/16 11:45

7.2.6 Theano如何处理形状信息

译者:Python 文档协作翻译小组,原文:How Shape Information is Handled by Theano。

本文以 CC BY-NC-SA 4.0 协议发布,转载请保留作者署名和文章出处。

Python 文档协作翻译小组人手紧缺,有兴趣的朋友可以加入我们,完全公益性质。交流群:467338606。

在构建图形时,不可能严格执行Theano变量的形状,因为在运行时间为Theano函数的参数提供的特定值可能会调整其图形中的Theano变量的形状。

目前,关于形状的信息在Theano中以两种方式使用:

  • 为了在CPU和GPU上生成用于2d卷积的更快的C代码,当预先知道确切的输出形状时。

  • 当我们只想知道形状,而不是变量的实际值时,去除图中的计算。这通过Op.infer_shape方法完成。

    例:

>>> import theano>>> x = theano.tensor.matrix('x')>>> f = theano.function([x], (x ** 2).shape)>>> theano.printing.debugprint(f) MakeVector{dtype='int64'} [id A] ''   2 |Shape_i{0} [id B] ''   1 | |x [id C] |Shape_i{1} [id D] ''   0 |x [id C]

此编译函数的输出不包含任何乘法或幂。Theano已经删除它们直接计算输出的形状。

Shape Inference Problem

Theano在图形中传播关于形状的信息。有时这可能会导致错误。考虑这个例子:

>>> import numpy>>> import theano>>> x = theano.tensor.matrix('x')>>> y = theano.tensor.matrix('y')>>> z = theano.tensor.join(0, x, y)>>> xv = numpy.random.rand(5, 4)>>> yv = numpy.random.rand(3, 3)
>>> f = theano.function([x, y], z.shape)>>> theano.printing.debugprint(f) MakeVector{dtype='int64'} [id A] ''   4 |Elemwise{Add}[(0, 0)] [id B] ''   3 | |Shape_i{0} [id C] ''   1 | | |x [id D] | |Shape_i{0} [id E] ''   2 |   |y [id F] |Shape_i{1} [id G] ''   0 |x [id D]
>>> f(xv, yv) # DOES NOT RAISE AN ERROR AS SHOULD BE.array([8, 4])
>>> f = theano.function([x,y], z)# Do not take the shape.>>> theano.printing.debugprint(f) Join [id A] ''   0 |TensorConstant{0} [id B] |x [id C] |y [id D]
>>> f(xv, yv)  Traceback (most recent call last):  ...ValueError: ...

正如你所看到的,当仅仅请求一些计算的形状(在示例中join)时,直接计算推断的形状,而不执行计算本身(没有join

这使得形状的计算更快,但它也可以隐藏错误。在此示例中,join的输出形状的计算仅基于第一个输入Theano变量完成,这导致错误。

这可能会发生在其他操作,例如elemwisedot。实际上,为了执行一些优化(例如,对于速度或稳定性),Theano假定计算是正确的,并且在第一位置是一致的,如在这里。

您可以通过运行代码而不进行此优化,使用Theano标志optimizer_excluding=local_shape_to_shape_i来检测这些问题。你也可以通过在模式FAST_COMPILE(它不会应用此优化,也不会应用大多数其他优化)或DebugMode优化(慢得多))。

Specifing Exact Shape

目前,指定一个形状不是那么容易和灵活,我们希望,我们计划一些升级。这里是可以做什么的当前状态:

  • 你可以直接传递形状信息到ConvOp创建时调用conv2d。你只需在调用中设置参数image_shapefilter_shape。它们必须是4个元素的元组。例如:
theano.tensor.nnet.conv2d(..., image_shape=(7, 3, 5, 5), filter_shape=(2, 3, 4, 4))
  • 您可以使用SpecifyShape op在图形中的任何位置添加形状信息。这允许执行一些优化。在以下示例中,这使得有可能将Theano函数预先计算为常数。
>>> import theano>>> x = theano.tensor.matrix()>>> x_specify_shape = theano.tensor.specify_shape(x, (2, 2))>>> f = theano.function([x], (x_specify_shape ** 2).shape)>>> theano.printing.debugprint(f) DeepCopyOp [id A] ''   0 |TensorConstant{(2,) of 2} [id B]

Future Plans

参数“恒定形状”将添加到theano.shared()。这可能是shared变量??最常见的情况。它将使代码更简单,并且可以检查更新shared变量??时形状不会改变。

Shape Inference Problem

Theano在图形中传播关于形状的信息。有时这可能会导致错误。考虑这个例子:

>>> import numpy>>> import theano>>> x = theano.tensor.matrix('x')>>> y = theano.tensor.matrix('y')>>> z = theano.tensor.join(0, x, y)>>> xv = numpy.random.rand(5, 4)>>> yv = numpy.random.rand(3, 3)
>>> f = theano.function([x, y], z.shape)>>> theano.printing.debugprint(f) MakeVector{dtype='int64'} [id A] ''   4 |Elemwise{Add}[(0, 0)] [id B] ''   3 | |Shape_i{0} [id C] ''   1 | | |x [id D] | |Shape_i{0} [id E] ''   2 |   |y [id F] |Shape_i{1} [id G] ''   0 |x [id D]
>>> f(xv, yv) # DOES NOT RAISE AN ERROR AS SHOULD BE.array([8, 4])
>>> f = theano.function([x,y], z)# Do not take the shape.>>> theano.printing.debugprint(f) Join [id A] ''   0 |TensorConstant{0} [id B] |x [id C] |y [id D]
>>> f(xv, yv)  Traceback (most recent call last):  ...ValueError: ...

正如你所看到的,当仅仅请求一些计算的形状(在示例中join)时,直接计算推断的形状,而不执行计算本身(没有join

这使得形状的计算更快,但它也可以隐藏错误。在此示例中,join的输出形状的计算仅基于第一个输入Theano变量完成,这导致错误。

这可能会发生在其他操作,例如elemwisedot。实际上,为了执行一些优化(例如,对于速度或稳定性),Theano假定计算是正确的,并且在第一位置是一致的,如在这里。

您可以通过运行代码而不进行此优化,使用Theano标志optimizer_excluding=local_shape_to_shape_i来检测这些问题。你也可以通过在模式FAST_COMPILE(它不会应用此优化,也不会应用大多数其他优化)或DebugMode优化(慢得多))。

Specifing Exact Shape

目前,指定一个形状不是那么容易和灵活,我们希望,我们计划一些升级。这里是可以做什么的当前状态:

  • 你可以直接传递形状信息到ConvOp创建时调用conv2d。你只需在调用中设置参数image_shapefilter_shape。它们必须是4个元素的元组。例如:
theano.tensor.nnet.conv2d(..., image_shape=(7, 3, 5, 5), filter_shape=(2, 3, 4, 4))
  • 您可以使用SpecifyShape op在图形中的任何位置添加形状信息。这允许执行一些优化。在以下示例中,这使得有可能将Theano函数预先计算为常数。
>>> import theano>>> x = theano.tensor.matrix()>>> x_specify_shape = theano.tensor.specify_shape(x, (2, 2))>>> f = theano.function([x], (x_specify_shape ** 2).shape)>>> theano.printing.debugprint(f) DeepCopyOp [id A] ''   0 |TensorConstant{(2,) of 2} [id B]
0 0
原创粉丝点击