Example #1
0
 def _model_fn(features, labels, mode):
   """Model function."""
   assert labels is None, labels
   (all_scores, model_predictions, losses,
    training_op) = clustering_ops.KMeans(
        self._parse_tensor_or_dict(features),
        self._num_clusters,
        self._training_initial_clusters,
        self._distance_metric,
        self._use_mini_batch,
        random_seed=self._random_seed,
        kmeans_plus_plus_num_retries=self.
        _kmeans_plus_plus_num_retries).training_graph()
   incr_step = state_ops.assign_add(variables.get_global_step(), 1)
   loss = math_ops.reduce_sum(losses, name=KMeansClustering.LOSS_OP_NAME)
   logging_ops.scalar_summary('loss/raw', loss)
   training_op = with_dependencies([training_op, incr_step], loss)
   predictions = {
       KMeansClustering.ALL_SCORES: all_scores[0],
       KMeansClustering.CLUSTER_IDX: model_predictions[0],
   }
   eval_metric_ops = {KMeansClustering.SCORES: loss,}
   return ModelFnOps(
       mode=mode,
       predictions=predictions,
       eval_metric_ops=eval_metric_ops,
       loss=loss,
       train_op=training_op)
Example #2
0
def kmeans_cluster_model_fn(features, labels, mode, params, config):
    """Model function for KMeansClustering estimator."""
    # https://wiki.python.org/moin/UsingAssertionsEffectively
    assert labels is None, "labels are not needed: " + labels

    #   clustering_ops.KMeans 是重要的算法实现过程
    #   https://github.com/tensorflow/tensorflow/blob/master/tensorflow
    # /contrib/factorization/python/ops/clustering_ops.py
    (all_scores, model_predictions, losses,
     is_initialized, cluster_centers_var, init_op, training_op) = \
        clustering_ops.KMeans(
        _parse_tensor_or_dict(features),
        params.get('num_clusters'),
        initial_clusters=clustering_ops.RANDOM_INIT,
        distance_metric=clustering_ops.SQUARED_EUCLIDEAN_DISTANCE,
        use_mini_batch=False,
        mini_batch_steps_per_iteration=1,
        # use_mini_batch = params.get('use_mini_batch'),
        # mini_batch_steps_per_iteration=params.get(
        #     'mini_batch_steps_per_iteration'),
        random_seed=params.get('random_seed'),
        kmeans_plus_plus_num_retries=params.get(
            'kmeans_plus_plus_num_retries')).training_graph()

    incr_step = state_ops.assign_add(variables.get_global_step(), 1)
    loss = math_ops.reduce_sum(losses, name='kmeans_loss')
    #  Outputs a Summary protocol buffer containing a single scalar value.
    #  Used for visualizing in TensorBoard
    summary.scalar('loss/raw', loss)
    #  https://github.com/tensorflow/tensorflow/blob/master/tensorflow
    # /python/ops/control_flow_ops.py
    #  with_dependencies(dependencies, output_tensor, name=None):
    #  Produces the content of `output_tensor` only after `dependencies`.
    training_op = with_dependencies([training_op, incr_step], loss)
    predictions = {
        'all_scores': all_scores[0],
        'cluster_idx': model_predictions[0],
    }
    eval_metric_ops = {'scores': loss}

    #  Hook for monitor
    training_hooks = [
        _InitializeClustersHook(init_op, is_initialized, config.is_chief)
    ]
    relative_tolerance = params.get('relative_tolerance')
    if relative_tolerance is not None:
        training_hooks.append(_LossRelativeChangeHook(relative_tolerance))

    return ModelFnOps(mode=mode,
                      predictions=predictions,
                      eval_metric_ops=eval_metric_ops,
                      loss=loss,
                      train_op=training_op,
                      training_hooks=training_hooks)
Example #3
0
def _kmeans_clustering_model_fn(features, labels, mode, params, config):
    """Model function for KMeansClustering estimator."""
    assert labels is None, labels
    (all_scores, model_predictions, losses, is_initialized, init_op,
     training_op) = clustering_ops.KMeans(
         _parse_tensor_or_dict(features),
         params.get('num_clusters'),
         initial_clusters=params.get('training_initial_clusters'),
         distance_metric=params.get('distance_metric'),
         use_mini_batch=params.get('use_mini_batch'),
         mini_batch_steps_per_iteration=params.get(
             'mini_batch_steps_per_iteration'),
         random_seed=params.get('random_seed'),
         kmeans_plus_plus_num_retries=params.get(
             'kmeans_plus_plus_num_retries')).training_graph()
    incr_step = state_ops.assign_add(variables.get_global_step(), 1)
    loss = math_ops.reduce_sum(losses, name=KMeansClustering.LOSS_OP_NAME)
    summary.scalar('loss/raw', loss)
    training_op = with_dependencies([training_op, incr_step], loss)
    predictions = {
        KMeansClustering.ALL_SCORES: all_scores[0],
        KMeansClustering.CLUSTER_IDX: model_predictions[0],
    }
    eval_metric_ops = {KMeansClustering.SCORES: loss}
    training_hooks = [
        _InitializeClustersHook(init_op, is_initialized, config.is_chief)
    ]
    relative_tolerance = params.get('relative_tolerance')
    if relative_tolerance is not None:
        training_hooks.append(_LossRelativeChangeHook(relative_tolerance))
    return ModelFnOps(mode=mode,
                      predictions=predictions,
                      eval_metric_ops=eval_metric_ops,
                      loss=loss,
                      train_op=training_op,
                      training_hooks=training_hooks)
