我在使用随机搜索 Keras 调谐器进行优化时遇到运行时错误

i got runtime error on use of Random Search Keras Tuner for optimization

我使用 Keras 调谐器对数字识别器数据集进行超参数调优,但出现错误
首先,我在 CNNHyperModel class 中创建了用于超参数调整的构建方法 其次,我使用 Conv2D、MaxPooling2D、Dropout 然后使用神经网络 我已经导入了这个程序所需的库

class CNNHyperModel(HyperModel):
  #def __init__(self, input_shape, num_classes):
    #self.input_shape =input_shape
    #self.num_classes =num_classes

  def build(self,hp)  :
    model=keras.Sequential()
    model.add( Conv2D(filters=hp.Choice('1Conv_num_classes',
                                        values=[32,64,128,256]),
                      activation="relu",strides=1,padding='same', kernal_size=(3,3),input_shape=(28,28,1))     
    )
    model.add(Conv2D(filters=hp.Choice("2Conv_num_classes",
                                       values=[32,54,128,256]),
                     activation='relu',strides=1,padding='same',kernal_size=(3,3)))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Dropout(rate=hp.Float("1Dropout",min_value=0.0,
                                    max_value=0.5,step=0.05)))
    model.add(Conv2D(filters=hp.Choice("3Conv_num_classes",
                                       values=[32,64,128,256]),
                     activation='relu',strides=1,padding='same',kernal_size=(3,3)))
    model.add(Conv2D(filters=hp.Choice("4Conv_num_classes",
                            values=[32,64,128,256]),
                     activation='relu',strides=1,padding='same',kernal_size=(3,3)))
    model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
    model.add(DropOut(rate=hp.Float("2Dropout", min_value=0.0,
                                    max_value=0.5,step=0.05)))
    model.add(Conv2d(filters=hp.Choice("5Conv_num_classes",
                                       values=[32,64,128,256]),
                     activation='relu',strides=1,padding='same',kernal_size=(3,3)))
    model.add(Conv2D(filters=hp.Choice("6Conv_NUM_CLASSES",
                                       values=[32,64,128,256]),
                     activation='relu',strides=1,padding='same',kernal_size=(3,3)))
    model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
    model.add(Dropout(rate=hp.Float("3Dropout",min_value=0.0,
                                    max_value=0.5,step=0.05)))
    model.add(Flatten())
    model.add(Dense(units=hp.Int("Dense",min_value=32,
                                 max_value=512,step=32),activation='relu'))
    model.add(Dropout(rate=hp.Float("Dense_Dropout",min_value=0.0,
                                    max_value=0.5,step=0.05)))
    model.add(Dense(units=hp.Int("2Dense",min_values=32,
                                 max_values=512,step=32),activation='relu'))
    model.add(Dropout(rate=hp.Float("2Dense_Dropout",min_value=0.0,
                                    max_value=0.5,step=0.05)))
    model.add(Dense(10,activation='sigmoid'))

    """model.compile(optimizer=keras.optimizers.Adam(
        hp.Float(
            "Learning_rate",
            min_value=le-4,
            max_value=le-2,
            sampling="LOG"
        )
    ),"""
    model.compie(optimizer="sgd",loss="sparse_categorical_crossentropy",metrics=['accuracy'])
    return model

#hypermodel=CNNHyperModel((28,28,1),10)    
hypermodel=CNNHyperModel()

tuner = RandomSearch(
    hypermodel,
    objective='accuracy',
    max_trials=15,executions_per_trial=3,directory='my_dir',
    project_name='digit'    
)

但是我遇到了 RuntimeError

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 104, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "<ipython-input-17-9b2a20a37331>", line 10, in build
    activation="relu",strides=1,padding='same', kernal_size=(3,3),input_shape=(28,28,1))
TypeError: __init__() missing 1 required positional argument: 'kernel_size'
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 104, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "<ipython-input-17-9b2a20a37331>", line 10, in build
    activation="relu",strides=1,padding='same', kernal_size=(3,3),input_shape=(28,28,1))
TypeError: __init__() missing 1 required positional argument: 'kernel_size'
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 104, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "<ipython-input-17-9b2a20a37331>", line 10, in build
    activation="relu",strides=1,padding='same', kernal_size=(3,3),input_shape=(28,28,1))
TypeError: __init__() missing 1 required positional argument: 'kernel_size'
Invalid model 0/5
Invalid model 1/5
Invalid model 2/5
Invalid model 3/5
Invalid model 4/5
Invalid model 5/5
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 104, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "<ipython-input-17-9b2a20a37331>", line 10, in build
    activation="relu",strides=1,padding='same', kernal_size=(3,3),input_shape=(28,28,1))
TypeError: __init__() missing 1 required positional argument: 'kernel_size'
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 104, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "<ipython-input-17-9b2a20a37331>", line 10, in build
    activation="relu",strides=1,padding='same', kernal_size=(3,3),input_shape=(28,28,1))
