![](/img/trans.png)
[英]any easy way to get imagenet dataset for training custom model in tensorflow?
[英]TensorFlow Model not performing any training
我正在训练一个简单的机器学习 model,它对物理系统(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 的准确度。
您处于回归设置中,其中准确性毫无意义(仅对分类问题有意义); 当损失为均方误差 (MSE) 时,请参阅 function 定义 Keras 中的准确度? 有关更多详细信息(尽管使用了不同的损失,它也适用于您的情况)。
从损失的减少中可以明显看出您的网络确实学习了这一事实,这是对回归问题感兴趣的实际数量(您在这里根本不需要任何metrics
)。
与上述无关,您可能应该将sigmoid
激活更改为relu
(我们现在通常不将sigmoid
用于中间层)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.