Home > other >  For big help, use sklearn random search parameter error
For big help, use sklearn random search parameter error

Time:12-04

use pycharm, full text code is as follows:
The import matplotlib as MPL
The import matplotlib. Pyplot as PLT
The import numpy as np
The import sklearn
The import pandas as pd
The import OS
The import sys
The import time
The import tensorflow as tf

The from tensorflow import keras

The from sklearn. Datasets import fetch_california_housing
Housing=fetch_california_housing ()

The from sklearn. Model_selection import train_test_split
X_train_all x_test, y_train_all, y_test=train_test_split (
Housing data, housing, target, random_state=7, test_size=0.25)
X_train x_valid, y_train, y_valid=train_test_split (
X_train_all y_train_all, random_state=11)

The from sklearn. Preprocessing import StandardScaler
Scaler=StandardScaler ()
X_train_scaled=scaler. Fit_transform (x_train)
X_valid_scaled=scaler. Fit_transform (x_valid)
X_test_scaled=scaler. Fit_transform (x_test)

Def build_model (hidden_layers=1, layer_size=30, learning_rate=3 e - 3) :
The model=tf. Keras. Models. Sequential ()
Model. The add (tf) keras. The layers. Dense (layer_size, activation='relu',
Input_shape=x_train. Shape [1]),)
For _ in range (hidden_layers - 1) :
Model. The add (tf) keras. The layers. Dense (layer_size, activation='relu'))
Model. The add (tf) keras. The layers. Dense (1))
Optimizer=tf. Keras. Optimizers. SGD (learning_rate)
Model.com from running (loss='mean_squared_error, optimizer=optimizer)
Return the model

Sklearn_model=tf. Keras. Wrappers. Scikit_learn. KerasRegressor (build_model)
Callbacks=[tf keras. Callbacks. EarlyStopping (patience=5, min_delta=1 e - 2)]
The history=sklearn_model. Fit (x_train_scaled y_train, epochs=2,
validation_data=https://bbs.csdn.net/topics/(x_valid_scaled y_valid),
Callbacks=callbacks)
Print (type (sklearn_model))

Def plot_learning_curves (history) :
Pd. DataFrame (history. History). The plot (figsize=(8, 5))
PLT. The grid (' true ')
PLT. Gca (.) set_ylim (0, 1)
PLT. The show ()

Plot_learning_curves (history)

the from scipy. Stats import reciprocal
The from scipy. Stats import uniform as sp_rand
Param_distribution={
'hidden_layers: [1],
'layer_size: np arange (1, 2) tolist (),
'learning_rate: sp_rand ()} # # reciprocal (1 e - 4, 1 e - 2)

The from sklearn. Model_selection import RandomizedSearchCV
Random_search_cv=RandomizedSearchCV (sklearn_model param_distribution,
N_iter=3, CV=2, n_jobs=1)

Random_search_cv. Fit (x_train_scaled y_train, epochs=2,
validation_data=https://bbs.csdn.net/topics/(x_valid_scaled y_valid),
Callbacks=callbacks)


The model=random_search_cv. Best_estimator_. Model
Model. The evaluate (x_test_scaled y_test)



but after the operation error, tips are as follows:
Traceback (the most recent call last) :
The File "& lt; Input>" , line 1, in
The File "D: \ JetBrains \ PyCharm 2019.3.2 \ plugins \ python \ helpers \ pydev \ _pydev_bundle \ pydev_umd py", line 197, in runfile
Pydev_imports. Execfile (filename, global_vars local_vars) # execute the script
The File "D: \ JetBrains \ PyCharm 2019.3.2 \ plugins \ python \ helpers \ pydev \ _pydev_imps \ _pydev_execfile py", line 18, in execfile
Exec (the compile () + "\ n", file, 'the exec), glob, loc)
The File "C:/Users/Administrator/PycharmProjects/Tensorflow_ML/Session2/tf_kears_regression - HP - search - sklearn. Py", line 85, in & lt; module>
Callbacks=callbacks)
The File "C: \ Users \ \ Administrator \ PycharmProjects \ All_Library \ venv \ lib \ site - packages \ sklearn \ model_selection \ _search py", line 736, fit in
* * self. Best_params_))
The File "C: \ Users \ \ Administrator \ PycharmProjects \ All_Library \ venv \ lib \ site - packages \ sklearn \ base py", line 82, clone in
(name), estimator)
RuntimeError: always clone object & lt; Tensorflow. Python. Keras. Wrappers. Scikit_learn. KerasRegressor object at 0 x0000020d4d0c3c18 & gt; , as the constructor either does not set the or modifies the parameter learning_rate








CodePudding user response:

Just remove the super parameter vector search, commented param_distribution vector in that line

CodePudding user response:

refer to the second floor kurama_xp response:
remove the super parameter vector search right, commented param_distribution vector in that row
why not vector to search

CodePudding user response:

I also met the same problem, and there is a problem

CodePudding user response:

reference Poker_Pan reply: 3/f
Quote: refer to the second floor kurama_xp response:
remove the super parameter vector search right, commented param_distribution vector in that row
why not to search more ah


nullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related