python-kerasHow can I get the weights of my Python Keras model?
To get the weights of a Python Keras model, you can use the get_weights()
method. This method returns a list of numpy arrays, each containing the weights of the model for a single layer.
Example code
model = Sequential()
model.add(Dense(64, input_dim=64, kernel_initializer='uniform', activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
weights = model.get_weights()
Output example
[array([[ 0.03738443, -0.00438571, -0.03912098, ..., 0.05268976,
0.06986515, -0.05466834],
[ 0.04930462, 0.06912202, -0.01011086, ..., 0.06700403,
-0.05242571, -0.06801051],
[ 0.05172399, -0.01971836, 0.03024069, ..., 0.06730981,
-0.00643077, 0.0496422 ],
...,
[-0.05837193, 0.00091718, 0.07959776, ..., 0.06146731,
-0.07918619, -0.07769193],
[-0.06421017, 0.0687856 , 0.05856385, ..., 0.07944132,
-0.06224829, -0.04787586],
[ 0.04507772, 0.05773481, -0.05817863, ..., 0.07451814,
0.06830471, 0.05868983]], dtype=float32),
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0.], dtype=float32)]
Code explanation
model = Sequential()
: This creates a Sequential model object, which is a linear stack of layers.model.add(Dense(64, input_dim=64, kernel_initializer='uniform', activation='relu'))
: This adds a dense layer with 64 neurons and a ReLU activation function. The input dimension is set to 64.model.add(Dense(32, activation='relu'))
: This adds a second dense layer with 32 neurons and a ReLU activation function.model.add(Dense(1, activation='sigmoid'))
: This adds a third dense layer with 1 neuron and a sigmoid activation function.weights = model.get_weights()
: This retrieves the weights of the model and stores them in a list of numpy arrays.
Helpful links
More of Python Keras
- How do I use zero padding in Python Keras?
- How do I use Python Keras to zip a file?
- What is Python Keras and how is it used?
- How do I use Keras with Python?
- How can I use word2vec and Keras to develop a machine learning model in Python?
- How can I install the python module tensorflow.keras in R?
- How do I use the to_categorical function from TensorFlow in Python to convert data into a format suitable for a neural network?
- How can I use Python Keras to create a neural network with zero hidden layers?
- How do I use Python Keras to create a Zoom application?
- How can I use XGBoost, Python and Keras together to build a machine learning model?
See more codes...