卷积神经网络(LeNet) 讨论区


#21

好奇 gluon.data.DataLoader() 与 utils.DataLoader() 主要区别在哪里?
尝试用 gluon.data.DataLoader() 来生成 data_iterator, 始终失败,改成utils.DataLoader(),训练成功


#23

求问,我尝试用之前讲过的自定义类的方式来实践训练,代码如下

class MLP(gluon.nn.Block):
   def __init__(self, **kwargs):
       super(MLP, self).__init__(**kwargs)
       self.net = gluon.nn.Sequential()
       with self.name_scope():
           self.net.add(gluon.nn.Conv2D(channels=20, kernel_size=5, activation='relu'))
           self.net.add(gluon.nn.MaxPool2D(strides=2))
           self.net.add(gluon.nn.Conv2D(channels=50, kernel_size=3, activation='relu'))
           self.net.add(gluon.nn.MaxPool2D(strides=2))
           self.net.add(gluon.nn.Flatten())
           self.dense0 = gluon.nn.Dense(128, activation='relu')
           self.dense1 = gluon.nn.Dense(10)

       def forward(self, x):
           return self.dense1(self.desne0(self.net(x)))

但是,却报错

Traceback (most recent call last):
  File "C:/Users/HuChen/PycharmProjects/卷积神经网络/convolution_03.py", line 93, in <module>
    output = net(data)
  File "C:\Users\HuChen\AppData\Local\Continuum\anaconda3\envs\gluon\lib\site-packages\mxnet\gluon\block.py", line 304, in __call__
    return self.forward(*args)
  File "C:\Users\HuChen\AppData\Local\Continuum\anaconda3\envs\gluon\lib\site-packages\mxnet\gluon\block.py", line 316, in forward
    raise NotImplementedError
NotImplementedError

是什么原因呢?


#24

self.net.name-scope()


#25

不行呀,改成如下的样子

class MLP(gluon.nn.Block):
    def __init__(self, **kwargs):
        super(MLP, self).__init__(**kwargs)
        self.net = gluon.nn.Sequential()
        with self.net.name_scope():
            self.net.add(gluon.nn.Conv2D(channels=20, kernel_size=5,
                                         activation='relu'))
            self.net.add(gluon.nn.MaxPool2D(strides=2))
            self.net.add(gluon.nn.Conv2D(channels=50, kernel_size=3,
                                         activation='relu'))
            self.net.add(gluon.nn.MaxPool2D(strides=2))
            self.net.add(gluon.nn.Flatten())
            self.dense0 = gluon.nn.Dense(128, activation='relu')
            self.dense1 = gluon.nn.Dense(10)

        def forward(self, x):
            return self.dense1(self.desne0(self.net(x)))

#26

不行啊,你试过了?


#27

:joy:


#28

__init__函数里定义了forward()函数?


#29

:joy:,不好意思,我SB了


#30

代码如下:

import numpy as np
import mxnet as mx
from mxnet import gluon
from mxnet.gluon import nn
from mxnet import nd

data = np.random.normal(size=(5000, 1, 20))
label = np.random.randint(1, 6, size=(5000,))


net = nn.Sequential()
with net.name_scope():
    net.add(
        nn.Conv1D(channels=5, kernel_size=5, activation='relu'),
        nn.Flatten(),
        nn.Dense(10, activation="relu"),
        nn.Dense(5)
    )

ctx = mx.cpu()
net.initialize(ctx=ctx)
print(net(nd.array(data, ctx=ctx)))


batch_size = 5
dataset = gluon.data.ArrayDataset(data, label)
data_iter = gluon.data.DataLoader(dataset, batch_size, shuffle=True)
for x, y in data_iter:
    print(x)
    print(y)
    print(net(x))
    break

让我疑惑的是print(net(nd.array(data, ctx=ctx)))运行正常,而print(net(x))则会出错,请诸位解惑!
错误提示为:
mxnet.base.MXNetError: [17:53:03] c:\projects\mxnet-distro-win\mxnet-build\src\operator\./convolution-inl.h:630: Check failed: (*in_type)[i] == dtype (0 vs. 1) This layer requires uniform type. Expected 'float64' v.s. given 'float32' at 'weight'


#31

你看范例,都是先将图片的像素转换为 float32 的格式,你这里随机产生的数时 float64 的,所以它底下提示你类型错误


#32

那问题就在于,为什么print(net(nd.array(data, ctx=ctx)))能够运行正常?


#33

为什么我觉得Gluon训练的准确率普遍比自己手写的高啊


#34

utils不是沐神的代码么


#35

有盆友遇到这个问题么?
TypeError: initialize() got an unexpected keyword argument ‘force_reinit’


#36

type error 是值类型错误 错误信息显示了 是模型初始化initialize 值错了


#37

诶,我遇到了耶。查了一下发现是模型权重都是float32的,但是生成的数据却是float64才导致了这个问题。我把数据astype(np.float32)的也就好了。


#38

num_epochs = 5
learning_rate = 0.2

trainer = gluon.Trainer(net.collect_params(), ‘sgd’, {‘learning_rate’:learning_rate})
import time
start = time.time()
for epoch in range(1):
train_loss = 0.
train_acc = 0.

net_time = 0.
back_time = 0.
loss_time = 0.
trainer_time = 0.
evaluate_time =0.

for data, label in train_dataset:
    ss = time.time()
    with ag.record():
        output = net(data)
        loss = softmax_cross_entropy(output, label)
    ee = time.time()
    net_time += ee-ss
    back_s = time.time()
    loss.backward()
    back_e = time.time()
    back_time += back_e - back_s

    trainer_s = time.time()
    trainer.step(batch_size)
    trainer_e = time.time()
    trainer_time += trainer_e - trainer_s

    loss_s = time.time()
    train_loss += loss.mean().asscalar()
    loss_e = time.time()
    loss_time += loss_e - loss_s

    evaluate_s = time.time()
    train_acc += accuracy(output, label)
    evaluate_e = time.time()
    evaluate_time += evaluate_e - evaluate_s

    test_evaluate_time_s = time.time()   
