tf.keras模块中的Model类
tf.keras模块中的Model类class Model从输入输出建立Model继承Model类建立ModelModel类中的methodclass ModelModel groups layers into an object with training and inference features.tensorflow API从输入输出建立ModelWith the “functional
·
tf.keras模块中的Model类
class Model
Model groups layers into an object with training and inference features.
tensorflow API
从输入输出建立Model
- 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 frominputs
andoutputs
:
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
- By subclassing the Model class: in that case, you should define your layers in
__init__
and you should implement the model’s forward pass incall
. - 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()
- Once the model is created, you can config the model with losses and
metrics withmodel.compile()
, train the model withmodel.fit()
, or
use the model to do prediction withmodel.predict()
.
Model类中的method
- compile
compile(
optimizer='rmsprop', loss=None, metrics=None, loss_weights=None,
sample_weight_mode=None, weighted_metrics=None, **kwargs
)
更多推荐
已为社区贡献1条内容
所有评论(0)