学习速率、动量和准确度的三维图

时间:2021-07-28 23:44:17

I have a basic neural net that I have trained in Keras. I'm playing around with the effect of the learning rate and momentum term and I would like to plot a nice 3d graph to visualise the effect of learning rate and momentum on the accuracy.

我有一个基本的神经网络,我在Keras培训过。我在研究学习速率和动量项的影响我想画一个很好的3d图来显示学习速率和动量对准确度的影响。

I've managed to successfully plot a trisurf plot using the example code, however whenever I use my own data I run into errors. The examples seem to use numpy arrays of around 1000 values, whereas I only have about 6 different learning rate and momentum values, giving me numpy arrays of sizes 6, 6 and 36. When I try to plot the graph using these values, I get the following error:

我已经成功地使用示例代码绘制了一个trisurf图,但是每当我使用自己的数据时,我就会遇到错误。这些示例似乎使用了大约1000个值的numpy数组,而我只有大约6个不同的学习率和动量值,得到了大小为6、6和36的numpy数组。当我试图用这些值绘制图形时,我得到以下错误:

RuntimeError: Error in qhull Delaunay triangulation calculation: singular input data (exitcode=2)

运行时误差:qhull Delaunay三角测量计算中的误差:奇异输入数据(exitcode=2)

I'm not understanding this error message, and why it works on the example data, but not my own. Any suggestions?

我不理解这个错误消息,也不理解它为什么在示例数据上工作,但我自己不理解。有什么建议吗?

My code is as follows:

我的代码如下:

momentum_terms = np.array([0.00001,0.0001,0.001,0.01, 0.1, 1])
learning_rates = np.array([0.00001,0.0001,0.001,0.01, 0.1, 1])
train_accuracies = np.empty([36])
test_accuracies = np.empty([36])
for learning_rate in learning_rates:
    for momentum in momentum_terms:
        model = Sequential()
        model.add(Dense(18, activation='relu', input_shape = (2,)))
        model.add(Dense(18, activation='relu'))
        model.add(Dense(1, activation='sigmoid'))
        model.summary()

        model.compile(loss='binary_crossentropy',
                      optimizer=SGD(lr = learning_rate, momentum = momentum),
                      metrics=[binary_accuracy])

        history = model.fit(x_train, y_train,
                            batch_size=batch_size,
                            epochs=epochs,
                            verbose=1,
                            validation_data=(x_test, y_test))
        score = model.evaluate(x_test, y_test, verbose=0)
        np.append(train_accuracies, history.history['binary_accuracy'][-1] * 100)
        np.append(test_accuracies, history.history['val_binary_accuracy'][-1] * 100)

x = momentum_terms
y = learning_rates
z = test_accuracies

ax = plt.axes(projection='3d')
ax.plot_trisurf(x, y, z, cmap='viridis', edgecolor='none');
plt.show()

1 个解决方案

#1


2  

You are not providing enough data to generate a 3d plot (see this related SO question). Instead of passing 6, 6, and 36, you need to pass 36, 36, and 36. Redo your code so that you store each pair of the momentum terms and learning rate terms in your loops with your accuracy.

您没有提供足够的数据来生成3d图(请参阅相关的问题)。你需要通过36 36 36 36 36 36 36,而不是通过6 6 6 36。重新编写代码,以便在循环中准确地存储每一对动量项和学习速率项。

So you should have:

所以你应该:

x = [0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.0001, 0.0001, .... ] for 36 values total from learning rate choices

x =(0.00001,0.00001,0.00001,0.00001,0.00001,0.00001,0.0001,0.0001,....从学习率的选择中选出36个值

y = [0.00001,0.0001,0.001,0.01, 0.1, 1, 0.00001, 0.0001,0.001,0.01, 0.1, 1, .... ] for 36 values total from momentum choices

y =(0.00001,0.0001,0.001,0.00001,0.0001,1,0.00001,0.0001,0.001,0.01,0.1,1,....从动量的选择中得到36个值

z = array of 36 accuracies for each of the combination above

z =对上述每一个组合的36个精度的数组。

#1


2  

You are not providing enough data to generate a 3d plot (see this related SO question). Instead of passing 6, 6, and 36, you need to pass 36, 36, and 36. Redo your code so that you store each pair of the momentum terms and learning rate terms in your loops with your accuracy.

您没有提供足够的数据来生成3d图(请参阅相关的问题)。你需要通过36 36 36 36 36 36 36,而不是通过6 6 6 36。重新编写代码,以便在循环中准确地存储每一对动量项和学习速率项。

So you should have:

所以你应该:

x = [0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.00001, 0.0001, 0.0001, .... ] for 36 values total from learning rate choices

x =(0.00001,0.00001,0.00001,0.00001,0.00001,0.00001,0.0001,0.0001,....从学习率的选择中选出36个值

y = [0.00001,0.0001,0.001,0.01, 0.1, 1, 0.00001, 0.0001,0.001,0.01, 0.1, 1, .... ] for 36 values total from momentum choices

y =(0.00001,0.0001,0.001,0.00001,0.0001,1,0.00001,0.0001,0.001,0.01,0.1,1,....从动量的选择中得到36个值

z = array of 36 accuracies for each of the combination above

z =对上述每一个组合的36个精度的数组。