sample_weight scikit-learn GridSearchCV 参数形状错误
sample_weight parameter shape error in scikit-learn GridSearchCV
将 sample_weight 参数传递给 GridSearchCV 会因形状不正确而引发错误。我怀疑交叉验证无法根据数据集相应地处理 sample_weights 的拆分。
第一部分:使用 sample_weight 作为模型参数效果很好
让我们考虑一个简单的例子,首先没有 GridSearch:
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
dataURL = 'https://raw.githubusercontent.com/mcasl/PAELLA/master/data/sinusoidal_data.csv'
x = pd.read_csv(dataURL, usecols=["x"]).x
y = pd.read_csv(dataURL, usecols=["y"]).y
occurrences = pd.read_csv(dataURL, usecols=["Occurrences"]).Occurrences
my_sample_weights = (1 - occurrences/10000)**3
my_sample_weights
包含我分配给 x、y 中每个观察值的重要性,如下图所示。正弦曲线的点比构成背景噪声的点获得更高的权重。
plt.scatter(x, y, c=my_sample_weights>0.9, cmap="cool")
让我们训练一个神经网络,首先不使用 my_sample_weights
中包含的信息:
def make_model(number_of_hidden_neurons=1):
model = Sequential()
model.add(Dense(number_of_hidden_neurons, input_shape=(1,), activation='tanh'))
model.add(Dense(1, activation='linear'))
model.compile(optimizer='sgd', loss='mse')
return model
net_Not_using_sample_weight = make_model(number_of_hidden_neurons=6)
net_Not_using_sample_weight.fit(x,y, epochs=1000)
plt.scatter(x, y, )
plt.scatter(x, net_Not_using_sample_weight.predict(x), c="green")
如下图所示,神经网络试图拟合正弦曲线的形状,但背景噪声使其无法很好地拟合。
现在,使用 my_sample_weights
的信息,预测的质量要好得多。
第二部分:使用 sample_weight 作为 GridSearchCV 参数会引发错误
my_Regressor = KerasRegressor(make_model)
validator = GridSearchCV(my_Regressor,
param_grid={'number_of_hidden_neurons': range(4, 5),
'epochs': [500],
},
fit_params={'sample_weight': [ my_sample_weights ]},
n_jobs=1,
)
validator.fit(x, y)
尝试将 sample_weights 作为参数传递会出现以下错误:
...
ValueError: Found a sample_weight array with shape (1000,) for an input with shape (666, 1). sample_weight cannot be broadcast.
sample_weight 向量似乎没有以与输入数组类似的方式拆分。
价值:
import sklearn
print(sklearn.__version__)
0.18.1
import keras
print(keras.__version__)
2.0.5
问题是,作为标准,GridSearch 使用 3 折交叉验证,除非另有明确说明。这意味着数据的 2/3 数据点用作训练数据,1/3 用于交叉验证,这确实符合错误信息。 fit_params 的 1000 个输入形状与用于训练的训练示例数 (666) 不匹配。调整大小,代码会运行.
my_sample_weights = np.random.uniform(size=666)
我们开发了 PipeGraph,它是 Scikit-Learn Pipeline 的扩展,允许您获取中间数据,构建类似工作流的图形,特别是解决这个问题(请参阅 http://mcasl.github.io/PipeGraph 画廊中的示例)
将 sample_weight 参数传递给 GridSearchCV 会因形状不正确而引发错误。我怀疑交叉验证无法根据数据集相应地处理 sample_weights 的拆分。
第一部分:使用 sample_weight 作为模型参数效果很好
让我们考虑一个简单的例子,首先没有 GridSearch:
import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import GridSearchCV
import matplotlib.pyplot as plt
dataURL = 'https://raw.githubusercontent.com/mcasl/PAELLA/master/data/sinusoidal_data.csv'
x = pd.read_csv(dataURL, usecols=["x"]).x
y = pd.read_csv(dataURL, usecols=["y"]).y
occurrences = pd.read_csv(dataURL, usecols=["Occurrences"]).Occurrences
my_sample_weights = (1 - occurrences/10000)**3
my_sample_weights
包含我分配给 x、y 中每个观察值的重要性,如下图所示。正弦曲线的点比构成背景噪声的点获得更高的权重。
plt.scatter(x, y, c=my_sample_weights>0.9, cmap="cool")
让我们训练一个神经网络,首先不使用 my_sample_weights
中包含的信息:
def make_model(number_of_hidden_neurons=1):
model = Sequential()
model.add(Dense(number_of_hidden_neurons, input_shape=(1,), activation='tanh'))
model.add(Dense(1, activation='linear'))
model.compile(optimizer='sgd', loss='mse')
return model
net_Not_using_sample_weight = make_model(number_of_hidden_neurons=6)
net_Not_using_sample_weight.fit(x,y, epochs=1000)
plt.scatter(x, y, )
plt.scatter(x, net_Not_using_sample_weight.predict(x), c="green")
如下图所示,神经网络试图拟合正弦曲线的形状,但背景噪声使其无法很好地拟合。
现在,使用 my_sample_weights
的信息,预测的质量要好得多。
第二部分:使用 sample_weight 作为 GridSearchCV 参数会引发错误
my_Regressor = KerasRegressor(make_model)
validator = GridSearchCV(my_Regressor,
param_grid={'number_of_hidden_neurons': range(4, 5),
'epochs': [500],
},
fit_params={'sample_weight': [ my_sample_weights ]},
n_jobs=1,
)
validator.fit(x, y)
尝试将 sample_weights 作为参数传递会出现以下错误:
...
ValueError: Found a sample_weight array with shape (1000,) for an input with shape (666, 1). sample_weight cannot be broadcast.
sample_weight 向量似乎没有以与输入数组类似的方式拆分。
价值:
import sklearn
print(sklearn.__version__)
0.18.1
import keras
print(keras.__version__)
2.0.5
问题是,作为标准,GridSearch 使用 3 折交叉验证,除非另有明确说明。这意味着数据的 2/3 数据点用作训练数据,1/3 用于交叉验证,这确实符合错误信息。 fit_params 的 1000 个输入形状与用于训练的训练示例数 (666) 不匹配。调整大小,代码会运行.
my_sample_weights = np.random.uniform(size=666)
我们开发了 PipeGraph,它是 Scikit-Learn Pipeline 的扩展,允许您获取中间数据,构建类似工作流的图形,特别是解决这个问题(请参阅 http://mcasl.github.io/PipeGraph 画廊中的示例)