簡體   English   中英

線性回歸使NaN損失

[英]Linear regression getting NaN for loss

不能理解為什么keras線性回歸模型不起作用。 使用波士頓住房數據。將Loss作為nan

path='/Users/admin/Desktop/airfoil_self_noise.csv'
df=pd.read_csv(path,sep='\t',header=None)

y=df[5] #TARGET

df2=df.iloc[:,:-1] 


X_train, X_test, y_train, y_test = train_test_split(df2, y, test_size=0.2)

p = Sequential()

p.add(Dense(units=20, activation='relu', input_dim=5))
p.add(Dense(units=20, activation='relu'))
p.add(Dense(units=1))

p.compile(loss='mean_squared_error',
              optimizer='sgd')

p.fit(X_train, y_train, epochs=10, batch_size=32)

這個產量:

Epoch 1/10
1202/1202 [==============================] - 0s 172us/step - loss: nan

Epoch 2/10
1202/1202 [==============================] - 0s 37us/step - loss: nan

Epoch 3/10
1202/1202 [==============================] - 0s 38us/step - loss: nan

Epoch 4/10
1202/1202 [==============================] - 0s 36us/step - loss: nan

Epoch 5/10
1202/1202 [==============================] - 0s 36us/step - loss: nan

Epoch 6/10
1202/1202 [==============================] - 0s 40us/step - loss: nan

入門, 在訓練回歸網絡時NaN損失為基礎

import pandas as pd
import keras

from keras.layers import Dense, Input
from keras import Sequential

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

sc = StandardScaler()

#Grabbing these 2 lines from your example
path='/Users/admin/Desktop/airfoil_self_noise.csv'
df = pd.read_csv("airfoil_self_noise.csv", sep = '\t', header = None)

y = df[5]
df2 = df.iloc[:, :-1]

#preprocessing. Vectorization and Scaling
X_train, X_test, y_train, y_test = train_test_split(df2.values, y.values, test_size = 0.2)
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)

p = Sequential()

p.add(Dense(units = 20, activation ='relu', input_dim = 5))
p.add(Dense(units = 20, activation ='relu'))
p.add(Dense(units = 1))

p.compile(loss = 'mean_squared_error', optimizer = 'adam')

print(p.fit(X_train, y_train, epochs = 100, batch_size = 64))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM