'Instantiate Keras model with some weights before training

I have Keras model: pre-trained CV model + a few added layers on top

I would want to be able to do model.predict before model.fit

Q: how do I instantiate model from the screenshot with some weights (random, zero or whatever)enter image description here



Solution 1:[1]

here a dummy example to initialize the model with some weights (random, zero or whatever)

def base_model(xx):
    
    x = Dense(32)(xx)
    x = Dense(8)(x)
    
    return Model(xx,x)

inp = Input((32,32,3))
x = base_model(inp)
x = GlobalAveragePooling2D()(x.output)
x = Dropout(0.3)(x)
out = Dense(10, activation='softmax')(x)

model = Model(inp,out)
model.summary()

# set weight with random number from a uniform... you can do the same also with zeros...
model.set_weights([np.random.uniform(0,1, i.shape) for i in model.get_weights()])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marco Cerliani