Python常见错误(1) 📅 2026/7/15 21:56:42 出现错误c torch.zeros()TypeError: zeros() received an invalid combination of arguments - got (), but expected one of:* (tuple of ints size, *, tuple of names names, torch.dtype dtype None, torch.layout layout None, torch.device device None, bool pin_memory False, bool requires_grad False)* (tuple of ints size, *, Tensor out None, torch.dtype dtype None, torch.layout layout None, torch.device device None, bool pin_memory False, bool requires_grad False)查看缘由摁住ctrl鼠标点击进入函数内可以看到是因为本身函数规定的需要一个sizesize是位置参数positional argument没有默认值解决方法c torch.zeros(n)#传递一个参数n必须传入张量的形状完整代码import math import time import numpy as np import torch from d2l import torch as d2l n1000 atorch.ones([n]) btorch.ones([n]) class Timer: 记录多次运行时间 def __init__(self): self.times [] self.start() def start(self): 计时器 self.tik time.time() def stop(self): 停止计时 self.times.append(time.time() - self.tik) return self.times[-1] def avg(self): 返回平均时间 return sum(self.times) / len(self.times) def sum(self): return sum(self.times) def cumsum(self): return np.array(self.times).cumsum().tolist() c torch.zeros(n) timer Timer() for i in range(n): c[i] a[i] b[i] print(fc{c})