Пример #1
0
 def __init__(self, session, model_name="CNN", save_tag="model"):
     # make output directory
     self.saver_path, self.summary_train_path, self.summary_dev_path = set_output_dir.make_dir(
         model_name)
     # set output directory of tensorflow output
     self.model_prefix = os.path.join(self.saver_path, save_tag)
     self.session = session
Пример #2
0
    def create_ml(self):
        print("create_ml")
        self.ml_dir = str(self.ml_sequence_num) + '_' + self.ml_dir

        self.dirpath_trained_model, self.dirpath_summary_train, self.dirpath_summary_validation = set_output_dir.make_dir(
            self.ml_dir, self.project_dirpath)
        self.model_filepath = os.path.join(self.dirpath_trained_model,
                                           self.trained_ml_save_tag)

        x_width = self.x.shape[-1]
        y_width = self.y.shape[-1]
        with self.graph.as_default():
            self.input_x = tf.placeholder(tf.float32,
                                          [None, self.x_height, x_width],
                                          name="input_x")
            self.expanded_input_x = tf.expand_dims(self.input_x, -1)
            self.input_y = tf.placeholder(tf.int32, [None, self.num_y_type],
                                          name="input_y")
            self.input_dropout_keep_prob = tf.placeholder(
                tf.float32, name="dropout_keep_prob")

            l2_loss = tf.constant(0.0)
            pooled_outputs = []

            for i, filter_size in enumerate(self.filter_sizes):
                print("1")
                with tf.name_scope("conv-maxpool-{}".format(filter_size[0])):
                    print("2")
                    filter_shape = [
                        int(filter_size[0]),
                        int(filter_size[1]), 1, self.num_filters
                    ]
                    print(filter_shape)
                    W = tf.Variable(tf.truncated_normal(filter_shape,
                                                        stddev=0.1),
                                    name="W")
                    b = tf.Variable(tf.constant(0.1, shape=[self.num_filters]),
                                    name="b")

                    print("2.1")
                    conv = tf.nn.conv2d(self.expanded_input_x,
                                        W,
                                        strides=[1, 1, 1, 1],
                                        padding="VALID",
                                        name="conv")
                    print("2.2")
                    conv_relu = tf.nn.relu(tf.nn.bias_add(conv, b),
                                           name="relu")

                    pooled = tf.nn.max_pool(
                        conv_relu,
                        ksize=[
                            1, self.x_height - int(filter_size[0]) + 1,
                            x_width - int(filter_size[1]) + 1, 1
                        ],
                        strides=[1, 1, 1, 1],
                        padding="VALID",
                        name="pool")
                    pooled_outputs.append(pooled)
            print("2.3")
            num_filters_total = self.num_filters * len(self.filter_sizes)
            pooled_concat = tf.concat(pooled_outputs, 3)
            pooled_flat = tf.reshape(pooled_concat, [-1, num_filters_total])

            with tf.name_scope("conv-dropout"):
                print("3")
                conv_drop = tf.nn.dropout(pooled_flat,
                                          self.input_dropout_keep_prob)
            print("4")
            pre_num_node = num_filters_total
            NN_result = [None] * (len(self.num_NN_nodes) + 1)
            NN_result[0] = conv_drop
            for index, num_node in enumerate(self.num_NN_nodes):
                print("5")
                if num_node == 0:
                    index = -1
                    break
                with tf.name_scope(
                        "completely_connected_NN_layer{}".format(index + 1)):
                    print("6")
                    W = tf.get_variable(
                        "W_layer{}".format(index + 1),
                        shape=[pre_num_node, num_node],
                        initializer=tf.contrib.layers.xavier_initializer())
                    b = tf.Variable(tf.constant(0.1, shape=[num_node]),
                                    name="b")
                    l2_loss += tf.nn.l2_loss(W)
                    l2_loss += tf.nn.l2_loss(b)
                    NN_result[index + 1] = tf.sigmoid(
                        tf.nn.xw_plus_b(NN_result[index],
                                        W,
                                        b,
                                        name="NN_result{}".format(index + 1)))
                    with tf.name_scope("dropout"):
                        NN_result[index + 1] = tf.nn.dropout(
                            NN_result[index + 1], self.input_dropout_keep_prob)
                    pre_num_node = num_node

            with tf.name_scope("output_layer"):
                print("7")
                W = tf.get_variable(
                    "w",
                    shape=[pre_num_node, self.num_y_type],
                    initializer=tf.contrib.layers.xavier_initializer())
                b = tf.Variable(tf.constant(0.1, shape=[self.num_y_type]),
                                name="b")
                l2_loss += tf.nn.l2_loss(W)
                l2_loss += tf.nn.l2_loss(b)

                self.scores = tf.nn.xw_plus_b(NN_result[index + 1],
                                              W,
                                              b,
                                              name="output")
                self.softmax = tf.nn.softmax(self.scores,
                                             name="softmax_scores")
                self.predictions = tf.argmax(self.scores,
                                             1,
                                             name="predictions")

            with tf.name_scope("eval_info"):
                losses = tf.nn.softmax_cross_entropy_with_logits(
                    logits=self.scores, labels=self.input_y)
                self.objective = tf.add(tf.reduce_mean(losses),
                                        (self.l2_reg_lambda * l2_loss),
                                        name="objective")
                tf.summary.scalar("loss", self.objective)
                correct_predictions = tf.equal(self.predictions,
                                               tf.argmax(self.input_y, 1))
                self.accuracy = tf.reduce_mean(tf.cast(correct_predictions,
                                                       "float"),
                                               name="accuracy")
                tf.summary.scalar("accuracy", self.accuracy)

            with tf.name_scope("train"):
                self.global_step = tf.Variable(0,
                                               name="global_step",
                                               trainable=False)
                optimizer = tf.train.AdamOptimizer(1e-3)
                grads_and_vars = optimizer.compute_gradients(self.objective)
                self.op_train = optimizer.apply_gradients(
                    grads_and_vars,
                    global_step=self.global_step,
                    name="op_train")

            self.op_summary = tf.summary.merge_all()
            self.saver_model = tf.train.Saver(tf.global_variables(),
                                              name="saver_model")

            self.session.run(tf.global_variables_initializer())

        print("create end")
Пример #3
0
    def create_ml(self):
        self.ml_dir = str(self.ml_sequence_num) + '_' + self.ml_dir
        # make output directory
        self.dirpath_trained_ml, self.dirpath_summary_train, self.dirpath_summary_validation = set_output_dir.make_dir(
            self.ml_dir, self.project_dirpath)
        #        self.ml_filepath = os.path.join(self.dirpath_trained_ml, self.ml_save_tag)

        x_width = self.x.shape[-1]
        with self.graph.as_default():
            self.input_x = tf.placeholder(tf.float32, [None, x_width],
                                          name="input_x")
            self.input_centroids = tf.placeholder(tf.float32,
                                                  [self.centroid_num, x_width],
                                                  name="input_centroids")
            self.input_step = tf.placeholder(tf.int32, name="input_step")

            centroids = tf.get_variable("centroids",
                                        shape=[self.centroid_num, x_width])
            init_centroids = tf.assign(centroids, self.input_centroids)
            expanded_centroids = tf.expand_dims(init_centroids, 0)
            expanded_point = tf.expand_dims(self.input_x, 1)
            distances = tf.reduce_sum(
                tf.square(tf.subtract(expanded_point, expanded_centroids)), -1)
            self.assignments = tf.argmin(distances, -1, name="assignment")

            points_per_centroid = [
                tf.gather(
                    self.input_x,
                    tf.reshape(
                        tf.where(tf.equal(self.assignments, centroid_index)),
                        [-1])) for centroid_index in range(self.centroid_num)
            ]
            updated_centroids = [
                tf.reduce_mean(points, reduction_indices=0)
                for points in points_per_centroid
            ]
            self.op_train = tf.assign(centroids,
                                      updated_centroids,
                                      name="op_train")
            self.sum_distances = tf.reduce_sum(tf.reduce_min(distances, -1),
                                               name="sum_distances")
            self.initial_step = 0
            self.global_step = tf.assign(tf.Variable(0, dtype=tf.int32),
                                         self.input_step,
                                         name="global_step")
            # saver operation
            self.saver_ml = tf.train.Saver(tf.global_variables(),
                                           name="saver_ml")
            # Initialize all variables of tensor
            self.session.run(tf.global_variables_initializer())
Пример #4
0
 def run(self):
     self.ml_dir = str(self.ml_sequence_num) + '_' + self.ml_dir
     self.dirpath_trained_ml, self.dirpath_summary_train, self.dirpath_summary_validation = set_output_dir.make_dir(
         self.ml_dir, self.project_dirpath)
     centroids = np.genfromtxt(os.path.join(self.dirpath_trained_ml,
                                            self.trained_centroid_outfile),
                               delimiter=',')
     feed_dict = {
         self.input_x: self.x,
         self.input_centroids: centroids,
     }
     result = self.session.run(self.assignments, feed_dict)
     output_filepath = os.path.join(self.project_dirpath, self.ml_dir,
                                    self.run_result_file)
     output = pd.DataFrame(data=result, columns=['cluster_num'])
     output.to_csv(output_filepath, index=False)
     print("result saved as \'{}\'".format(output_filepath))
     return result
