是的。
您可能必须保存图层的权重和偏差,而不是保存图层本身,但这是可能的。
Keras也允许你save entire models。
假设你在var model
有一个模型:
weightsAndBiases = model.layers[i].get_weights()
这是numpy的阵列的列表,很可能与两个数组:重和偏见。你可以简单地使用numpy.save()
保存这两个数组,以后你可以创建一个类似的层,并给它的权重:
from keras.layers import *
from keras.models import Model
inp = Input(....)
out1 = SomeKerasLayer(...)(inp)
out2 = AnotherKerasLayer(....)(out1)
....
model = Model(inp,out2)
#above is the usual process of creating a model
#supposing layer 2 is the layer you want (you can also use names)
weights = numpy.load(...path to your saved weights)
biases = numpy.load(... path to your saved biases)
model.layers[2].set_weights([weights,biases])
可以使层untrainable(必须在模型编译之前完成):
model.layers[2].trainable = False
然后编译模型:
model.compile(.....)
而且你去那里,一个模型,其一层为untrainable并具有由您定义的权重和偏见,从s拍摄其他地方。