2021年1月4日星期一

Using pandas.pct_change() on dataset results in 'nan' loss in tensorflow model

Let's say I have a dataframe like so:

df = pd.DataFrame({'Inputs': np.arange(100), 'Labels': np.multiply(np.arange(100),5)})  df.head()  
 Inputs Labels  0   0   0  1   1   5  2   2   10  3   3   15  4   4   20  

For simplicity, let model be just one Dense layer with 1 unit and no activation.

Training like so works just fine:

model.fit(x=df['Inputs'], y=df['Labels'])  

But now, if I turn my dataframe into percent changes:

pct_change_df = df.pct_change(axis=0)  pct_change_df.head()  
Inputs  Labels  0   NaN NaN  1   inf inf  2   1.000000    1.000000  3   0.500000    0.500000  4   0.333333    0.333333  

training will result in only nan losses. I figured this is due to there being NaN and (possibly) inf values in my dataset which will blow the gradients to infinity or -infinity. However, after removing all of those:

# removing inf values from pct_change_df  pct_change_df = pct_change_df.replace([np.inf,-np.inf],np.nan).dropna(axis=0)  pct_change_df.head()  
Inputs  Labels  2   1.000000    1.000000  3   0.500000    0.500000  4   0.333333    0.333333  5   0.250000    0.250000  6   0.200000    0.200000  

I still get the same results. I have no idea what the issue is.

# loss is still nan!!  model.fit(x=pct_change_df['Inputs'], y=pct_change_df['Labels'], epochs=5)  
Epoch 1/5  4/4 [==============================] - 0s 1ms/step - loss: nan  Epoch 2/5  4/4 [==============================] - 0s 2ms/step - loss: nan  Epoch 3/5  4/4 [==============================] - 0s 2ms/step - loss: nan  Epoch 4/5  4/4 [==============================] - 0s 3ms/step - loss: nan  Epoch 5/5  4/4 [==============================] - 0s 3ms/step - loss: nan  
https://stackoverflow.com/questions/65569919/using-pandas-pct-change-on-dataset-results-in-nan-loss-in-tensorflow-model January 05, 2021 at 05:13AM

没有评论:

发表评论