python - 尝试使用 Keras 进行预测时返回 nan

Tad*_*. A 5 python machine-learning keras

对于一个学校项目,我尝试使用 keras 框架预测数据,但当我尝试获取预测数据时,它返回“nan”损失和值。

源代码 :

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=5)

# create model
model = Sequential()
model.add(Dense(950, input_shape=(425,), activation='relu'))
model.add(Dense(425, activation='relu'))
model.add(Dense(200, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# Compile model
sgd = optimizers.SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', optimizer='sgd')

# Fit the model
model.fit(X_train, y_train, epochs=20, batch_size=1, verbose=1)

#evaluate the model
y_pred = model.predict(X_test)

score = model.evaluate(X_test, y_test,verbose=1)
print(score)

# calculate predictions
predictions = model.predict(X_pred)
Run Code Online (Sandbox Code Playgroud)

数据 :

X_train 和 X_test 是 5000 行(样本数)* 425 列(维度数)的(panda)数据帧。

y_train 和 y_test 看起来像:

array([ 1.17899644,  1.46080518,  0.9662137 , ...,  2.40157461,
        0.53870386,  1.3192718 ])
Run Code Online (Sandbox Code Playgroud)

你能帮我吗?

谢谢你的帮助!

Jod*_*odo 3

通常,这意味着某物收敛于无穷大。正如@desertnaut 在评论中指出的那样,降低学习率可能会有所帮助。

但问题的根源是你的输入数据。这425个数据点意味着什么?它们的来源、特征、参数是否不同?寻找大纲或标准化数据可能会有所帮助。

否则你的代码看起来不错。