Reputation: 43
I would like to ask if I can set the weights initializer in (any) Keras layer to binary values - for example for the weights of simple Dense layer to be 0 and 1 only? This would be helpful for instance in the case of the Conv1D layer to relax the computational time.
Thank you, J
Upvotes: 1
Views: 583
Reputation: 7745
Yes this is possible by creating a custom initializer:
def binary_weights(shape, dtype=tf.float32):
"""This function generates weights of random 0s and 1s based on the provided shape"""
# build logits matrix:
logits = tf.fill((shape[0], 2), 0.5)
# uniformly pick the class.
return tf.cast(tf.random.categorical(tf.math.log(logits), shape[1]), dtype=dtype)
Then when you specify the layer:
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(units, kernel_initializer=binary_weights, input_shape=[num_features,]),
...
])
To check the generated weights:
print(model.layers[0].get_weights()[0])
Upvotes: 2