test_acc = evaluate_accuracy(test_dataset, net)
test_evaluate_time_e = time.time()


print("  net_time:%f" % net_time)
print("  back_time:%f"% back_time)
print("  trainer_time: %f" % trainer_time)
print("  loss_time: %f" % loss_time)
print("  evaluate_time:%f"%evaluate_time)
print("  test evaluate_time: %f" % (test_evaluate_time_e-test_evaluate_time_s))

print(" Epoch %d. Loss: %f, Train acc %f, Test acc %f" %(
    epoch, train_loss/len(train_dataset), train_acc/len(train_dataset), test_acc))

end = time.time()
print(" Costs %.2f seconds" % (end-start))

运行结果:
net_time:0.469062
back_time:0.381536
trainer_time: 0.239618
loss_time: 34.754270
evaluate_time:0.129269
test evaluate_time: 2.616012
Epoch 0. Loss: 0.152286, Train acc 0.943412, Test acc 0.911133
Costs 38.77 seconds

这里的时间计算哪里出了问题,loss.mean()不可能花那么长时间;如何准确知道每个部分用了多长时间的


#39

而且将 计算loss的函数注释掉之后, 计算精度的时间就变成了 30多秒到底是什么问题


#40

求助,代码与课程中基本一致,只是把数据集换成了手写字体mnist,但是一直报错,是使用gpu的问题吗?
from mxnet.gluon.data.vision.datasets import MNIST
import numpy as np
import mxnet as mx
from mxnet import autograd, gluon, init, nd
from mxnet.gluon import loss as gloss, nn
import time

net = nn.Sequential()
net.add(nn.Conv2D(channels=6, kernel_size=5, activation=‘sigmoid’),
nn.MaxPool2D(pool_size=2, strides=2),
nn.Conv2D(channels=16, kernel_size=5, activation=‘sigmoid’),
nn.MaxPool2D(pool_size=2, strides=2),
# Dense 会默认将(批量大小,通道,高,宽)形状的输入转换成
# (批量大小,通道 宽)形状的输入。
nn.Dense(120, activation=‘sigmoid’),
nn.Dense(84, activation=‘sigmoid’),
nn.Dense(10))

X = nd.random.uniform(shape=(1, 1, 28, 28))
net.initialize()
for layer in net:
X = layer(X)
print(layer.name, ‘output shape:\t’, X.shape)

batch_size = 64
train = MNIST(train=True, transform=lambda data, label: (data.astype(np.float32)/255, label)) # 训练集
train_iter = gluon.data.DataLoader(train, batch_size=batch_size, num_workers=0)
test = MNIST(train=False, transform=lambda data, label: (data.astype(np.float32)/255, label)) # 测试集
test_iter = gluon.data.DataLoader(test, batch_size=batch_size, num_workers=0)

def try_gpu4():
try:
ctx = mx.gpu()
_ = nd.zeros((1,), ctx=ctx)
except mx.base.MXNetError:
ctx = mx.cpu()
return ctx

ctx = try_gpu4()
print(ctx)

def accuracy(y_hat, y): # -------------------------------------------------------------------
return (y_hat.argmax(axis=1) == y.astype(‘float32’)).mean().asscalar()

def evaluate_accuracy(data_iter, net, ctx):
acc = nd.array([0], ctx=ctx)
for X, y in data_iter:
# 如果 ctx 是 GPU,将数据复制到 GPU 上。
X, y = X.as_in_context(ctx), y.as_in_context(ctx)
acc += accuracy(net(X), y)
return acc.asscalar() / len(data_iter)

def train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx,
num_epochs):
print(‘training on’, ctx)
loss = gloss.SoftmaxCrossEntropyLoss()
for epoch in range(num_epochs):
train_l_sum, train_acc_sum, start = 0, 0, time.time()
for X, y in train_iter:
X, y = X.as_in_context(ctx), y.as_in_context(ctx)
with autograd.record():
y_hat = net(X)
l = loss(y_hat, y)
l.backward()
trainer.step(batch_size)
train_l_sum += l.mean().asscalar()
train_acc_sum += accuracy(y_hat, y)
test_acc = evaluate_accuracy(test_iter, net, ctx)
print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, ’
‘time %.1f sec’ % (epoch + 1, train_l_sum / len(train_iter),
train_acc_sum / len(train_iter),
test_acc, time.time() - start))

lr, num_epochs = 0.9, 5
net.initialize(force_reinit=True, ctx=ctx, init=init.Xavier())
trainer = gluon.Trainer(net.collect_params(), ‘sgd’, {‘learning_rate’: lr})
train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx, num_epochs)

另外使用gpu的时候会停止
finished with exit code -1073740791 (0xC0000409)
强制使用cpu的时候就会
报错如下
mxnet.base.MXNetError: Shape inconsistent, Provided = [6,1,5,5], inferred shape=(6,28,5,5)
卡了两个礼拜了,真的不知道该怎么解决,谢谢了


#41
from mxnet.gluon.data.vision.datasets import MNIST


batch_size = 256
transformer = gdata.vision.transforms.ToTensor()
mnist_train = MNIST(train=True) # 训练集
mnist_test = MNIST(train=False) # 测试集
train_iter = gdata.DataLoader(mnist_train.transform_first(transformer),
                              batch_size, shuffle=True,
                              num_workers=0)
test_iter = gdata.DataLoader(mnist_test.transform_first(transformer),
                             batch_size, shuffle=False,
                             num_workers=0)