Пример #5
0
    def create_ml(self):
        dt = data_transform.Data_transform()
        self.x = np.array(self.x)
        self.y = dt._make_node_y_input(self.y, self.y_type_num)
        self.model_dir = str(self.model_sequence) + '_' + self.model_dir
        # make output directory
        self.dirpath_trained_model, self.dirpath_summary_train, self.dirpath_summary_validation = set_output_dir.make_dir(
            self.model_dir, self.project_dirpath)
        self.model_filepath = os.path.join(self.dirpath_trained_model,
                                           self.model_save_tag)

        x_width = self.x.shape[-1]
        num_y_type = self.y.shape[-1]
        with self.graph.as_default():
            # Placeholders for input, output and dropout
            self.input_x = tf.placeholder(tf.float32, [None, x_width],
                                          name="input_x")
            self.input_y = tf.placeholder(tf.int32, [None, num_y_type],
                                          name="input_y")
            # used when evaluation(keep_prob = 1.0)
            self.input_dropout_keep_prob = tf.placeholder(
                tf.float32, name="dropout_keep_prob")

            # Keeping track of 12 regularization loss (optional)
            l2_loss = tf.constant(0.0)

            # ANN layer
            pre_num_node = x_width
            NN_result = [None] * (len(self.nodes_num) + 1)
            NN_result[0] = self.input_x
            for index, num_node in enumerate(self.nodes_num):
                if num_node == 0:
                    print(
                        "the number of ANN layer node(num_node=0) is not valid"
                    )
                    index = -1
                    sys.exit()
                with tf.name_scope(
                        "completely_connected_NN_layer{}".format(index + 1)):
                    W = tf.get_variable(
                        "W_layer{}".format(index + 1),
                        shape=[pre_num_node, num_node],
                        initializer=tf.contrib.layers.xavier_initializer())
                    b = tf.Variable(tf.constant(0.1, shape=[num_node]),
                                    name="b")
                    l2_loss += tf.nn.l2_loss(W)
                    l2_loss += tf.nn.l2_loss(b)
                    NN_result[index + 1] = tf.sigmoid(
                        tf.nn.xw_plus_b(NN_result[index],
                                        W,
                                        b,
                                        name="NN_result{}".format(index + 1)))
                    with tf.name_scope("dropout"):
                        NN_result[index + 1] = tf.nn.dropout(
                            NN_result[index + 1], self.input_dropout_keep_prob)
                    pre_num_node = num_node

            # Predict & Classify layer
            with tf.name_scope("output_layer"):
                W = tf.get_variable(
                    "W",
                    shape=[pre_num_node, num_y_type],
                    initializer=tf.contrib.layers.xavier_initializer())
                b = tf.Variable(tf.constant(0.1, shape=[num_y_type]), name="b")
                l2_loss += tf.nn.l2_loss(W)
                l2_loss += tf.nn.l2_loss(b)

                self.scores = tf.nn.xw_plus_b(NN_result[index + 1],
                                              W,
                                              b,
                                              name="output")
                self.softmax = tf.nn.softmax(self.scores,
                                             name="softmax_scores")
                self.predictions = tf.argmax(self.scores,
                                             1,
                                             name="predictions")

            # Evaluation layer
            with tf.name_scope("eval_info"):
                losses = tf.nn.softmax_cross_entropy_with_logits(
                    logits=self.scores, labels=self.input_y)
                self.objective = tf.add(tf.reduce_mean(losses),
                                        (self.l2_reg_lambda * l2_loss),
                                        name="objective")
                tf.summary.scalar("loss", self.objective)
                correct_predictions = tf.equal(self.predictions,
                                               tf.argmax(self.input_y, 1))
                self.accuracy = tf.reduce_mean(tf.cast(correct_predictions,
                                                       "float"),
                                               name="accuracy")
                tf.summary.scalar("accuracy", self.accuracy)

            # Training operation
            with tf.name_scope("train"):
                self.global_step = tf.Variable(0,
                                               name="global_step",
                                               trainable=False)
                optimizer = tf.train.AdamOptimizer(1e-3)
                grads_and_vars = optimizer.compute_gradients(self.objective)
                self.op_train = optimizer.apply_gradients(
                    grads_and_vars,
                    global_step=self.global_step,
                    name="op_train")

            # Summary & Saver(save learned model)
            self.op_summary = tf.summary.merge_all()
            self.saver_model = tf.train.Saver(tf.global_variables(),
                                              name="saver_model")

            # Initiazlize all variables of tensor
            self.session.run(tf.global_variables_initializer())