How do I set custom weights for my sequential model?
Solution 1:
Using tf.keras.initializers.random_normal()
like that will not work when trying to use it for a Keras
layer. Check the docs here for example. Also, you should not hard-code the shape of your weights beforehand. It will be inferred based on the input to your model. You could try something like this:
import tensorflow as tf
def random_normal_init(shape, dtype=None):
return tf.random.normal(shape) * 100
model = tf.keras.Sequential([
tf.keras.layers.Dense(5, activation="tanh", input_shape=(5,), kernel_initializer=random_normal_init),
tf.keras.layers.Dense(2, activation="tanh"),
tf.keras.layers.Dense(2, activation="tanh"),
tf.keras.layers.Dense(1, activation="sigmoid")
])
samples = 20
print(model(tf.random.normal((samples, 5))))
tf.Tensor(
[[0.2567306 ]
[0.79331714]
[0.74326944]
[0.35187328]
[0.18808913]
[0.81191087]
[0.6069946 ]
[0.74326944]
[0.65107304]
[0.39300534]
[0.6069946 ]
[0.81191087]
[0.61664075]
[0.35496145]
[0.81191087]
[0.2567306 ]
[0.38335925]
[0.2567306 ]
[0.50955486]
[0.74326944]], shape=(20, 1), dtype=float32)