我有3个numpy阵列,需要在它们之间形成笛卡尔积.阵列的尺寸不固定,因此它们可以采用不同的值,一个例子可以是A =(10000,50),B =(40,50),C =(10000,50).
然后,我执行一些处理(如a + bc)以下是我用于产品的功能.
def cartesian_2d(arrays, out=None):
arrays = [np.asarray(x) for x in arrays]
dtype = arrays[0].dtype
n = np.prod([x.shape[0] for x in arrays])
if out is None:
out = np.empty([n, len(arrays), arrays[0].shape[1]], dtype=dtype)
m = n // arrays[0].shape[0]
out[:, 0] = np.repeat(arrays[0], m, axis=0)
if arrays[1:]:
cartesian_2d(arrays[1:], out=out[0:m, 1:, :])
for j in range(1, arrays[0].shape[0]):
out[j * m:(j + 1) * m, 1:] = out[0:m, 1:]
return out
a = [[ 0, -0.02], [1, -0.15]]
b …Run Code Online (Sandbox Code Playgroud) python numpy out-of-memory cartesian-product python-itertools
我想弄清楚为什么会这样:
In [1]: import time, h5py as h5
In [2]: f = h5.File('myfile.hdf5', 'r')
In [3]: st = time.time(); data = f["data"].value[0,:,1,...]; elapsed = time.time() - st;
In [4]: elapsed
Out[4]: 11.127676010131836
In [5]: st = time.time(); data = f["data"][0,:,1,...]; elapsed2 = time.time() - st;
In [6]: elapsed2
Out[6]: 59.810582399368286
In [7]: f["data"].shape
Out[7]: (1, 4096, 6, 16, 16, 16, 16)
In [8]: f["data"].chunks
Out[8]: (1, 4096, 1, 16, 16, 16, 16)
Run Code Online (Sandbox Code Playgroud)
如您所见,将整个数据集加载到内存中然后获取切片比从数据集中获取相同切片要快.
块大小与切片匹配,因此它应该都是连续的内存,对吧?为什么那么慢呢?
使用gzip(opts=2)压缩数据集.
在Andrew的评论之后,我运行它清除两个读取之间的缓存:
elapsed1: 11.001180410385132 …Run Code Online (Sandbox Code Playgroud) 我注意到,如果我使用h5py库而不是pytables库,则编写.h5文件的时间会更长。是什么原因?当阵列的形状以前已知时,也是如此。此外,我使用相同的块大小,没有压缩过滤器。
以下脚本:
import h5py
import tables
import numpy as np
from time import time
dim1, dim2 = 64, 1527416
# append columns
print("PYTABLES: append columns")
print("=" * 32)
f = tables.open_file("/tmp/test.h5", "w")
a = f.create_earray(f.root, "time_data", tables.Float32Atom(), shape=(0, dim1))
t1 = time()
zeros = np.zeros((1, dim1), dtype="float32")
for i in range(dim2):
a.append(zeros)
tcre = round(time() - t1, 3)
thcre = round(dim1 * dim2 * 4 / (tcre * 1024 * 1024), 1)
print("Time to append %d columns: %s sec (%s …Run Code Online (Sandbox Code Playgroud) 我正在尝试将瓶颈值保存到新创建的hdf5文件中。瓶颈值成批出现(120,10,10, 2048)。单独保存一个批处理将占用超过16个演出,而python似乎在冻结该批处理。根据最近的发现(请参阅更新,看来hdf5占用大内存是可以的,但是冻结的部分似乎是一个小故障。
我只是想保存前两批用于测试目的,而只保存训练数据集(再一次,这是一次测试运行),但是我什至不能超过第一批。它只会在第一批中停顿,并且不会循环到下一个迭代。如果我尝试检查hdf5,资源管理器将变慢,Python将冻结。如果我尝试杀死Python(即使不检查hdf5文件),Python也无法正确关闭,并且会强制重启。
以下是相关的代码和数据:
总数据点约为90,000 ish,分120个批次发布。
Bottleneck shape is (120,10,10,2048)
Run Code Online (Sandbox Code Playgroud)
所以我要保存的第一批是 (120,10,10,2048)
这是我尝试保存数据集的方式:
with h5py.File(hdf5_path, mode='w') as hdf5:
hdf5.create_dataset("train_bottle", train_shape, np.float32)
hdf5.create_dataset("train_labels", (len(train.filenames), params['bottle_labels']),np.uint8)
hdf5.create_dataset("validation_bottle", validation_shape, np.float32)
hdf5.create_dataset("validation_labels",
(len(valid.filenames),params['bottle_labels']),np.uint8)
#this first part above works fine
current_iteration = 0
print('created_datasets')
for x, y in train:
number_of_examples = len(train.filenames) # number of images
prediction = model.predict(x)
labels = y
print(prediction.shape) # (120,10,10,2048)
print(y.shape) # (120, 12)
print('start',current_iteration*params['batch_size']) # 0
print('end',(current_iteration+1) * params['batch_size']) # 120
hdf5["train_bottle"][current_iteration*params['batch_size']: (current_iteration+1) * params['batch_size'],...] …Run Code Online (Sandbox Code Playgroud)