当前位置: 首页>>代码示例>>Python>>正文


Python keras.Model方法代码示例

本文整理汇总了Python中tensorflow.keras.Model方法的典型用法代码示例。如果您正苦于以下问题:Python keras.Model方法的具体用法?Python keras.Model怎么用?Python keras.Model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tensorflow.keras的用法示例。


在下文中一共展示了keras.Model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: YoloConv

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def YoloConv(filters, name=None):
    def yolo_conv(x_in):
        if isinstance(x_in, tuple):
            inputs = Input(x_in[0].shape[1:]), Input(x_in[1].shape[1:])
            x, x_skip = inputs

            # concat with skip connection
            x = DarknetConv(x, filters, 1)
            x = UpSampling2D(2)(x)
            x = Concatenate()([x, x_skip])
        else:
            x = inputs = Input(x_in.shape[1:])

        x = DarknetConv(x, filters, 1)
        x = DarknetConv(x, filters * 2, 3)
        x = DarknetConv(x, filters, 1)
        x = DarknetConv(x, filters * 2, 3)
        x = DarknetConv(x, filters, 1)
        return Model(inputs, x, name=name)(x_in)

    return yolo_conv 
开发者ID:akkaze,项目名称:tf2-yolo3,代码行数:23,代码来源:models.py

示例2: YoloConvTiny

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def YoloConvTiny(filters, name=None):
    def yolo_conv(x_in):
        if isinstance(x_in, tuple):
            inputs = Input(x_in[0].shape[1:]), Input(x_in[1].shape[1:])
            x, x_skip = inputs

            # concat with skip connection
            x = DarknetConv(x, filters, 1)
            x = UpSampling2D(2)(x)
            x = Concatenate()([x, x_skip])
        else:
            x = inputs = Input(x_in.shape[1:])
            x = DarknetConv(x, filters, 1)

        return Model(inputs, x, name=name)(x_in)

    return yolo_conv 
开发者ID:akkaze,项目名称:tf2-yolo3,代码行数:19,代码来源:models.py

示例3: YoloV3Tiny

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def YoloV3Tiny(size=None,
               num_channels=3,
               anchors=yolo_tiny_anchors,
               masks=yolo_tiny_anchor_masks,
               num_classes=10,
               training=False):
    x = inputs = Input([*size, num_channels])

    x_8, x = DarknetTiny(name='yolo_darknet', num_channels=num_channels)(x)

    x = YoloConvTiny(128, name='yolo_conv_0')(x)
    output_0 = YoloOutput(128, len(masks[0]), num_classes, name='yolo_output_0')(x)

    x = YoloConvTiny(64, name='yolo_conv_1')((x, x_8))
    output_1 = YoloOutput(64, len(masks[1]), num_classes, name='yolo_output_1')(x)
    if training:
        return Model(inputs, (output_0, output_1), name='yolov3')
    boxes_0 = Lambda(lambda x: yolo_boxes(x, anchors[masks[0]], num_classes), name='yolo_boxes_0')(output_0)

    boxes_1 = Lambda(lambda x: yolo_boxes(x, anchors[masks[1]], num_classes), name='yolo_boxes_1')(output_1)
    outputs = Lambda(lambda x: yolo_nms(x, anchors, masks, num_classes), name='yolo_nms')((boxes_0[:3], boxes_1[:3]))
    return Model(inputs, outputs, name='yolov3_tiny') 
开发者ID:akkaze,项目名称:tf2-yolo3,代码行数:24,代码来源:models.py

示例4: __call__

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def __call__(self, model):
        """
        :param model: Keras model to be accelerated
        :type model: Union[keras.Model, keras.Sequential]
        :return: Accelerated Keras model
        :rtype: Union[keras.Model, keras.Sequential]
        """
        if isinstance(model, tfk.Model) or isinstance(model, tfk.Sequential):
            self.model = model
        else:
            raise TypeError(f'FastMCInference expects tensorflow.keras Model, you gave {type(model)}')
        new_input = tfk.layers.Input(shape=(self.model.input_shape[1:]), name='input')
        mc_model = tfk.models.Model(inputs=self.model.inputs, outputs=self.model.outputs)

        mc = FastMCInferenceMeanVar()(tfk.layers.TimeDistributed(mc_model)(FastMCRepeat(self.n)(new_input)))
        new_mc_model = tfk.models.Model(inputs=new_input, outputs=mc)

        return new_mc_model 
开发者ID:henrysky,项目名称:astroNN,代码行数:20,代码来源:layers.py

示例5: build_model

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def build_model(self, bert_params):
        l_bert = bert.BertModelLayer.from_params(bert_params, name="bert")

        l_input_ids = keras.layers.Input(shape=(128,), dtype='int32', name="input_ids")
        l_token_type_ids = keras.layers.Input(shape=(128,), dtype='int32', name="token_type_ids")
        output = l_bert([l_input_ids, l_token_type_ids])
        output = keras.layers.Lambda(lambda x: x[:, 0, :])(output)
        output = keras.layers.Dense(2)(output)
        model = keras.Model(inputs=[l_input_ids, l_token_type_ids], outputs=output)

        model.build(input_shape=(None, 128))
        model.compile(optimizer=keras.optimizers.Adam(),
                      loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                      metrics=[keras.metrics.SparseCategoricalAccuracy(name="acc")])

        for weight in l_bert.weights:
            print(weight.name)

        return model, l_bert 
开发者ID:kpe,项目名称:bert-for-tf2,代码行数:21,代码来源:test_load_pretrained_weights.py

示例6: _test_single_mode

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def _test_single_mode(layer, **kwargs):
    sparse = kwargs.pop('sparse', False)
    A_in = Input(shape=(None,), sparse=sparse)
    X_in = Input(shape=(F,))
    inputs = [X_in, A_in]
    if sparse:
        input_data = [X, sp_matrix_to_sp_tensor(A)]
    else:
        input_data = [X, A]

    if kwargs.pop('edges', None):
        E_in = Input(shape=(S, ))
        inputs.append(E_in)
        input_data.append(E_single)

    layer_instance = layer(**kwargs)
    output = layer_instance(inputs)
    model = Model(inputs, output)

    output = model(input_data)

    assert output.shape == (N, kwargs['channels']) 
开发者ID:danielegrattarola,项目名称:spektral,代码行数:24,代码来源:test_convolutional.py

示例7: _test_batch_mode

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def _test_batch_mode(layer, **kwargs):
    A_batch = np.stack([A] * batch_size)
    X_batch = np.stack([X] * batch_size)

    A_in = Input(shape=(N, N))
    X_in = Input(shape=(N, F))
    inputs = [X_in, A_in]
    input_data = [X_batch, A_batch]

    if kwargs.pop('edges', None):
        E_batch = np.stack([E] * batch_size)
        E_in = Input(shape=(N, N, S))
        inputs.append(E_in)
        input_data.append(E_batch)

    layer_instance = layer(**kwargs)
    output = layer_instance(inputs)
    model = Model(inputs, output)

    output = model(input_data)

    assert output.shape == (batch_size, N, kwargs['channels']) 
开发者ID:danielegrattarola,项目名称:spektral,代码行数:24,代码来源:test_convolutional.py

示例8: _test_mixed_mode

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def _test_mixed_mode(layer, **kwargs):
    sparse = kwargs.pop('sparse', False)
    X_batch = np.stack([X] * batch_size)
    A_in = Input(shape=(N,), sparse=sparse)
    X_in = Input(shape=(N, F))
    inputs = [X_in, A_in]
    if sparse:
        input_data = [X_batch, sp_matrix_to_sp_tensor(A)]
    else:
        input_data = [X_batch, A]

    layer_instance = layer(**kwargs)
    output = layer_instance(inputs)
    model = Model(inputs, output)

    output = model(input_data)

    assert output.shape == (batch_size, N, kwargs['channels']) 
开发者ID:danielegrattarola,项目名称:spektral,代码行数:20,代码来源:test_convolutional.py

示例9: get_data_and_model

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def get_data_and_model(optimizer="moving_avg"):
    x = tf.random.normal([TRAIN_SAMPLES, INPUT_DIM])
    y = tf.random.normal([TRAIN_SAMPLES, NUM_CLASSES])
    moving_avg = MovingAverage(
        tf.keras.optimizers.SGD(lr=2.0), sequential_update=True, average_decay=0.5
    )
    if optimizer == "moving_avg":
        optimizer = moving_avg
    inputs = keras.layers.Input(INPUT_DIM)
    hidden_layer = keras.layers.Dense(
        NUM_HIDDEN, input_dim=INPUT_DIM, activation="relu"
    )(inputs)
    outputs = keras.layers.Dense(NUM_CLASSES, activation="softmax")(hidden_layer)
    model = keras.Model(inputs=inputs, outputs=outputs)
    model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["acc"])
    return x, y, model 
开发者ID:tensorflow,项目名称:addons,代码行数:18,代码来源:avg_model_checkpoint_test.py

示例10: __init__

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def __init__(self,
                 model: Model,
                 tasks: List[Task],
                 optimizer: str = 'adam',
                 learning_rate: Union[float, Callable[[int], float]] = 1e-3,
                 gradient_clipping: str = 'norm',
                 gradient_clipping_bounds: Union[float, Tuple[float, float]] = 1.,
                 return_loss_summaries: bool = False,
                 return_variable_summaries: bool = False,
                 return_grad_summaries: bool = False,
                 distribution_strategy: Type[DistributionStrategy] = MirroredStrategy,
                 use_memory_saving_gradients: bool = False) -> None:

        learning_rate_func = PiecewiseSchedule(
            [(0, 1e-6),
             (100, 1e-4),
             (1000, learning_rate)],
            outside_value=learning_rate)

        super().__init__(
            model, optimizer, learning_rate_func.value, gradient_clipping, gradient_clipping_bounds,
            return_loss_summaries, return_variable_summaries, return_grad_summaries,
            distribution_strategy(), use_memory_saving_gradients)
        self._tasks = tasks 
开发者ID:songlab-cal,项目名称:tape-neurips2019,代码行数:26,代码来源:experiments.py

示例11: create_model

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def create_model(trainable=False):
    model = MobileNetV2(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), include_top=False, alpha=ALPHA)

    # to freeze layers
    for layer in model.layers:
        layer.trainable = trainable

    out = model.layers[-1].output

    x = Conv2D(4, kernel_size=3)(out)
    x = Reshape((4,), name="coords")(x)

    y = GlobalAveragePooling2D()(out)
    y = Dense(CLASSES, name="classes", activation="softmax")(y)

    return Model(inputs=model.input, outputs=[x, y]) 
开发者ID:lars76,项目名称:object-localization,代码行数:18,代码来源:train.py

示例12: create_model

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def create_model(trainable=False):
    model = MobileNetV2(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3), include_top=False, alpha=ALPHA, weights="imagenet")

    for layer in model.layers:
        layer.trainable = trainable

    block = model.get_layer("block_16_project_BN").output

    x = Conv2D(112, padding="same", kernel_size=3, strides=1, activation="relu")(block)
    x = Conv2D(112, padding="same", kernel_size=3, strides=1, use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    x = Conv2D(5, padding="same", kernel_size=1, activation="sigmoid")(x)

    model = Model(inputs=model.input, outputs=x)

    # divide by 2 since d/dweight learning_rate * weight^2 = 2 * learning_rate * weight
    # see https://arxiv.org/pdf/1711.05101.pdf
    regularizer = l2(WEIGHT_DECAY / 2)
    for weight in model.trainable_weights:
        with tf.keras.backend.name_scope("weight_regularizer"):
            model.add_loss(regularizer(weight)) # in tf2.0: lambda: regularizer(weight)

    return model 
开发者ID:lars76,项目名称:object-localization,代码行数:27,代码来源:train.py

示例13: hybrid_model

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def hybrid_model():
  """hybrid model that mixes qkeras and keras layers."""

  x = x_in = keras.layers.Input((784,), name="input")
  x = keras.layers.Dense(300, name="d0")(x)
  x = keras.layers.Activation("relu", name="d0_act")(x)
  x = QDense(100, kernel_quantizer=quantizers.quantized_bits(4, 0, 1),
             bias_quantizer=quantizers.quantized_bits(4, 0, 1),
             name="d1")(x)
  x = QActivation("quantized_relu(4,0)", name="d1_qr4")(x)
  x = QDense(
      10, kernel_quantizer=quantizers.quantized_bits(4, 0, 1),
      bias_quantizer=quantizers.quantized_bits(4, 0, 1),
      name="d2")(x)
  x = keras.layers.Activation("softmax", name="softmax")(x)

  return keras.Model(inputs=[x_in], outputs=[x]) 
开发者ID:google,项目名称:qkeras,代码行数:19,代码来源:example_get_energy.py

示例14: hybrid_model

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def hybrid_model():
  """hybrid model that mixes qkeras and keras layers."""

  x = x_in = keras.layers.Input((784,), name="input")
  x = keras.layers.Dense(300, name="d0")(x)
  x = keras.layers.Activation("relu", name="d0_act")(x)
  x = QDense(100, kernel_quantizer=quantizers.quantized_po2(4),
             bias_quantizer=quantizers.quantized_po2(4),
             name="d1")(x)
  x = QActivation("quantized_relu(4,0)", name="d1_qr4")(x)
  x = QDense(
      10, kernel_quantizer=quantizers.quantized_po2(4),
      bias_quantizer=quantizers.quantized_po2(4),
      name="d2")(x)
  x = keras.layers.Activation("softmax", name="softmax")(x)

  return keras.Model(inputs=[x_in], outputs=[x]) 
开发者ID:google,项目名称:qkeras,代码行数:19,代码来源:example_generate_json.py

示例15: qdense_model_fork

# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import Model [as 别名]
def qdense_model_fork():
  x = x_in = keras.layers.Input((23,), name="input")
  x = QDense(
      10,
      kernel_quantizer=quantizers.quantized_bits(5, 0, 1),
      bias_quantizer=quantizers.quantized_bits(5, 0, 1),
      activation=quantizers.quantized_po2(3, 1),
      name="qdense_0")(x)
  x = QDense(
      20,
      kernel_quantizer=quantizers.quantized_bits(5, 0, 1),
      bias_quantizer=quantizers.quantized_bits(5, 0, 1),
      activation=quantizers.quantized_relu(6, 2),
      name="qdense_1")(x)
  x = QActivation("quantized_relu(4)", name="QA_2")(x)
  x_1 = QDense(
      30,
      kernel_quantizer=quantizers.binary(),
      bias_quantizer=quantizers.binary(),
      name="qdense_3")(x)
  x_2 = QActivation("quantized_relu(6,2)", name="QA_3")(x)

  model = keras.Model(
      inputs=[x_in], outputs=[x_1, x_2,])
  return model 
开发者ID:google,项目名称:qkeras,代码行数:27,代码来源:qtools_model_test.py


注:本文中的tensorflow.keras.Model方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。