没有多个张量作为输出

2024-10-01 09:36:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

#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结构来返回数组中的两个张量,这样我就可以像这样检索它们:

^{pr2}$

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

这是我使用的错误张量堆栈或堆叠列表:

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])

Tags: the列表fortypedotdeltafuncinputs
1条回答
网友
1楼 · 发布于 2024-10-01 09:36:14

Theano不支持这一点。当您调用theano.function(inputs, outputs)时,输出只能是两种情况:

1)一个Theano变量 2) 无变量列表

(2)不允许您在顶层列表中有一个列表,因此您应该在输出中展平列表。这将返回2个以上的输出。在

解决问题的一个可行的解决方案是将内部列表复制到1个变量中。在

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

这要求纳布拉·维的所有元素都是相同的形状。这将在计算图中添加一个额外的副本(因此可能会稍微慢一点)。在

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

更新2:

到现在为止,我们添加了一个约束,即所有元素都将具有不同的形状,因此不能使用堆栈。如果它们都具有相同数量的维度和数据类型,则可以使用typed_list,否则您将需要自己修改ano或展平输出的列表。在

相关问题 更多 >