是否可以通过使用其他延迟值指定其形状来从延迟值创建一个dask数组?
我的算法要等到计算的最后阶段才会给出数组的形状。
最终,我将创建一些块,其形状由计算的中间结果指定,最终在所有结果上调用da.concatenate
(如果更灵活,则调用da.block
)
如果不能的话,我认为这不会太有害,但如果可以的话,那会很酷。
示例代码
from dask import delayed
from dask import array as da
import numpy as np
n_shape = (3, 3)
shape = delayed(n_shape, nout=2)
d_shape = (delayed(n_shape[0]), delayed(n_shape[1]))
n = delayed(np.zeros)(n_shape, dtype=np.float)
# this doesn't work
# da.from_delayed(n, shape=shape, dtype=np.float)
# this doesn't work either, but I think goes a little deeper
# into the function call
da.from_delayed(n, shape=d_shape, dtype=np.float)
答案 0 :(得分:3)
您不能提供延迟的形状,但是您可以使用np.nan
作为不知道尺寸的位置的值来声明形状是未知的
import random
import numpy as np
import dask
import dask.array as da
@dask.delayed
def f():
return np.ones((5, random.randint(10, 20))) # a 5 x ? array
values = [f() for _ in range(5)]
arrays = [da.from_delayed(v, shape=(5, np.nan), dtype=float) for v in values]
x = da.concatenate(arrays, axis=1)
>>> x
dask.array<concatenate, shape=(5, nan), dtype=float64, chunksize=(5, nan)>
>>> x.shape
(5, np.nan)
>>> x.compute().shape
(5, 88)
请参见http://dask.pydata.org/en/latest/array-chunks.html#unknown-chunks