def test_set_weight():
input_imgs = Input(shape=(784, ))
W = np.random.rand(784, 10)
b = np.random.rand(10)
encoded = Dense(10, activation='relu').set_weights(np.array([W, b]))(input_imgs)
if __name__ == '__main__':
test_set_weight()
Here, I want to set a self-defined weights for the Dense layer, however, I got this error:
Using TensorFlow backend.
Traceback (most recent call last):
File "/home/thl/my_task/DeepMT/pyspace/ML/SdA_demo_mnist_v2.py", line 134, in <module>
test_set_weight()
File "/home/thl/my_task/DeepMT/pyspace/ML/SdA_demo_mnist_v2.py", line 128, in test_set_weight
encoded = Dense(10, activation='relu').set_weights(np.array([W, b]))(input_imgs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 1169, in set_weights
str(weights)[:50] + '...')
ValueError: You called `set_weights(weights)` on layer "dense_1" with a weight list of length 2, but the layer was expecting 0 weights. Provided weights: [ array([[ 0.12146454, 0.43726934, 0.61852376, ....
then I delete [W, b] and run it again
def test_set_weight():
input_imgs = Input(shape=(784, ))
encoded = Dense(10, activation='relu').set_weights()(input_imgs)
if __name__ == '__main__':
test_set_weight()
it gives:
Using TensorFlow backend.
Traceback (most recent call last):
File "/home/thl/my_task/DeepMT/pyspace/ML/SdA_demo_mnist_v2.py", line 134, in <module>
test_set_weight()
File "/home/thl/my_task/DeepMT/pyspace/ML/SdA_demo_mnist_v2.py", line 128, in test_set_weight
encoded = Dense(10, activation='relu').set_weights()(input_imgs)
TypeError: set_weights() takes exactly 2 arguments (1 given)
It is really confusing.
From the second error, the set_weights()needs 2 arguments. I think they are W and b. However, when I added [W, b] in set_weights(), it returns but the layer was expecting 0 weights. (the first error)
What am I supposed to do?
Thanks!
This works:
from keras.layers import Input, Dense
import numpy as np
def test_set_weight():
input_imgs = Input(shape=(784, ))
W = np.random.rand(784, 10)
b = np.random.rand(10)
encoded = Dense(10, activation='relu', weights=[W,b])(input_imgs)
if __name__ == '__main__':
test_set_weight()
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.
I have faced a similar problem and found the solution is to add the layer to an existing model first, and then invoke set_weights. So for your example. I propose to do the following:
def test_set_weight():
input_imgs = Input(shape=(784, ))
W = np.random.rand(784, 10)
b = np.random.rand(10)
dense_layer = Dense(10, activation='relu').set_weights(np.array([W, b]))
encoded = dense_layer(input_imgs)
model = Model(inputs=input_imgs, outputs=encoded)
dense_layer.set_weights(weights)
if you use it in this way, no error occurred.
def test_set_weight():
input_imgs = Input(shape=(784, ))
W = np.random.rand(784, 10)
b = np.random.rand(10)
encoded = Dense(10, activation='relu').set_weights([W, b])(input_imgs)
if __name__ == '__main__':
test_set_weight()
Most helpful comment
I have faced a similar problem and found the solution is to add the layer to an existing model first, and then invoke
set_weights. So for your example. I propose to do the following: