# Python library
# -----------------------------------------------------------------
import pandas as pd
import numpy as np
import seaborn as sns
from tensorflow import keras
import matplotlib.pyplot as plt
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
# -----------------------------------------------------------------
# 1) created from the data
#-----------------------------------------------------------------
np.random.seed(0)
m = 100
X = np.linspace(0, 10, m).reshape(m,1)
y = X + np.random.randn(m, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
scaler = MinMaxScaler()
X_train= scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
#-----------------------------------------------------------------
# 2) Data visualization
#-----------------------------------------------------------------
print('dimensions de X:', X.shape)
print('dimensions de y:', y.shape)
plt.scatter(X,y)
plt.show()
#-----------------------------------------------------------------
# 3) Configuration of the Neural Network Layers
#-----------------------------------------------------------------
model = keras.Sequential()
model.add(keras.layers.Dense(100, activation='relu', input_dim=1))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(100, activation='relu'))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(1, activation='relu'))
#-----------------------------------------------------------------
# 4) Use the validation stick to train the model and display the learning curve
#-----------------------------------------------------------------
Model = keras.Sequential([
keras.layers.Dense(4, activation='relu', input_dim=2),
keras.layers.Dense(2, activation='relu'),
keras.layers.Dense(1, activation='relu')])
opt = keras.optimizers.Adam()
Model.compile(opt, loss= 'mse')
Model = KerasRegressor(Model,batch_size=10,verbose=1, epochs=1000)
val_score = cross_val_score(Model, X_train, y_train, cv=10)
#plt.plot(val_score)
#-----------------------------------------------------------------
当我运行附加代码正常它应该工作,但由于某种原因,它显示这个错误:
:14:弃用警告:KerasRegressor已弃用,请改用Sci-Keras(https://github.com/adriangb/scikeras)。有关迁移的帮助,请参阅https://www.adriangb.com/scikeras/stable/migration.html。模型= KerasRegressor(模型,批处理大小= 10,详细信息= 1,epochs = 1000)/usr/local/lib/python3.8/dist-packages/sklearn/模型选择/验证。py:372:拟合失败警告:总共10次拟合中有10次失败。这些参数在这些训练测试分区上的分数将设置为nan。如果这些失败不是预期的,您可以尝试通过设置error_score ='raise '来调试它们。
以下是有关故障的更多详细信息:
10 fits failed with the following error: Traceback (most recent call last): File "/usr/local/lib/python3.8/dist-packages/sklearn/model_selection/_validation.py", line 680, in _fit_and_score estimator.fit(X_train, y_train,**fit_params) File "/usr/local/lib/python3.8/dist-packages/keras/wrappers/scikit_learn.py", line 152, in fit self.model = self.build_fn( File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None File "/usr/local/lib/python3.8/dist-packages/keras/engine/base_layer.py", line 3100, in _split_out_first_arg raise ValueError( ValueError: The first argument to Layer.call
must always be passed.
警告. warn(一些拟合失败消息,拟合失败警告)
1条答案
按热度按时间xmq68pz91#
我不知道这个警告是否总是触发错误,但是它告诉你使用内置的 Package 器是过时的。migrating guide值得一读,但是基本的步骤是:
1.安装
scikeras
(另请参见:docs)1.将
keras.wrappers
替换为scikeras.wrappers
: