Example #1
0
    def rnn_predict(self, x, m, t, median_vals):
        """Impute missing data using RNN block.
    
    Args:
      - x: incomplete data
      - m: mask matrix
      - t: time matrix
      - median_vals: median imputation for variables without observations
    
    Returns:
      - imputed_x: imputed data by rnn block
    """
        # Output Initialization
        imputed_x = np.zeros([self.no, self.seq_len, self.dim])

        # For each feature
        for f in tqdm(range(self.dim)):

            temp_input = np.dstack((x[:, :, f], m[:, :, f], t[:, :, f]))
            temp_input_reverse = np.flip(temp_input, 1)

            forward_input = np.zeros([self.no, self.seq_len, 3])
            forward_input[:, 1:, :] = temp_input[:, :(self.seq_len - 1), :]

            backward_input = np.zeros([self.no, self.seq_len, 3])
            backward_input[:, 1:, :] = temp_input_reverse[:, :(self.seq_len -
                                                               1), :]

            save_file_name = self.save_file_directory + '/rnn_feature_' + str(
                f + 1) + '/'

            # Load saved model
            graph = tf.Graph()
            with graph.as_default():
                with tf.compat.v1.Session() as sess:
                    sess.run(tf.compat.v1.global_variables_initializer())
                    tf.compat.v1.saved_model.loader.load(
                        sess, [tf.compat.v1.saved_model.SERVING],
                        save_file_name)
                    fw_input = graph.get_tensor_by_name('inputs:0')
                    bw_input = graph.get_tensor_by_name('inputs_rev:0')
                    output = graph.get_tensor_by_name(
                        'map/TensorArrayStack/TensorArrayGatherV3:0')

                    imputed_data = sess.run(output,
                                            feed_dict={
                                                fw_input: forward_input,
                                                bw_input: backward_input
                                            })

                    imputed_x[:, :, f] = (1-m[:,:,f]) * np.transpose(np.squeeze(imputed_data)) + \
                                         m[:,:,f] * x[:,:,f]

        # Initial poitn interpolation for better performance
        imputed_x = initial_point_interpolation(x, m, t, imputed_x,
                                                median_vals)

        return imputed_x
Example #2
0
    def rnn_fc_predict(self, x, m, t, median_vals):
        """Impute missing data using RNN and FC.
    
    Args:
      - x: incomplete data
      - m: mask matrix
      - t: time matrix
      - median_vals: median imputation for variables without observations
    
    Returns:
      - fc_imputed_x: imputed data using RNN and FC
    """
        # rnn imputation results
        rnn_imputed_x = self.rnn_predict(x, m, t, median_vals)

        print('Finish M-RNN imputations with RNN')

        # Reshape the data for FC predict
        x = np.reshape(x, [self.no * self.seq_len, self.dim])
        rnn_imputed_x = np.reshape(rnn_imputed_x,
                                   [self.no * self.seq_len, self.dim])
        m = np.reshape(m, [self.no * self.seq_len, self.dim])

        save_file_name = self.save_file_directory + '/fc_feature/'

        # Load saved data
        graph = tf.Graph()
        with graph.as_default():
            with tf.compat.v1.Session() as sess:

                sess.run(tf.compat.v1.global_variables_initializer())
                tf.compat.v1.saved_model.loader.load(
                    sess, [tf.compat.v1.saved_model.SERVING], save_file_name)
                x_input = graph.get_tensor_by_name('Placeholder:0')
                target = graph.get_tensor_by_name('Placeholder_1:0')
                mask = graph.get_tensor_by_name('Placeholder_2:0')
                outputs = graph.get_tensor_by_name('Sigmoid_1:0')

                fc_imputed_x = sess.run(outputs,
                                        feed_dict={
                                            x_input: x,
                                            target: rnn_imputed_x,
                                            mask: m
                                        })

        # Reshape imputed data to 3d array
        fc_imputed_x = np.reshape(fc_imputed_x,
                                  [self.no, self.seq_len, self.dim])
        m = np.reshape(m, [self.no, self.seq_len, self.dim])
        x = np.reshape(x, [self.no, self.seq_len, self.dim])

        fc_imputed_x = fc_imputed_x * (1 - m) + x * m
        fc_imputed_x = initial_point_interpolation(x, m, t, fc_imputed_x,
                                                   median_vals)

        print('Finish M-RNN imputations with RNN and FC')

        return fc_imputed_x
Example #3
0
    def rnn_predict(self, x, m, t, median_vals):
        """Impute missing data using RNN block.
        
        Args:
            - x: incomplete data
            - m: mask matrix
            - t: time matrix
            - median_vals: median imputation for variables without observations
        
        Returns:
            - imputed_x: imputed data by rnn block
        """
        # Check `x` dimensions.
        self.no_predict, seq_len, dim = x.shape
        assert seq_len == self.seq_len, "Sequence length dimension (1) of `x` passed to `rnn_predict()` "\
            f"differs from that of `x` used in `rnn_train()` ({seq_len} vs {self.seq_len,})"
        assert dim == self.dim, "Features dimension (2) of `x` passed to `rnn_predict()` "\
            f"differs from that of `x` used in `rnn_train()` ({dim} vs {self.dim})"

        # Output Initialization
        imputed_x = np.zeros([self.no_predict, self.seq_len, self.dim])

        # For each feature
        for f in tqdm(range(self.dim)):

            temp_input = np.dstack((x[:, :, f], m[:, :, f], t[:, :, f]))
            temp_input_reverse = np.flip(temp_input, 1)

            forward_input = np.zeros([self.no_predict, self.seq_len, 3])
            forward_input[:, 1:, :] = temp_input[:, : (self.seq_len - 1), :]

            backward_input = np.zeros([self.no_predict, self.seq_len, 3])
            backward_input[:, 1:, :] = temp_input_reverse[:, : (self.seq_len - 1), :]

            save_file_name = self.save_file_directory + "/rnn_feature_" + str(f + 1) + "/"

            # Load saved model
            graph = tf.Graph()
            with graph.as_default():
                with tf.compat.v1.Session() as sess:
                    sess.run(tf.compat.v1.global_variables_initializer())
                    tf.compat.v1.saved_model.loader.load(sess, [tf.compat.v1.saved_model.SERVING], save_file_name)
                    fw_input = graph.get_tensor_by_name("inputs:0")
                    bw_input = graph.get_tensor_by_name("inputs_rev:0")
                    output = graph.get_tensor_by_name("map/TensorArrayStack/TensorArrayGatherV3:0")

                    imputed_data = sess.run(output, feed_dict={fw_input: forward_input, bw_input: backward_input})

                    imputed_x[:, :, f] = (1 - m[:, :, f]) * np.transpose(np.squeeze(imputed_data)) + m[:, :, f] * x[
                        :, :, f
                    ]

        # Initial point interpolation for better performance
        imputed_x = initial_point_interpolation(x, m, t, imputed_x, median_vals)

        return imputed_x