paddle深度学习基础之训练调试与优化

2022-10-04 16:50:10 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

paddle深度学习基础之训练调试与优化

前言

上一节咱们讨论了四种不同的优化算法,这一节,咱们讨论训练过程中的优化问题。本次代码修改模型全是在卷积神经网络

文章目录
  • paddle深度学习基础之训练调试与优化
  • 前言
  • 网络结构
  • 优化思路
  • 一、计算模型的分类准确率
  • 二、检查模型训练过程
  • 三、加入校验或者测试,更好的评价模型
  • 四、加入正则化项,避免模型过拟合
    • 过拟合现象
    • 导致过拟合原因
    • 正则化项
    • 代码实现

  • 五、可视化库
    • **Matplotlib** 显示
    • tb-paddle

网络结构

优化思路

  1. 计算分类准确率,观测模型训练效果
  2. 检查模型训练过程,通过输出训练过程中的某些参数或者中间结果,识别潜在问题
  3. 加入校验或测试,更好评价模型效果
  4. 加入正则化项,避免模型过拟合
  5. 可视化分析

一、计算模型的分类准确率

通过计算训练的准确度,能够比较直接的反应模型的精准程度。

在paddle框架中,我们可以使用自带的准确率计算方法:

代码语言:javascript复制
fluit.layers.accuracy(prediction,lable)

第一个参数是预测值,第二个参数是实际标签值。下面是代码中需要修改的地方:

代码语言:javascript复制
    def forward(self, inputs,label):
conv1 = self.conv1(inputs)
pool1 = self.pool1(conv1)
conv2 = self.conb2(pool1)
pool2 = self.pool2(conv2)
pool2 = fluid.layers.reshape(pool2, [pool2.shape[0], -1])
outputs = self.linear(pool2)
if label is not None:#添加
acc = fluid.layers.accuracy(input=outputs,label=label)#添加
return outputs,acc
else:
return outputs

输出结果:

代码语言:javascript复制
epoch: 0, batch: 0, loss is: [2.796657], acc is [0.04]
epoch: 0, batch: 200, loss is: [0.50403804], acc is [0.88]
epoch: 0, batch: 400, loss is: [0.2659506], acc is [0.92]
epoch: 1, batch: 0, loss is: [0.22079289], acc is [0.92]
epoch: 1, batch: 200, loss is: [0.23240374], acc is [0.92]
epoch: 1, batch: 400, loss is: [0.16370663], acc is [0.95]
epoch: 2, batch: 0, loss is: [0.37291032], acc is [0.92]
epoch: 2, batch: 200, loss is: [0.23772442], acc is [0.92]
epoch: 2, batch: 400, loss is: [0.18071894], acc is [0.95]
epoch: 3, batch: 0, loss is: [0.15938215], acc is [0.95]
epoch: 3, batch: 200, loss is: [0.21112804], acc is [0.92]
epoch: 3, batch: 400, loss is: [0.05794979], acc is [0.99]
epoch: 4, batch: 0, loss is: [0.24466723], acc is [0.93]
epoch: 4, batch: 200, loss is: [0.14045799], acc is [0.96]
epoch: 4, batch: 400, loss is: [0.12366832], acc is [0.94]

二、检查模型训练过程

在我们训练模型时,时常会出现结果和我们预期有很大差距。此时,我们就想了解训练过程中数据的变化过程。恰巧,paddle深度学习框架支持这些功能,我们一起去看看如何做的:

