【问题标题】:Create a shared row in theano在 theano 中创建共享行
【发布时间】:2016-11-26 00:22:24
【问题描述】:

我注意到在 theano 中,当一个基于 1D numpy 数组创建共享变量时,它变成了一个向量,而不是一行:

import theano.tensor as T
import theano, numpy

shared_vector = theano.shared(numpy.zeros((10,)))
print(shared_vector.type)
# TensorType(float64, vector)
print(shared_vector.broadcastable)
# (False,)

对于一个 1xN 矩阵也是如此,它变成一个矩阵而不是一行:

shared_vector = theano.shared(numpy.zeros((1,10,)))
print(shared_vector.type)
# TensorType(float64, matrix)
print(shared_vector.broadcastable)
# (False, False)

当我想将 M x N 矩阵添加到 1 X N 行向量时,这很麻烦,因为共享向量在第一维中不可广播。首先,这是行不通的:

row = T.row('row')
mat=T.matrix('matrix')
f=theano.function(
    [],
    mat + row,
    givens={
        mat: numpy.zeros((20,10), dtype=numpy.float32),
        row: numpy.zeros((10,), dtype=numpy.float32)
    },
    on_unused_input='ignore'
)

出现错误:

TypeError: Cannot convert Type TensorType(float32, vector) (of Variable <TensorType(float32, vector)>) into Type TensorType(float32, row). You can try to manually convert <TensorType(float32, vector)> into a TensorType(float32, row).

好的,很明显,我们不能将向量分配给行。不幸的是,这也不好:

row = T.matrix('row')
mat=T.matrix('matrix')
f=theano.function(
    [],
    mat + row,
    givens={
        mat: numpy.zeros((20,10), dtype=numpy.float32),
        row: numpy.zeros((1,10,), dtype=numpy.float32)
    },
    on_unused_input='ignore'
)
f()

出现错误:

ValueError: Input dimension mis-match. (input[0].shape[0] = 20, input[1].shape[0] = 1)
Apply node that caused the error: Elemwise{add,no_inplace}(<TensorType(float32, matrix)>, <TensorType(float32, matrix)>)
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)]
Inputs shapes: [(20, 10), (1, 10)]
Inputs strides: [(40, 4), (40, 4)]
Inputs values: ['not shown', 'not shown']

Backtrace when the node is created:
  File "<ipython-input-55-0f03bee478ec>", line 5, in <module>
    mat + row,

HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

所以我们也不能只使用 1 x N 矩阵作为行(因为 1 x N 矩阵的第一个维度不可广播)。

问题仍然存在,我们“能”做什么?如何创建行类型的共享变量,这样我可以使用矩阵行加法进行广播?

【问题讨论】:

  • 我好像在发帖几秒后就发现了。我想我可以在向量上使用 .reshape 来创建一个 1 x N 矩阵,该矩阵具有可广播 (True, False)

标签: python theano


【解决方案1】:

使用reshape(1, N) 的替代方法是将dimshuffle('x', 0) 用作described in the documentation

这是两种方法的演示:

import numpy
import theano

x = theano.shared(numpy.arange(10))
print x
print x.dimshuffle('x', 0).type
print x.dimshuffle(0, 'x').type
print x.reshape((1, x.shape[0])).type
print x.reshape((x.shape[0], 1)).type

f = theano.function([], outputs=[x, x.dimshuffle('x', 0), x.reshape((1, x.shape[0]))])
theano.printing.debugprint(f)

打印出来

<TensorType(int32, vector)>
TensorType(int32, row)
TensorType(int32, col)
TensorType(int32, row)
TensorType(int32, col)
DeepCopyOp [@A] ''   2
 |<TensorType(int32, vector)> [@B]
DeepCopyOp [@C] ''   4
 |InplaceDimShuffle{x,0} [@D] ''   1
   |<TensorType(int32, vector)> [@B]
DeepCopyOp [@E] ''   6
 |Reshape{2} [@F] ''   5
   |<TensorType(int32, vector)> [@B]
   |MakeVector{dtype='int64'} [@G] ''   3
     |TensorConstant{1} [@H]
     |Shape_i{0} [@I] ''   0
       |<TensorType(int32, vector)> [@B]

证明dimshuffle 可能更可取,因为它比reshape 涉及的工作更少。

【讨论】:

  • 出于可读性的原因,我总是更喜欢 reshape,但我现在会考虑 dimshuffle。谢谢!
【解决方案2】:

我会使用:

shared_row = theano.shared(numpy.zeros((1,10,)), broadcastable=(True, False))
print(shared_row.type)
# TensorType(float64, row)
print(shared_row.broadcastable)
(True, False)

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 2015-06-21
    • 2018-10-26
    • 2011-04-06
    • 1970-01-01
    • 2016-06-23
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多