简体   繁体   English

部分阅读大型numpy文件的有效方法?

[英]Efficient way to partially read large numpy file?

I have a huge numpy 3D tensor which is stored in a file on my disk (which I normally read using np.load ). 我有一个巨大的numpy 3D张量存储在我的磁盘上的文件中(我通常使用np.load读取)。 This is a binary .npy file. 这是一个二进制.npy文件。 On using np.load , I quickly end up using most of my memory. 在使用np.load ,我很快就会占用大部分内存。

Luckily, at every run of the program, I only require a certain slice of the huge tensor. 幸运的是,在程序的每次运行中,我只需要一定量的巨大张量。 The slice is of a fixed size and its dimensions are provided from an external module. 切片具有固定的尺寸,其尺寸由外部模块提供。

What's the best way to do this? 最好的方法是什么? The only way I could figure out is somehow storing this numpy matrix into a MySQL database. 我能弄清楚的唯一方法是以某种方式将这个numpy矩阵存储到MySQL数据库中。 But I'm sure there are much better / easier ways. 但我敢肯定有更好 /更简单的方法。 I'll also be happy to build my 3D tensor file differently if it will help. 我也很乐意以不同的方式构建我的3D张量文件,如果有帮助的话。


Does the answer change if my tensor is sparse in nature? 如果我的张量在本质上是稀疏的,答案会改变吗?

use numpy.load as normal, but be sure to specify the mmap_mode keyword so that the array is kept on disk, and only necessary bits are loaded into memory upon access. 正常使用numpy.load ,但一定要指定mmap_mode关键字,以便将数组保存在磁盘上,并且在访问时只将必要的位加载到内存中。

mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional If not None, then memory-map the file, using the given mode (see numpy.memmap for a detailed description of the modes). mmap_mode: {None,'r +','r','w +','c'},可选如果不是None,则使用给定模式对文件进行内存映射(有关模式的详细说明,请参阅numpy.memmap) )。 A memory-mapped array is kept on disk. 内存映射阵列保留在磁盘上。 However, it can be accessed and sliced like any ndarray. 但是,它可以像任何ndarray一样访问和切片。 Memory mapping is especially useful for accessing small fragments of large files without reading the entire file into memory. 内存映射对于访问大型文件的小片段而不将整个文件读入内存特别有用。

The modes are descirbed in numpy.memmap : 这些模式在numpy.memmap中numpy.memmap

mode : {'r+', 'r', 'w+', 'c'}, optional The file is opened in this mode: 'r' Open existing file for reading only. mode: {'r +','r','w +','c'},可选文件在此模式下打开:'r'打开现有文件以供阅读。 'r+' Open existing file for reading and writing. 'r +'打开现有文件进行读写。 'w+' Create or overwrite existing file for reading and writing. 'w +'创建或覆盖现有文件以进行读写。 'c' Copy-on-write: assignments affect data in memory, but changes are not saved to disk. 'c'Copy-on-write:赋值会影响内存中的数据,但更改不会保存到磁盘。 The file on disk is read-only. 磁盘上的文件是只读的。

*be sure to not use 'w+' mode, as it will erase your file's contents. *请务必不要使用'w +'模式,因为它会删除文件的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取大型二进制文件python的最有效方法是什么 - What is the most efficient way to read a large binary file python 从大型二进制文件读取数据的任何有效方法? - Any efficient way to read datas from large binary file? 读取大型 csv 文件中特定列的最有效方法 - Most efficient way to read a specific column in large csv file 循环遍历大型numpy数组的有效方法 - Efficient way to loop through large numpy arrays 将大型NumPy数组写入文件的有效方法 - Efficient ways to write a large NumPy array to a file 有没有更有效的方法将行从大文件追加到numpy数组? - MemoryError - Is there a more efficient way to append lines from a large file to a numpy array? - MemoryError 快速有效的从HDF5文件序列化和检索大量numpy数组的方法 - Fast and efficient way of serializing and retrieving a large number of numpy arrays from HDF5 file 读取包含存储为 numpy 数组的图像的 hdf5 文件的最有效方法是什么? - What is the most efficient way to read an hdf5 file containing an image stored as a numpy array? 将CSV文件处理为numpy数组的有效方法 - Efficient way to process CSV file into a numpy array 在Python中将numpy数组写入文件的有效方法 - Efficient way of writing numpy arrays to file in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM