简体   繁体   中英

How to detect tf/keras model objects automatically

I'm working on supporting automatic model detection/logging for Keras/Tensorflow models for our Machine learning platform https://iko.ai and I have some questions:

  • What are the different ways we can define a tf/keras model?

    1. tf.keras.Model
    2. tf.Estimator
    3. tensorflow_estimator
  • Any other ways I'm not aware of? Why are there so many ways to do the same thing?

  • What are the proper functions to save/load them?

  • How could we differentiate TF/Keras model instances from other non-model objects? I want to be able to write a function that checks if an object is a TF/Keras model, something like

def is_tf_or_keras_model(obj):
    # check somehow if the obj is a TF/Keras model
    pass

Regarding questions 1 and 2:

Another way to represent a neural.network model is by using tf.keras.Sequential . It allows you to easily create a model that follows a sequential structure, for example:

import tensorflow as tf
# tf.__version__ == 2.4

model_seq = tf.keras.Sequential()
model_seq.add(tf.keras.Input(shape=(64,1)))
model_seq.add(tf.keras.layers.Conv1D(32, 2, activation='relu'))
model_seq.add(tf.keras.layers.Dense(16, activation='relu'))
model_seq.add(tf.keras.layers.Dense(4, activation='softmax'))

This is the same as using the tf.keras functional API. This allows to build more complex.networks that do not follow a sequential structure, but of course you can build sequential models anyway:

i = tf.keras.Input(shape=(64,1))
x = tf.keras.layers.Conv1D(32, 2, activation='relu')(i)
x = tf.keras.layers.Dense(16, activation='relu')(x)
x = tf.keras.layers.Dense(4, activation='softmax')(x)
model_func = tf.keras.Model(inputs=i, outputs=x)

tf.estimator.Estimator is used to train prebuilt.networks, so you only have to define some hyperparameter values and train out of the box model.

In conclusion, the method used to build a model depends on how complex/ad-hoc the.network is.

Regarding question 3:

The tf.keras implementation allows you to either save the weights of a model ( model.save_weights ) or save the whole model ( model.save ). To load the model weights afterwards you need to create the model first, following the same structure of the model used to train those weights.

Regarding question 4:

model_classes = [tf.keras.Model, keras.Model, tf.estimator.Estimator]

def is_keras_or_tf_model(obj, model_classes):
    return isinstance(obj, model_clases)

All I have said here is in the TensorFlow documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM