当前位置:网站首页>[Deep Learning] Today's bug (August 2)
[Deep Learning] Today's bug (August 2)
2022-08-03 16:18:00 【O o o front】
前言
博主主页:阿阿阿阿锋的主页_CSDN
代码来源:《动手学深度学习》
Got an error message today:TypeError: 'method' object is not iterable
.
意思是:类型错误:“方法”对象不可迭代.
然后对mxnetThe understanding of automatic gradient calculation is a little clearer.
文章目录
一. TypeError: ‘method’ object is not iterable
1. 错误提示 && 部分代码
错误提示:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-40-9ae7d7a05a23> in <module>
4 # From numeric labels to text labels
5 true_labels = d2l.get_fashion_mnist_labels(y.asnumpy())
----> 6 pred_labels = d2l.get_fashion_mnist_labels(net(X).argmax(axis=1).asnumpy)
7 titles = [true + '\n' + pred for true, pred in zip(true_labels, pred_labels)]
8
D:\anaconda\lib\site-packages\d2lzh\utils.py in get_fashion_mnist_labels(labels)
183 text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat',
184 'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
--> 185 return [text_labels[int(i)] for i in labels]
186
187
TypeError: 'method' object is not iterable
Usually the error message is still very useful,It can effectively help us locatebug的位置.
部分代码段:
for X, y in test_iter:
break
# From numeric labels to text labels
true_labels = d2l.get_fashion_mnist_labels(y.asnumpy())
pred_labels = d2l.get_fashion_mnist_labels(net(X).argmax(axis=1).asnumpy)
titles = [true + '\n' + pred for true, pred in zip(true_labels, pred_labels)]
d2l.show_fashion_mnist(X[0:9], titles[0:9])
2. 消灭bug
I am looking at this error,当时就懵了,I didn't understand what it meant.A closer inspection of the code later found that,It turned out to be calling a function
asnumpy
时,函数名后面的()
掉了.于是Originally I wanted to call the function,recognized as an object,passed as a parameter to another function,Then a further type error was thrown(TypeError
).
plus missing
()
后,一切正常.程序跑起来了:
二. 自动求梯度,Find the value of the function?
代码:
num_epochs = 3
lr = 0.1
# 用于训练模型
def train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params=None, lr=None, trainer=None):
for epoch in range(num_epochs):
train_l_sum, train_acc_sum, n = 0.0, 0.0, 0
for X, y in train_iter:
with autograd.record():
y_hat = net(X)
l = loss(y_hat, y).sum()
l.backward()
d2l.sgd(params, lr, batch_size)
y = y.astype('float32')
train_l_sum += l.asscalar()
train_acc_sum += (y_hat.argmax(axis=1) == y).sum().asscalar()
n += y.size
test_acc = evaluate_accuracy(test_iter, net)
print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f' % (epoch + 1, train_l_sum / n, train_acc_sum / n, test_acc))
I've been a little confused about this code,主要在
train_l_sum += l.asscalar()
这一语句.Variables are used herel
的值,Used to calculate the loss of the model on the training set.但是l
where does the value come from?
我们看下面这段代码:
%matplotlib inline
import d2lzh as d2l
from mxnet import gluon, autograd, nd
X = nd.array([2, 3, 4])
X.attach_grad()
with autograd.record():
y = X ** 2
# y.backward()
y, X.grad
输出:
原来在使用mxnetin the process of automatically finding the gradient,在
y = X ** 2
这一步,It has already been requestedy的值.It is more than just a function expression that requires derivation,It is also an assignment statement.
小结
Spend a lot of time on some small mistakes,真的划不来.
Don't do it again next time马虎了啊.
There are often questions,想明白了之后,I just feel so stupid before.
边栏推荐
- DC-DC 2C (40W/30W) JD6606SX2 power back application
- 在 360 度绩效评估中应该问的 20 个问题
- 如何将二维空间先验注入到ViT中? UMA&港理工&阿里提出SP-ViT,为视觉Transformer学习2D空间先验知识!...
- 基于DMS的数仓智能运维服务,知多少?
- window.open不显示favicon.icon
- 面了个腾讯35k出来的,他让我见识到什么叫精通MySQL调优
- 土耳其国防部:联合协调中心将对首艘乌克兰粮船进行安全检查
- I am doing open source in Didi
- 【899. Ordered Queue】
- 如何分析周活跃率?
猜你喜欢
开源一夏 | 阿里云物联网平台之极速体验
美国国防部更“青睐”光量子系统研究路线
A new round of competition for speech recognition has started. Will natural dialogue be the next commanding height?
How to start an NFT collection
小熊派——无线联网开发
我写了个”不贪吃蛇“小游戏
DataGrip数据仓库工具
CS免杀姿势
如何将二维空间先验注入到ViT中? UMA&港理工&阿里提出SP-ViT,为视觉Transformer学习2D空间先验知识!...
Fortinet产品导入AWS AMI操作文档
随机推荐
JD6606SP5_JD6606SSP_JD6606SASP_JD6621W7百盛新纪元授权代理商
【深度学习】今日bug(8月2)
[Code Hoof Set Novice Village 600 Questions] Define a function as a macro
posgresql 到 es 报这个错误 ,啥意思
[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window
[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 02
Leetcode76. Minimal Covering Substring
GTK实现旋转加载动画
产品以及研发团队有使用专业的办公软件,如禅道、蓝湖等,他们应该如何使用 Tita 系统?
I am doing open source in Didi
Yuan xiaolin: Volvo focus on travel security, and put it perfectly
Essentially a database data recovery 】 【 database cannot read data recovery case
Go Go 简单的很,标准库之 fmt 包的一键入门
ReentrantReadWriteLock详解
Convex Optimization of Optimal Power Flow (OPF) in Microgrids and DC Grids (Matlab Code Implementation)
Optimal Power Flow (OPF) for High Voltage Direct Current (HVDC) (Matlab code implementation)
leetcode-268.丢失的数字
Difference and performance comparison between HAL and LL library of STM32
ffplay视频播放原理分析
瞌睡检测系统介绍