示例#1
0
    def getLikelihoodMatrices_FO2a(self):
        """
        Returns the likelihood matrices of first order
        :return:
        """

        weightFns = len(activation_fn.fn_indices);
        nodeFns = len(output_fn.fn_indices);

        #create transfer function combination

        transferFns_HL = np.zeros((weightFns,nodeFns),np.float32);
        transferFns_OL = np.zeros((weightFns,nodeFns),np.float32);

        #Caculate the weights
        for h_j,hid_node_j in enumerate(self.hiddenNodes):

            #get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(hid_node_j.activation_fn);
            out_fn_j = output_fn.getFunctionIndex(hid_node_j.output_fn);

            transferFns_HL[act_fn_j][out_fn_j]+=1;

        for o_j, out_node_j in enumerate(self.outputNodes):

            #get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(hid_node_j.activation_fn);
            out_fn_j = output_fn.getFunctionIndex(hid_node_j.output_fn);

            transferFns_OL[act_fn_j][out_fn_j]+=1;




        return {'transferFns_HL': transferFns_HL, 'transferFnsOL': transferFns_OL}
示例#2
0
    def getLikelihoodMatrices_FO1(self):
        """
        Returns the likelihood matrices of first order
        :return:
        """

        weightFns = len(activation_fn.fn_indices)
        nodeFns = len(output_fn.fn_indices)

        # create transfer function combination

        weightFns_HL = np.zeros((weightFns), np.float32)
        weightFns_OL = np.zeros((weightFns), np.float32)
        nodeFns_HL = np.zeros((nodeFns), np.float32)
        nodeFns_OL = np.zeros((nodeFns), np.float32)

        # Caculate the weights
        for h_j, hid_node_j in enumerate(self.hiddenNodes):

            # get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(hid_node_j.activation_fn)
            out_fn_j = output_fn.getFunctionIndex(hid_node_j.output_fn)

            weightFns_HL[act_fn_j] += 1
            nodeFns_HL[out_fn_j] += 1

        for o_j, out_node_j in enumerate(self.outputNodes):

            # get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(hid_node_j.activation_fn)
            out_fn_j = output_fn.getFunctionIndex(hid_node_j.output_fn)

            weightFns_OL[act_fn_j] += 1
            nodeFns_OL[out_fn_j] += 1

        return {
            "weightFns_HL": weightFns_HL,
            "nodeFns_HL": nodeFns_HL,
            "weightFns_OL": weightFns_OL,
            "nodeFns_OL": nodeFns_OL,
        }
示例#3
0
    def getLikelihoodMatrices_FO2b(self):
        """
        Returns the likelihood matrices of first order
        :return:
        """

        weightFns = len(activation_fn.fn_indices);
        nodeFns = len(output_fn.fn_indices);

        #create transfer function combination
        #Unlike the first method, this method gathers staticstics on a specific node position
        transferFn_HL_N = dict();
        transferFn_OL_N = dict();

        for i in self.hiddenNodes:
            transferFn_HL_N[str(i)] = np.zeros((weightFns,nodeFns),np.float32);

        for i in self.hiddenNodes:
            transferFn_OL_N[str(i)] = np.zeros((weightFns,nodeFns), np.float32);


        #Caculate the weights
        for h_j,hid_node_j in enumerate(self.hiddenNodes):

            #get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(hid_node_j.activation_fn);
            out_fn_j = output_fn.getFunctionIndex(hid_node_j.output_fn);

            transferFn_HL_N[h_j][act_fn_j][out_fn_j]+=1;

        for o_j, out_node_j in enumerate(self.outputNodes):

            #get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(hid_node_j.activation_fn);
            out_fn_j = output_fn.getFunctionIndex(hid_node_j.output_fn);

            transferFn_OL_N[o_j][act_fn_j][out_fn_j]+=1;


        return {'transferFn_HL' : transferFn_HL_N, 'transferFn_OL':transferFn_OL_N};
