The class :class:`ModelLayer` converts a :class:`Model` to a :class:`Layer` instance. Note that only a :class:`Model` with specified inputs and outputs can be converted to a :class:`ModelLayer`. For example, a customized model in dynamic eager mode normally does NOT have specified inpu
| 468 | |
| 469 | |
| 470 | class ModelLayer(Layer): |
| 471 | """ |
| 472 | The class :class:`ModelLayer` converts a :class:`Model` to a :class:`Layer` instance. |
| 473 | |
| 474 | Note that only a :class:`Model` with specified inputs and outputs can be converted to a :class:`ModelLayer`. |
| 475 | For example, a customized model in dynamic eager mode normally does NOT have specified inputs and outputs so the |
| 476 | customized model in dynamic eager mode can NOT be converted to a :class:`ModelLayer`. |
| 477 | |
| 478 | Parameters |
| 479 | ---------- |
| 480 | model: tl.models.Model |
| 481 | A model. |
| 482 | name : str or None |
| 483 | A unique layer name. If None, a unique name will be automatically assigned. |
| 484 | |
| 485 | Methods |
| 486 | --------- |
| 487 | __init__() |
| 488 | Initializing the ModelLayer. |
| 489 | weights() |
| 490 | Same as the weights of the given model. |
| 491 | build() |
| 492 | Do nothing because the given model has already been built. |
| 493 | forward() |
| 494 | Forward the computation. Simply call the forward() of the given model. |
| 495 | """ |
| 496 | |
| 497 | def __init__(self, model, name=None): |
| 498 | """ |
| 499 | Initializing the ModelLayer given a instance of Model. |
| 500 | |
| 501 | :param model: tl.models.Model |
| 502 | """ |
| 503 | super(ModelLayer, self).__init__(name=name) |
| 504 | |
| 505 | self.model = model |
| 506 | |
| 507 | # Layer building state |
| 508 | self._built = True |
| 509 | |
| 510 | # Layer weight state |
| 511 | self._all_weights = model.all_weights |
| 512 | self._trainable_weights = model.trainable_weights |
| 513 | self._nontrainable_weights = model.nontrainable_weights |
| 514 | |
| 515 | # Layer training state |
| 516 | self.is_train = True |
| 517 | |
| 518 | logging.info("ModelLayer %s from Model: %s" % (self.name, self.model.name)) |
| 519 | |
| 520 | def __repr__(self): |
| 521 | tmpstr = 'ModelLayer' + '(\n' |
| 522 | |
| 523 | modstr = self.model.__repr__() |
| 524 | modstr = _addindent(modstr, 2) |
| 525 | |
| 526 | tmpstr += modstr + ')' |
| 527 | return tmpstr |
no outgoing calls
searching dependent graphs…