x, y = x.float().to(device), y.float().to(device)错误:’str’对象没有属性’float’。

huangapple go评论88阅读模式
英文:

x, y = x.float().to(device), y.float().to(device)AttributeError: 'str' object has no attribute 'float'

问题

在训练时遇到了这个问题。

我尝试将字符串转换为浮点数,但使用普通的浮点数函数失败了。

英文:
for epoch in range(intial_epoch, nb_epoch):
    model.train()
    for batch_ndx, (x,y) in enumerate(train_loader):
       
        x, y = x.float().to(device), y.float().to(device)
        pred = model(x)
        loss = criterion(pred, y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

while training giving this problem.

I tried to convert str to float but failed by normal float function.

答案1

得分: 1

xy假定为字符串格式的数字(否则转换将出错),将x.float()y.float()更改为float(x)float(y)

英文:

Supposing x and y are numbers in string format (or else the conversion will give error), change x.float() and y.float() to float(x) and float(y).

huangapple
  • 本文由 发表于 2023年6月15日 18:31:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定