Mars Tensor¶
Mars tensor 能从 numpy ndarray 创建,或者从外部文件读取。
从 numpy ndarray 创建 Mars tensor。
>>> import mars.tensor as mt
>>> import numpy as np
>>> t = mt.tensor(np.random.rand(4, 4))
读取 HDF5 文件以创建 Mars tensor。
>>> import mars.tensor as mt
>>> t = mt.from_hdf5('t.hdf5', dataset='t')
参考 创建 Tensor 和 从外部文件读取 获取更多信息。
Mars tensor 用起来和 numpy 非常相似,区别在于 Mars tensor 是延迟执行的。需要调用 .execute()
来得到最终结果。
记住,.execute()
会返回 Mars tensor 本身。
>>> (t - (t + 1).sum()).execute()
array([[-23.06773811, -22.86112123, -23.03988405, -22.48884341],
[-22.54959727, -22.13498645, -22.97627675, -23.09852276],
[-23.11085224, -22.63999173, -22.27187961, -22.34163038],
[-22.40633932, -22.17864095, -23.04577731, -22.76189835]])
参考 tensor API 获取更多实现的 tensor 接口。
一旦一个 tensor 执行,可以调用 .fetch()
将结果转换为 numpy 多维数组。.to_numpy()
是 .execute().fetch()
的快捷用法。
>>> t.to_numpy()
array([[0.06386055, 0.27047743, 0.09171461, 0.64275525],
[0.5820014 , 0.99661221, 0.15532191, 0.0330759 ],
[0.02074642, 0.49160693, 0.85971905, 0.78996828],
[0.72525934, 0.95295771, 0.08582136, 0.36970032]])
>>> type(t.execute())
mars.tensor.core.Tensor
>>> type(t.execute().fetch())
numpy.ndarray
>>> t.execute().fetch()
array([[0.06386055, 0.27047743, 0.09171461, 0.64275525],
[0.5820014 , 0.99661221, 0.15532191, 0.0330759 ],
[0.02074642, 0.49160693, 0.85971905, 0.78996828],
[0.72525934, 0.95295771, 0.08582136, 0.36970032]])
注解
应当优先考虑使用 .execute()
而不是 .to_numpy()
,因为当 tensor 很大的时候,.execute()
只会获取角上的数据来展示。而 .to_numpy()
会在服务端生成完整的数组,然后返回到客户端,这非常低效,而且很有可能导致内存溢出。
使用 mars.execute()
来执行多个 tensor。
>>> import mars
>>> import mars.tensor as mt
>>> z = mt.zeros((3, 3))
>>> t = mt.ones((3, 3))
>>> mars.execute(z, t)
(array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]),
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]))
Tensor 能被保存到外部文件如 HDF5。
>>> import mars.tensor as mt
>>> mt.to_hdf5('my.hdf5', mt.random.rand(3, 3), dataset='r').execute()
array([], shape=(0, 0), dtype=float64)
参考 写到外部文件 了解如何保存 tensor 到外部文件。