在用sklearn网格搜索tensorflow的组合层时,报错如下
class MyLSTM (keras.Model):
def __init__(self,units=128):
super(MyLSTM,self).__init__()
self.lstm1=keras.Sequential([layers.Dense(512,activation=tf.nn.relu),
layers.Dropout(0.2),
layers.Dense(256,activation=tf.nn.relu),
layers.Dropout(0.2),
layers.Dense(128,activation=tf.nn.relu),
layers.Dropout(0.2),
])
self.lstm2=keras.Sequential([layers.LSTM(units,return_sequences=True,activation=tf.nn.relu),
layers.Dropout(0.2),
layers.LSTM(units,return_sequences=True,activation=tf.nn.relu),
layers.Dropout(0.2),
layers.LSTM(units,return_sequences=True,activation=tf.nn.relu),
layers.Dropout(0.2),
layers.Dense(128,activation=tf.nn.relu),
layers.Dropout(0.2),
layers.Dense(256,activation=tf.nn.relu),
layers.Dropout(0.2),
layers.Dense(512,activation=tf.nn.relu),
layers.Dropout(0.2),
layers.Dense(1004)])
def call(self,inputs,training=None):
x=inputs
x=self.lstm1(x,training=training)
#x=tf.dtypes.as_dtype(x)
#x=tf.reshape(x,[x.shape[0],x.shape[1],1])
#x=tf.dtypes.as_dtype(x)
x=tf.expand_dims(x,axis=1)
x=self.lstm2(x)
return x
param_grid={"units":np.arange(10,301,10)}
model=MyLSTM()
model.compile(optimizer = keras.optimizers.Adam(0.001),
loss=tf.losses.BinaryCrossentropy(),
metrics=['accuracy'])
kmodel=KerasClassifier(build_fn=model)
grid=GridSearchCV(kmodel,param_grid=param_grid,scoring=r2_score,cv=5)
history=grid.fit(train_dataset,train_labels)
ValueError: units is not a legal parameter
param_grid={"units":np.arange(10,301,10)}
看看这里,units参数是否不合法
Exception: URL fetch failure on https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)
在使用sklearn网格搜索tensorflow的组合层时,报错:ValueError: units is not a legal parameter。这是因为在定义参数网格时,没有把units参数加入,只有把units参数加入参数网格,才能正确搜索。可以在param_grid中添加units参数,如:
param_grid={"units":np.arange(10,301,10)}