# Initial weights in Network with Keras / Tensorflow

Update Oct/2021: Deprecated predict_class syntax,Deep Learning (keras),Deep Learning with Python (my book),,Gentle Introduction to the Adam Optimization Algorithm for Deep Learning

TensorShapeProto

Initializers define the way to set the initial random weights of Keras layers,,Initializer capable of adapting its scale to the shape of weights tensors

```
from tensorflow.keras
import layers
from tensorflow.keras
import initializers
layer = layers.Dense(
units = 64,
kernel_initializer = initializers.RandomNormal(stddev = 0.01),
bias_initializer = initializers.Zeros()
)
```

We can set the kernel_initializer arugment of all the Dense layers in our model to zeros to initialize our weight vectors to all zeros, Since the bias is a scalar quantity, even if we set it to zeros it won’t matter that much as it would for the weights

`tf.keras.layers.Dense(256, activation = 'relu', kernel_initializer = init_scheme, bias_initializer = 'zeros')`

I am trying to get the initial weights for a given network,, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Save the initial weights right after compiling the model but before training it

test_model = Test(n_inputs = 1, neurons = 100) test_model(np.random.random((32, 1))) print(test_model.get_weights()) #[array([ [0.00057544] ]), array([0.3752869])]

test_model.build(input_shape = (32, 1)) print(test_model.get_weights()) #[array([ [8.942684e-05] ], dtype = float32), array([-1.6799461], dtype = float32)]

```
class Test(tf.keras.Model):
def __init__(self, n_inputs: int, neurons = 10):
super(Test, self).__init__(name = "Test")
self.neurons = neurons
# Initilializers
mean, std = 0., 0.0005
bias_normalization = None
kernel_initializer = tf.keras.initializers.RandomNormal(mean = mean,
stddev = std)
model_input = tf.keras.layers.Input(shape = (n_inputs, ))
x = tf.keras.layers.Dense(n_inputs, activation = "linear", name = "h1",
kernel_initializer = kernel_initializer,
bias_initializer = bias_normalization)(model_input)
self.model = tf.keras.Model(model_input, x)
```

test_model = Test(n_inputs = 1, neurons = 100) print(test_model.get_weights()) #[array([ [0.00045629] ], dtype = float32), array([0.9945322], dtype = float32)]

First, instantiate a base model with pre-trained weights,,Here's what the first workflow looks like in Keras:,Instantiate a base model and load pre-trained weights into it

```
import numpy as np
import tensorflow as tf
from tensorflow
import keras
```

#### Other "network-initial" answers related to "Initial weights in Network with Keras / Tensorflow"

- Using data from one Django application in another
- Python: __qualname__ of function with decorator
- Creating a drop-down menu of Member IDs
- How to remove an item from a dictionary that is iterated?
- Return price as an object/variable using Interactive Brokers TWS API - Python
- For loop is not working while appending rows
- Python help to debug an error when modify a file in a stream mode
- Mido - How to get midi data in realtime from different ports
- Using Java's ByteBuffer to replicate Python's struct.pack
- CreateConsoleScreenBuffer in Python
- QOpenGLWidget only updates when resized
- Python requests 'certificate verify failed' for a specific SNI certificate
- Django ORM's contains vs SQL's LIKE
- Module 'tensorflow' has no attribute 'random'
- Redirection using WSGIref
- Pygame Mixer module not available [duplicate]
- How do I see and customize the source code to Python's defined functions; describe, skew, and kurtosis definitions?
- Delaying parts of text being inserted into Scroll Text Box (scrolledtext)
- Dataflow GCS to BigQuery - How to output multiple rows per input?
- Identify Format of date in python