当前位置:网站首页>[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.
边栏推荐
- Interpretation of the 2021 Cost of Data Breach Report
- Leetcode76. Minimal Covering Substring
- 《安富莱嵌入式周报》第276期:2022.07.25--2022.07.31
- Convex Optimization of Optimal Power Flow (OPF) in Microgrids and DC Grids (Matlab Code Implementation)
- leetcode-268.丢失的数字
- DataGrip:非常好用的数据库工具,安装与使用教程,亮点介绍
- Tolstoy: There are only two misfortunes in life
- 详谈RDMA技术原理和三种实现方式
- MarkDown常用代码片段和工具
- ruoyi若依框架@DataScope注解使用以及碰到的一些问题
猜你喜欢
深入浅出Flask PIN
Interpretation of the 2021 Cost of Data Breach Report
一文看懂推荐系统:召回02:Swing 模型,和itemCF很相似,区别在于计算相似度的方法不一样
【QT】Qt 给已经开发好的程序快速封装成动态库
甲方不让用开源【监控软件】?大不了我自己写一个
常见分布式理论(CAP、BASE)和一致性协议(Gosssip、Raft)
用友YonSuite与旺店通数据集成对接-技术篇2
How Navicat connects to MySQL on a remote server
DC-DC 2C (40W/30W) JD6606SX2 power back application
Yuan xiaolin: Volvo focus on travel security, and put it perfectly
随机推荐
Why do I strongly recommend using smart async?
ReentrantReadWriteLock详解
leetcode: 899. Ordered Queue [Thinking Question]
使用Make/CMake编译ARM裸机程序(基于HT32F52352 Cortex-M0+)
攻防世界----bug
Difference and performance comparison between HAL and LL library of STM32
49 万奖金等你来拿!第四届实时计算 Flink 挑战赛启动,Beyond Stream Processing!
【Unity入门计划】基本概念(8)-瓦片地图 TileMap 01
Awesome!Coroutines are finally here!Thread is about to be in the past
DC-DC 2C(40W/30W) JD6606SX2退功率应用
js中的基础知识点 —— 事件
Small Tools(4) 整合Seata1.5.2分布式事务
Neural networks, cool?
mysql delete 执行报错:You can‘t specify target table ‘doctor_info‘ for update in FROM clause
【899. Ordered Queue】
深入浅出Flask PIN
JD6606SP5_JD6606SSP_JD6606SASP_JD6621W7百盛新纪元授权代理商
深入浅出Flask PIN
leetcode:899. 有序队列【思维题】
红蓝对抗经验分享:CS免杀姿势