当前位置:网站首页>[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.
边栏推荐
- WordPress建站技术笔记
- 《安富莱嵌入式周报》第276期:2022.07.25--2022.07.31
- "Avnet Embedded Weekly" Issue 276: 2022.07.25--2022.07.31
- AWS China SDN Connector
- 简易网络传输方法
- Optimal Power Flow (OPF) for High Voltage Direct Current (HVDC) (Matlab code implementation)
- TCP 可靠吗?为什么?
- 我在滴滴做开源
- DC-DC 2C(40W/30W) JD6606SX2退功率应用
- 泰山OFFICE技术讲座:段落边框的绘制难点在哪里?
猜你喜欢
ModelWhale 云端运行 WRF 中尺度数值气象模式,随时随地即开即用的一体化工作流
证实了,百度没有快照了
Essentially a database data recovery 】 【 database cannot read data recovery case
AI+BI+Visualization, Deep Analysis of Sugar BI Architecture
想进阿里?先来搞懂一下分布式事务
瞌睡检测系统介绍
Research on power flow in DC microgrid based on Newton's method (Matlab code implementation)
DataGrip:非常好用的数据库工具,安装与使用教程,亮点介绍
袁小林:沃尔沃专注于出行的安全感,并且把它做到极致
[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01
随机推荐
Introduction to spark learning - 1
【深度学习】今日bug(8月2)
I am doing open source in Didi
生态剧变,电子签名SaaS模式迎来新突破,网络效应加速到来
Small Tools (4) integrated Seata1.5.2 distributed transactions
新一代网状网协议T-Mesh无线通信技术优势介绍
STM32的HAL和LL库区别和性能对比
Reptile attention
Yuan xiaolin: Volvo focus on travel security, and put it perfectly
Neural networks, cool?
ECCV 2022 | 基于关系查询的时序动作检测方法
Optimal Power Flow (OPF) for High Voltage Direct Current (HVDC) (Matlab code implementation)
mysql delete execution error: You can't specify target table 'doctor_info' for update in FROM clause
Small Tools(4) 整合Seata1.5.2分布式事务
49 万奖金等你来拿!第四届实时计算 Flink 挑战赛启动,Beyond Stream Processing!
leetcode:899. 有序队列【思维题】
MPLS的wpn实验
语音识别新一轮竞争打响,自然对话会是下一个制高点吗?
为什么我强烈推荐使用智能化async?
土耳其国防部:联合协调中心将对首艘乌克兰粮船进行安全检查