TypeError: __init__() missing 1 required positional argument: 'kernel_size'
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 104, in build
    model = self.hypermodel.build(hp)
  File "/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py", line 64, in _build_wrapper
    return self._build(hp, *args, **kwargs)
  File "<ipython-input-17-9b2a20a37331>", line 10, in build
    activation="relu",strides=1,padding='same', kernal_size=(3,3),input_shape=(28,28,1))
TypeError: __init__() missing 1 required positional argument: 'kernel_size'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py in build(self, hp)
    103                 with maybe_distribute(self.distribution_strategy):
--> 104                     model = self.hypermodel.build(hp)
    105             except:

9 frames
TypeError: __init__() missing 1 required positional argument: 'kernel_size'

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/kerastuner/engine/hypermodel.py in build(self, hp)
    111                 if i == self._max_fail_streak:
    112                     raise RuntimeError(
--> 113                         'Too many failed attempts to build model.')
    114                 continue
    115 

RuntimeError: Too many failed attempts to build model.

内核大小应该是 3x3 而不是 3 。即 kernel_size=(3,3) 。内核是一个矩阵而不是一个数字。

以上代码存在拼写错误,需要改进 拼写错误,例如 kernal_size ->kernel_size 所以这是具有相同改进的工作核心

import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.layers import ( Conv2D ,MaxPooling2D,
                                     Dropout,Dense,Flatten)

from kerastuner.tuners import RandomSearch
from kerastuner.engine.hyperparameters import HyperParameters
from kerastuner import HyperModel

import pandas as pd
import numpy as np



class CNNHyperModel(HyperModel):
  #def __init__(self, input_shape, num_classes):
    #self.input_shape =input_shape
    #self.num_classes =num_classes

  def build(self,hp)  :
    model=keras.Sequential()
    model.add( Conv2D(filters=hp.Int('1Conv_num_classes',default=32,min_value=32,step=16,
                                        max_value=256),
                      activation="relu",strides=1,padding='same', kernel_size=(3,3),input_shape=(28,28,1))     
    )
    model.add(Conv2D(filters=hp.Int("2Conv_num_classes",default=32,min_value=32,
                                      max_value=256,step=16),
                     activation='relu',strides=1,padding='same',kernel_size=(3,3)))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Dropout(rate=hp.Float("1Dropout",min_value=0.0,
                                    max_value=0.5,step=0.05)))
    model.add(Conv2D(filters=hp.Int("3Conv_num_classes",default=64,min_value=32,
                                       max_value=256,step=16),
                     activation='relu',strides=1,padding='same',kernel_size=(3,3)))
    model.add(Conv2D(filters=hp.Int("4Conv_num_classes",default=64,min_value=32,
                            max_value=256,step=16),
                     activation='relu',strides=1,padding='same',kernel_size=(3,3)))
    model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
    model.add(Dropout(rate=hp.Float("2Dropout", min_value=0.0,
                                    max_value=0.5,step=0.05)))
    model.add(Conv2D(filters=hp.Int("5Conv_num_classes",default=128,min_value=32,
                                       max_value=256,step=16),
                     activation='relu',strides=1,padding='same',kernel_size=(3,3)))
    model.add(Conv2D(filters=hp.Int("6Conv_NUM_CLASSES",default=128,min_value=32,
                                       max_value=256,step=16),
                     activation='relu',strides=1,padding='same',kernel_size=(3,3)))
    model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
    model.add(Dropout(rate=hp.Float("3Dropout",min_value=0.0,
                                    max_value=0.5,step=0.05)))
    model.add(Flatten())
    model.add(Dense(units=hp.Int("Dense",min_value=32,default=516,
                                 max_value=512,step=16),activation='relu'))
    model.add(Dropout(rate=hp.Float("Dense_Dropout",min_value=0.0,
                                    max_value=0.5,step=0.05)))
    model.add(Dense(units=hp.Int("2Dense",min_value=32,default=516,
                                 max_value=512,step=16),activation='relu'))
    model.add(Dropout(rate=hp.Float("2Dense_Dropout",min_value=0.0,
                                    max_value=0.5,step=0.05)))
    model.add(Dense(10,activation='sigmoid'))

    """model.compile(optimizer=keras.optimizers.Adam(
        hp.Float(
            "Learning_rate",
            min_value=le-4,
            max_value=le-2,
            sampling="LOG"
        ),loss="sparse_categorical_crossentropy",metrics=['accuracy'])
    ),"""
    model.compile(optimizer="sgd",loss="sparse_categorical_crossentropy",metrics=['accuracy'])
    return model

#hypermodel=CNNHyperModel((28,28,1),10)    
hypermodel=CNNHyperModel()

如您所见,我在 Conv2D 中传递 strides=1,padding='same' 以进行更多优化

快乐编码