Numpy ndarray乘法切换到矩阵乘法 [英] Numpy ndarray multiplication switching to matrix multiplication

查看:232
本文介绍了Numpy ndarray乘法切换到矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我得到的错误:

File "/data/eduardoj/linear.py", line 305, in _fit_model
        de_dl = (dl_dt + de_dt) * dt_dl
      File "/data/eduardoj/MSc-env/lib/python3.4/site-packages/numpy/matrixlib/defmatrix.py", line 343, in __mul__
        return N.dot(self, asmatrix(other))
    ValueError: shapes (1,53097) and (1,53097) not aligned: 53097 (dim 1) != 1 (dim 0)

这是numpy崩溃的那段代码:

And this is the piece of code of numpy where it is crashing:

   340     def __mul__(self, other):                                           
>* 341         if isinstance(other, (N.ndarray, list, tuple)) :                
   342             # This promotes 1-D vectors to row vectors                  
   343             return N.dot(self, asmatrix(other))                         
   344         if isscalar(other) or not hasattr(other, '__rmul__') :          
   345             return N.dot(self, other)                                   
   346         return NotImplemented  

(那里有一个断点>*)

在我的脚本中,我有一个包含以下行的循环:

In my script I have a loop containing the following line:

de_dl = (dloss_dt + dr_dt) * dt_dl

de_dldloss_dtdr_dtdt_dl的预期类型和形状为:

The expected types and shapes for de_dl, dloss_dt, dr_dt and dt_dl are:

ndarray float32 (1, 53097)

所以,我只想计算按元素的乘法.我正在使用pudb3调试脚本.我检查了第一次迭代(i == 0)的效果(最初产生零).我注意到,在第一次迭代中,线程没有达到了我设置的断点.在下一次迭代(i==1)中,我决定在调用乘法之前停止以确保dloss_dtdr_dtdt_dl的类型和形状仍然相同.他们是.

So, I just want to compute the element-wise multiplication. I am using pudb3 for debugging my script. I checked that in the first iteration (i == 0) it works great (initially producing zeros). I notice that for this first iteration the thread did NOT reach the break-point I set. In the next iteration (i==1), I decided to stopped right before to calling the multiplication just to make sure the type and shape of dloss_dt, dr_dt and dt_dl were the still the same. They were.

尽管它们是相同的,但似乎程序经历了一组不同的步骤,并且以这种方式结束了N.dot乘法.

Though they were the same, it seems that the program went through a different set of steps and some how ended in this N.dot multiplication.

因此,我想知道什么可能使我无法进行简单的按元素乘法运算的任何线索.

So, I am asking for any clue of what might be keeping me from operating just a simple element-wise multiplication.

推荐答案

问题中显示的numpy源代码片段与

The snippet of numpy source code you showed in your question corresponds to the __mul__ method of np.matrixlib.defmatrix.matrix.

当您将一个矩阵对象与另一个矩阵或数组右乘时调用此方法,即,如果A是矩阵,则A * B等效于A.__mul__(B)(B是否为ndarray或矩阵).

This method gets called when you right-multiply a matrix object with another matrix or array, i.e. A * B is equivalent to A.__mul__(B) if A is a matrix (it doesn't matter whether B is an ndarray or matrix).

可能唯一调用方法的方法是(dl_dt + de_dt)是矩阵(或其他派生的矩阵类)而不是ndarray.因此,dl_dtde_dt都将被强制转换为代码中某个矩阵.

The only way that method could possibly be called is if (dl_dt + de_dt) is a matrix (or some other derived matrix class) rather than an ndarray. Therefore, either dl_dt or de_dt is being cast to a matrix somewhere in your code.

由于在第一次迭代中未调用__mul__,因此必须在代码(de_dl = (dl_dt + de_dt) * dt_dl)的第305行之后的某个位置进行.

Since __mul__ is not called on the first iteration, this must occur somewhere after line 305 in your code (de_dl = (dl_dt + de_dt) * dt_dl).

这篇关于Numpy ndarray乘法切换到矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