Reputation: 815
I used a simple regression problem as below to illustrate my question.
I have a model with various layers. For two specific layers, conv1
and dense2
, they have parameters in size of "2x2 = 4" and "4x3 + 1x3 = 15" respectively.
I want to import the weight into the layers before doing the training. To be specific, I want to import w_conv=np.array([[1,2],[3,4]])
into conv1
, and I want to import w_dense = np.array([[2,3,1],[6,7,7],[8,9,4],[5,4,8]])
and b_dense = np.array([4,7,9])
into dense2
. What should I do to import those values?
Besides, is it also possible to have some codes to check the value of the weight of the layers, before and after importing the values? Many thanks!
from __future__ import absolute_import, division, print_function
from scipy import misc
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, BatchNormalization, Activation
import numpy as np
import matplotlib.pyplot as plt
img1=np.array([[3,5,7,8],[4,7,9,6],[2,9,7,3],[0,2,4,7]])
img2=np.array([[4,6,2,7],[9,2,1,5],[7,5,4,8],[0,8,1,4]])
data1=np.reshape(img1,(4,4,1))
data2=np.reshape(img2,(4,4,1))
train_images=np.stack((data1, data2))
train_labels=np.array([[2,8,9],[1, 0, 7]])
def simple_model():
input = keras.layers.Input((4,4,1))
a=keras.layers.Conv2D(kernel_size=2,filters=1,strides=1, \
padding='same', use_bias=False, name='conv1')(input)
b=output = keras.layers.Flatten()(a)
c = keras.layers.Dense(4, activation=tf.nn.sigmoid)(b)
output = keras.layers.Dense(3, activation=tf.nn.sigmoid)(c)
model = keras.models.Model(input, output)
return model
model = simple_model()
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['accuracy','mean_squared_error'])
print(model.summary())
model.fit(train_images, train_labels, epochs=2)
Upvotes: 0
Views: 312
Reputation: 6034
You can use the get_weights()
and set_weights()
to get and set the weights of a layer respectively.
Iterate through the layers and set the weights:
for layer in model.layers:
print(layer.name)
input_1
conv1
flatten
dense
dense_1
print(model.layers[1].get_weights()[0].shape)
(2, 2, 1, 1) # (kernel_size), ip_channels, op_channels
Setting the weights of conv1 layer by knowing the layer index:
w_conv=np.array([[1,2],[3,4]])
w_conv = np.expand_dims(np.expand_dims(w_conv, -1), -1)
# pass a list of weights, since bias is false, the list has only one element
model.layers[1].set_weights([w_conv])
# get the weights back, first element of the list
assert np.array_equal(w_conv, model.layers[1].get_weights()[0])
Similarly, for the last dense layer:
w_dense = np.array([[2,3,1],[6,7,7],[8,9,4],[5,4,8]])
b_dense = np.array([4,7,9])
model.layers[-1].set_weights([w_dense, b_dense])
model.layers[-1].get_weights()
Upvotes: 1