代码语言:javascript复制
class MNIST(fluid.dygraph.Layer):
def __init__(self):
super(MNIST, self).__init__()
# self.linear1 = Linear(input_dim=28*28,output_dim=10,act=None)
# self.linear2 = Linear(input_dim=10,output_dim=10,act='sigmoid')
# self.linear3 = Linear(input_dim=10,output_dim=1,act='sigmoid')
self.conv1 = Conv2D(num_channels=1, num_filters=20, filter_size=5, stride=1, padding=2, act='relu')
self.pool1 = Pool2D(pool_size=2, pool_stride=2, pool_type='max')
self.conb2 = Conv2D(num_channels=20, num_filters=20, filter_size=5, stride=1, padding=2, act='relu')
self.pool2 = Pool2D(pool_size=2, pool_stride=2, pool_type='max')
self.linear = Linear(input_dim=980, output_dim=10, act='softmax')
def forward(self, inputs,label,check_shape=False,check_content=False):
conv1 = self.conv1(inputs)
pool1 = self.pool1(conv1)
conv2 = self.conb2(pool1)
pool2 = self.pool2(conv2)
pool21 = fluid.layers.reshape(pool2, [pool2.shape[0], -1])
outputs = self.linear(pool21)
# hidden1 = self.linear1(inputs)
# hidden2 = self.linear2(hidden1)
# outputs = self.linear3(hidden2)
if(check_shape):
print("n------------打印各个层设置的网络超参数的尺寸 -------------")
print("conv1-- kernel_size:{}, padding:{}, stride:{}".format(self.conv1.weight.shape, self.conv1._padding, self.conv1._stride))
print("conv2-- kernel_size:{}, padding:{}, stride:{}".format(self.conv2.weight.shape, self.conv2._padding, self.conv2._stride))
print("pool1-- pool_type:{}, pool_size:{}, pool_stride:{}".format(self.pool1._pool_type, self.pool1._pool_size, self.pool1._pool_stride))
print("pool2-- pool_type:{}, poo2_size:{}, pool_stride:{}".format(self.pool2._pool_type, self.pool2._pool_size, self.pool2._pool_stride))
print("liner-- weight_size:{}, bias_size_{}, activation:{}".format(self.fc.weight.shape, self.fc.bias.shape, self.fc._act))
print("n------------打印各个层的形状 -------------")
print("inputs_shape: {}".format(inputs.shape))
print("outputs1_shape: {}".format(conv1.shape))
print("outputs2_shape: {}".format(pool1.shape))
print("outputs3_shape: {}".format(conv2.shape))
print("outputs4_shape: {}".format(pool2.shape))
print("outputs5_shape: {}".format(outputs.shape))
if check_content:
# 打印卷积层的参数-卷积核权重,权重参数较多,此处只打印部分参数
print("n########## print convolution layer's kernel ###############")
print("conv1 params -- kernel weights:", self.conv1.weight[0][0])
print("conv2 params -- kernel weights:", self.conv2.weight[0][0])
# 创建随机数,随机打印某一个通道的输出值
idx1 = np.random.randint(0, conv1.shape[1])
idx2 = np.random.randint(0, conv1.shape[1])
# 打印卷积-池化后的结果,仅打印batch中第一个图像对应的特征
print("nThe {}th channel of conv1 layer: ".format(idx1), conv1[0][idx1])
print("The {}th channel of conv2 layer: ".format(idx2), conv1[0][idx2])
print("The output of last layer:", conv1[0], 'n')
if label is not None:
acc = fluid.layers.accuracy(input=outputs,label=label)
return outputs,acc
else:
return outputs

输出结果:

代码语言:javascript复制
------------打印各个层设置的网络超参数的尺寸 -------------
conv1-- kernel_size:[20, 1, 5, 5], padding:[2, 2], stride:[1, 1]
conv2-- kernel_size:[20, 20, 5, 5], padding:[2, 2], stride:[1, 1]
pool1-- pool_type:max, pool_size:[2, 2], pool_stride:[2, 2]
pool2-- pool_type:max, poo2_size:[2, 2], pool_stride:[2, 2]
liner-- weight_size:[980, 10], bias_size_[10], activation:softmax
------------打印各个层的形状 -------------
inputs_shape: [20, 1, 28, 28]
outputs1_shape: [20, 20, 28, 28]
outputs2_shape: [20, 20, 14, 14]
outputs3_shape: [20, 20, 14, 14]
outputs4_shape: [20, 20, 7, 7]
outputs5_shape: [20, 10]

三、加入校验或者测试,更好的评价模型

通常在我们获取数据集时,通常将数据集分成三个部分,分别是训练集,校验集,测试集。

  • 训练集 :用于训练模型的参数,即训练过程中主要完成的工作。
  • 校验集 :用于对模型超参数的选择,比如网络结构的调整、正则化项权重的选择等。
  • 测试集 :用于模拟模型在应用后的真实效果。因为测试集没有参与任何模型优化或参数训练的工作,所以它对模型来说是完全未知的样本。在不以校验数据优化网络结构或模型超参数时,校验数据和测试数据的效果是类似的,均更真实的反映模型效果。

