Ejemplo n.º 1
0
    def __generate_update_taum_graph(self, T):
        new_taum = tf.expand_dims(self.tau1, axis=0)
        for i in range(T - 1):
            einsum = tf.einsum("iw,iwq->iq", new_taum[i], self.taut[i])
            new_taum = tf.concat([new_taum, tf.expand_dims(einsum, axis=0)], axis=0)

        return fix_numeric_issues(new_taum, above=False, normalize=True)
Ejemplo n.º 2
0
    def __generate_update_taut_graph(self, data: GraphData, density: tf.Variable, trans: tf.Variable):
        T = data.T
        N = data.N
        Q = data.Q
        K = data.K

        data_correction_array = tf.one_hot(tf.reshape(data.graph, [-1]), K + 1, dtype=tf.float64)
        data_correction_array = tf.reshape(data_correction_array, [T, N, 1, N, 1, K + 1])
        correction_i_eq_j = reverse_01_values(tf.one_hot(tf.range(0, N), N, dtype=tf.float64))
        correction_i_eq_j = tf.reshape(correction_i_eq_j, [1, N, 1, N, 1, 1])

        # t, i, q, qprime, j, l, k
        taum_reshaped = tf.reshape(self.taum, [T, 1, 1, N, Q, 1])
        taum_reshaped = tf.tile(taum_reshaped, [1, N, Q, 1, 1, K + 1])
        density_reshaped = tf.reshape(density, [T, 1, Q, 1, Q, K + 1])

        logp = taum_reshaped * density_reshaped * data_correction_array * correction_i_eq_j
        logp = tf.reduce_sum(logp, [3, 4, 5])
        tauti = tf.reshape(logp, [T, N, 1, Q]) + tf.reshape(tf.log(trans), [1, 1, Q, Q])
        tauti = tf.exp(tauti - tf.reduce_max(tauti))

        tauti_sum = tf.reduce_sum(tauti, [3], keep_dims=True)
        taut = tauti / tauti_sum
        taut = tf.slice(taut, [1, 0, 0, 0], [-1, -1, -1, -1])
        taut = fix_numeric_issues(taut, above=False, normalize=True)
        return taut
Ejemplo n.º 3
0
    def __generate_update_stationary_graph(tau: Tau):
        stationary = tf.reduce_sum(tau.taum, [0, 1])
        stationary = fix_numeric_issues(stationary, above=False)

        stationary_sum = tf.reduce_sum(stationary)
        stationary = stationary / stationary_sum
        return stationary
Ejemplo n.º 4
0
    def __generate_update_trans_graph(data: GraphData, tau: Tau):
        T = data.T
        N = data.N
        Q = data.Q

        taum_slice = tf.slice(tau.taum, [0, 0, 0], [T - 1, -1, -1])
        reshaped_taum = tf.reshape(taum_slice, [T - 1, N, Q, 1])
        trans_1 = tau.taut * reshaped_taum
        trans = tf.reduce_sum(trans_1, [0, 1])

        trans = fix_numeric_issues(trans, above=False, normalize=True)
        return trans
Ejemplo n.º 5
0
    def __generate_update_tau1_graph(self, data: GraphData, density: tf.Variable, stationary):
        N = data.N
        Q = data.Q
        K = data.K

        data_correction_array = tf.one_hot(tf.reshape(data.graph[0], [-1]), K + 1, dtype=tf.float64)
        data_correction_array = tf.reshape(data_correction_array, [N, N, 1, 1, K + 1])
        correction_i_eq_j = reverse_01_values(tf.one_hot(tf.range(0, N), N, dtype=tf.float64))
        correction_i_eq_j = tf.reshape(correction_i_eq_j, [N, N, 1, 1, 1])

        taul_reshaped = tf.reshape(self.tau1, [1, N, 1, Q, 1])
        density_reshaped = tf.reshape(density[0], [1, 1, Q, Q, K + 1])
        logp = tf.reduce_sum(taul_reshaped * density_reshaped * data_correction_array * correction_i_eq_j, [1, 3, 4])
        tau1i = logp + tf.reshape(tf.log(stationary), [1, Q])
        # normalization
        tau1i = tf.exp(tau1i - tf.reduce_max(tau1i))

        tau1i_sum = tf.reduce_sum(tau1i, [1], keep_dims=True)
        tau1i = tau1i / tau1i_sum

        tau1 = fix_numeric_issues(tau1i, above=False, normalize=True)
        return tau1
Ejemplo n.º 6
0
    def __prepare_update_density(self, data: GraphData, tau: Tau):
        T = data.T
        Q = data.Q
        N = data.N
        K = data.K

        correction_ij_array = tf.sequence_mask(tf.range(0, N),
                                               N,
                                               dtype=tf.float64)
        correction_ql_array = tf.sequence_mask(tf.range(0, Q),
                                               Q,
                                               dtype=tf.float64)

        # betaql is calculated when data is eq 0, transform data into array with 1 when data eq 0 and 0 otherwise
        data_boolean_mask = tf.cast(tf.cast(data.graph, dtype=tf.bool),
                                    dtype=tf.float64)
        data_correction_array = reverse_01_values(data_boolean_mask)

        correction_array_1 = tf.reshape(correction_ij_array, [1, N, N, 1, 1]) * \
                             tf.reshape(correction_ql_array, [1, 1, 1, Q, Q]) * \
                             tf.reshape(data_correction_array, [T, N, N, 1, 1])

        correction_array_2 = tf.reshape(correction_ij_array,
                                        [1, N, N]) * data_correction_array
        betaql = self.__betaql(data, tau, correction_array_1,
                               correction_array_2)

        # calculate betaqlsum
        correction_array_1 = tf.reshape(correction_ij_array, [N, N, 1, 1]) * \
                             tf.reshape(correction_ql_array, [1, 1, Q, Q])
        correction_array_1 = tf.reshape(correction_array_1, [1, N, N, Q, Q])

        correction_array_2 = tf.expand_dims(correction_ij_array, 0)
        betasumql = self.__betaql(data, tau, correction_array_1,
                                  correction_array_2)
        betaql = tf.divide(betaql, betasumql)
        betaql = fix_numeric_issues(betaql)

        # calculate multinmprobaql
        data_correction_array = tf.one_hot(tf.reshape(data.graph - 1, [-1]),
                                           K,
                                           dtype=tf.float64)
        data_correction_array = tf.reshape(data_correction_array, [T, N, N, K])

        correction_array_1 = tf.multiply(
            tf.reshape(correction_ij_array, [1, N, N, 1]),
            data_correction_array)
        correction_array_1 = tf.multiply(
            tf.reshape(correction_array_1, [T, N, N, 1, 1, K]),
            tf.reshape(correction_ql_array, [1, 1, 1, Q, Q, 1]))

        multinomprobaql_1a = tf.reshape(tau.taum, [T, N, 1, Q, 1, 1]) * \
                             tf.reshape(tau.taum, [T, 1, N, 1, Q, 1]) * correction_array_1
        multinomprobaql_1a = tf.reduce_sum(multinomprobaql_1a, [1, 2])

        multinomprobaql_1b = tf.reshape(tau.taum, [T, N, 1, 1, Q, 1]) * \
                             tf.reshape(tau.taum, [T, 1, N, Q, 1, 1]) * correction_array_1
        multinomprobaql_1b = tf.reduce_sum(multinomprobaql_1b, [1, 2])

        multinomprobaql_1 = multinomprobaql_1a + multinomprobaql_1b
        multinomprobaql_1 = tf.reduce_sum(
            [multinomprobaql_1,
             tf.transpose(multinomprobaql_1, [0, 2, 1, 3])], 0)

        correction_array_2 = tf.reshape(correction_ij_array,
                                        [1, N, N, 1]) * data_correction_array

        multinomprobaql_2 = tf.reshape(tau.taum, [T, N, 1, Q, 1]) * tf.reshape(tau.taum, [T, 1, N, Q, 1]) * \
                            tf.reshape(correction_array_2, [T, N, N, 1, K])
        multinomprobaql_2 = tf.reduce_sum(multinomprobaql_2, [0, 1, 2])

        mask = tf.one_hot(tf.range(0, Q), Q, dtype=tf.float64)
        # multinomprobaql_2 = tf.tile(tf.reshape(multinomprobaql_2, [Q, 1, K]), [1, Q, 1])
        multinomprobaql_2 = tf.reshape(multinomprobaql_2, [Q, 1, K])
        multinomprobaql_2 = multinomprobaql_2 * tf.reshape(mask, [Q, Q, 1])
        # multinomprobaql_2 = tf.tile([multinomprobaql_2], [T, 1, 1, 1])
        multinomprobaql_2 = tf.expand_dims(multinomprobaql_2, 0)

        multinomprobaql = multinomprobaql_1 + multinomprobaql_2
        multinomprobaql = multinomprobaql / tf.reduce_sum(
            multinomprobaql, 3, keep_dims=True)
        multinomprobaql = fix_numeric_issues(multinomprobaql)

        betaql_log = tf.log(betaql)
        density_1 = tf.pad(tf.reshape(betaql_log, [T, Q, Q, 1]),
                           [[0, 0], [0, 0], [0, 0], [0, K]])

        betaql_log = tf.log(
            tf.subtract(tf.constant(1, dtype=tf.float64), betaql))
        multinomprobaql_log = tf.log(multinomprobaql)

        density_2 = tf.add(tf.reshape(betaql_log, [T, Q, Q, 1]),
                           multinomprobaql_log)

        density = tf.add(density_1,
                         tf.pad(density_2, [[0, 0], [0, 0], [0, 0], [1, 0]]))
        return density