TensorFlow模型不执行任何训练

时间:2019-10-22 16:06:14

标签: python tensorflow machine-learning keras neural-network

我正在训练一个简单的机器学习模型,该模型采用一维描述物理系统(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,417
Trainable params: 36,417
Non-trainable params: 0
_________________________________________________________________

这是我用于训练,评估和预测的源代码:

# imports
import os
import ast
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt

# load the dataset from the csv file
data = pd.read_csv('1e_data.csv')

# load in the data
x_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 model
inputs = 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 model
with open('model_info.dat','w') as fh:
    model.summary(print_fn=lambda x: fh.write(x + '\n'))

# compile the model
model.compile(optimizer='adam', loss='mean_absolute_percentage_error', metrics=['accuracy'])

# perform the training
model.fit(x_train, y_train, epochs=10)

# evaluate the model for accuracy
model.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。

1 个答案:

答案 0 :(得分:2)

您处于回归设置中,其中准确性毫无意义(仅对分类问题有意义);有关更多详细信息,请参见What function defines accuracy in Keras when the loss is mean squared error (MSE)?(尽管使用了其他损失,但它也适用于您的情况)。

从损失的减少中可以明显看出您的网络确实可以学习的事实,这是您对回归问题感兴趣的实际数量(您根本不需要metrics这里)。

独立于上述内容,您应该将sigmoid激活更改为relu(如今,通常中间层不使用sigmoid)。