def main(): batch_size = 256 # 下载、读取数据集 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) num_inputs = 784 num_outputs = 10 W = nd.random.normal(scale=0.01, shape=(num_inputs, num_outputs)) b = nd.zeros(num_outputs) # 附上梯度,开辟梯度缓冲区 W.attach_grad() b.attach_grad() # 迭代周期数 学习率 num_epochs, lr = 5, 0.1 train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs, batch_size, num_inputs, W, b, [W, b], lr) for X, y in test_iter: print(X, y) break true_labels = d2l.get_fashion_mnist_labels(y.asnumpy()) pred_labels = d2l.get_fashion_mnist_labels( net(X, num_inputs, W, b).argmax(axis=1).asnumpy()) titles = [ true + '\n' + pred for true, pred in zip(true_labels, pred_labels) ] show_fashion_mnist(X[0:9], titles[0:9])
def test(self, test_iter, W, b): X, y = iter(test_iter).next() true_labels = d2l.get_fashion_mnist_labels(y.numpy()) pred_labels = d2l.get_fashion_mnist_labels( self.net(X, W, b).argmax(dim=1).numpy()) titles = [ true + '\n' + pred for true, pred in zip(true_labels, pred_labels) ] d2l.show_fashion_mnist(X[0:9], titles[0:9])
def Method0(): params = [w1, b1, w2, b2] for param in params: param.attach_grad() loss = gloss.SoftmaxCrossEntropyLoss() num_epochs, lr = 5, 0.5 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr) for x, y in test_iter: break true_labels = d2l.get_fashion_mnist_labels(y.asnumpy()) pre_labels = d2l.get_fashion_mnist_labels(net(x).argmax(axis=1).asnumpy()) titles = [ true + '\n' + pred for true, pred in zip(true_labels, pre_labels) ] d2l.show_fashion_mnist(x[0:9], titles[0:9])
def method1(): vnet = nn.Sequential() vnet.add(nn.Dense(10)) vnet.initialize(init.Normal(sigma=0.01)) loss = gloss.SoftmaxCrossEntropyLoss() trainer = gluon.Trainer(vnet.collect_params(), 'sgd', {'learning_rate': 0.1}) num_epochs = 5 d2l.train_ch3(vnet, train_iter, test_iter, loss, num_epochs, batch_size, None, None, trainer) for x, y in test_iter: break true_labels = d2l.get_fashion_mnist_labels(y.asnumpy()) pre_labels = d2l.get_fashion_mnist_labels(vnet(x).argmax(axis=1).asnumpy()) titles = [ true + '\n' + pred for true, pred in zip(true_labels, pre_labels) ] d2l.show_fashion_mnist(x[0:9], titles[0:9])
def method0(): #batch_size = 256 #train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) #num_inputs = 28 * 28 #num_outputs = 10 #w = nd.random.normal(scale=0.01, shape=(num_inputs, num_outputs)) #b = nd.zeros(num_outputs) #w.attach_grad() #b.attach_grad() c = evaluate_accuracy(train_iter, net, w, b, num_inputs) # print(c) num_epochs, lr = 5, 0.1 train_ch3(net, w, b, num_inputs, train_iter, test_iter, cross_entropy, num_epochs, batch_size, [w, b], lr) for x, y in test_iter: break true_labels = d2l.get_fashion_mnist_labels(y.asnumpy()) pre_labels = d2l.get_fashion_mnist_labels( net(x, w, b, num_inputs).argmax(axis=1).asnumpy()) titles = [ true + '\n' + pred for true, pred in zip(true_labels, pre_labels) ] d2l.show_fashion_mnist(x[0:9], titles[0:9])
def net(X): X = X.reshape((-1, num_inputs)) H = relu(nd.dot(X, W1) + b1) return nd.dot(H, W2) + b2 # In[7]: #损失函数 loss = gloss.SoftmaxCrossEntropyLoss() # In[8]: num_epochs, lr = 5, 0.5 #调用了3.6中的函数 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr) # In[9]: for X, y in test_iter: break 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]) # In[ ]:
def showPlt(self): X, y = self.info() d2l.show_fashion_mnist(X, d2l.get_fashion_mnist_labels(y))
import d2lzh as d2l from mxnet import gluon, init from mxnet.gluon import loss as gloss, nn bathsize = 256 trainer_iter, test_iter = d2l.load_data_fashion_mnist(bathsize) net = nn.Sequential() net.add(nn.Dense(10)) net.initialize(init.Normal(sigma=0.01)) loss = gloss.SoftmaxCELoss() trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.1}) num = 5 d2l.train_ch3(net, trainer_iter, test_iter, loss, num, bathsize, None, None, trainer) for x, y in test_iter: break truelabes = d2l.get_fashion_mnist_labels(y.asnumpy()) falselabes = d2l.get_fashion_mnist_labels(net(x).argmax(axis=1).asnumpy()) title = [true + '\n' + pred for true, pred in zip(truelabes, falselabes)] d2l.show_fashion_mnist(x[0:9], title[0:9])
print (d2l.evaluate_accuracy(test_iter, net)) num_epochs, lr = 5, 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() if trainer is None: d2l.sgd(params, lr, batch_size) else: trainer.step(batch_size) # “softmax回归的简洁实现”一节将用到 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 = d2l.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)) train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs, batch_size,[w, b], lr) 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])