问题
For example, if I load somebody else's model, this is what I see:
I want to get the code representation of this, for example:
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
... etc
Not saying that the above is correct, but I want to know if there a way for me to physically reconstruct the model in code, including all activation functions?
I guess I can read the summary, but I don't know if I will be able to determine activation from it.
How would one do this?
回答1:
If you have a model saved with complete architecture along with its training states. ie u have used something like this.
model.save('myfirstmodel.h5')
You can use
pprint(model.to_json())
pprint(model.to_yaml())
Output for json:
('{"class_name": "Sequential", "config": {"name": "sequential", "layers": '
'[{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 13], '
'"dtype": "float32", "sparse": false, "ragged": false, "name": "d1_input"}}, '
'{"class_name": "Dense", "config": {"name": "d1", "trainable": true, '
'"batch_input_shape": [null, 13], "dtype": "float32", "units": 4, '
'"activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": '
'"Ones", "config": {}}, "bias_initializer": {"class_name": "Zeros", "config": '
'{}}, "kernel_regularizer": null, "bias_regularizer": null, '
'"activity_regularizer": null, "kernel_constraint": null, "bias_constraint": '
'null}}, {"class_name": "Dense", "config": {"name": "d2", "trainable": true, '
'"dtype": "float32", "units": 6, "activation": "relu", "use_bias": true, '
'"kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": '
'null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, '
'"kernel_regularizer": null, "bias_regularizer": null, '
'"activity_regularizer": null, "kernel_constraint": null, "bias_constraint": '
'null}}, {"class_name": "Dropout", "config": {"name": "dropout", "trainable": '
'true, "dtype": "float32", "rate": 0.2, "noise_shape": null, "seed": null}}, '
'{"class_name": "Dense", "config": {"name": "out", "trainable": true, '
'"dtype": "float32", "units": 2, "activation": "sigmoid", "use_bias": true, '
'"kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": '
'null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, '
'"kernel_regularizer": null, "bias_regularizer": null, '
'"activity_regularizer": null, "kernel_constraint": null, "bias_constraint": '
'null}}]}, "keras_version": "2.4.0", "backend": "tensorflow"}')
However, if you have a frozen model in which your normal means dont work you can take a look at the structure of the model using netron. It shows layer-wise architecture as well as which activation functions are used, parameter as well as its weights. You can download these weights as NumPy arrays.
You can use Netron in order to find the architecture of the model along with weights. Using this structural information you can rebuild your model.
See Link.
You get output like this:
回答2:
You can use model.get_config()
to get a dict
of all the information you need to re-instantiate an identical model (at least in theory, since this depends on each layer in the model having a correct get_config
method itself, which historically hasn't always been the case).
If your imported model is well-behaved, then you might even be able to create a clone model just by doing
new_model = tf.keras.Model.from_config(model.get_config())
This won't always work, so in general, you can just start with model.get_config()
to inspect the details of a model.
回答3:
The answer is NO. There is no way in tf or Keras to get code representation from model file or YAML or JSON. But yes, you can write a piece of code which will do this job. It should be pretty easy. If you save your model like the following:
model.to_yaml()
It saves your model configuration along with the activation functions. All you need to do it to go through the layers one by one and add the code representation of the layer. But loss function can be an issue for you.
Btw, if you want to retrain the saved model, you don't need the code structure. All you can is just load and train. Just don't freeze any layers. It will retrain and update all the path weights.
来源:https://stackoverflow.com/questions/63604713/in-tensorflow-is-it-possible-to-see-another-models-build-structure