def train_pytorch_ch7(optimizer_fn, optimizer_hyperparams, features, labels, batch_size=10, num_epochs=2): net = nn.Sequential(nn.Linear(features.shape[-1], 1)) loss = nn.MSELoss() optimizer = optimizer_fn(net.parameters(), **optimizer_hyperparams) def eval_loss(): return loss(net(features).view(-1), labels).item() / 2 ls = [eval_loss()] data_iter = torch.utils.data.DataLoader(torch.utils.data.TensorDataset( features, labels), batch_size, shuffle=True) for _ in range(num_epochs): start = time.time() for batch_i, (X, y) in enumerate(data_iter): l = loss(net(X).view(-1), y) / 2 optimizer.zero_grad() l.backward() optimizer.step() if (batch_i + 1) * batch_size % 100 == 0: ls.append(eval_loss()) print('loss: %f, %f sec per epoch' % (ls[-1], time.time() - start)) d2l.set_figsize() d2l.plt.plot(np.linspace(0, num_epochs, len(ls)), ls) d2l.plt.xlabel('epoch') d2l.plt.ylabel('loss') d2l.plt.show()
def train_ch7(optimizer_fn, states, hyperparams, features, labels, batch_size = 10, num_epochs = 2): net, loss = d2l.linreg, d2l.squared_loss w = torch.nn.Parameter(torch.tensor(np.random.normal(0, 0.01, size = (features.shape[1], 1)), dtype = torch.float32), requires_grad = True) b = torch.nn.Parameter(torch.zeros(1, dtype = torch.float32), requires_grad = True) def eval_loss(): return loss(net(features, w, b), labels).mean().item() ls = [eval_loss()] data_iter = torch.utils.data.DataLoader(torch.utils.data.TensorDataset(features, labels), batch_size, shuffle = True) for _ in range(num_epochs): start = time.time() for batch_i, (X, y) in enumerate(data_iter): l = loss(net(X, w, b), y).mean() if w.grad is not None: w.grad.data.zero_() b.grad.data.zero_() l.backward() optimizer_fn([w, b], states,hyperparams) if (batch_i + 1) * batch_size % 100 == 0: ls.append(eval_loss()) print ('loss: %f, %f sec per epoch' % (ls[-1], time.time() - start)) d2l.set_figsize() d2l.plt.plot(np.linspace(0, num_epochs, len(ls)), ls) d2l.plt.xlabel('epoch') d2l.plt.ylabel('loss') d2l.plt.savefig(str(batch_size) + '.jpg') d2l.plt.cla()
def xyplot(x_vals, y_vals, name): d2l.set_figsize(figsize=(5, 2.5)) d2l.plt.plot(x_vals.detach().numpy(), y_vals.detach().numpy()) #detach()返回一个从当前图中分离的variable d2l.plt.xlabel('x') d2l.plt.ylabel(name + '(x)') d2l.plt.savefig(name + ".jpg") d2l.plt.clf()
def semilogy(x_vals, y_vals, x_label, y_label, x2_vals=None, y2_vals=None, legend=None, figsize=(12, 8)): d2l.set_figsize(figsize) d2l.plt.xlabel(x_label) d2l.plt.ylabel(y_label) d2l.plt.semilogy(x_vals, y_vals) if x2_vals and y2_vals: d2l.plt.semilogy(x2_vals, y2_vals, linestyle=':') d2l.plt.legend(legend)
def show_trace(res): n = max(abs(min(res)), abs(max(res)), 10) f_line = np.arange(-n, n, 0.1) d2l.set_figsize() d2l.plt.plot(f_line, [x * x for x in f_line]) d2l.plt.plot(res, [x * x for x in res], '-o') d2l.plt.xlabel('x') d2l.plt.ylabel('f(x)') plt.show()
def semilogy(x_vals, y_vals, x_label, y_label, x2_vals = None, y2_vals = None, legend = None, figsize = (3.5, 2.5)):#作图函数 d2l.set_figsize(figsize) d2l.plt.xlabel(x_label) d2l.plt.ylabel(y_label) d2l.plt.semilogy(x_vals, y_vals) if x2_vals and y2_vals: d2l.plt.semilogy(x2_vals, y2_vals, linestyle = ':')#':'点直线 d2l.plt.legend(legend) d2l.plt.savefig("semilogy.jpg")
def display_anchors(fmap_w, fmap_h, s): d2l.set_figsize((3.5, 2.5)) fig = plt.imshow(img) feature_map_size = (fmap_h, fmap_w) # (h,w) anchors = d2l.MultiBoxPrior(feature_map_size, sizes=s, ratios=[1, 2, 0.5]) bbox_scale = torch.tensor([[w, h, w, h]], dtype=torch.float32) d2l.show_bboxes(fig.axes, anchors * bbox_scale) plt.savefig('multiscale_anchor_{}_{}.png'.format(fmap_h, fmap_w))
def main(): # 3.2.1 生成数据集 num_inputs = 2 num_examples = 1000 true_w = [2, -3.4] true_b = 4.2 features = torch.from_numpy( np.random.normal(0, 1, (num_examples, num_inputs))) labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b labels += torch.from_numpy(np.random.normal(0, 0.01, size=labels.size())) d2l.set_figsize() plt.scatter(features[:, 1].numpy(), labels.numpy(), 1) # plt.show() # 3.2.2 读取数据(在 d2lzh 包中已实现) data_iter = d2l.data_iter # 3.2.3 初始化模型参数 w = torch.tensor(np.random.normal(0, 0.01, (num_inputs, 1)), dtype=torch.float64) b = torch.zeros(1, dtype=torch.float64) w.requires_grad_(True) b.requires_grad_(True) # 3.2.4 定义模型(在 d2lzh 包中已实现) net = d2l.linreg # 3.2.5 定义损失函数(在 d2lzh 包中已实现) loss = d2l.squared_loss # 3.2.6 定义优化算法(在 d2lzh 包中已实现) sgd = d2l.sgd # 3.2.7 训练模型 batch_size = 10 lr = 0.03 num_epochs = 3 for epoch in range(num_epochs): for X, y in data_iter(batch_size, features, labels): batch_loss = loss(net(X, w, b), y).mean() # 小批量的 loss batch_loss.backward() # 反向传播,求梯度 sgd([w, b], lr) # 使用小批量随机梯度下降更新模型参数 # 不要忘了梯度清零 w.grad.data.zero_() b.grad.data.zero_() epoch_loss = loss(net(features, w, b), labels).mean() print("epoch %d, loss %f" % (epoch + 1, epoch_loss.item())) # 查看训练后的模型参数 print(w.data, b.data)
def display(img, output): d2l.set_figsize((5, 5)) fig = d2l.plt.imshow(img) h, w = img.shape[0:2] bbox_scale = torch.tensor([w, h, w, h], dtype=torch.float32) bbox_list = [] labels_list = [] for row in output: bbox_list.append(row[2:6] * bbox_scale) labels_list.append('%.2f' % row[1]) d2l.show_bboxes(fig.axes, bbox_list, labels_list, 'w') plt.savefig('pikachu_detect.png')
def semilogy(x_vals, y_vals, x_label, y_label, x2_vals=None, y2_vals=None, legend=None, figsize=(3.5, 2.5)): d2l.set_figsize(figsize) d2l.plt.xlabel(x_label) d2l.plt.ylabel(y_label) d2l.plt.semilogy(x_vals, y_vals) if x2_vals and y2_vals: d2l.plt.semilogy(x2_vals, y2_vals, linestyle=":") d2l.plt.legend(legend) d2l.plt.show()
def train1(net): image_shape = (150, 225) net = net.to(device) content_X, contents_Y = get_contents(image_shape, device) style_X, styles_Y = get_styles(image_shape, device) output = train(content_X, contents_Y, styles_Y, device, 0.01, 500, 200) d2l.plt.imshow(postprocess(output)) d2l.plt.show() image_shape = (300, 450) _, content_Y = get_contents(image_shape, device) _, style_Y = get_styles(image_shape, device) X = preprocess(postprocess(output), image_shape).to(device) big_output = train(X, content_Y, style_Y, device, 0.01, 500, 200) d2l.set_figsize((7, 5)) d2l.plt.imshow(postprocess(big_output)) d2l.plt.show()
Created on Sun Mar 29 17:22:32 2020 @author: Administrator """ import time import torch import torch.nn.functional as F import torchvision import numpy as np from PIL import Image import d2lzh_pytorch as d2l device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # 读取图片 d2l.set_figsize() content_img = Image.open('./small_dataset/rainier.jpg') # d2l.plt.imshow(content_img) style_img = Image.open('./small_dataset/autumn_oak.jpg') # ============================================================================= # torchvision.transforms模块有大量现成的转换方法,不过需要注意的 # 是有的方法输入的是PIL图像,如Resize;有的方法输入的是tensor,如 # Normalize;而还有的是用于二者转换,如ToTensor将PIL图像转换成tensor # ============================================================================= rgb_mean = np.array([0.485, 0.456, 0.406]) rgb_std = np.array([0.229, 0.224, 0.225]) # 转换尺寸,tensor转换,归一化转换(应用训练好的模型) def preprocess(PIL_img, image_shape):
def xyplot(x, y, name): d2l.set_figsize((5, 2.5)) plt.plot(x.detach().numpy(), y.detach().numpy()) plt.xlabel('x') plt.ylabel(name + '(x)') plt.show()
def train(net, train_iter, valida_iter, loss, optimizer, device, epochs=30, early_stopping=True, early_num=20): loss_list = [100] early_epoch = 0 net = net.to(device) print("training on ", device) start = time.time() train_loss_list = [] valida_loss_list = [] train_acc_list = [] valida_acc_list = [] for epoch in range(epochs): train_acc_sum, n = 0.0, 0 time_epoch = time.time() lr_adjust = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, 15, eta_min=0.0, last_epoch=-1) for X, y in train_iter: batch_count, train_l_sum = 0, 0 X = X.to(device) y = y.to(device) y_hat = net(X) l = loss(y_hat, y.long()) optimizer.zero_grad() l.backward() optimizer.step() train_l_sum += l.cpu().item() train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item() n += y.shape[0] batch_count += 1 lr_adjust.step(epoch) valida_acc, valida_loss = evaluate_accuracy(valida_iter, net, loss, device) loss_list.append(valida_loss) train_loss_list.append(train_l_sum) train_acc_list.append(train_acc_sum / n) valida_loss_list.append(valida_loss) valida_acc_list.append(valida_acc) print( 'epoch %d, train loss %.6f, train acc %.3f, valida loss %.6f, valida acc %.3f, time %.1f sec' % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, valida_loss, valida_acc, time.time() - time_epoch)) PATH = "./net_DBA.pt" if early_stopping and loss_list[-2] < loss_list[-1]: if early_epoch == 0: torch.save(net.state_dict(), PATH) early_epoch += 1 loss_list[-1] = loss_list[-2] if early_epoch == early_num: net.load_state_dict(torch.load(PATH)) break else: early_epoch = 0 d2l.set_figsize() d2l.plt.figure(figsize=(8, 8.5)) train_accuracy = d2l.plt.subplot(221) train_accuracy.set_title('train_accuracy') d2l.plt.plot(np.linspace(1, epoch, len(train_acc_list)), train_acc_list, color='green') d2l.plt.xlabel('epoch') d2l.plt.ylabel('train_accuracy') test_accuracy = d2l.plt.subplot(222) test_accuracy.set_title('valida_accuracy') d2l.plt.plot(np.linspace(1, epoch, len(valida_acc_list)), valida_acc_list, color='deepskyblue') d2l.plt.xlabel('epoch') d2l.plt.ylabel('test_accuracy') loss_sum = d2l.plt.subplot(223) loss_sum.set_title('train_loss') d2l.plt.plot(np.linspace(1, epoch, len(valida_acc_list)), valida_acc_list, color='red') d2l.plt.xlabel('epoch') d2l.plt.ylabel('train loss') test_loss = d2l.plt.subplot(224) test_loss.set_title('valida_loss') d2l.plt.plot(np.linspace(1, epoch, len(valida_loss_list)), valida_loss_list, color='gold') d2l.plt.xlabel('epoch') d2l.plt.ylabel('valida loss') d2l.plt.show() print('epoch %d, loss %.4f, train acc %.3f, time %.1f sec' % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, time.time() - start))
def xyplot(x_val, y_val, name): d2l.set_figsize(figsize=(5,2.5)) d2l.plt.plot(x_val.detach().numpy(), y_val.detach().numpy()) d2l.plt.xlabel('x') d2l.plt.ylabel(name+"(x)")
""" Created on Sat Mar 14 21:17:13 2020 目标检测和边界框 @author: as """ import sys sys.path.append("../../") import d2lzh_pytorch as d2l import numpy as np from matplotlib import pyplot as plt d2l.set_figsize((3.5, 2.5)) img = plt.imread('catdog.jpg') # bbox is the abbreviation for bounding box dog_bbox, cat_bbox = [60, 45, 378, 516], [400, 112, 655, 493] # has save to the d2l package. def bbox_to_rect(bbox, color): """Convert bounding box to matplotlib format. Convert the bounding box (top-left x, top-left y, bottom-right x, bottom-right y) format to matplotlib format: ((upper-left x, upper-left y), width, height) """ return plt.Rectangle(xy=(bbox[0], bbox[1]), width=bbox[2] - bbox[0], height=bbox[3] - bbox[1],
import d2lzh_pytorch as d2l from mpl_toolkits import mplot3d import numpy as np def f(x): return x * np.cos(np.pi * x) d2l.set_figsize((4.5, 2.5)) # x = np.arange(-1.0, 2.0, 0.1) # fig, = d2l.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 = '->')) d2l.plt.xlabel('x') d2l.plt.ylabel('f(x)') # d2l.plt.show() x = np.arange(-2.0, 2.0, 0.1) fig, = d2l.plt.plot(x, x ** 3) fig.axes.annotate('saddle point', xy = (0, -0.2), xytext = (-0.52, -5.0), arrowprops = dict(arrowstyle = '->')) d2l.plt.show()
import torch import sys sys.path.append("..") import d2lzh_pytorch as d2l img = Image.open('img/catdog.jpg') w, h = img.size # (728, 561) # 我们在5.1节(二维卷积层)中将卷积神经网络的二维数组输出称为特征图。 我们可以通过定义特征图的形状 # 来确定任一图像上均匀采样的锚框中心。下面定义display_anchors函数。我们在特征图fmap上以每个单元( # 像素)为中心生成锚框anchors。由于锚框anchors中x和y轴的坐标值分别已除以特征图fmap的宽和高,这些 # 值域在0和1之间的值表达了锚框在特征图中的相对位置。由于锚框anchors的中心遍布特征图fmap上的所有单 # 元,anchors的中心在任一图像的空间相对位置一定是均匀分布的。具体来说,当特征图的宽和高分别设为fmap_w # 和fmap_h时,该函数将在任一图像上均匀采样fmap_h行fmap_w列个像素,并分别以它们为中心生成大小为s(假 # 设列表s长度为1)的不同宽高比(ratios)的锚框。 d2l.set_figsize() def display_anchors(fmap_w, fmap_h, s): # 前两维的取值不影响输出结果(原书这里是(1, 10, fmap_w, fmap_h), 我认为错了) fmap = torch.zeros((1, 10, fmap_h, fmap_w), dtype=torch.float32) # 平移所有锚框使均匀分布在图片上 offset_x, offset_y = 1.0 / fmap_w, 1.0 / fmap_h anchors = d2l.MultiBoxPrior(fmap, sizes=s, ratios=[1, 2, 0.5]) + \ torch.tensor([offset_x/2, offset_y/2, offset_x/2, offset_y/2]) bbox_scale = torch.tensor([[w, h, w, h]], dtype=torch.float32) d2l.show_bboxes(d2l.plt.imshow(img).axes, anchors[0] * bbox_scale)
def train(net, train_iter, valida_iter, loss, optimizer, device, epochs=30, early_stopping=True, early_num=20): loss_list = [100] early_epoch = 0 net = net.to(device) print("training on ", device) start = time.time() train_loss_list = [] valida_loss_list = [] train_acc_list = [] valida_acc_list = [] for epoch in range(epochs): train_acc_sum, n = 0.0, 0 time_epoch = time.time() lr_adjust = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, 15, eta_min=0.0, last_epoch=-1) #lr_adjust = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=0.01, max_lr=0.1, step_size_up=2000, step_size_down=None, mode='triangular', gamma=1.0, scale_fn=None, scale_mode='cycle', cycle_momentum=True, base_momentum=0.8, max_momentum=0.9, last_epoch=-1) for X, y in train_iter: batch_count, train_l_sum = 0, 0 X = X.to(device) y = y.to(device) y_hat = net(X) # print('y_hat', y_hat) # print('y', y) l = loss(y_hat, y.long()) optimizer.zero_grad() l.backward() optimizer.step() train_l_sum += l.cpu().item() train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item() n += y.shape[0] batch_count += 1 lr_adjust.step() valida_acc, valida_loss = evaluate_accuracy(valida_iter, net, loss, device) loss_list.append(valida_loss) train_loss_list.append(train_l_sum) # / batch_count) train_acc_list.append(train_acc_sum / n) valida_loss_list.append(valida_loss) valida_acc_list.append(valida_acc) print( 'epoch %d, train loss %.6f, train acc %.3f, valida loss %.6f, valida acc %.3f, time %.1f sec' % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, valida_loss, valida_acc, time.time() - time_epoch)) PATH = "./net_DBA.pt" # if loss_list[-1] <= 0.01 and valida_acc >= 0.95: # torch.save(net.state_dict(), PATH) # break if early_stopping and loss_list[-2] < loss_list[ -1]: # < 0.05) and (loss_list[-1] <= 0.05): if early_epoch == 0: # and valida_acc > 0.9: torch.save(net.state_dict(), PATH) early_epoch += 1 loss_list[-1] = loss_list[-2] if early_epoch == early_num: net.load_state_dict(torch.load(PATH)) break else: early_epoch = 0 d2l.set_figsize() d2l.plt.figure(figsize=(8, 8.5)) train_accuracy = d2l.plt.subplot(221) train_accuracy.set_title('train_accuracy') d2l.plt.plot(np.linspace(1, epoch, len(train_acc_list)), train_acc_list, color='green') d2l.plt.xlabel('epoch') d2l.plt.ylabel('train_accuracy') # train_acc_plot = np.array(train_acc_plot) # for x, y in zip(num_epochs, train_acc_plot): # d2l.plt.text(x, y + 0.05, '%.0f' % y, ha='center', va='bottom', fontsize=11) test_accuracy = d2l.plt.subplot(222) test_accuracy.set_title('valida_accuracy') d2l.plt.plot(np.linspace(1, epoch, len(valida_acc_list)), valida_acc_list, color='deepskyblue') d2l.plt.xlabel('epoch') d2l.plt.ylabel('test_accuracy') # test_acc_plot = np.array(test_acc_plot) # for x, y in zip(num_epochs, test_acc_plot): # d2l.plt.text(x, y + 0.05, '%.0f' % y, ha='center', va='bottom', fontsize=11) loss_sum = d2l.plt.subplot(223) loss_sum.set_title('train_loss') d2l.plt.plot(np.linspace(1, epoch, len(valida_acc_list)), valida_acc_list, color='red') d2l.plt.xlabel('epoch') d2l.plt.ylabel('train loss') # ls_plot = np.array(ls_plot) test_loss = d2l.plt.subplot(224) test_loss.set_title('valida_loss') d2l.plt.plot(np.linspace(1, epoch, len(valida_loss_list)), valida_loss_list, color='gold') d2l.plt.xlabel('epoch') d2l.plt.ylabel('valida loss') # ls_plot = np.array(ls_plot) d2l.plt.show() print('epoch %d, loss %.4f, train acc %.3f, time %.1f sec' % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, time.time() - start))
def fusion_train_test(index_iter, MODEL_NAME, net, A_train_iter, B_train_iter, A_valida_iter, B_valida_iter, loss, optimizer, device, epochs, day_str): log = open("../log/%d_%s_log_%s.txt" % (index_iter, MODEL_NAME, day_str), 'a') loss_list = [100] early_epoch = 0 start = time.time() train_loss_list = [] valida_loss_list = [] train_acc_list = [] valida_acc_list = [] best_acc = 0.0 for epoch in range(epochs): # 交叉验证 train_acc_sum, n = 0.0, 0 time_epoch = time.time() lr_adjust = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, 15, eta_min=0.0, last_epoch=-1) print("--------------valid--------------") for dataA, dataB in tqdm(zip(A_valida_iter, B_valida_iter)): A_data = dataA[0].to(device) B_data = dataB[0].to(device) train_label = dataA[1].to(device) # (=dataB[1]) batch_count, train_l_sum = 0, 0 train_pre = net(A_data, B_data) _, pre = torch.max(train_pre, 1) ''' print("------------------------------------------------------------------") print(pre) input(train_label.int()) ''' l = loss(train_pre, train_label.long()) optimizer.zero_grad() l.backward() optimizer.step() train_l_sum += l.cpu().item() train_acc_sum += (train_pre.argmax( dim=1) == train_label).sum().cpu().item() n += train_label.shape[0] batch_count += 1 lr_adjust.step(epoch) valida_acc, valida_loss = evaluate_accuracy_fusion( A_train_iter, B_train_iter, net, loss, device) loss_list.append(valida_loss) # 绘图部分 train_loss_list.append(train_l_sum) # / batch_count) train_acc_list.append(train_acc_sum / n) valida_loss_list.append(valida_loss) valida_acc_list.append(valida_acc) print( 'epoch %d, train loss %.6f, train acc %.3f, valida loss %.6f, valida acc %.3f, time %.1f sec' % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, valida_loss, valida_acc, time.time() - time_epoch)) log_epoch = 'epoch %d, train loss %.6f, train acc %.3f, valida loss %.6f, valida acc %.3f, time %.1f sec\n' % ( epoch + 1, train_l_sum / batch_count, train_acc_sum / n, valida_loss, valida_acc, time.time() - time_epoch) log.write(log_epoch) log.flush() PATH = "../trained_model/%s_%d_%s_%d_train1.pt" % ( MODEL_NAME, index_iter, day_str, epoch) # if loss_list[-1] <= 0.01 and valida_acc >= 0.95: # torch.save(net.state_dict(), PATH) # break if valida_acc > best_acc and epoch + 1 % 5 == 0: best_acc = valida_acc print("best_acc is %.6f, and save model successfully" % (best_acc)) torch.save(net.state_dict(), PATH) train_acc_sum, n = 0.0, 0 time_epoch = time.time() print("--------------train--------------") for dataA, dataB in tqdm(zip(A_train_iter, B_train_iter)): A_data = dataA[0].to(device) B_data = dataB[0].to(device) train_label = dataA[1].to(device) # (=dataB[1]) batch_count, train_l_sum = 0, 0 train_pre = net(A_data, B_data) _, pre = torch.max(train_pre, 1) ''' print("------------------------------------------------------------------") print(pre) print(train_label.int()) ''' l = loss(train_pre, train_label.long()) optimizer.zero_grad() l.backward() optimizer.step() train_l_sum += l.cpu().item() train_acc_sum += (train_pre.argmax( dim=1) == train_label).sum().cpu().item() n += train_label.shape[0] batch_count += 1 lr_adjust.step(epoch) valida_acc, valida_loss = evaluate_accuracy_fusion( A_valida_iter, B_valida_iter, net, loss, device) loss_list.append(valida_loss) # 绘图部分 train_loss_list.append(train_l_sum) # / batch_count) train_acc_list.append(train_acc_sum / n) valida_loss_list.append(valida_loss) valida_acc_list.append(valida_acc) print( 'epoch %d, train loss %.6f, train acc %.3f, valida loss %.6f, valida acc %.3f, time %.1f sec' % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, valida_loss, valida_acc, time.time() - time_epoch)) log_epoch = 'epoch %d, train loss %.6f, train acc %.3f, valida loss %.6f, valida acc %.3f, time %.1f sec\n' % ( epoch + 1, train_l_sum / batch_count, train_acc_sum / n, valida_loss, valida_acc, time.time() - time_epoch) log.write(log_epoch) log.flush() PATH = "../trained_model/%s_%d_%s_%d_train2.pt" % ( MODEL_NAME, index_iter, day_str, epoch) # if loss_list[-1] <= 0.01 and valida_acc >= 0.95: # torch.save(net.state_dict(), PATH) # break if valida_acc > best_acc and epoch + 1 % 5 == 0: best_acc = valida_acc print("best_acc is %.6f, and save model successfully" % (best_acc)) torch.save(net.state_dict(), PATH) d2l.set_figsize() d2l.plt.figure(figsize=(8, 8.5)) train_accuracy = d2l.plt.subplot(221) train_accuracy.set_title('train_accuracy') d2l.plt.plot(np.linspace(1, epoch, len(train_acc_list)), train_acc_list, color='green') d2l.plt.xlabel('epoch') d2l.plt.ylabel('train_accuracy') # train_acc_plot = np.array(train_acc_plot) # for x, y in zip(num_epochs, train_acc_plot): # d2l.plt.text(x, y + 0.05, '%.0f' % y, ha='center', va='bottom', fontsize=11) test_accuracy = d2l.plt.subplot(222) test_accuracy.set_title('valida_accuracy') d2l.plt.plot(np.linspace(1, epoch, len(valida_acc_list)), valida_acc_list, color='deepskyblue') d2l.plt.xlabel('epoch') d2l.plt.ylabel('test_accuracy') # test_acc_plot = np.array(test_acc_plot) # for x, y in zip(num_epochs, test_acc_plot): # d2l.plt.text(x, y + 0.05, '%.0f' % y, ha='center', va='bottom', fontsize=11) loss_sum = d2l.plt.subplot(223) loss_sum.set_title('train_loss') d2l.plt.plot(np.linspace(1, epoch, len(train_loss_list)), train_loss_list, color='red') d2l.plt.xlabel('epoch') d2l.plt.ylabel('train loss') # ls_plot = np.array(ls_plot) test_loss = d2l.plt.subplot(224) test_loss.set_title('valida_loss') d2l.plt.plot(np.linspace(1, epoch, len(valida_loss_list)), valida_loss_list, color='gold') d2l.plt.xlabel('epoch') d2l.plt.ylabel('valida loss') # ls_plot = np.array(ls_plot) d2l.plt.savefig('../log_fig/%s_%d_%s_fig.png' % (MODEL_NAME, index_iter, day_str)) print('epoch %d, loss %.4f, train acc %.3f, time %.1f sec' % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, time.time() - start)) log_result = '\nepoch %d, loss %.4f, train acc %.3f, time %.1f sec\n' % ( epoch + 1, train_l_sum / batch_count, train_acc_sum / n, time.time() - start) log.write(log_result) log.flush()
def xyplot(x_vals, y_vals, name): d2l.set_figsize(figsize=(12, 8)) d2l.plt.plot(x_vals.detach().numpy(), y_vals.detach().numpy()) d2l.plt.xlabel('x') d2l.plt.ylabel(name + '(x)')
import sys sys.path.append("..") import d2lzh_pytorch as d2l from mpl_toolkits import mplot3d # 三维画图 import numpy as np ''' def f(x): return x * np.cos(np.pi * x) d2l.set_figsize((4.5, 2.5)) x = np.arange(-1.0, 2.0, 0.1) fig, = d2l.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='->')) d2l.plt.xlabel('x') d2l.plt.ylabel('f(x)') # d2l.plt.show() ''' ''' d2l.set_figsize((5., 5.)) x = np.arange(-2.0, 2.0, 0.01) fig, = d2l.plt.plot(x, x**3) fig.axes.annotate('saddle point', xy =(0., 0.), xytext=(0.0,2.0), arrowprops=dict(arrowstyle='->')) d2l.plt.xlabel('x') d2l.plt.ylabel('f(x)') d2l.plt.show()