Weights for model sequential have not yet been created error
See original GitHub issueI’m getting a weights error when run a simple tuner.
-
ENV : tf version 22.0.0-beta1 keras 2.2.4-tf
-
RUNNING on Kaggle GPU powered
class FBPDHyperModel(HyperModel):
def __init__(self,features):
self.features_layers = features # DenseFeatures Layer
def build(self, hp):
model = tf.keras.Sequential()
model.add(self.features_layers)
model.add(layers.Dense(units=hp.Range('units',32, 512, 32),activation='relu'))
model.add(layers.Dense(3, activation='softmax'))
model.compile(optimizer=tf.keras.optimizers.Adam(),loss='sparse_categorical_crossentropy',metrics=['accuracy'])
return model
tuner = RandomSearch(
FBPDHyperModel(features=features_layer),
objective='val_accuracy',
max_trials=5,
directory='tuner_dir')
On running, I get this error
ValueError Traceback (most recent call last) <ipython-input-49-720dad76188f> in <module> 3 objective=‘val_accuracy’, 4 max_trials=5, ----> 5 directory=‘tuner_dir’)
/opt/conda/lib/python3.6/site-packages/kerastuner/tuners/randomsearch.py in init(self, hypermodel, objective, max_trials, seed, **kwargs) 120 objective, 121 max_trials, –> 122 **kwargs)
/opt/conda/lib/python3.6/site-packages/kerastuner/engine/tuner.py in init(self, oracle, hypermodel, objective, max_trials, executions_per_trial, max_model_size, optimizer, loss, metrics, hyperparameters, tune_new_entries, allow_new_entries, distribution_strategy, directory, project_name) 200 # Populate initial search space 201 if not self.hyperparameters.space and self.tune_new_entries: –> 202 self._build_model(self.hyperparameters) 203 204 def search(self, *fit_args, **fit_kwargs):
/opt/conda/lib/python3.6/site-packages/kerastuner/engine/tuner.py in _build_model(self, hp) 599 600 # Check model size. –> 601 size = utils.compute_model_size(model) 602 if self.max_model_size and size > self.max_model_size: 603 oversized_streak += 1
/opt/conda/lib/python3.6/site-packages/kerastuner/utils.py in compute_model_size(model) 21 def compute_model_size(model): 22 “comput the size of a given model” —> 23 params = [K.count_params(p) for p in set(model.trainable_weights)] 24 return int(np.sum(params)) 25
/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py in trainable_weights(self) 578 @property 579 def trainable_weights(self): –> 580 self._assert_weights_created() 581 return trackable_layer_utils.gather_trainable_weights( 582 trainable=self.trainable,
/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py in _assert_weights_created(self)
1662 'Weights are created when the Model is first called on ’
1663 ‘inputs or build()
is called with an input_shape
.’ %
-> 1664 self.name)
1665
1666 @property
ValueError: Weights for model sequential have not yet been created. Weights are created when the Model is first called on inputs or build()
is called with an input_shape
.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top GitHub Comments
I was able to repro. A workaround for now is to pass
input_shape=(<my_shape>)
to the first layer of the Sequential modelIf the number of layers do not depend upon the hyperparameters used I still get an error.
First, following the previous exchange: If I don’t implement get_config, I find in
keras/engine/network.py
, line 919:where ```self```` is my custom model.
If I implement get_config, now the traceback becomes:
I haven’t dig in yet, layers in my model are build at
__init__