英文:
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
将x和y假定为字符串格式的数字(否则转换将出错),将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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论