def show_trace(res): n = max(abs(min(res)), abs(max(res)), 10) f_line = np.arange(-n, n, 0.1) util.set_figsize() util.plt.plot(f_line, [x * x for x in f_line]) # 显示f(x)=x*x的图像 util.plt.plot(res, [x * x for x in res], '-o') # 显示梯度下降图像 util.plt.xlabel('x') util.plt.ylabel('f(x)') util.plt.show()
def train_gluon_ch7(trainer_name, trainer_hyperparams, features, labels, batch_size=10, num_epochs=2): # 初始化模型 net = gluon.nn.Sequential() net.add(gluon.nn.Dense(1)) net.initialize(init.Normal(sigma=0.01)) loss = gluon.loss.L2Loss() def eval_loss(): return loss(net(features), labels).mean().asscalar() ls = [eval_loss()] data_iter = gluon.data.DataLoader(gluon.data.ArrayDataset( features, labels), batch_size, shuffle=True) # 创建Trainer实例来迭代模型参数 trainer = gluon.Trainer(net.collect_params(), trainer_name, trainer_hyperparams) start = time.time() for _ in range(num_epochs): for batch_i, (X, y) in enumerate(data_iter): with autograd.record(): l = loss(net(X), y) l.backward() trainer.step(batch_size) # 在Trainer实例中做梯度平均 if (batch_i + 1) * batch_size % 100 == 0: ls.append(eval_loss()) end = time.time() # 打印结果和作图 print('loss: %f, %f sec per epoch' % (ls[-1], end - start)) util.set_figsize() util.plt.plot(np.linspace(0, num_epochs, len(ls)), ls) util.plt.xlabel('epoch') util.plt.ylabel('loss') util.plt.show()
def train_ch7(trainer_fn, states, hyperparams, features, labels, batch_size=10, num_epochs=2): # 初始化模型 net, loss = util.linreg, util.squared_loss w = nd.random.normal(scale=0.01, shape=(features.shape[1], 1)) b = nd.zeros(1) w.attach_grad() b.attach_grad() def eval_loss(): return loss(net(features, w, b), labels).mean().asscalar() ls = [eval_loss()] data_iter = gluon.data.DataLoader(gluon.data.ArrayDataset( features, labels), batch_size, shuffle=True) start = time.time() for _ in range(num_epochs): for batch_i, (X, y) in enumerate(data_iter): with autograd.record(): l = loss(net(X, w, b), y).mean() # 使用平均损失 l.backward() trainer_fn([w, b], states, hyperparams) # 迭代模型参数 if (batch_i + 1) * batch_size % 100 == 0: ls.append(eval_loss()) # 每100个样本记录下当前训练误差 end = time.time() # 打印结果和作图 print('loss: %f, %f sec per epoch' % (ls[-1], end - start)) util.set_figsize() util.plt.plot(np.linspace(0, num_epochs, len(ls)), ls) util.plt.xlabel('epoch') util.plt.ylabel('loss') util.plt.show()
X = batch.data[0].as_in_context(ctx) Y = batch.label[0].as_in_context(ctx) with autograd.record(): # ⽣成多尺度的锚框,为每个锚框预测类别和偏移量 anchors, cls_preds, bbox_preds = net(X) # 为每个锚框标注类别和偏移量 bbox_labels, bbox_masks, cls_labels = contrib.nd.MultiBoxTarget( anchors, Y, cls_preds.transpose((0, 2, 1))) # 根据类别和偏移量的预测和标注值计算损失函数 l = calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels, bbox_masks) l.backward() trainer.step(batch_size) acc_sum += cls_eval(cls_preds, cls_labels) n += cls_labels.size mae_sum += bbox_eval(bbox_preds, bbox_labels, bbox_masks) m += bbox_labels.size if (epoch + 1) % 5 == 0: print( 'epoch %2d, class err %.2e, bbox mae %.2e, time %.1f sec' % (epoch + 1, 1 - acc_sum / n, mae_sum / m, time.time() - start)) # 预测 img = image.imread('../img/pikachu.jpg') feature = image.imresize(img, 256, 256).astype('float32') X = feature.transpose((2, 0, 1)).expand_dims(axis=0) output = predict(X) util.set_figsize((5, 5)) display(img, output, threshold=0.3)
np.set_printoptions(2) img = image.imread('../img/catdog.jpg').asnumpy() h, w = img.shape[0:2] print(h, w) # 生成锚框变量Y的形状(批量大小,锚框个数,4) X = nd.random.uniform(shape=(1, 3, h, w)) Y = contrib.nd.MultiBoxPrior(X, sizes=[0.75, 0.5, 0.25], ratios=[1, 2, 0.5]) print(Y.shape) boxes = Y.reshape((h, w, 5, 4)) print(boxes[250, 250, 0, :]) util.set_figsize() bbox_scale = nd.array((w, h, w, h)) fig = util.plt.imshow(img) show_bboxes(fig.axes, boxes[250, 250, :, :] * bbox_scale, [ 's=0.75,r=1', 's=0.5,r=1', 's=0.25,r=1', 's=0.75,r=2', 's=0.75,r=0.5' ]) util.plt.show() ground_truth = nd.array([[0, 0.1, 0.08, 0.52, 0.92], [1, 0.55, 0.2, 0.9, 0.88]]) anchors = nd.array([[0, 0.1, 0.2, 0.3], [0.15, 0.2, 0.4, 0.4], [0.63, 0.05, 0.88, 0.98], [0.66, 0.45, 0.8, 0.8], [0.57, 0.3, 0.92, 0.9]]) fig = util.plt.imshow(img) show_bboxes(fig.axes, ground_truth[:, 1:] * bbox_scale, ['dog', 'cat'],
from utils import util from mpl_toolkits import mplot3d import numpy as np def f(x): return x * np.cos(np.pi * x) def f2(x): return x**3 if __name__ == '__main__': # 局部最小值,全局最小值 util.set_figsize((4.5, 2.5)) x = np.arange(-1.0, 2.0, 0.1) fig, = util.plt.plot(x, f(x)) fig.axes.annotate('local minimum', xy=(-0.3, -0.25), xytext=(-0.77, -1.0), arrowprops=dict(arrowstyle='->')) fig.axes.annotate('global minimum', xy=(1.1, -0.95), xytext=(0.6, 0.8), arrowprops=dict(arrowstyle='->')) util.plt.xlabel('x') util.plt.ylabel('f(x)') util.plt.show() # 鞍点,曲线的鞍点