Theano:函数内部张量变量的形状

Theano: Shape of a tensor variable inside a function

假设我有这个 theano 函数,它定义了神经网络的一层:

def layer(W, x):
    b = np.array(np.ones(N))  # append 1 for intercept
    newx = T.concatenate([x, T.stack(b)], 0)
    return T.dot(W, newx)

我将输入数据矩阵 x 的末尾(即 b 向量)连接起来,然后再通过 W 矩阵。问题是我需要为 b 指定一个长度。数据矩阵有 N 列,但假设我们事先不知道。

我找不到如何在 Matlab 中正确地执行相当于 size(x, 2) 的 theano。我知道 x 此时是一个符号变量,但我找不到如何在函数内找到张量变量的形状。

您可以尝试使用 theano.tensor.shape to get the shape of the object in question, or, more directly, you can use theano.tensor.ones_like

Theano 使用 "type" 了解对象的形状(参见 Tensor basics

所以x.type.dtype、x.type.ndim、x.type.broadcastable,给你一组很好的关于张量变量的类型和形状的信息。