【发布时间】: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