损失:nan在建立自行车共享模型时

时间:2019-03-03 07:38:47

标签: python pandas dataframe tensorflow keras

我是新来的并且正在学习机器学习,如果提问的方式不好并且问题这么简单,请多多包涵。

问题是我开发的模型正在返还nan,如果我做错了任何事情,请告诉我。下面是详细信息。

程序逻辑

import tensorflow as tf
import pandas as pd
# Reading the csv file from local drive as a dataframe
bike_df = pd.read_csv('C:\\Users\\HOME\\MLPythonPractice\\Data sets\\Bike-Sharing-Dataset\\day.csv')
bike_result_df = pd.read_csv('C:\\Users\\HOME\\MLPythonPractice\\Data sets\\Bike-Sharing-Dataset\\day.csv')

# Remove unwanted columns from the data frame
bike_df = bike_df.drop(columns=['instant','dteday','cnt'])
# shape of the dataframe
print(bike_df.shape)
# Exact attribute to see the columns of the dataframe
print(bike_df.columns)
# To know the type 
print(type(bike_df))
# To see the information of the dataframe
print(bike_df.info())
# Converting from dataframe to ndarray
bike_s = bike_df.values
print(type(bike_s))
print(bike_s.shape)
# Remove all the columns except cnt column which is result set
bike_result_df['cnt'] = bike_result_df['cnt'].values.astype(np.float64)  #converting to float
bike_result_df = bike_result_df['cnt']  # Removing all columns except cnt column
bike_result_s = bike_result_df.values   # Converting dataframe to ndarray
print(type(bike_result_s))
print(bike_result_s)
import numpy as np
print(type(bike_df))
print(bike_df.shape)
print(bike_result_df.shape)
#As the data frame is available, we will build the graph using keras (## are part of build graph)

## Initialise the sequential model
model = tf.keras.models.Sequential()
## Normalize the input data by creating a normalisation layer
model.add(tf.keras.layers.BatchNormalization(input_shape = (13,)))
## Add desnse layer for predition -- Keras declares weights and bias - dense(1) 1 here is expected value
model.add(tf.keras.layers.Dense(1))
# Compile the model - add loss and gradient descen optimiser
model.compile(optimizer='sgd',loss='mse')
print(type(bike_s))
print(type(bike_result_s))
print(bike_s.shape)
print(bike_result_s.shape)
print(bike_result_s)
# Execute the graph
model.fit(bike_s,bike_result_s,epochs=10)
model.save('models/bike_sharing_lr.h5')

我正在获取输出

Epoch 1/10
731/731 [==============================] - 1s 895us/step - loss: nan     
Epoch 2/10
731/731 [==============================] - 0s 44us/step - loss: nan
Epoch 3/10
731/731 [==============================] - 0s 46us/step - loss: nan
Epoch 4/10
731/731 [==============================] - 0s 44us/step - loss: nan
Epoch 5/10
731/731 [==============================] - 0s 39us/step - loss: nan
Epoch 6/10
731/731 [==============================] - 0s 39us/step - loss: nan
Epoch 7/10
731/731 [==============================] - 0s 47us/step - loss: nan
Epoch 8/10
731/731 [==============================] - 0s 40us/step - loss: nan
Epoch 9/10
731/731 [==============================] - 0s 43us/step - loss: nan
Epoch 10/10
731/731 [==============================] - 0s 42us/step - loss: nan

1 个答案:

答案 0 :(得分:0)

要防止渐变爆炸,可以像这样裁剪它。

doctype html
html
  body
  head
    title= title
    script(src='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js' integrity='sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM' crossorigin='anonymous')
    link(rel='stylesheet' href='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css' integrity='sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T' crossorigin='anonymous')
    link(rel='stylesheet' href='stylesheets/style.css')
    block content
      .dropdown-menu(aria-labelledby='navbarDropdownMenuLink')
        a.dropdown-item(href='#') Action
        a.dropdown-item(href='#') Another action
        a.dropdown-item(href='#') Something else here
      a#navbarDropdownMenuLink.nav-link.dropdown-toggle(href='#' role='button' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false')
        | Dropdown link
      ul.navbar-nav
        li.nav-item.active
          a.nav-link(href='#')
            | Home
            span.sr-only (current)
        li.nav-item
          a.nav-link(href='#') Features
        li.nav-item
          a.nav-link(href='#') Pricing
        li.nav-item.dropdown
    nav.navbar.navbar-expand-lg.navbar-light.bg-light
      a.navbar-brand(href='#') Navbar
      button.navbar-toggler(type='button' data-toggle='collapse' data-target='#navbarNavDropdown' aria-controls='navbarNavDropdown' aria-expanded='false' aria-label='Toggle navigation')
        span.navbar-toggler-icon
      #navbarNavDropdown.collapse.navb

根据https://keras.io/optimizers/,设置model.compile(optimizer=tf.keras.optimizers.SGD(clipnorm=1), loss='mse') 可使梯度下降优化器控制梯度削波。所有参数梯度将被限制为最大范数1。这可以防止损失函数发散。

另请参见https://www.dlology.com/blog/how-to-deal-with-vanishingexploding-gradients-in-keras/,以了解控制爆炸梯度的其他方式。


通过上述调整,损失函数不会发散,但也不会随时间减少。我注意到您建立模型的方式很奇怪。批标准化通常应在激活层之后。我不确定为什么需要标准化输入,但是您不应该为此使用clipnorm=1。如果您将模型更改为

BatchNormalize

您将获得更有意义的结果,损失函数值现在从2,000万减小到120,000。