四、加入正则化项,避免模型过拟合

过拟合现象

对于样本量有限、但需要使用强大模型的复杂任务,模型很容易出现过拟合的表现,即在训练集上的损失小,在验证集或测试集上的损失较大,如 图2 所示。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jGYNAyE0-1585745105788)(https://ai-studio-static-online.cdn.bcebos.com/e291cf2ea42947a7b671a71f862b96005270ef0f4b31478f9ef155581622b616)]

​ 图2:过拟合现象,训练误差不断降低,但测试误差先降后增

反之,如果模型在训练集和测试集上均损失较大,则称为欠拟合。过拟合表示模型过于敏感,学习到了训练数据中的一些误差,而这些误差并不是真实的泛化规律(可推广到测试集上的规律)。欠拟合表示模型还不够强大,还没有很好的拟合已知的训练样本,更别提测试样本了。因为欠拟合情况容易观察和解决,只要训练loss不够好,就不断使用更强大的模型即可,因此实际中我们更需要处理好过拟合的问题。

导致过拟合原因

造成过拟合的原因是模型过于敏感,而训练数据量太少或其中的噪音太多。

图3 所示,理想的回归模型是一条坡度较缓的抛物线,欠拟合的模型只拟合出一条直线,显然没有捕捉到真实的规律,但过拟合的模型拟合出存在很多拐点的抛物线,显然是过于敏感,也没有正确表达真实规律。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jNca1VU7-1585745105789)(https://ai-studio-static-online.cdn.bcebos.com/53ffb08a6a4a4e92ac2e622953dceda225b0452418fe4835863d1814bbe4f744)]

​ 图3:回归模型的过拟合,理想和欠拟合状态的表现

图4 所示,理想的分类模型是一条半圆形的曲线,欠拟合用直线作为分类边界,显然没有捕捉到真实的边界,但过拟合的模型拟合出很扭曲的分类边界,虽然对所有的训练数据正确分类,但对一些较为个例的样本所做出的妥协,高概率不是真实的规律。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7OfBRW1N-1585745105790)(https://ai-studio-static-online.cdn.bcebos.com/d3361a54e3524701ad993c2bb0e341d22c1277f575a7422798ed6fb7a4e5c626)]

​ 图4:分类模型的欠拟合,理想和过拟合状态的表现

正则化项

前面咱们提到,过拟合现象就是因为模型过于复杂,对数据太敏感。所以,我们在无法获取更多数据时,只能降低模型的复杂度。

具体来说,在模型的优化目标(损失)中人为加入对参数规模的惩罚项。当参数越多或取值越大时,该惩罚项就越大。通过调整惩罚项的权重系数,可以使模型在“尽量减少训练损失”和“保持模型的泛化能力”之间取得平衡。泛化能力表示模型在没有见过的样本上依然有效。正则化项的存在,增加了模型在训练集上的损失。

代码实现
代码语言:javascript复制
 optimizer = fluid.optimizer.AdamOptimizer(learning_rate=0.01, regularization=fluid.regularizer.L2Decay(regularization_coeff=0.1),
parameter_list=model.parameters())

主要是通过这一句:

代码语言:javascript复制
regularization=fluid.regularizer.L2Decay(regularization_coeff=0.1)

我们可以调节正则化项的权重,也就是regularization_coeff的值。

五、可视化库

训练模型时,经常需要观察模型的评价指标,分析模型的优化过程,以确保训练是有效的。可视化分析有两种工具:Matplotlib库和tb-paddle。

  • Matplotlib库:Matplotlib库是Python中使用的最多的2D图形绘图库,它有一套完全仿照MATLAB的函数形式的绘图接口,使用轻量级的PLT库(Matplotlib)作图是非常简单的。
  • tb-paddle:如果期望使用更加专业的作图工具,可以尝试tb-paddle。tb-paddle能够有效地展示飞桨框架在运行过程中的计算图、各种指标随着时间的变化趋势以及训练中使用到的数据信息。
Matplotlib 显示

通常,我们会把模型训练的的损失值和训练次数存进列表中,再通过matplotlib 相关函数进行可视化显示。

代码语言:javascript复制
 if batch_id % 100 == 0:
print("epoch: {}, batch: {}, loss is: {}, acc is {}".format(epoch_id, batch_id, avg_loss.numpy(), acc.numpy()))
iters.append(iter)
losses.append(avg_loss.numpy())
iter = iter   100
#画出训练过程中Loss的变化曲线
plt.figure()
plt.title("train loss", fontsize=24)
plt.xlabel("iter", fontsize=14)
plt.ylabel("loss", fontsize=14)
plt.plot(iters, losses,color='red',label='train loss') 
plt.grid()
plt.show()

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sizcOGq6-1585745105794)(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYkAAAEjCAYAAADHWv01AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1 /AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAIABJREFUeJzt3XeYlNXd//H3F1iWsgoqunTBHlsUcNFoEI0aOxpTNMYaRVNMHtErsaLxSSy/RH00 jxKbNHYkmgiKhY0rCWKCooFFEEhiqCiILB02O/vj3MPOzs7s0zbe3Z3Pq/rmmtm7rnLObOz85lzzl3M3REREUmnQ6kLICIirZdCQkREMlJIiIhIRgoJERHJSCEhIiIZKSRERCQjhYRICjO7y8zczC6PcZsjo23OjWubItnoVOoCiCSY2anAIOCf7j6ttKUREVBISOtyKrA/MBcoZUgsAGYCX5SwDCKtgkJCJIW7XwhcWOpyiLQGGpMQEZGMFBJScmZ2qpk5oasJ4M5oENdTB3MT85pZbfT8RDN7zsy jKYfE03vaGaHmdmtZjbVzD4zszVmNt/M/mFmBzZTnrQD12Y2KFGm6PmuZvaAmX1qZqvM7D0zu9TMOhf1DWrY/gFm9nC0vTXR/cbqsklUpqlmtizpPZhiZr83s13TLLO/mf3dzOZF8y8xs1lm9k8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45w5pkwa1b4Qn77bfjpT0N3T5cu6ZfZbLMQIJWVcNhh4df/OeeErqyzzw7z3HorzJwJf/hDaIGcf344 nvECDjxRHa8/voQUi 9FLqYknXtCn//O5xxRhj/GD067IIr6U2ZAvvvH8L02mvh9dfhlFPCkfcihVi HK65Br74onRlcPc2fRs6dKjna9KkSXkv22LWrs1 3tdec /WzR3cO3d2/9GP3F96yb2 vum877/v/t//7b7zzu7gHx933Ma3VV/vfsklYf3f/GZYd1u0fr377be777ijL/76190vvdT96afdly0rfN21te6bbOI aJD77Nlh2u9/H96zsWMLX3 RtMrPegzS1nvpUvfPPou9LHk57bTwWfrxj3NaLJu/NzDFs/iOLfmXfKG3dhcSuXruOffrrnP//PPsl6mry63ud9zhvtVW4eNy5JHu06blXMy8TZjg/uST S//8svue 0Vyj5smC/ZcUf3Dh3C844d3Wtq3M87z33mzNzX/fjj7l26uH/ta 7z5jVMr69v Oe 7778y15ERfmsf/KJ 403ui9YUPi6YtKk3h995L7NNu59 7rX1ZWkTFm7997wGRo0KHxmp0/PelGFhEKiYDnXfdky9yuvdO/ZM3xsfvAD9/fea3h96VL3qVPd77/f/Yor3M89N/zKXr8 vwLW14ftgXtFhfu//53b8vPnu598cli b1/3v/zFvb4 1Hvp0hA8F18cWkidO7tvuaX7jBnZr/ BB9w7dXIfMiR9QK9aFdZdWek eXJuZW8BBX/Wn346vEcQ6nTWWQ0tp1asUb0TAdG9e6jHFVeUrFwbNXt2aKHuu6/7p5 6b7qp 9FHZ724QkIhUbC86754cfhy7d49/LoZPty9T5/wUUq VVY2/Aq69FL3WbOy38bate5nntkQRttsE7aRzS/YtWvdr7nGvaoqfPlfeGGjbqW09Z450726OoTJBx80v/76evcbbnA3CyHw1VeZ51240H3w4LDujz5q vr69aEFsnr1xutVoLz/3uvWuV92WajvLru4P/VUCIjOncPf//jj3d94o5hFLaoN9U4ExKabur/yivuxx4bPSGvsdlq92n3YsPCDbO7cMC3xg mFF7JahUJCIVGwguv 2WfuY8aEL8rTTnO/6ir3v//d/a233FescF PPx6P/jg8AUD4VfRuHHuixZlXu/Spe6HHhrmv ii8EX65pvuXbu677ef 5o1mZddvjx0h4H7UUelDaaM9X77bffNNw hlu4LPbH H/0orH/UqPB8Y6ZPD19MX/ 6 223uf/61 ELatddQ1cVuG 7bWiFtaC8/t6ffur rW FMp58cuPumfnzQ1022SS8fuihoWuvlZk0aVLTgHAPreCOHd1/9rOSli t888P7 lDDzVMW748/FD6xjfSjzmmUEgoJAoWa90//jiEyE47 YZB9lGj3B98sPEX7bx57nvsEf55x41rvI777gvL/uIX6bfx5Zfu wTAul//zdjUZqt95Qp4Ytkhx3CF2SyDz4IX/RmYQeAXLrRnniiYRykosJ9xx1DmI0ZE8aT vcP78nNN2f1BZCz9ev95fvvD11sN9zg/pOfuB94YNju4MHuxxzjfvnl7v/8p/ucOaEMtbXuvXuHILv99szlWrw4/G0TXVFHHx1 KLQSLz34YNOASDj77NBl P77pSlcOk88Ed7Hs89u tq4ceG1f/xjo6tRSCgkClaSutfXu7/6ahivSHRRVVWFX d//nP40qqqCv8o6Zx7bljmnnsaT//oozB43LlzaM00Y6P1fuGFsMfYbruF4HEPX66bbRaa/48/nl1dU330UQiadHuULVzofvjhoW7f 17zXVjZ uKL8D59//vuPXp4o67AHj3CgP1JJ4XXd9ihobUHoZ4dOrhvv31oxWVj2TL33/0urNvM/cQTSz9m8dFHvqJv3/QB4R66L7t3D 95rpYtC Nkf/pT8UJxwYKwg8iuu4bWeKq1a8MPrZ122uieiQoJhUTBSl73devc//Uv9zPOaBgM79u3 f7tNWvc998/dD0l5ps PYTLppu6Z1GnrOo9cWIInL32cv/Nb8KX3m67teyX3vr1YSylY8f8up/q693fecf96qtDt1yi5VJd7X766f7emDFhT7hPP03fKqirC91Ft9wSfsWOGeO ZEnu9fjyS/cLLgh/o06dwvhF8p5fLWXlyrBL K23hm3W1LhXVvra7t3TB0TC2LHhfWpu54L6evdnngktph/8oGmogvtBB4U98fLdUWP9 rCOrl3D3zGTf/wjbO9Pf2p2dQoJhUTBWlXdV60KgZHNbryffurer1/oJnnssfALv3fvrHfLzbre48eHLzlwP GE HaXfPHFhu6niy92f SRMLCe7pfjnDmhK jEExvvPLDnnmFngVdf3fClFfvfe/5895/ NLyHXbqE8YvmxqLysX59aDnW1DT8rRItpQMOcB8zxl 9/fbm17F0afj1PmJE vD89NOGMbLEjhjHHBN PIwf7/7uuyE8 vYNr 0UwjabMar3ENX1 WXh CBpt2sqerrQ7dq377NbkMhoZAoWJuu TJ4UsU3Lfbzv3DD7NeNKd6P/WU 513tsw4QXMWLgx9 8m/VCsqQpfasceGQeTBgxteq64OQTZuXBj/SaNkf 8PPgghZhZajFdfnf0XaCb19eELeo89Gr6YL7rI/W9/C9tL ntlVe bbw7refTRxtMfeyyMtXTpEsZymgu51avDjhpDhoR1bbFF6M4bOzYcZzRpUgj1tWtDt9L11zccv2PmPnJbU4mKAAAI4UlEQVRk6HLN5rP2/PNhuSuvzDiLQkIhUbA2X/d77w2/6HLchbFN1Xvx4hCId90VunCOOSZ8IW61VXh8442ha6JIe7u0qGnTGsZd vYN3ULN7amWTn19CO6aGt wV9g994SuywyyqveaNWH8ZZddwrpWrAh7PYH77rs33/2TrozPPef ne EFmFqt1THjg1dgXvu6f6HP2QM9mYddVToYv3ii7QvFzMkOpXobCAihfnhD8OtPevZM5yld/jwUpekcF//Ojz eDjP2IUXhotnnXNOOCNynz6Nbz17wqpV4SSJK1eG xUrYMYMePnlcAGv226Dk08OlwguVEUFXHklfO97cPHF8OijYVvnnhumZzqHWjpm4fxoI0aE52vWwMcfhytXzpkT7jt3Dtv62tfyL/NVV8Huu4fyXXtt/uvJQqwhYWaHAjcAHYHb3P3qlNcrgbuBocCXwA/cfW6cZRSRFjRiRLgq44QJITAWLAi3WbPC80WLGs9fWRlOONmtW7gC5M03w49/HKYX03HHhTC 5poQXE8 Gc6SXKjOncO1YLbdtvB1JdtlF7jllnBRshYWW0iYWUfgZuBgYB7wmpmNd/cZSbP9GFjs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uzWvb0hAS7yggpLVTSIgUXwfgceD8NK99lvR4eTzFEcmfQkKkMGuAjinTXge D/zH3dfGXySR4tHAtUhh5gI1ZjbIzHpFA9M3E66A9qCZDTezbczsIDMbl7hwjEhboZAQKcwfCK2JGcBCYKC7zydcZrMeeJJwkZibgdXRTaTN0HESIiKSkVoSIiKSkUJCREQyUkiIiEhGCgkREclIISEiIhkpJEREJCOFhIiIZKSQEBGRjP4/FMStuIqsa7IAAAAASUVORK5CYII=)]

tb-paddle
  • 步骤1:引入tb_paddle库,定义作图数据存储位置(供第3步使用),本案例的路径是“log/data”。
代码语言:javascript复制
from tb_paddle import SummaryWriter
data_writer = SummaryWriter(logdir="log/data")
  • 步骤2:在训练过程中插入作图语句。当每100个batch训练完成后,将当前损失作为一个新增的数据点(scalar_x和loss的映射对)存储到第一步设置的文件中。使用变量scalar_x记录下已经训练的批次数,作为作图的X轴坐标。
代码语言:javascript复制
data_writer.add_scalar("train/loss", avg_loss.numpy(), scalar_x)
data_writer.add_scalar("train/accuracy", avg_acc.numpy(), scalar_x)
scalar_x = scalar_x   100
  • 步骤3:命令行启动 tensorboard。

使用“tensorboard –logdir [数据文件所在文件夹路径] 的命令启动Tensor board。在Tensor board启动后,命令行会打印出可用浏览器查阅图形结果的网址。

代码语言:javascript复制
$ tensorboard --logdir log/data
  • 步骤4:打开浏览器,查看作图结果,如 图6 所示。

查阅的网址在第三步的启动命令后会打印出来(如TensorBoard 2.0.0 at http://localhost:6006/),将该网址输入浏览器地址栏刷新页面的效果如下图所示。除了右侧对数据点的作图外,左侧还有一个控制板,可以调整诸多作图的细节。

注意:这里需要安装tensorboard,要不然无法打开这个这个。

令行会打印出可用浏览器查阅图形结果的网址。

代码语言:javascript复制
$ tensorboard --logdir log/data
  • 步骤4:打开浏览器,查看作图结果,如 图6 所示。

查阅的网址在第三步的启动命令后会打印出来(如TensorBoard 2.0.0 at http://localhost:6006/),将该网址输入浏览器地址栏刷新页面的效果如下图所示。除了右侧对数据点的作图外,左侧还有一个控制板,可以调整诸多作图的细节。

注意:这里需要安装tensorboard,要不然无法打开这个这个。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/196350.html原文链接:https://javaforall.cn

0 人点赞