Example #4
0
def embed_lestm_model_fn(features, labels, mode, params):
    inputs = features["inputs"]

    with tf.name_scope("Embedding"):
        embeddings = tf.get_variable(
            name="embeddings",
            shape=[params["vocabulary_size"], params["embedding_size"]],
            initializer=tf.random_uniform_initializer(minval=-1, maxval=1))

        embedding_lookup = tf.nn.embedding_lookup(params=embeddings,
                                                  ids=inputs)

    with tf.name_scope("LSTM"):
        if params["use_gru"]:
            cell = tf.contrib.rnn.GRUCell(num_units=params["lstm_units"])
        else:
            cell = tf.contrib.rnn.LSTMCell(num_units=params["lstm_units"],
                                           num_proj=params["lstm_projection"],
                                           activation=tf.tanh)

        cell = tf.contrib.rnn.MultiRNNCell(cells=[cell] *
                                           params["lstm_cell_count"])

        lstm_output, lstm_state = tf.nn.dynamic_rnn(cell=cell,
                                                    inputs=embedding_lookup,
                                                    dtype=tf.float32)

        lstm_output = tf.reshape(
            tensor=lstm_output,
            shape=[-1, params["lstm_projection"] * params["max_inputs_len"]])

    with tf.name_scope("Fully_connected"):
        fc_layer = lstm_output
        for fc_layer_size in params["fc_layers_size"]:
            fc_layer = tf.layers.dense(
                inputs=fc_layer,
                units=fc_layer_size,
                activation=tf.nn.relu,
                use_bias=True,
                kernel_initializer=tf.truncated_normal_initializer(mean=0,
                                                                   stddev=0.1),
                bias_initializer=tf.constant_initializer(value=0))

    dropout = tf.layers.dropout(inputs=fc_layer,
                                rate=params["dropout"],
                                training=mode == learn.ModeKeys.TRAIN)

    logits = tf.layers.dense(
        inputs=dropout,
        units=params["nb_classes"],
        activation=tf.identity,
        use_bias=True,
        kernel_initializer=tf.truncated_normal_initializer(mean=0, stddev=0.1),
        bias_initializer=tf.constant_initializer(value=0))

    predictions = \
    {
        "classes" : tf.argmax(input = logits, axis = 1),
        "probabilities" : tf.nn.softmax(logits = logits)
    }

    loss = None
    train_op = None
    eval_metric_ops = None

    if mode != learn.ModeKeys.INFER:
        cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
            logits=logits, labels=tf.one_hot(labels, params["nb_classes"]))
        loss = tf.reduce_mean(cross_entropy, name="loss")

    if mode == learn.ModeKeys.TRAIN:
        train_op = tf.contrib.layers.optimize_loss(
            loss=loss,
            global_step=tf.contrib.framework.get_global_step(),
            learning_rate=params["learning_rate"],
            optimizer=params["optimizer"])

    if mode == learn.ModeKeys.EVAL:
        eval_metric_ops = {
            "accuracy":
            tf.metrics.accuracy(labels=labels,
                                predictions=predictions["classes"])
        }

    return ModelFnOps(mode=mode,
                      predictions=predictions,
                      loss=loss,
                      train_op=train_op,
                      eval_metric_ops=eval_metric_ops)
