gpt4 book ai didi

python - 运行后memmap文件在哪里?

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:40 25 4
gpt4 key购买 nike

我有一个使用 numpy memmap 的矩阵乘法代码

def test_memmap():
fA = np.lib.format.open_memmap('A.npy', dtype='uint8', mode='r+')
fB = np.lib.format.open_memmap('B.npy', dtype='uint8', mode='r+')

#need to predefine size of result?
#fC = np.memmap('C.npy', dtype='uint16', mode='w+', shape=(rows,rows))

# print fA
# print fB

#where it stored if not gived filename?

t0= time.time()
fC= np.dot(fA,fB)
print (time.time()-t0)

print fC.filename
print type(fC)

# print fC[12:10]
# print fC

运行后memmap文件fC在哪?以及如何指定保存路径?

我如何指向保存 fC 的位置(而不是将其存储在内存中)?可以自动检测数组的类型和大小吗?

最佳答案

虽然fC是numpy.core.memmap.memmap的一个实例,它不与任何文件关联:

print(type(fC))
# <class 'numpy.core.memmap.memmap'>

print(fC._mmap)
# None

print(fC.filename)
# None

fC 是 memmap 实例的原因是因为 np.dot 与大多数 NumPy 函数一样,试图返回与其参数类型相同的数组。所以 memmap.__array_wrap__ method被调用。 __array_finalize__ method最后调用它,将 _mmapfilenameoffsetmode 属性设置为 None。

如果您使用像 pdb 这样的调试器单步执行代码,您可以看到 __array_finalize__ 被调用。

所以虽然fC是一个memmap实例,但它只存在于内存中。


计算 np.dot 并将结果写入 memmap:

fC = np.memmap('/tmp/C.npy', dtype='float', mode='w+', shape=(Arows, Bcols))
np.dot(fA,fB, out=fC)
print fC.filename
# /tmp/C.npy
print type(fC)
# <class 'numpy.core.memmap.memmap'>

关于python - 运行后memmap文件在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23518663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com