我是靠谱客的博主 醉熏裙子,最近开发中收集的这篇文章主要介绍tf.keras模块中的Model类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

tf.keras模块中的Model类

    • class Model
    • 从输入输出建立Model
    • 继承Model类建立Model
    • Model类中的method

class Model

Model groups layers into an object with training and inference features.
tensorflow API

从输入输出建立Model

  1. With the “functional API”, where you start from Input, you chain
    layer calls to specify the model’s forward pass, and finally you
    create your model from inputs and outputs:
import tensorflow as tf

inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)

继承Model类建立Model

  1. By subclassing the Model class: in that case, you should define your layers in __init__ and you should implement the model’s forward pass in call.
  2. If you subclass Model, you can optionally have a training argument (boolean) in call, which you can use to specify a different behavior in training and inference:
import tensorflow as tf

class MyModel(tf.keras.Model):

  def __init__(self):
    super(MyModel, self).__init__()
    self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
    self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)

  def call(self, inputs):
    x = self.dense1(inputs)
    if training:
      x = self.dropout(x, training=training)
    return self.dense2(x)

model = MyModel()
  1. Once the model is created, you can config the model with losses and
    metrics with model.compile(), train the model with model.fit(), or
    use the model to do prediction with model.predict().

Model类中的method

  1. compile
compile(
    optimizer='rmsprop', loss=None, metrics=None, loss_weights=None,
    sample_weight_mode=None, weighted_metrics=None, **kwargs
)

最后

以上就是醉熏裙子为你收集整理的tf.keras模块中的Model类的全部内容,希望文章能够帮你解决tf.keras模块中的Model类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部