[英]Slicing a matrix in the givens argument of a theano function
I have the following piece of code, in which I attempt to apply PCA to the MNIST dataset. 我有以下代码,尝试将PCA应用于MNIST数据集。
X_train, y_train = mnist.data[:60000] / 255., mnist.target[:60000]
X_train, y_train = shuffle(X_train, y_train)
X_train, y_train = X_train[:5000], y_train[:5000]
pca = PCA(M=2)
X = T.matrix('X', dtype='float64')
i = T.scalar()
j = T.scalar()
# Theano function which fits the model to the
# data i.e. applies dimensionality reduction
transform = theano.function(
inputs=[i, j],
outputs=pca.transform(X),
givens={
X: X_train[(y_train == i) + (y_train == j)]
}
)
X_transformed = transform(i, j)
y_ = y_train[(y_train == i) + (y_train == j)]
As can be seen in the code above, I try to substitute X
with a slice of the training dataset in the givens
params. 如可以在上面的代码中可以看出,我试图取代X
与训练数据集在片givens
PARAMS。 The above code, however, results in the following error: 但是,上面的代码导致以下错误:
TypeError: Cannot convert Type TensorType(float64, vector) (of Variable <TensorType(float64, vector)>) into Type TensorType(float64, matrix). You can try to manually convert <TensorType(float64, vector)> into a TensorType(float64, matrix).
Which implies that I am trying to assign a vector to a matrix, which isn't the behaviour I would expect (I doubled checked using numpy). 这意味着我正在尝试将向量分配给矩阵,这不是我期望的行为(我使用numpy进行了两次检查)。 I also tried a different approach, whereby I index the training dataset X_train with the array of booleans directly instead of using indices are performing the slices my self, but that also didn't work. 我还尝试了另一种方法,即我直接使用布尔数组对训练数据集X_train进行索引,而不是使用索引来执行我自己的切片,但这也没有用。
X_train, y_train = mnist.data[:60000] / 255., mnist.target[:60000]
X_train, y_train = shuffle(X_train, y_train)
X_train, y_train = X_train[:5000], y_train[:5000]
pca = PCA(M=2)
X = T.matrix('X', dtype='float64')
bool_arr = T.vector()
# Theano function which fits the model to the
# data i.e. applies dimensionality reduction
transform = theano.function(
inputs=[i, j],
outputs=pca.transform(X),
givens={
X: X_train[bool_arr]
}
)
X_transformed = transform((y_train == i) + (y_train == j))
y_ = y_train[(y_train == i) + (y_train == j)]
Which gives the following error: 出现以下错误:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
The only approach that did work is using disregarding the givens
param, and using only the inputs and outputs, as such: 即没有工作的唯一方法是使用不顾givens
PARAM,并且仅使用输入和输出,因此:
X = T.matrix('X', dtype='float64')
# Theano function which fits the model to the
# data i.e. applies dimensionality reduction
transform = theano.function(
inputs=[X],
outputs=pca.transform(X),
)
X_ = X_train[(y_train == i) + (y_train == j)]
y_ = y_train[(y_train == i) + (y_train == j)]
X_transformed = transform(X_)
None the less, I feel curious as to why my first two approaches do not work, and would appreciate if any one could point me to where I might be going wrong, since I am just starting with Theano. 尽管如此,我对为什么我的前两种方法不起作用感到好奇,并希望知道是否有人可以指出我可能会出错的地方,因为我只是从Theano开始。
The theano documentation states: theano文档指出:
givens
(iterable over pairs(Var1, Var2)
of Variables. List, tuple or dict. TheVar1
andVar2
in each pair must have the same Type.) – specific substitutions to make in the computation graph (Var2
replacesVar1
).givens
(可在变量对(Var1, Var2)
进行迭代。列表,元组或字典。每对变量中的Var1
和Var2
必须具有相同的Type。)–在计算图中进行特定替换(Var2
替代Var1
)。
And in the tutorial examples , there is the statement (emphasis mine) 在教程示例中 ,有一个语句(强调我的意思)
In practice, a good way of thinking about the
givens
is as a mechanism that allows you to replace any part of your formula with a different expression that evaluates to a tensor of same shape and dtype . 在实践中,思考的好办法givens
是作为一种机制,使您可以用不同的表达式计算结果为相同的形状和D型张量替换公式的任何部分。
So, you cannot replace a matrix by a vector by means of the givens
parameter since they don't have the same shape. 所以,你无法通过的方式取代由矢量矩阵givens
的参数,因为它们不具有相同的形状。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.