我想基于我的自定义图层创建一个简单的NN模型,并从中提取权重。到目前为止,权重列表为空。起初,我无意训练,也没有定义任何损失函数或优化器。 我应该如何更改以便可以看到和提取权重?
我知道Saving model weights in Keras: what is model weights?,并打算像在那儿一样对model.save_weights()进行建模。
import tensorflow as tf
from tensorflow.contrib.keras import layers, models
import numpy as np
from tensorflow.contrib import eager as tfe
tfe.enable_eager_execution()
class MyLayer(layers.Layer):
def __init__(self, out_channels):
print("initializing MyLayer")
super(MyLayer, self).__init__()
self.out_channels = out_channels
self.bn = layers.BatchNormalization(name="bn1", momentum=0.04)
self.linear = layers.Dense(self.out_channels, activation=None, use_bias=False)
self.bn2 = layers.BatchNormalization(name="bn2", momentum=0.04)
self.linear2 = layers.Dense(32, activation=None, use_bias=False)
def build(self, input_shape):
print("building MyLayer")
super(MyLayer, self).build(input_shape)
def call(self, inputs, **kwargs):
print("calling MyLayer")
x = self.linear(tf.convert_to_tensor(inputs))
x = self.bn(x)
x = tf.nn.relu(x)
x = self.linear2(x)
x = self.bn2(x)
x = tf.nn.relu(x)
return x
if __name__ == '__main__':
o = MyLayer(16)
inputs = np.random.rand(1,2,3)
input_layer = layers.Input(shape=(2,3), batch_size=1)
features = o(input_layer)
model = models.Model([input_layer], [features])
feat_out = model.predict_on_batch([inputs])
print("weights: {}".format(model.get_weights()))
print("features: {}".format(feat_out))