使用new_zeros和zeros_like分别返回与输入相同尺寸/类型/device的tensor和ndarray

  • torch.Tensor.new_zeros(size, dtype=None, device=None)

返回尺寸为size的全为0的tensor,默认,返回的tensor与该tensor具有相同的dtype和device,可以用于在模型训练过程中创建新tensor,并保证该tensor在对应的device上

1)size: list或者tuple,定义输出tensor的尺寸

2)dtype:返回tensor的期望类型,默认与输入tensor具有相同的torch.dtype

3)device:返回tensor的期望device,默认与输入tensor具有相同的torch.device

  • 例如:
python 复制代码
tensor = torch.tensor((), dtype=torch.float64)
tensor.new_zeros((2, 3))
# tensor([[ 0.,  0.,  0.],
#         [ 0.,  0.,  0.]], dtype=torch.float64)
  • numpy.zeros_like(a, dtype=None, order='K', subok=True, shape=None)

返回与给定数组具有相同形状和类型的零数组。

a:输入的数组

dtype:输出数组的期望类型,默认与a相同

shape:输出数组的期望尺寸,默认与a相同

  • 例如:
python 复制代码
x = np.arange(6)
x = x.reshape((2, 3))
# x
# array([[0, 1, 2],
#        [3, 4, 5]])
np.zeros_like(x)
# array([[0, 0, 0],
#        [0, 0, 0]])
相关推荐
啊啊啊啊啊!!!!2 小时前
【c++】stack和queue的接口使用以及底层实现
开发语言·c++
oyguyteggytrrwwwrt2 小时前
3dgs渲染部分详细注释
python
松仔log3 小时前
Java中级——组合和继承
android·java·开发语言
天天爱吃肉82184 小时前
【重磅发布:拿下新超仁达代理权 】
大数据·人工智能·python·功能测试·汽车
(Charon)4 小时前
【C++】锁与原子操作(四):自旋锁的完整实现与性能优化
c语言·开发语言·c++
神王宝宝 王者小学4 小时前
面向领域驱动架构的查询实现方式
前端·python·架构
雨田言炎5 小时前
五、Qt控件使用说明大全
开发语言·qt
bu_shuo5 小时前
MATLAB中的meshgrid和ndgrid
开发语言·matlab
ningmengjing_5 小时前
Redis 从入门到实战:Python操作全攻略
数据库·redis·python
️学习的小王6 小时前
智能文档助手:基于RAG的本地化文档问答系统实战指南
人工智能·python·机器学习