【问题标题】:Theano multiple tensors as outputTheano 多张量作为输出
【发布时间】:2015-01-19 18:59:15
【问题描述】:

我正在使用 Theano 创建一个神经网络,但是当我尝试在一个列表中同时返回两个张量列表时,我得到了错误:

#This is the line that causes the error
#type(nabla_w) == <type 'list'>
#type(nabla_w[0]) == <class 'theano.tensor.var.TensorVariable'>
backpropagate = function(func_inputs, [nabla_w, nabla_b])

TypeError: Outputs must be theano Variable or Out instances. Received [dot.0, dot.0, dot.0, dot.0] of type <type 'list'>

我应该使用哪种 Theano 结构将两个张量一起返回到一个数组中,这样我就可以像这样检索它们:

nabla_w, nabla_b = backpropagate(*args)

我尝试使用我在basic Tensor functionality page 中找到的一些东西,但这些都不起作用。 (例如我尝试了堆栈或堆栈列表)

这是我使用 theano.tensor.stack 或 stacklists 时遇到的错误:

ValueError: all the input array dimensions except for the concatenation axis must match exactly
Apply node that caused the error: Join(TensorConstant{0}, Rebroadcast{0}.0, Rebroadcast{0}.0, Rebroadcast{0}.0, Rebroadcast{0}.0)
Inputs shapes: [(), (1, 10, 50), (1, 50, 100), (1, 100, 200), (1, 200, 784)]
Inputs strides: [(), (4000, 400, 8), (40000, 800, 8), (160000, 1600, 8), (1254400, 6272, 8)]
Inputs types: [TensorType(int8, scalar), TensorType(float64, 3D), TensorType(float64, 3D), TensorType(float64, 3D), TensorType(float64, 3D)]
Use the Theano flag 'exception_verbosity=high' for a debugprint of this apply node.

代码的一些额外上下文:

weights = [T.dmatrix('w'+str(x)) for x in range(0, len(self.weights))]
biases = [T.dmatrix('b'+str(x)) for x in range(0, len(self.biases))]
nabla_b = []
nabla_w = []
# feedforward
x = T.dmatrix('x')
y = T.dmatrix('y')
activations = []
inputs = []
activations.append(x)
for i in xrange(0, self.num_layers-1):
    inputt = T.dot(weights[i], activations[i])+biases[i]
    activation = 1 / (1 + T.exp(-inputt))
    activations.append(activation)
    inputs.append(inputt)

delta = activations[-1]-y
nabla_b.append(delta)
nabla_w.append(T.dot(delta, T.transpose(inputs[-2])))

for l in xrange(2, self.num_layers):
    z = inputs[-l]
    spv = (1 / (1 + T.exp(-z))*(1 - (1 / (1 + T.exp(-z)))))
    delta = T.dot(T.transpose(weights[-l+1]), delta) * spv
    nabla_b.append(delta)
    nabla_w.append(T.dot(delta, T.transpose(activations[-l-1])))
T.set_subtensor(nabla_w[-l], T.dot(delta, T.transpose(inputs[-l-1])))
func_inputs = list(weights)
func_inputs.extend(biases)
func_inputs.append(x)
func_inputs.append(y)


backpropagate = function(func_inputs, [nabla_w, nabla_b])

【问题讨论】:

  • 默认情况下,[some, stuff] 构造是一个 python 列表。如果 Theano 需要一个数组,您可能需要使用 array() 构造函数,该构造函数(对于 python 2.x)位于 here
  • 忘了说 nabla_w 和 nabla_b 已经在列表中了。我正在做的问题是它不接受张量列表。
  • 如果做得正确应该可以工作(我经常使用这个)。不幸的是,代码不是自包含的,所以我无法弄清楚可能出了什么问题。您能否提供一个可以复制和粘贴的最小示例以进行分析?否则,您将不得不依赖这个论坛中已经知道错误消息必须发生的事情的极少数人。
  • 我只是为上下文添加了一些额外的代码,但我认为为此添加“可复制粘贴”部分并不容易,但我稍后会尝试更改它。

标签: python neural-network theano


【解决方案1】:

Theano 不支持此功能。当你调用theano.function(inputs, outputs) 时,输出只能是两件事:

1) Theano 变量 2) Theano 变量列表

(2) 不允许您在顶级列表中拥有列表,因此您应该展平输出中的列表。这将返回超过 2 个输出。

您的问题的一个组合解决方案是将内部列表复制到 1 个变量中。

tensor_nabla_w = theano.tensor.stack(*nabla_w).

这要求 nabla_w 中的所有元素都是相同的形状。这将在计算图中添加一个额外的副本(因此可能会慢一些)。

更新 1:修复对 stack() 的调用

更新 2:

到目前为止,我们已经添加了所有元素将具有不同形状的约束,因此不能使用堆栈。如果它们都具有相同的维度数和 dtype,您可以使用typed_list,否则您将需要自己修改 Theano 或展平输出的列表。

【讨论】:

  • 我提到我尝试了 stack 和 stacklist theano.tensor.stack() 但我找不到 theano.stack() 但如果我错了请纠正我。同样不幸的是, nabla_w 中的张量形状不同,因为每层的神经网络神经元不同。我正在添加使用 tensor.stack() 时收到的错误消息
  • 我昨天编辑了我的答案,告诉你如何使它与键入列表的不同形状一起工作。但它们需要具有相同数量的维度。
  • 感谢您的评论(我之前没有检查过)我会尝试输入的列表并报告!
猜你喜欢
  • 2019-02-10
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 2016-11-06
相关资源
最近更新 更多