當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。