简体   繁体   English

Keras 序列模型训练的 Nan 损失

[英]Nan loss from Keras Sequential Model Training

I have a Tensorflow Sequential Network which is returning a loss value of Nan consistently during training.我有一个 Tensorflow 顺序网络,它在训练期间始终返回 Nan 的损失值。 I am using pandas and keras.我正在使用熊猫和keras。

An example of the data is:数据示例如下:

Actual_GP1  Budgeted_GP_Value_Cleanup  Budgeted_GP_Value_New  \
0          2.0                        2.0                  95.00   
1          2.0                        2.0               63684.55   
3          2.0                        2.0               26022.57   
4          2.0                        2.0              440759.17   
6          2.0                        2.0                  95.00   
7          2.0                        2.0             3519120.00   
9          2.0                        2.0                   4.00   
12         2.0                        2.0                   4.00   
13         2.0                        2.0              355960.00   
14         2.0                        2.0               62745.00  



Costing_Date  Created_Time  Date_Time_16  Delivery_Date  Engineering_Date  \
0              4      1.579523           4.0            4.0                 4   
1              4      1.575390           4.0            4.0                 4   
3              4      1.575471           4.0            4.0                 4   
4              4      1.575020           4.0            4.0                 4   
6              4      1.579508           4.0            4.0                 4   
7              4      1.578304           4.0            4.0                 4   
9              4      1.574600           4.0            4.0                 4   
12             4      1.570805           4.0            4.0                 4   
13             4      1.573831           4.0            4.0                 4   
14             4      1.576153           4.0            4.0                 4   

Exchange_Rate     GP  ...  Last_Activity_Time  Modified_Time  \
0             2.0  100.0  ...            4.000000       1.579523   
1             2.0   30.0  ...            1.579519       1.579519   
3             2.0   44.0  ...            1.579516       1.579516   
4             2.0   37.0  ...            1.579516       1.579516   
6             2.0  100.0  ...            4.000000       1.579508   
7             2.0   44.0  ...            1.579507       1.579507   
9             2.0  100.0  ...            1.579506       1.579506   
12            2.0   32.0  ...            1.579506       1.579506   
13            2.0   44.0  ...            1.579506       1.579506   
14            2.0   44.5  ...            1.579506       1.579506   

Next_step_actioned_by   PO_Date   PO_Week  Production_End_Date  \
0                     4.0  1.580429  4.000000                    4   
1                     4.0  1.579824  1.579478                    4   
3                     4.0  1.575850  1.575850                    4   
4                     4.0  1.575418  1.575245                    4   
6                     4.0  1.580429  4.000000                    4   
7                     4.0  1.583798  1.583798                    4   
9                     4.0  1.579219  1.578874                    4   
12                    4.0  1.580429  1.580083                    4   
13                    4.0  1.585613  1.585526                    4   
14                    4.0  1.580429  1.580083                    4   

Production_Start_Date  Project_Value  Prototype_Date  \
0                       4          95.00               4   
1                       4      212281.82               4   
3                       4           3.00               4   
4                       4           4.00               4   
6                       4          95.00               4   
7                       4     7998000.00               4   
9                       4           4.00               4   
12                      4           4.00               4   
13                      4      809000.00               4   
14                      4      141000.00               4   

Revenue_Forecast_Probability_Weighting  
0                                      1.0  
1                                      2.0  
3                                      3.0  
4                                      4.0  
6                                      1.0  
7                                      5.0  
9                                      4.0  
12                                     4.0  
13                                     7.0  
14                                     8.0  

I understand some of the dates in this sample are categorically labelled, but that is due to missing values.我知道此示例中的某些日期已明确标记,但这是由于缺少值所致。 The target value for this model is a probability of success, which is based on historical data, and i have left that out of this question.该模型的目标值是基于历史数据的成功概率,我已将其排除在这个问题之外。 It's a value [0,100].它是一个值 [0,100]。 and the network configuration is:网络配置是:

dataset=tf.data.Dataset.from_tensor_slices((df.values, target.values))
train_dataset=dataset.shuffle(len(df)).batch(1)
print(df.shape)
def get_compiled_model():
    model = tf.keras.Sequential([
    tf.keras.layers.Dense(24, activation='relu', input_shape=(df.shape[-1],)),
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dense(8, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
    ])


    model.compile(optimizer='adam', loss='mse',metrics=['accuracy'])

    return model
model=get_compiled_model()
model.fit(train_dataset, epochs=20)
model.save("keras_saved_model.h5")

with an output of输出为

(574, 24)
WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
Train on 574 steps

Epoch 1/20
574/574 [==============================] - 2s 3ms/step - loss: nan - acc: 0.3275
Epoch 2/20
574/574 [==============================] - 1s 1ms/step - loss: nan - acc: 0.6655
Epoch 3/20
574/574 [==============================] - 1s 1ms/step - loss: nan - acc: 0.6655
Epoch 4/20
574/574 [==============================] - 1s 1ms/step - loss: nan - acc: 0.6655
Epoch 5/20
574/574 [==============================] - 1s 1ms/step - loss: nan - acc: 0.6655
Epoch 7/20
574/574 [==============================] - 1s 1ms/step - loss: nan - acc: 0.6655
and so on. 

Could someone please point me in the right direction regarding this consistent accuracy and these null loss values.关于这种一致的准确性和这些空损失值,有人能给我指出正确的方向吗?

EDIT: The solution was to divide the target value by 100 so it would fit in the range [0,1], since the final activation layer is a sigmoid function.编辑:解决方案是将目标值除以 100,使其适合范围 [0,1],因为最终激活层是 S 型函数。 Thanks to Matias Valdenegro for pointing this out感谢Matias Valdenegro指出这一点

Providing answer here for the community even if the answer is provided in the comment section.在这里为社区提供答案,即使在评论部分提供了答案。

Since the target value ranges from [0,100] the user has normalized the value by diving it by 100, and used the sigmoid activation function, which resolved the issue.由于目标值的范围从 [0,100] 开始,用户通过将其除以 100 对值进行了归一化,并使用了 sigmoid 激活函数,从而解决了该问题。
You can apply the normalize function for a feature using the below code.您可以使用以下代码为特征应用规范化功能。

To get min and max value of a numerical column:要获取数字列的最小值和最大值:

def _z_score_params(column):
    mean = traindf[column].min()
    std = traindf[column].max()
    return {'min': min, 'max': max}


def zscore(col):

  min_value = _z_score_params(col)[min]
  max_value = _z_score_params(col)[max]
  return (col - min_value)/max_value 

feature_name = ‘column_name_to_normalize’
normalized_feature = tf.feature_column.numeric_column(
  feature_name,
  normalizer_fn=zscore) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keras神经网络训练期间的“损失:难忘” - 'loss: nan' during training of Neural Network in Keras 加载和检查 Keras 序列模型的总损失/验证准确性 - Load and Check Total Loss / Validation accuracy of Keras Sequential Model 训练自定义 YOLO 模型时,损失归 NAN - Loss goes to NAN when training the custom YOLO model NAN有时会在训练我的yolo tensorflow模型造成损失时获得相同的图像 - NAN sometimes for same image on training my yolo tensorflow model for loss 奇怪的南方损失为自定义Keras损失 - Weird Nan loss for custom Keras loss Tensorflow-Probability 中具有 3 个隐藏密集变分层的回归模型在训练期间返回 nan 作为损失 - Regression Model with 3 Hidden DenseVariational Layers in Tensorflow-Probability returns nan as loss during training 当我训练 LSTM 模型时,我的损失显示为 NaN,准确度为 0 - My loss is showing to be NaN and accuracy to be 0 when I'm training my LSTM model 使用 model.fit、model.fit_generator 和 model.train_on_batch 训练 tf.keras.Sequential 模型的预期性能 - Expected performance of training tf.keras.Sequential model with model.fit, model.fit_generator and model.train_on_batch 尝试从训练模型中获得val_loss - Trying to get the val_loss from training a model 计算损失时的 Keras Nan 值 - Keras Nan value when computing the loss
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM