question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weights for model sequential have not yet been created error

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
omalleyt12commented, Aug 23, 2019

I was able to repro. A workaround for now is to pass input_shape=(<my_shape>) to the first layer of the Sequential model

0reactions
franchesonicommented, Aug 30, 2019

If 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:

  def get_config(self):
    if not self._is_graph_network:
      raise NotImplementedError

where ```self```` is my custom model.

If I implement get_config, now the traceback becomes:

Traceback (most recent call last):
  File "/home/franchesoni/mine/les/our_research/redes/0.7/tcn_1_search.py", line 116, in <module>
    tuner.search(train_gen, validation_data=test_gen)
  File "/home/franchesoni/aminiconda3/envs/tf/lib/python3.6/site-packages/kerastuner/engine/tuner.py", line 223, in search
    self.run_trial(trial, hp, fit_args, fit_kwargs)
  File "/home/franchesoni/aminiconda3/envs/tf/lib/python3.6/site-packages/kerastuner/tuners/hyperband.py", line 326, in run_trial
    super(Hyperband, self).run_trial(trial, hp, fit_args, fit_kwargs)
  File "/home/franchesoni/aminiconda3/envs/tf/lib/python3.6/site-packages/kerastuner/engine/tuner.py", line 253, in run_trial
    self.on_execution_begin(trial, execution, model)
  File "/home/franchesoni/aminiconda3/envs/tf/lib/python3.6/site-packages/kerastuner/tuners/hyperband.py", line 321, in on_execution_begin
    model.load_weights(best_checkpoint)
  File "/home/franchesoni/aminiconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 162, in load_weights
    return super(Model, self).load_weights(filepath, by_name)
  File "/home/franchesoni/aminiconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1415, in load_weights
    saving.load_weights_from_hdf5_group(f, self.layers)
  File "/home/franchesoni/aminiconda3/envs/tf/lib/python3.6/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 671, in load_weights_from_hdf5_group
    ' layers.')
ValueError: You are trying to load a weight file containing 4 layers into a model with 0 layers.

I haven’t dig in yet, layers in my model are build at __init__

Read more comments on GitHub >

github_iconTop Results From Across the Web

ValueError : Weights for model sequential have not yet been ...
The error is : ValueError: Weights for model sequential have not yet been created. Weights are created when the Model is first called...
Read more >
How to implement Sequential model with tk.keras - gcptutorials
ValueError : Weights for model sequential_13 have not yet been created. Weights are created when the Model is first called on inputs or...
Read more >
tf.keras.Sequential | TensorFlow v2.11.0
Sequential groups a linear stack of layers into a tf.keras.Model. ... model.weights not created yet ... Model has been trained/evaluated on actual data....
Read more >
The Sequential model - TensorFlow for R - RStudio
: ValueError: Weights for model sequential_3 have not yet been created. Weights are created when the Model is first called on inputs or...
Read more >
Model saving & serialization APIs - Keras
Note that the model weights may have different scoped names after being loaded. ... Raises. ImportError: If save format is hdf5, and h5py...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found