使用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 分钟前
Kotlin抽象类与接口:相爱相杀的编程“CP”
android·开发语言·kotlin
2501_944448472 分钟前
数据可视化 Kotlin KMP OpenHarmony图表生成
开发语言·信息可视化·harmonyos
Arkerman_Liwei3 分钟前
Android 新开发模式深度实践:Kotlin + 协程 + Flow+MVVM
android·开发语言·kotlin
财经资讯数据_灵砚智能4 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年4月10日
人工智能·python·信息可视化·自然语言处理·ai编程
xinhuanjieyi7 分钟前
MCP分析某wordpress网站 时间所在的背景动画,并用php框架webman复刻下来
开发语言·php
jwn9998 分钟前
Laravel1.x:PHP框架的初心与革新
开发语言·php
蜡台10 分钟前
JavaScript async和awiat 使用
开发语言·前端·javascript·async·await
啦啦啦_999911 分钟前
0. Python进阶概要
python
蹦哒11 分钟前
Kotlin DSL 风格编程详解
android·开发语言·kotlin
枫叶丹413 分钟前
【HarmonyOS 6.0】ArkWeb 深度解读:getPageOffset20 与网页滚动偏移量获取能力的演进
开发语言·华为·harmonyos