我正在训练一个简单的机器学习模型,该模型接受一个物理系统的一维描述(502个元素)并预测总能量(1个元素)。由于我是TensorFlow的新手,我使用了一个包含两个隐藏层(每个隐藏层有64个神经元)的简单密集神经网络:
Model: "total_energy"_________________________________________________________________Layer (type) Output Shape Param # =================================================================charge_density_x_max (InputL [(None, 502)] 0 _________________________________________________________________hidden_1 (Dense) (None, 64) 32192 _________________________________________________________________hidden_2 (Dense) (None, 64) 4160 _________________________________________________________________dense (Dense) (None, 1) 65 =================================================================Total params: 36,417Trainable params: 36,417Non-trainable params: 0_________________________________________________________________
这是我用于训练、评估和预测的源代码:
# importsimport osimport astimport numpy as npimport pandas as pdimport tensorflow as tfimport matplotlib.pyplot as plt# load the dataset from the csv filedata = pd.read_csv('1e_data.csv')# load in the datax_train = np.zeros(shape=(600, 502))x_test = np.zeros(shape=(400, 502))y_train = np.zeros(shape=(600))y_test = np.zeros(shape=(400))for i in range(0, 1000): if i < 600: x_train[i,:] = np.append(np.array(ast.literal_eval(data.loc[i,'n'])), float(data.loc[i,'xmax'])) y_train[i] = float(data.loc[i,'E']) else: x_test[i-600,:] = np.append(np.array(ast.literal_eval(data.loc[i,'n'])), float(data.loc[i,'xmax'])) y_test[i-600] = float(data.loc[i,'E'])# build the neural network modelinputs = tf.keras.Input(shape=(502,), name='charge_density_x_max')hidden1 = tf.keras.layers.Dense(64, activation='sigmoid', name='hidden_1')(inputs)hidden2 = tf.keras.layers.Dense(64, activation='sigmoid', name='hidden_2')(hidden1)outputs = tf.keras.layers.Dense(1)(hidden2)model = tf.keras.Model(inputs=inputs, outputs=outputs, name='total_energy')# save the info of the modelwith open('model_info.dat','w') as fh: model.summary(print_fn=lambda x: fh.write(x + '\n'))# compile the modelmodel.compile(optimizer='adam', loss='mean_absolute_percentage_error', metrics=['accuracy'])# perform the trainingmodel.fit(x_train, y_train, epochs=10)# evaluate the model for accuracymodel.evaluate(x_test, y_test, verbose=2)
然而,当我运行这段代码时,似乎根本没有进行训练,准确率为0.0000e+00:
Epoch 1/10 600/600 [==============================] - 0s 196us/sample - loss: 289.0616 - acc: 0.0000e+00 Epoch 2/10 600/600 [==============================] - 0s 37us/sample - loss: 144.5967 - acc: 0.0000e+00 Epoch 3/10 600/600 [==============================] - 0s 46us/sample - loss: 97.2109 - acc: 0.0000e+00 Epoch 4/10 600/600 [==============================] - 0s 46us/sample - loss: 108.0698 - acc: 0.0000e+00 Epoch 5/10 600/600 [==============================] - 0s 47us/sample - loss: 84.5921 - acc: 0.0000e+00 Epoch 6/10 600/600 [==============================] - 0s 38us/sample - loss: 79.9309 - acc: 0.0000e+00 Epoch 7/10 600/600 [==============================] - 0s 38us/sample - loss: 80.6755 - acc: 0.0000e+00 Epoch 8/10 600/600 [==============================] - 0s 47us/sample - loss: 87.5954 - acc: 0.0000e+00 Epoch 9/10 600/600 [==============================] - 0s 46us/sample - loss: 73.6634 - acc: 0.0000e+00 Epoch 10/10 600/600 [==============================] - 0s 38us/sample - loss: 78.0825 - acc: 0.0000e+00 400/400 - 0s - loss: 70.3813 - acc: 0.0000e+00
我可能犯了一个简单的错误,但我不知道如何开始调试。这应该至少进行一些训练,但目前看来它似乎只是跳过了训练并给出了0的准确率。
回答:
您正处于一个回归设置中,准确率是毫无意义的(它仅在分类问题中有意义);有关更多详细信息,请参见当损失函数为均方误差(MSE)时,Keras中的准确率是如何定义的?(尽管您使用了不同的损失函数,但这同样适用于您的情况)。
从损失的减少可以明显看出您的网络确实在学习,这是在回归问题中真正关注的量(您在这里根本不需要任何metrics
)。
独立于上述情况,您可能应该将sigmoid
激活函数改为relu
(我们现在通常不在中间层使用sigmoid
)。