首页 > 解决方案 > 如何用另一个大向量减去一个大向量?

问题描述

我想计算两个大尺寸向量之间的向量差。两者都是数据框的列。我能够将一个数组列表减去一个数组并将其置于 2 的幂

(train["quest_emb"][0] - train["sent_emb"][0])**2

但不要用数组列表的数据框列将其概括为数组的数据框:

train["quest_emb"] - train["sent_emb"]

因为它冻结了我的电脑。

阵列分析

这是他们的内容的一个例子。

>>> print((train["quest_emb"][2]))
[[0.03949683 0.04509903 0.01808935 ... 0.04610749 0.0416535  0.02240689]]

>>> print((train["sent_emb"][2]))
[array([0.03037658, 0.04433101, 0.08135635, ..., 0.06764812, 0.04971079,
       0.02240689], dtype=float32), array([0.05260669, 0.04548098, 0.0382337 , ..., 0.04823414, 0.07656007,
       0.03501297], dtype=float32), array([0.0502927 , 0.04480611, 0.02038252, ..., 0.03942193, 0.03132772,
       0.04595207], dtype=float32), array([0.06769167, 0.03393815, 0.0625218 , ..., 0.05555448, 0.03059104,
       0.03422254], dtype=float32)]

似乎有大小差异:

>>> print(len(train["quest_emb"][0]))
1
>>> print(len(train["sent_emb"][0]))
4

这是它们的类型,不同但在将一行减去另一行时似乎没有任何问题:

>>> print((train["quest_emb"][2][0]))
[0.03949683 0.04509903 0.01808935 ... 0.04610749 0.0416535  0.02240689]

>>> print((train["sent_emb"][2][0]))
[0.03037658 0.04433101 0.08135635 ... 0.06764812 0.04971079 0.02240689]

的长度train["quest_emb"]train["sent_emb"]: 130318相同

这是数组的类型

>>> print(type(train["quest_emb"][2]))
<class 'numpy.ndarray'>

>>> print(type(train["sent_emb"][2]))
<class 'list'>

有什么方法可以使具有 8G RAM 的计算机可以计算这种差异吗?或者如果不是一个近似的方式?

尝试使用 Theano

import theano.tensor as T
from theano import function
x = T.dscalar('x')
y = T.dscalar('y')
z = x - y
f = function([x, y], z)   
f(train["quest_emb"],train["sent_emb"])

但它给了我

ValueError: Bad input argument with name "quest_emb" to theano function with name "<ipython-input-41-c53eb459cbc4>:6" at index 0 (0-based).

就我可以逐行计算而言,我也在考虑迭代地进行这个向量减法,但我不知道如何在每次减法后将新行添加到数据帧中。

标签: python-3.xdataframemaththeano

解决方案


推荐阅读