首页 > 解决方案 > sklearn.model_selection.RandomizedSearchCV.fit 抛出的 0x7f466c02fe20> 处的 KerasRegressor 对象和 0x7f46ed175580> 处的 rv_frozen 对象是什么意思?

问题描述

我正在尝试使用方法 sklearn.model_selection.RandomizedSearchCV.fit 优化(Cent OS 8,Anaconda,Jupyter Notebook,Python)中神经网络的超参数,但对该方法的调用最终输出了我认为的错误. 您是否知道以下输出的含义以及是否需要修复?

RandomizedSearchCV(cv=3, estimator=<tensorflow.python.keras.wrappers.scikit_learn.KerasRegressor object at 0x7f466c02fe20>, param_distributions={'learning_rate': <scipy.stats._distn_infrastructure.rv_frozen object at 0x7f46ed175580>, 'n_hidden': [ 0, 1, 2, 3], 'n_neurons': 数组([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])})

给出上面显示的消息的整个代码如下所示:

## Data extraction  and standardization

from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Getting the data
housing = fetch_california_housing()

#Splitting the data in training and testing data
X_train_full, X_test, y_train_full, y_test = train_test_split(
housing.data, housing.target)

# Splitting the training data in training data and validation data
X_train, X_valid, y_train, y_valid = train_test_split(
X_train_full, y_train_full)


# Standardizing the data: first substracting the mean value (so standardized
# values always have a zero mean), and then dividing by the standard deviation so that
# the resulting distribution has unit variance.
scaler = StandardScaler()

# Scale the training data with the mean and variance of the training data.
X_train_scaled = scaler.fit_transform(X_train)
# Scale the validation data with the mean and variance of the training data.
X_valid_scaled = scaler.transform(X_valid)
# Scale the test dta with the mean and variance of the training data.
X_test_scaled = scaler.transform(X_test)

## Building a generic model for optimization of hyperparameters
def build_model(n_hidden=1, n_neurons=30, learning_rate=3e-3, input_shape=[8]):
    model = keras.models.Sequential()
    # inpute layer
    model.add(keras.layers.InputLayer(input_shape=input_shape))
    for layer in range(n_hidden):
        model.add(keras.layers.Dense(n_neurons, activation="relu"))
    #output layer
    model.add(keras.layers.Dense(1))
    
    # defining optimizer, learning rate and loss function of the model
    optimizer = keras.optimizers.SGD(lr=learning_rate)
    model.compile(loss="mse", optimizer=optimizer)
    return model

## Creating a Keras regressor based on the previous model
from tensorflow import keras
# KerasRegressor is a wrapper of the model 
keras_reg = keras.wrappers.scikit_learn.KerasRegressor(build_model)

## Defining randomized search for hyperparameters optimization
import numpy as np
from scipy.stats import reciprocal
from sklearn.model_selection import RandomizedSearchCV
# Defining dictionary of hyperparameter distributions for randomized search
param_distribs = {
    "n_hidden": [0, 1, 2, 3],
    "n_neurons": np.arange(1, 100), #[1,2,......,98,99]
    "learning_rate": reciprocal(3e-4, 3e-2),
    }
# cv: The chosen number of cross validation folds determining how many times it will train each model on
#     a different subset of data in order to assess model quality.
# n_iter: amount of iterations. Each iteration represents a new model trained on a new draw from the dictionary
#         of hyperparameter distributions
# The total number of models random search trains is then equal to n_iter * cv
rnd_search_cv = RandomizedSearchCV(keras_reg,
                                   param_distribs,
                                   n_iter=10, cv=3)

## Training
# Training all the data of the scaled input training data set in each cycle out of the total 100 cycles.
# Too many epochs can lead to overfitting of the training dataset, whereas too few may result in an underfit model.
# Early stopping is a method that allows you to specify an arbitrary large number of training epochs and
# stop training once the model performance stops improving on a hold out validation dataset.
rnd_search_cv.fit(X_train_scaled, y_train, epochs=100,
                  validation_data=(X_valid_scaled, y_valid),
                  callbacks=[keras.callbacks.EarlyStopping(patience=10)])

我还尝试了在 Linux 终端而不是 Jupyter Notebook 上运行它,如下所示:

 python3 test.py > /dev/null 2>trace.txt

然后运行

cat trace.txt 

在 Linux 终端上输出以下内容:

2021-11-08 00:03:30.792768:我 tensorflow/compiler/jit/xla_cpu_device.cc:41] 未创建 XLA 设备,未设置 tf_xla_enable_xla_devices 2021-11-08 00:03:30.797482:我 tensorflow/core/platform/ cpu_feature_guard.cc:142] 这个 TensorFlow 二进制文件使用 oneAPI 深度神经网络库 (oneDNN) 进行了优化,以在性能关键操作中使用以下 CPU 指令: SSE4.1 SSE4.2 AVX AVX2 AVX512F FMA 要在其他操作中启用它们,请重新构建具有适当编译器标志的 TensorFlow。2021-11-08 00:03:30.804638: I tensorflow/core/common_runtime/process_util.cc:146] 使用默认互操作设置创建新线程池:2. 使用 inter_op_parallelism_threads 进行调整以获得最佳性能。2021-11-08 00:03:30.848028:我 tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:

谢谢

标签: pythonscikit-learntf.keras

解决方案


推荐阅读