示例#4
0
    def drawNetwork(self,scene, model):
        """ Draws the visualisation of the network"""
        nodes = [];
        
        #create input nodes
        for n in xrange(model.numI):

            #get output function
            #node representation
            INPUT_NODE = 1;
            IDENTITY = 1;
            n_rep = Node(self,nodeType=INPUT_NODE,node_fn = IDENTITY);
            #add to nodes
            nodes.append(n_rep);
            
            #add to scene
            scene.addItem(n_rep);

        #create hidden nodes
        for n in xrange(model.numH):

            #get the node
            node = model.hiddenNodes[n];

            out_fn = node.output_fn;
            out_fni = output_fn.getFunctionIndex(out_fn);

            #node representation
            HIDDEN_NODE = 2;
            n_rep = Node(self,nodeType=HIDDEN_NODE,node_fn=out_fni);
            #add to nodes
            nodes.append(n_rep);

            #add to scene
            scene.addItem(n_rep);

        #create output nodes
        for n in xrange(model.numO):

            #get the node
            node = model.outputNodes[n];

            out_fn = node.output_fn;
            out_fni = output_fn.getFunctionIndex(out_fn);

            #node representation
            OUTPUT_NODE = 3;
            n_rep = Node(self,nodeType=OUTPUT_NODE,node_fn=out_fni);
            #add to nodes
            nodes.append(n_rep);

            #add to scene
            scene.addItem(n_rep);
                
            
        #create connections (input to hidden units)
        for ni in xrange(model.numI):
            nodei = nodes[ni];

            for nj in xrange(model.numH):
                nodej = nodes[model.numI + nj];
                weightFn = model.hiddenNodes[nj].activation_fn;
                wfni = activation_fn.getFunctionIndex(weightFn);

                #Get connection details
                active = model.connActive_IH[ni][nj];
                connWeight = model.connWeight_IH[ni][nj];

                if active:
                    conn = Edge.Edge(nodei,nodej,wfni,connWeight);
                    scene.addItem(conn);

        #create connections (hidden to hidden units)
        for ni in xrange(model.numH):
            nodei = nodes[model.numI+ni];

            for nj in xrange(model.numH):
                nodej = nodes[model.numI + nj];
                weightFn = model.hiddenNodes[nj].activation_fn;
                wfni = activation_fn.getFunctionIndex(weightFn);

                #Get connection info.
                active = model.connActive_HH[ni][nj];
                connWeight = model.connWeight_HH[ni][nj];

                #connect if connection is active
                if active:
                    conn = Edge.Edge(nodei,nodej,wfni,connWeight);
                    scene.addItem(conn);


        #create connections (hidden to hidden units)
        for ni in xrange(model.numH):
            nodei = nodes[model.numI+ni];

            for nj in xrange(model.numO):
                nodej = nodes[model.numI + model.numH + nj];
                weightFn = model.hiddenNodes[nj].activation_fn;
                wfni = activation_fn.getFunctionIndex(weightFn);

                #get connection info
                active = model.connActive_HO[ni][nj];
                connWeight = model.connWeight_HO[ni][nj];

                if active:
                    conn = Edge.Edge(nodei,nodej,wfni,connWeight);
                    scene.addItem(conn);


                    
        #set node positions randomly
        energy = 50;
        posX = 0.0;
        posY = 0.0;
        
        for n in nodes:
            posX = numpy.random.rand() * 50;
            posY = numpy.random.rand() * 50;
            
            n.setPos(posX,posY);
示例#5
0
    def getNodesNEdges(self):
        """  Returns the nodes and edges """


        #return object
        NodesNEdges = dict()
        NodesNEdges['nodes'] = [];
        NodesNEdges['edges'] = [];


        # HIDDEN TO HIDDEN CONNECTIONS
        for h_j,hid_node_j in enumerate(self.hiddenNodes):

            #get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(hid_node_j.activation_fn);
            out_fn_j = output_fn.getFunctionIndex(hid_node_j.output_fn);

            #pair the transfer functions
            trans_hj = (act_fn_j, out_fn_j);


            for h_i, hid_node_i in enumerate(self.hiddenNodes):

                #get the hidden node
                conn_status = self.connActive_HH[h_i][h_j];

                #get hidden node activation and output function
                act_fn_i = activation_fn.getFunctionIndex(hid_node_i.activation_fn);
                out_fn_i = output_fn.getFunctionIndex(hid_node_i.output_fn);

                #pair the transfer functions
                trans_hi = (act_fn_i, out_fn_i);

                ## COEXIST IN MODEL
                #if not self connection
                if h_i != h_j and conn_status :
                    NodesNEdges['nodes'].append(trans_hi);
                    NodesNEdges['nodes'].append(trans_hj);
                    NodesNEdges['edges'].append((trans_hi,trans_hj));


        ##### HIDDEN TO OUTPUT UNITS #####
        #Caculate the weights
        for o_j,out_node_j in enumerate(self.outputNodes):

            #get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(out_node_j.activation_fn);
            out_fn_j = output_fn.getFunctionIndex(out_node_j.output_fn);

            #pair the transfer functions
            trans_hj = (act_fn_j, out_fn_j);

            #Incoming connections from hidden to output nodes
            for h_i, hid_node_i in enumerate(self.hiddenNodes):

                #get the hidden node
                conn_status = self.connActive_HO[h_i][o_j];

                #get hidden node activation and output function
                act_fn_i = activation_fn.getFunctionIndex(hid_node_i.activation_fn);
                out_fn_i = output_fn.getFunctionIndex(hid_node_i.output_fn);

                #pair the transfer functions
                trans_hi = (act_fn_i, out_fn_i);

                ## COEXIST IN PATH
                if conn_status == 1:
                    NodesNEdges['nodes'].append(trans_hi);
                    NodesNEdges['nodes'].append(trans_hj);
                    NodesNEdges['edges'].append([trans_hi,trans_hj]);


        #debug
        if debug or True:

            print NodesNEdges;



        return NodesNEdges;
示例#6
0
    def getLikelihoodMatrices(self):# METHOD SHOULD BE OKAY
        """
        Returns the problem signature for the given model

        @params: None
        @returns: Coexistence likelihood matrix as Numpy Matrix.
        """

        ##### DO NOT FORGET #######################################################################
        #TODO: Don't forget to only copy this method for the Rest of the HypSpaceSpread Experiments
        #TODO: Seperate each propagation method to make things easier to debug e.g. have propagate_IH(),propagate_HH(),and propagate_HO()
        #TODO : update this to repo (3/7/14) - [Not Done ]

        #weight and node functions
        weightFns = len(activation_fn.fn_indices);
        nodeFns = len(output_fn.fn_indices);

        #create transfer function combination
        Comb = [(w,n) for w in np.arange(1,weightFns+1) for n in np.arange(1,nodeFns+1)];
        numComb = len(Comb);
        CoexistModelMatHL = np.zeros((numComb,numComb),np.float32);
        CoexistPathMatHL = np.zeros((numComb,numComb),np.float32);
        CoexistLayerMatHL = np.zeros((numComb,numComb),np.float32);
        CoexistModelMatOL = np.zeros((numComb,numComb),np.float32);
        CoexistPathMatOL = np.zeros((numComb,numComb),np.float32);
        CoexistLayerMatOL = np.zeros((numComb,numComb),np.float32);

        if debug:
            print "* Possible Combinations: ", Comb;
            print "* CoExistenceMat(Model):", CoexistModelMatHL,CoexistLayerMatOL;
            print "* CoExistenceMat(Path):", CoexistPathMatHL,CoexistPathMatOL;
            print "* CoExistenceMat(Layer):", CoexistLayerMatHL, CoexistPathMatOL;
            print "* Weight Functions: ", weightFns;
            print "* Node Functions: ", nodeFns;

        ##### HIDDEN TO HIDDEN UNITS #####

        #Caculate the weights
        for h_j,hid_node_j in enumerate(self.hiddenNodes):

            #get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(hid_node_j.activation_fn);
            out_fn_j = output_fn.getFunctionIndex(hid_node_j.output_fn);

            #pair the transfer functions
            trans_hj = ((act_fn_j), (out_fn_j));


            for h_i, hid_node_i in enumerate(self.hiddenNodes):

                #get the hidden node
                conn_status = self.connActive_HH[h_i][h_j];

                #get hidden node activation and output function
                act_fn_i = activation_fn.getFunctionIndex(hid_node_i.activation_fn);
                out_fn_i = output_fn.getFunctionIndex(hid_node_i.output_fn);

                #pair the transfer functions
                trans_hi = ((act_fn_i), (out_fn_i));

                ## COEXIST IN MODEL
                #if not self connection
                if h_i != h_j :

                    #get transfer functions indices and mark coordinates on coexistence matrix
                    itransFn_i = Comb.index(trans_hi);
                    itransFn_j = Comb.index(trans_hj);

                    ##############################(1) COEXIST IN SAME NETWORK #################
                    CoexistModelMatHL[itransFn_i][itransFn_j] += 1;

                    ## COEXIST IN PATH
                    if conn_status == 1:

                        #get transfer functions indices and mark coordinates on coexistence matrix
                        itransFn_i = Comb.index(trans_hi);
                        itransFn_j = Comb.index(trans_hj);
                        ##############################(2) COEXIST IN SAME PATH #################
                        CoexistPathMatHL[itransFn_i][itransFn_j] += 1;

                    ##############################(3) COEXIST IN SAME LAYER #################
                    CoexistLayerMatHL[itransFn_i][itransFn_j] += 1;


        ##### HIDDEN TO OUTPUT UNITS #####
        #Caculate the weights
        for o_j,out_node_j in enumerate(self.outputNodes):

            #get hidden node activation and output function
            act_fn_jhh = activation_fn.getFunctionIndex(out_node_j.activation_fn);
            out_fn_jhh = output_fn.getFunctionIndex(out_node_j.output_fn);

            #pair the transfer functions
            trans_hj = ((act_fn_jhh), (out_fn_jhh));

            #Incoming connections from hidden to output nodes
            for h_i, hid_node_i in enumerate(self.hiddenNodes):

                #get the hidden node
                conn_status = self.connActive_HO[h_i][o_j];

                #get hidden node activation and output function
                act_fn_i = activation_fn.getFunctionIndex(hid_node_i.activation_fn);
                out_fn_i = output_fn.getFunctionIndex(hid_node_i.output_fn);

                #pair the transfer functions
                trans_hi = ((act_fn_i), (out_fn_i));

                ## COEXIST IN MODEL
                #get transfer functions indices and mark coordinates on coexistence matrix
                itransFn_i = Comb.index(trans_hi);
                itransFn_j = Comb.index(trans_hj);

                ##############################(1) COEXIST IN SAME NETWORK #################
                CoexistModelMatOL[itransFn_i][itransFn_j] += 1;

                ## COEXIST IN PATH
                if conn_status == 1:
                    #pair the transfer functions
                    trans_hi = ((act_fn_i), (out_fn_i));

                    #get transfer functions indices and mark coordinates on coexistence matrix
                    itransFn_i = Comb.index(trans_hi);
                    itransFn_j = Comb.index(trans_hj);
                    ##############################(2) COEXIST IN SAME PATH #################
                    CoexistPathMatOL[itransFn_i][itransFn_j] += 1;

                    ##############################(3) COEXIST IN SAME LAYER #################
                    CoexistLayerMatOL[itransFn_i][itransFn_j] += 1;


        return {'onPathHL': CoexistPathMatHL,'inLayerHL': CoexistLayerMatHL, 'inModelHL': CoexistModelMatHL,
                'onPathOL': CoexistPathMatOL,'inLayerOL': CoexistLayerMatOL, 'inModelOL': CoexistModelMatOL };
示例#7
0
    def getSignatures(self):
        """

        :return: signatures
        """

        weightFns = len(activation_fn.fn_indices);
        nodeFns = len(output_fn.fn_indices);


        #by node position
        transferFn_HL_N = dict();
        transferFn_OL_N = dict();

        #by layer
        transferFns_HL = np.zeros((weightFns,nodeFns),np.float32);
        transferFns_OL = np.zeros((weightFns,nodeFns),np.float32);

        #First order
        weightFns_HL = np.zeros((weightFns),np.float32);
        weightFns_OL = np.zeros((weightFns),np.float32);
        nodeFns_HL = np.zeros((nodeFns),np.float32);
        nodeFns_OL = np.zeros((nodeFns),np.float32);

        #Second order
        Comb = [(w,n) for w in np.arange(1,weightFns+1) for n in np.arange(1,nodeFns+1)];
        numComb = len(Comb);
        #hidden to hidden higher order signatures
        CoexistModelMatHL = np.zeros((numComb,numComb),np.float32);
        CoexistPathMatHL = np.zeros((numComb,numComb),np.float32);
        CoexistLayerMatHL = np.zeros((numComb,numComb),np.float32);

        #hidden to output higher order signatures
        CoexistModelMatOL = np.zeros((numComb,numComb),np.float32);
        CoexistPathMatOL = np.zeros((numComb,numComb),np.float32);
        CoexistLayerMatOL = np.zeros((numComb,numComb),np.float32);

        #connection strength for the hidden to hidden layer
        ConnWeightPathHL = np.zeros((numComb, numComb), np.float32);
        ConnWeightLayerHL = np.zeros((numComb,numComb), np.float32);
        ConnWeightModelHL = np.zeros((numComb,numComb), np.float32);

        #connection strength for the hidden to output layer
        ConnWeightPathOL = np.zeros((numComb, numComb), np.float32);
        ConnWeightLayerOL = np.zeros((numComb,numComb), np.float32);
        ConnWeightModelOL = np.zeros((numComb,numComb), np.float32);

        for i in xrange(len(self.hiddenNodes)):
            transferFn_HL_N[str(i)] = np.zeros((weightFns,nodeFns),np.float32);

        for i in xrange(len(self.outputNodes)):
            transferFn_OL_N[str(i)] = np.zeros((weightFns,nodeFns), np.float32);

        # HIDDEN NODE
        #Caculate the weights
        for h_j,hid_node_j in enumerate(self.hiddenNodes):

            #get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(hid_node_j.activation_fn);
            out_fn_j = output_fn.getFunctionIndex(hid_node_j.output_fn);

            #for each node position
            #transferFn_HL_N[str(h_j)][act_fn_j][out_fn_j]+=1;
            #for each layer
            transferFns_HL[act_fn_j][out_fn_j] +=1;

            #first order
            weightFns_HL[act_fn_j]+=1;
            nodeFns_HL[out_fn_j]+=1;

            #higher order
            #pair the transfer functions
            trans_hj = ((act_fn_j), (out_fn_j));
            for h_i, hid_node_i in enumerate(self.hiddenNodes):

                conn_status = self.connActive_HH[h_i][h_j];
                conn_weight = self.connWeight_IH[h_i][h_j];

                #get hidden node activation and output function
                act_fn_i = activation_fn.getFunctionIndex(hid_node_i.activation_fn);
                out_fn_i = output_fn.getFunctionIndex(hid_node_i.output_fn);

                #pair the transfer functions
                trans_hi = ((act_fn_i), (out_fn_i));

                ## COEXIST IN MODEL
                #if not self connection
                if h_i != h_j :

                    #get transfer functions indices and mark coordinates on coexistence matrix
                    itransFn_i = Comb.index(trans_hi);
                    itransFn_j = Comb.index(trans_hj);

                    ##############################(1) COEXIST IN SAME NETWORK #################
                    CoexistModelMatHL[itransFn_i][itransFn_j] += 1;
                    ConnWeightModelHL[itransFn_i][itransFn_j]+= conn_weight;

                    ## COEXIST IN PATH
                    if conn_status == 1:

                        #get transfer functions indices and mark coordinates on coexistence matrix
                        itransFn_i = Comb.index(trans_hi);
                        itransFn_j = Comb.index(trans_hj);
                        ##############################(2) COEXIST IN SAME PATH #################
                        CoexistPathMatHL[itransFn_i][itransFn_j] += 1;
                        ConnWeightPathHL[itransFn_j][itransFn_j]+= conn_weight;
                    ##############################(3) COEXIST IN SAME LAYER #################
                    CoexistLayerMatHL[itransFn_i][itransFn_j] += 1;
                    ConnWeightLayerHL[itransFn_j][itransFn_j]+=conn_weight;


        # HIDDEN TO OUTPUT CONNECTIONS
        for o_j, out_node_j in enumerate(self.outputNodes):

            #get hidden node activation and output function
            act_fn_j = activation_fn.getFunctionIndex(out_node_j.activation_fn);
            out_fn_j = output_fn.getFunctionIndex(out_node_j.output_fn);

            #transferFn_OL_N[o_j][act_fn_j][out_fn_j]+=1;
            transferFns_OL[act_fn_j][out_fn_j]+=1;
            #first order
            weightFns_OL[act_fn_j]+=1;
            nodeFns_OL[out_fn_j]+=1;

            #second order signatures
            #higher order
            #pair the transfer functions
            trans_oj = ((act_fn_j), (out_fn_j));
            for h_i, hid_node_i in enumerate(self.hiddenNodes):

                conn_status = self.connActive_HO[h_i][o_j];
                conn_weight = self.connWeight_HO[h_i][o_j];

                #get hidden node activation and output function
                act_fn_i = activation_fn.getFunctionIndex(hid_node_i.activation_fn);
                out_fn_i = output_fn.getFunctionIndex(hid_node_i.output_fn);

                #pair the transfer functions
                trans_hi = ((act_fn_i), (out_fn_i));

                ## COEXIST IN MODEL
                #if not self connection


                #get transfer functions indices and mark coordinates on coexistence matrix
                itransFn_i = Comb.index(trans_hi);
                itransFn_j = Comb.index(trans_oj);

                ##############################(1) COEXIST IN SAME NETWORK #################
                CoexistModelMatOL[itransFn_i][itransFn_j] += 1;
                ConnWeightModelOL[itransFn_j][itransFn_j]+= conn_weight;
                ## COEXIST IN PATH
                if conn_status == 1:

                    #get transfer functions indices and mark coordinates on coexistence matrix
                    itransFn_i = Comb.index(trans_hi);
                    itransFn_j = Comb.index(trans_oj);
                    ##############################(2) COEXIST IN SAME PATH #################
                    CoexistPathMatOL[itransFn_i][itransFn_j] += 1;
                    ConnWeightPathOL[itransFn_i][itransFn_j]+=conn_weight;
                ##############################(3) COEXIST IN SAME LAYER #################
                CoexistLayerMatOL[itransFn_i][itransFn_j] += 1;
                ConnWeightLayerOL[itransFn_i][itransFn_j] +=conn_weight



        return {'transferFns_HL_POS' : transferFn_HL_N, 'transferFns_OL_POS':transferFn_OL_N,
                'transferFns_HL_LAY': transferFns_HL, 'transferFns_OL_LAY': transferFns_OL,
                'weightFns_HL': weightFns_HL, 'weightFns_OL': weightFns_OL,
                'nodeFns_HL':nodeFns_HL, 'nodeFns_OL':nodeFns_OL,
                'CoexistPathMatHL':CoexistPathMatHL, 'CoexistPathMatOL': CoexistPathMatOL,
                'CoexistModelMatHL':CoexistModelMatHL,'CoexistModelMatHL':CoexistModelMatOL,
                'CoexistLayerMatHL':CoexistLayerMatHL, 'CoexistLayerMatOL':CoexistLayerMatOL,
                'ConnWeightLayerHL':ConnWeightLayerHL, 'ConnWeightLayerOL':ConnWeightLayerOL,
                'ConnWeightModelHL':ConnWeightModelHL, 'ConnWeightModelOL':ConnWeightModelOL
                    };