python - Theano 多个张量作为输出

标签 python neural-network theano

我正在使用 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 或堆栈列表时遇到的错误:

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

最佳答案

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:

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

关于python - Theano 多个张量作为输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27064617/

相关文章:

python - 复制一组并排除一个项目

python - 在 selenium python 中通过 href 查找链接

java - 设置 Encog EarlyStoppingStrategy 参数的正确方法

pycuda vs theano vs pylearn2

python - 为幻方写一个类型契约(Contract)

python - 在 Python 中访问类变量的性能

machine-learning - 如何在 TensorFlow 中正确实现卷积的 dropout

python - 警告 :tensorflow :`write_grads` will be ignored in TensorFlow 2. 0 为 `TensorBoard` 回调

python - Keras - 将函数式 API 模型连接在一起

import - "import theano"运行需要多长时间?