python-3.x - 如何用另一个大向量减去一个大向量?
问题描述
我想计算两个大尺寸向量之间的向量差。两者都是数据框的列。我能够将一个数组列表减去一个数组并将其置于 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).
就我可以逐行计算而言,我也在考虑迭代地进行这个向量减法,但我不知道如何在每次减法后将新行添加到数据帧中。
解决方案
推荐阅读
- r - R只覆盖文件的前几行
- javascript - 渲染后如何用 span 标签包装字符
- r - kable 在 blogdown 的 lapply 函数中生成格式错误的参考链接
- mvvm - Xamarin.Forms MVVM 如何在 ViewModel 中使用来自异步进程的数据加载可绑定的 Picker?
- typescript - 在 TypeScript 中,是否有一种安全的方式来编写 axiosResult.data.attendeeResults.username
- julia - 使用隐式欧拉和共轭梯度线性求解器求解非零 Dirichlet BC 的热方程?
- spring - SecurityContextHolder.getContext().getAuthentication() 从 @ControllerAdvice 类调用时返回 null
- python - 如何使用 for range 循环遍历 Python 中的两个列表?
- bash - 启用 bash-completion 会禁用自动完成文件名的默认行为
- node.js - Nodejs - 使用预签名的 url 从 s3 存储桶反应下载文件