本文整理匯總了Python中tensorflow.python.keras.layers.Layer方法的典型用法代碼示例。如果您正苦於以下問題:Python layers.Layer方法的具體用法?Python layers.Layer怎麽用?Python layers.Layer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.python.keras.layers
的用法示例。
在下文中一共展示了layers.Layer方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: restore_model
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Layer [as 別名]
def restore_model(cls, model_bytes): # todo: restore optimizer to support incremental learning
with tempfile.TemporaryDirectory() as tmp_path:
with io.BytesIO(model_bytes) as bytes_io:
with zipfile.ZipFile(bytes_io, 'r', zipfile.ZIP_DEFLATED) as f:
f.extractall(tmp_path)
# Comment this block because tf 1.15 is not supporting Keras Customized Layer
# try:
# keras_model = tf.keras.models.load_model(filepath=tmp_path,
# custom_objects={'ConstantLayer': ConstantLayer})
# except IOError:
# import warnings
# warnings.warn('loading the model as SavedModel is still in experimental stages. '
# 'trying tf.keras.experimental.load_from_saved_model...')
keras_model = \
tf.keras.experimental.load_from_saved_model(saved_model_path=tmp_path,
custom_objects={'ConstantLayer': ConstantLayer})
model = cls()
model._set_model(keras_model)
return model
示例2: activation_fun
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Layer [as 別名]
def activation_fun(activation, fc):
if isinstance(activation, str):
fc = tf.keras.layers.Activation(activation)(fc)
elif issubclass(activation, Layer):
fc = activation()(fc)
else:
raise ValueError(
"Invalid activation,found %s.You should use a str or a Activation Layer Class." % (activation))
return fc
示例3: export_model
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Layer [as 別名]
def export_model(self):
with tempfile.TemporaryDirectory() as tmp_path:
# Comment this block because tf 1.15 is not supporting Keras Customized Layer
# try:
# # LOGGER.info("Model saved with model.save method.")
# tf.keras.models.save_model(self._model, filepath=tmp_path, save_format="tf")
# except NotImplementedError:
# import warnings
# warnings.warn('Saving the model as SavedModel is still in experimental stages. '
# 'trying tf.keras.experimental.export_saved_model...')
tf.keras.experimental.export_saved_model(self._model, saved_model_path=tmp_path)
model_bytes = zip_dir_as_bytes(tmp_path)
return model_bytes
示例4: activation_layer
# 需要導入模塊: from tensorflow.python.keras import layers [as 別名]
# 或者: from tensorflow.python.keras.layers import Layer [as 別名]
def activation_layer(activation):
if activation == "dice" or activation == "Dice":
act_layer = Dice()
elif (isinstance(activation, str)) or (sys.version_info.major == 2 and isinstance(activation, (str, unicode))):
act_layer = tf.keras.layers.Activation(activation)
elif issubclass(activation, Layer):
act_layer = activation()
else:
raise ValueError(
"Invalid activation,found %s.You should use a str or a Activation Layer Class." % (activation))
return act_layer