2016-11-08 20:43:45 8 Comments

I'd like to reset (randomize) the weights of all layers in my Keras (deep learning) model. The reason is that I want to be able to train the model several times with different data splits without having to do the (slow) model recompilation every time.

Inspired by this discussion, I'm trying the following code:

```
# Reset weights
for layer in KModel.layers:
if hasattr(layer,'init'):
input_dim = layer.input_shape[1]
new_weights = layer.init((input_dim, layer.output_dim),name='{}_W'.format(layer.name))
layer.trainable_weights[0].set_value(new_weights.get_value())
```

However, it only partly works.

Partly, becuase I've inspected some layer.get_weights() values, and they seem to change. But when I restart the training, the cost values are much lower than the initial cost values on the first run. It's almost like I've succeeded resetting some of the weights, but not all of them.

Any tips on where I'm going wrong would be deeply appreciated. Thx..

### Related Questions

#### Sponsored Content

#### 4 Answered Questions

### [SOLVED] Adding regularizer to an existing layer of a trained model without resetting weights?

**2018-01-18 20:54:33****Moondra****2412**View**6**Score**4**Answer- Tags: python tensorflow keras

#### 3 Answered Questions

### [SOLVED] Understanding Keras LSTMs

**2016-08-02 08:04:13****sachinruk****42749**View**278**Score**3**Answer- Tags: python deep-learning keras lstm

#### 1 Answered Questions

### [SOLVED] Keras layers unfreezing causes training process to start from scratch

**2018-04-26 07:10:36****Vsevolod Ryabykh****722**View**0**Score**1**Answer- Tags: python deep-learning keras keras-layer

#### 1 Answered Questions

### [SOLVED] OR-Lambda-Layer operation with Keras

**2018-11-16 15:15:34****Vinicius****205**View**1**Score**1**Answer- Tags: python tensorflow keras keras-layer

#### 1 Answered Questions

### [SOLVED] How to reset specific layer weights for transfer learning?

**2018-09-20 12:38:04****thebeancounter****592**View**2**Score**1**Answer- Tags: machine-learning neural-network keras deep-learning conv-neural-network

#### 1 Answered Questions

### [SOLVED] Set weights by name for a keras TimeDistributed layer

**2018-10-10 12:52:25****BMillikan****229**View**0**Score**1**Answer- Tags: python tensorflow keras

#### 1 Answered Questions

### [SOLVED] Copying weights from one Conv2D layer to another

**2017-12-06 05:41:04****snella****1526**View**4**Score**1**Answer- Tags: python tensorflow keras conv-neural-network keras-layer

#### 1 Answered Questions

### [SOLVED] The initialization of model training in keras

**2018-03-08 21:01:31****RayZ****1062**View**1**Score**1**Answer- Tags: keras

## 6 comments

## @Mendi Barel 2018-08-07 13:08:49

Reset all layers by checking for initializers:

Update: kernel_initializer is kernel.initializer now.

## @SuperNES 2019-02-15 19:22:02

This is the best approach in my view.

## @Xiaohong Deng 2019-03-28 22:01:40

Is it outdated? Now

`kernel_initializer`

has no attribute`run`

. In my case`kernel_initializer`

is a VarianceScaling object## @tkchris 2019-07-15 20:05:38

@XiaohongDeng try

`kernel.initializer.run(session=session)`

instead. I had the same problem## @Bersan 2020-01-02 17:02:05

`AttributeError: module 'tensorflow_core.keras.backend' has no attribute 'get_session'`

using tensorflow.keras## @Mendi Barel 2020-01-02 17:29:13

Yes, it won't work in tf2, look here toward the end for tf2: github.com/keras-team/keras/issues/341

## @Andrew - OpenGeoCode 2019-09-06 17:17:52

To "random" re-initialize weights of a compiled untrained model in TF 2.0 (tf.keras):

Note the "if wdim > 1 else w". You don't want to re-initialize the biases (they stay 0 or 1).

## @BallpointBen 2018-05-09 15:48:23

If you want to truly re-randomize the weights, and not merely restore the initial weights, you can do the following. The code is slightly different depending on whether you're using TensorFlow or Theano.

## @guillefix 2018-12-21 22:47:48

Nice and simple solution!

## @Bersan 2018-12-31 20:16:56

`Cannot evaluate tensor using `eval()`: No default session is registered.`

## @Ashot Matevosyan 2018-09-12 10:00:52

## @bendl 2018-12-14 14:18:48

Not quite as portable but works well for tensorflow backend!

## @ezchx 2017-05-13 20:45:09

Save the initial weights right after compiling the model but before training it:

and then after training, "reset" the model by reloading the initial weights:

This gives you an apples to apples model to compare different data sets and should be quicker than recompiling the entire model.

## @Tor 2017-05-15 13:06:23

I ended up doing something similar. Saving to disk and loading takes a lot of time, so I just keep the weights in a variable: weights = model.get_weights() I get the initial weights like this before running the first training. Then, before each subsequentt training, I reload the initial weights and run jkleint's shuffle method, as mentioned in the link that I posted. Seems to work smoothly..

## @BallpointBen 2018-05-07 21:01:33

For the full code snippet of @Tor's suggestion:

`weights = model.get_weights()`

,`model.compile(args)`

,`model.fit(args)`

,`model.set_weights(weights)`

## @Andrew 2019-07-05 14:15:49

Based on this, I've started making a lambda function when I initialize my model. I build the model, then do something like

`weights = model.get_weights()`

;`reset_model = lambda model: model.set_weights(weights)`

, that way I can just call`reset_model(model)`

later.## @maz 2017-03-08 05:15:07

Try

set_weights.for example:

build a model with say, two convolutional layers

then define your weights (i'm using a simple w, but you could use np.random.uniform or anything like that if you want)

Take a peek at what are the layers inside a model

Set each weight for each convolutional layer (you'll see that the first layer is actually input and you don't want to change that, that's why the range starts from 1 not zero).

Generate some input for your test and predict the output from your model

You could change it again if you want and check again for the output:

Sample output:

From your peek at .layers you can see that the first layer is input and the others your convolutional layers.