Example #5
0
def cldnn_model_fn(features, labels, mode, params):
    inputs = tf.cast(features["mfcc"], tf.float32)
    inputs_lengths = features["mfcc_lengths"]

    #with tf.name_scope("Convolution"):
    #    input_layer = tf.reshape(inputs, shape = [-1, params["max_timesteps"], params["mfcc_features_count"], 1])

    #    conv_layer1 = tf.layers.conv2d(
    #        inputs = input_layer,
    #        filters = params["conv1_features_count"],
    #        kernel_size = [params["conv1_kernel_size1"], params["conv1_kernel_size2"]],
    #        padding = "same",
    #        activation = tf.nn.relu)

    #    pool_layer = tf.layers.max_pooling2d(
    #        inputs = conv_layer1,
    #        pool_size = [1, params["max_pooling_size"]],
    #        strides = [1, params["max_pooling_size"]])

    #    conv_layer2 = tf.layers.conv2d(
    #        inputs = pool_layer,
    #        filters = params["conv2_features_count"],
    #        kernel_size = [params["conv2_kernel_size1"], params["conv2_kernel_size2"]],
    #        padding = "same",
    #        activation = tf.nn.relu)

    #    pool_output_freq_size = int(params["mfcc_features_count"] / params["max_pooling_size"])

    #    conv_layer2_flat = tf.reshape(
    #        conv_layer2,
    #        shape = [-1, params["max_timesteps"] * pool_output_freq_size * params["conv2_features_count"]]
    #        )

    #with tf.name_scope("Dimension_reduction"):
    #    dim_reduction_layer = tf.layers.dense(
    #        inputs = conv_layer2_flat,
    #        units = params["max_timesteps"] *params["dimension_reduction_size"],
    #        activation = tf.nn.relu)

    #dim_reduction_layer = tf.reshape(
    #    tensor = dim_reduction_layer,
    #    shape = [-1, params["max_timesteps"], params["dimension_reduction_size"]])

    #with tf.name_scope("Concatenation"):
    #    inputs = tf.reshape(
    #        tensor = inputs,
    #        shape = [-1, params["max_timesteps"], params["mfcc_features_count"]])
    #    concatenation_layer = tf.concat(
    #        values = [inputs, dim_reduction_layer],
    #        axis = 2)

    with tf.name_scope("Recurrent"):
        inputs_reshape = tf.reshape(
            tensor=inputs,
            shape=[-1, params["max_timesteps"], params["mfcc_features_count"]])

        inputs_reshape = tf.transpose(inputs_reshape, [0, 2, 1])

        lstm_cell = tf.contrib.rnn.LSTMCell(num_units=params["lstm_units"],
                                            num_proj=params["lstm_projection"],
                                            activation=tf.tanh)

        lstm_cell = tf.contrib.rnn.MultiRNNCell(cells=[lstm_cell] *
                                                params["lstm_cell_count"])

        lstm_output, lstm_state = tf.nn.dynamic_rnn(
            cell=lstm_cell,
            inputs=inputs_reshape,
            sequence_length=inputs_lengths,
            dtype=tf.float32)

        lstm_output = tf.reshape(
            tensor=lstm_output,
            #shape = [-1, params["max_timesteps"] * params["lstm_projection"]])
            shape=[
                -1, params["lstm_projection"] * params["mfcc_features_count"]
            ])

    with tf.name_scope("Fully_connected"):
        dense_layer = lstm_output

        if params["fully_connected_sizes"] is not None:
            for size in params["fully_connected_sizes"]:
                dense_layer = tf.layers.dense(
                    inputs=dense_layer,
                    units=size,
                    activation=tf.nn.relu,
                    use_bias=True,
                    kernel_initializer=tf.truncated_normal_initializer(
                        stddev=0.2, mean=0),
                    bias_initializer=tf.constant_initializer(value=0.01))

    dropout = tf.layers.dropout(inputs=dense_layer,
                                rate=0.4,
                                training=mode == learn.ModeKeys.TRAIN)

    with tf.name_scope("Logits"):
        logits_flat = tf.layers.dense(
            inputs=dropout,
            units=params["labels_class_count"],
            use_bias=True,
            kernel_initializer=tf.truncated_normal_initializer(stddev=0.2,
                                                               mean=0),
            bias_initializer=tf.constant_initializer(value=0))

        logits = tf.reshape(logits_flat,
                            shape=[-1, params["labels_class_count"]])

    predictions = \
    {
        "classes" : tf.argmax(input = logits, axis = 1),
        "probabilities" : tf.nn.softmax(logits, name = "softmax_tensor"),
    }

    loss = None
    train_op = None
    eval_metric_ops = None

    if mode != learn.ModeKeys.INFER:
        cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
            logits=logits,
            labels=tf.one_hot(labels, params["labels_class_count"]))
        loss = tf.reduce_mean(cross_entropy)

    if mode == learn.ModeKeys.TRAIN:

        train_op = tf.contrib.layers.optimize_loss(
            loss=loss,
            global_step=tf.contrib.framework.get_global_step(),
            learning_rate=params["learning_rate"],
            optimizer=params["optimizer"])

    if mode == learn.ModeKeys.EVAL:
        batch_size = int(logits.get_shape()[0])
        eval_metric_ops = {
            "accuracy":
            tf.metrics.accuracy(labels=labels,
                                predictions=tf.argmax(input=logits, axis=1))
        }

    return ModelFnOps(mode=mode,
                      predictions=predictions,
                      loss=loss,
                      train_op=train_op,
                      eval_metric_ops=eval_metric_ops)