def __init__( self, coroutine, sock_job_for_me_port, name=None, connections=None, main_connection_name=None): """ Parameters ---------- coroutine : Class instance that contains init, run and finish methods sock_job_for_me_port: str Port number for input socket url name: str Stage name main_connection_name : str Default next step name. Used to send data when destination is not provided connections: dict {'STEP_NANE' : (zmq STEP_NANE port in)} Port number for socket for each next steps """ Process.__init__(self) Component.__init__(self, parent=None) self.name = name Connections.__init__(self, main_connection_name, connections) self.coroutine = coroutine self.sock_job_for_me_url = 'tcp://localhost:' + sock_job_for_me_port self.done = False self.waiting_since = Value('i', 0) self._nb_job_done = Value('i', 0) self._stop = Value('i', 0) self._running = Value('i', 0)
def __init__(self): # Redis Connection self.redis = Connections.redisConnection() # Connection to kafka server as a producer self.producer = Connections.kafkaProducerConnection() # Topic of kafka self.topic = cfg.get('KAFKA', 'topic')
def init_connections(self): """ Initialise zmq sockets. Because this class is s Process, This method must be call in the run method to be hold by the correct process. """ self.context = zmq.Context() Connections.init_connections(self) return True
def UnitTestCase(): print "Ready!" # for line in sys.stdin: # dev1 = Connections.dev1 # dev2 = Connections.dev2 while True: line = raw_input(">> ") # print 'please input command' line = line.strip("\n") if line == "": continue elif line == "exit": break linesplit = line.split(" ") if linesplit[0] == "connect": Connections.connect_device(r"Config.json") elif linesplit[0] == "playback": if Connections.dev1 != None: Connections.dev1.playback.load_record_file_and_excute(linesplit[1]) print "Finish playback" else: print "Connect phone first" elif linesplit[0] == "calltest": if len(linesplit) != 2: print "usage: calltest test_num" continue if not linesplit[1].isdigit(): print "test_num should be an integer" continue if Connections.dev1 != None and Connections.dev2 != None: print "DO TEST:" UnitCallTestClass.total_test_num = int(linesplit[1]) unittest.main(defaultTest="suite_call_test") print "Finish call test" else: print "Connect phone first" elif linesplit[0] == "messagetest": if len(linesplit) != 2: print "usage: messagetest test_num" continue if not linesplit[1].isdigit(): print "test_num should be an integer" continue if Connections.dev1 != None and Connections.dev2 != None: print "DO TEST:" UnitMessageTestClass.total_test_num = int(linesplit[1]) unittest.main(defaultTest="suite_message_test") print "Finish message test" else: print "Connect phone first" else: print "unkown command" continue
def ConstructNet(tokens, leafCntString, Wleft, Wright, Bidx, tokenMap, numFea, tokenNum): leafCnt = leafCntString.split(' ') for idx, cnt in enumerate(leafCnt): leafCnt[idx] = int(leafCnt[idx]) totalLeaf = sum(leafCnt) + .0 leafCnt = np.array(leafCnt) leafCnt = leafCnt / totalLeaf layers = [] rootLayer = Lay.layer( tokens[0], \ range(numFea*tokenNum, numFea*(tokenNum+1) ),\ numFea, ) totalChildren = len(tokens) for idx in range(1, totalChildren): childName = tokens[idx] childIdx = tokenMap[childName] # cunstruct node (layer) childLayer = Lay.layer( name= childName,\ Bidx = range(numFea*childIdx, numFea*(childIdx+1)),\ numunit = numFea, ) # add connection if totalChildren == 2: leftCoef = .5 rightCoef = .5 else: rightCoef = (idx - 1.0) / (totalChildren - 2) leftCoef = 1 - rightCoef #print idx, len(leafCnt), len(tokens), leafCnt leftCoef *= leafCnt[idx - 1] rightCoef *= leafCnt[idx - 1] if leftCoef != 0: leftcon = Con.connection(childLayer, rootLayer, numFea, numFea,\ Wleft, leftCoef\ ) if rightCoef != 0: rightcon = Con.connection(childLayer, rootLayer, numFea, numFea,\ Wright, rightCoef\ ) layers.append(childLayer) # end of each layer layers.append(rootLayer) for idx in xrange(0, len(layers) - 1): layers[idx].successiveUpper = layers[idx + 1] layers[idx + 1].successiveLower = layers[idx] return layers
def UnitTestCase(): print 'Ready!' #for line in sys.stdin: #dev1 = Connections.dev1 #dev2 = Connections.dev2 while True: line = raw_input(">> ") #print 'please input command' line = line.strip('\n') if line == '': continue elif line == 'exit': break linesplit = line.split(' ') if linesplit[0] == 'connect': Connections.connect_device(r'Config.json') elif linesplit[0] == 'playback': if Connections.dev1!=None: Connections.dev1.playback.load_record_file_and_excute(linesplit[1]) print 'Finish playback' else: print 'Connect phone first' elif linesplit[0] == 'calltest': if len(linesplit)!=2: print 'usage: calltest test_num' continue if not linesplit[1].isdigit(): print 'test_num should be an integer' continue if Connections.dev1!=None and Connections.dev2!=None: print 'DO TEST:' UnitCallTestClass.total_test_num = int(linesplit[1]) unittest.main(defaultTest='suite_call_test') print 'Finish call test' else: print 'Connect phone first' elif linesplit[0] == 'messagetest': if len(linesplit)!=2: print 'usage: messagetest test_num' continue if not linesplit[1].isdigit(): print 'test_num should be an integer' continue if Connections.dev1!=None and Connections.dev2!=None: print 'DO TEST:' UnitMessageTestClass.total_test_num = int(linesplit[1]) unittest.main(defaultTest='suite_message_test') print 'Finish message test' else: print 'Connect phone first' else: print 'unkown command' continue
def __init__(self, queue: Queue): super().__init__() # Window settings self.minsize(width=1100, height=700) self.maxsize(width=1100, height=700) self.resizable(width=False, height=False) self.protocol("WM_DELETE_WINDOW", self.quit) # self.state('zoomed') self.config(menu=LedMenubar(self)) self.title(info["programName"]) # Led List settings self.ledList = LedList() self.saveDir = "" # Toolbar settings self.toolbar = LedToolbar(self) self.sidebar = LedSidebar(self) # Canvas settings self.canvasSize = Dimension(0, 0) self.ledCanvas = LedCanvas(self) # Connection self.queue = queue self.transmitting = False self.connection = conn.Shredder(self) self.toolbar.transmit() self.packEverything() self.setTool("add") self.RUNNING = True
def testFindCameraSpacePointsThree(): v = Connections() srcPts01 = np.array([[1, 1], [4, 4]]) dstPts01 = np.array([[2, 2], [5, 5]]) v.addConnection(0, 1, None, None, srcPts01, dstPts01) srcPts02 = np.array([[8, 8]]) dstPts02 = np.array([[9, 9]]) v.addConnection(0, 2, None, None, srcPts02, dstPts02) srcPts12 = np.array([[2, 2], [6, 6]]) dstPts12 = np.array([[3, 3], [7, 7]]) v.addConnection(1, 2, None, None, srcPts12, dstPts12) # v.debugConnections() listOfMatches = [] discovered = [dict(), dict(), dict()] v.findMatches(listOfMatches, discovered) [match.debugViews() for match in listOfMatches]
def testFindCameraSpacePointsTwo(): v = Connections() srcPts = np.array([[1, 1], [3, 3], [5, 5]]) dstPts = np.array([[2, 2], [4, 4], [6, 6]]) v.addConnection(0, 1, None, None, srcPts, dstPts) v.debugConnections() listOfMatches = [] discovered = [dict(), dict()] v.findMatches(listOfMatches, discovered) [match.debugViews() for match in listOfMatches]
def init_connections(self): """ Initialise zmq sockets. Because this class is s Process, This method must be call in the run method to be hold by the correct process. """ Connections.init_connections(self) context = Context() self.sock_for_me = context.socket(REQ) self.sock_for_me.connect(self.sock_job_for_me_url) # Use a ZMQ Pool to get multichannel message self.poll = Poller() # Register sockets self.poll.register(self.sock_for_me, POLLIN) # Send READY to next_router to inform about my capacity to compute new # job self.sock_for_me.send_pyobj("READY") return True
def Guard(): #checks were current connections are, and if not approved locations, reports it. global locations ip_con = Connections.Connections('ip') #find the ip connections ip_dom = Connections.Connections('domain') #find domain connections ip_loc = '' for ip in ip_con: #get locations of ip connections ip_loc = ip_loc + Locate.Locate(ip) + '\n' dom_loc = '' for d in ip_dom: #get locations of domain connections dom_loc = dom_loc + Locate.Locate(DomainLookup.Domain_to_IP(d)) + '\n' locations = ip_loc + '\n' + dom_loc states = Locate.FindStates(locations) #finds the states of the connections for s in states: #check to see if in approved locaitons if (s == 'RI' or s == 'NY'): continue else: return True #if there is a problem, returns true return False #otherwise returns false
def __init__(self, layers): """ 初始化一个全连接神经网络 :param layers: 二维数组,描述神经网络每层节点数 """ self.connections = Connections() self.layers = [] layer_count = len(layers) node_count = 0 for i in range(layer_count): self.layers.append(Layer(i, layers[i])) for layer in range(layer_count - 1): connections = [ Connection(upstream_node, downstream_node) for upstream_node in self.layers[layer].nodes for downstream_node in self.layers[layer + 1].node[:-1] ] for conn in connections: self.connections.add_connection(conn) conn.downstream_node.append_upstream_connection(conn) conn.upstream_node.append_downstream_connection(conn)
def __init__(self, coroutine, name, main_connection_name, connections=None): """ Parameters ---------- coroutine : Class instance It contains init, run and finish methods name: str Producer name main_connection_name : str Default next step name. Used to send data when destination is not provided connections: dict {'STEP_NANE' : (zmq STEP_NANE port in)} Port number for socket for each next steps """ Process.__init__(self) Component.__init__(self, parent=None) self.name = name Connections.__init__(self, main_connection_name, connections) self.coroutine = coroutine self.other_requests = dict() self._nb_job_done = Value('i', 0) self._running = Value('i', 0) self.done = False
class Network(object): def __init__(self, layers): """ 初始化一个全连接神经网络 :param layers: 二维数组,描述神经网络每层节点数 """ self.connections = Connections() self.layers = [] layer_count = len(layers) node_count = 0 for i in range(layer_count): self.layers.append(Layer(i, layers[i])) for layer in range(layer_count - 1): connections = [ Connection(upstream_node, downstream_node) for upstream_node in self.layers[layer].nodes for downstream_node in self.layers[layer + 1].node[:-1] ] for conn in connections: self.connections.add_connection(conn) conn.downstream_node.append_upstream_connection(conn) conn.upstream_node.append_downstream_connection(conn) def train(self, labels, data_set, rate, iteration): """ 训练神经网络 :param labels:数组,训练样本标签;每个元素时一个样本的标签 :param data_set:二维数组,训练样本特征;每个元素时一个样本的特征 :param rate: :param iteration: :return: """ for i in range(iteration): for d in range(len(data_set)): self.train_one_sample(labels[d], data_set[d], rate) def train_one_sample(self, label, sample, rate): """ 内部函数,用一个样本训练网络 :param label: :param sample: :param rate: :return: """ self.predict(sample) self.calc_delta(label) self.update_weight(rate) def calc_delta(self, label): """ 内部函数,计算每个节点的delta :param label: :return: """ output_nodes = self.layers[-1].nodes for i in range(len(label)): output_nodes[i].calc_output_layer_delta(label[i]) for layer in self.layer.nodes: for node in layer.nodes: node.calc_hidden_layer_delta() def update_weight(self, rate): """ 内部函数,更新每个连接权重 :param rate: :return: """ for layer in self.layers[:-1]: for node in layer.nodes: for conn in node.downstream: conn.update_weight(rate) def calc_gradient(self): """ 内部函数,计算每个连接的梯度 :return: """ for layer in self.layers[:-1]: for node in layer.nodes: for conn in node.downstream: conn.calc_gradient() def get_gradient(self, label, sample): """ 获得网络在一个样本下,每个连接上的梯度 :param label: 样本标签 :param sample: 样本输入 :return: """ self.predict(sample) self.calc_delta(label) self.calc_gradient() def predict(self, sample): """ 根据输入的样本预测输出值 :param sample: 数组,样本的特征,也就是网络的输入向量 :return: """ self.layers[0].set_output(sample) for i in range(1, len(self.layers)): self.layers[i].calc_out_put() return map(lambda node: node.output, self.layers[-1].nodes[:-1]) def dump(self): """ 打印网络信息 :return: """ for layer in self.layers: layer.dump()
def addConnection(self, A, Z, weight, innovation): self.connectionGenes.append( Connections.connection(A, Z, weight, True, innovation))
def ConstructTreeConvolution(nodes, numFea, numOut,\ Wleft, Wright, Bconstruct,\ Woutput, Boutput,\ ): numNodes = len(nodes) layers = [] numLeaf = 0 for idx in xrange(numNodes): node = nodes[idx] if len(node.children) == 0: numLeaf += 1 tmplayer = Lay.layer('vec_'+str(idx)+'_' + node.word,\ node.bidx,\ numFea ) tmplayer.act = 'embedding' layers.append(tmplayer) # auto encoding # layers = |---leaf---|---non_leaf(autoencoded)---| (numNodes) numNonLeaf = numNodes - numLeaf layers.extend([None] * (numNonLeaf)) for idx in xrange(numLeaf, numNodes): node = nodes[idx] #layers[ idx + numNonLeaf ] = layers [idx] tmplayer = Lay.layer('ae_'+str(idx)+'_'+node.word,\ Bconstruct[0], numFea) tmplayer.act = 'autoencoding' layers[idx] = tmplayer # add reconstruction connections for idx in xrange(0, numNodes): node = nodes[idx] if node.parent == None: continue tmplayer = layers[idx] parent = layers[node.parent] if node.leftRate != 0: Con.connection(tmplayer, parent,\ numFea, numFea, Wleft[0], Wcoef = node.leftRate * node.leafNum/nodes[node.parent].leafNum) if node.rightRate != 0: Con.connection(tmplayer, parent,\ numFea, numFea, Wright[0], Wcoef = node.rightRate * node.leafNum/nodes[node.parent].leafNum) output = Lay.layer('outputlayer', Boutput[0], numOut) Con.connection(layers[-1], output, numFea, numOut, Woutput[0]) if numOut > 1: output._activate = Activation.softmax output._activatePrime = None output.act = 'softmax' else: output._activate = Activation.dummySigmoid output._activatePrime = Activation.dummySigmoidPrime #layers.append(discriminative) layers.append(output) # add successive connections numlayers = len(layers) for idx in xrange(numlayers): if idx > 0: layers[idx].successiveLower = layers[idx - 1] if idx < numlayers - 1: layers[idx].successiveUpper = layers[idx + 1] return layers
def testAddOneConnection(R01, t01): v = Connections() v.addConnection(0, 1, R01, t01, None, None) v.debugConnections()
def lstm(sen): layers = [] ########################################### # construct a layer for each word # Layers # |-----vector-----------| (nWords) # constructing a layer # def __init__(self, name, Bidx, numunit): layers = [] for idx, w in enumerate(sen): if w in vocab: word_id = vocab[w] # d is the word else: word_id = 0 embedLayer = Lay.layer( w, word_id * numEmbed, numEmbed, '0') i = Lay.layer( 'i_' + w, B_i[0], numLSTM, 'l' ) f = Lay.layer( 'f_' + w, B_f[0], numLSTM, 'l' ) o = Lay.layer( 'o_' + w, B_o[0], numLSTM, 'l' ) g = Lay.layer( 'g_' + w, B_g[0], numLSTM, 'l' ) # c_tilde is the c after applying activation function c = Lay.layer( 'c_' + w, -1, numLSTM, '0' ) c_tilde = Lay.layer( 'c_tilde_' + w, -1, numLSTM, 't' ) h = Lay.layer( 'h_' + w, -1, numLSTM, '0' ) layers.append(embedLayer) layers.append( i ) layers.append( f ) layers.append( o ) layers.append( g ) layers.append( c ) layers.append( c_tilde ) layers.append( h ) ######################## # connections within this time slot # connection: # def __init__(self, xlayer, ylayer, Widx, Wcoef = 1.0) # BilinearConnection: # def __init__(self, xlayer1, xlayer2, ylayer, Widx) Con.connection(embedLayer, i, W_i[0]) Con.connection(embedLayer, f, W_f[0]) Con.connection(embedLayer, o, W_o[0]) Con.connection(embedLayer, g, W_g[0]) Con.BilinearConnection( i, g, c, -1 ) Con.connection( c, c_tilde, -1) Con.BilinearConnection( o, c_tilde, h, -1) ######################## # recurrent connections # layers[-9]: hidden layer of last time slot (h) # layers[-11]: cell of last time slot (c) if idx != 0: Con.connection(layers[-9], i, U_i[0]) Con.connection(layers[-9], f, U_f[0]) Con.connection(layers[-9], o, U_o[0]) Con.connection(layers[-9], g, U_g[0]) #print 'layer[-9].name: ', layers[-9].name #print 'layer[-11].name: ', layers[-11].name # self loop in c: Con.BilinearConnection( layers[-11], f, c, -1) ########################### # output layer # softmax outlayer = Lay.layer('output', Bout[0], numOut, 's') Con.connection(layers[-1], outlayer, Wout[0]) layers.append(outlayer) return layers
] # convert ppm to numpy array images = [cv2.imread(f, cv2.IMREAD_GRAYSCALE) for f in imgFiles] # images = images[0:2] numImages = len(images) # load the camera matrices matrixFiles = [open(f) for f in cameraFiles] matrixList = [np.zeros((3, 3)) for f in cameraFiles] for i in range(len(matrixFiles)): for j in range(3): row = matrixFiles[i].readline().split() for k in range(3): matrixList[i][j, k] = np.float(row[k]) # instantiate viewSet object c = Connections(len(images)) # set instrinsics c.setIntrinsics(matrixList[0]) # fill connection table for i in range(numImages): for j in range(i + 1, numImages): R, t, srcPts, dstPts = estimateRelativeExtrinsics( images[i], images[j], i, j, c.intrinsics) # estimate time 35.433154821395874 c.addConnection(i, j, R, t, srcPts, dstPts) # find the matches listOfMatches = [] discovered = [dict() for i in range(numImages)]
class Network(object): def __init__(self, layers): ''' 初始化一个全连接神经网络 layers: 二维数组,描述神经网络每层节点数 ''' self.connections = Connections() self.layers = [] layer_count = len(layers) node_count = 0 for i in range(layer_count): self.layers.append(Layer(i, layers[i])) for layer in range(layer_count - 1): connections = [ Connection(upstream_node, downstream_node) for upstream_node in self.layers[layer].nodes for downstream_node in self.layers[layer + 1].nodes[:-1] ] for conn in connections: self.connections.add_connection(conn) conn.downstream_node.append_upstream_connection(conn) conn.upstream_node.append_downstream_connection(conn) def train(self, labels, data_set, rate, iteration): ''' 训练神经网络 labels: 数组,训练样本标签。每个元素是一个样本的标签。 data_set: 二维数组,训练样本特征。每个元素是一个样本的特征。 ''' for i in range(iteration): for d in range(len(data_set)): self.train_one_sample(labels[d], data_set[d], rate) def train_one_sample(self, label, sample, rate): ''' 内部函数,用一个样本训练网络 ''' self.predict(sample) self.calc_delta(label) self.update_weight(rate) def calc_delta(self, label): ''' 内部函数,计算每个节点的delta ''' output_nodes = self.layers[-1].nodes for i in range(len(label)): output_nodes[i].calc_output_layer_delta(label[i]) for layer in self.layers[-2::-1]: for node in layer.nodes: node.calc_hidden_layer_delta() def update_weight(self, rate): ''' 内部函数,更新每个连接权重 ''' for layer in self.layers[:-1]: for node in layer.nodes: for conn in node.downstream: conn.update_weight(rate) def calc_gradient(self): ''' 内部函数,计算每个连接的梯度 ''' for layer in self.layers[:-1]: for node in layer.nodes: for conn in node.downstream: conn.calc_gradient() def get_gradient(self, label, sample): ''' 获得网络在一个样本下,每个连接上的梯度 label: 样本标签 sample: 样本输入 ''' self.predict(sample) self.calc_delta(label) self.calc_gradient() def predict(self, sample): ''' 根据输入的样本预测输出值 sample: 数组,样本的特征,也就是网络的输入向量 ''' self.layers[0].set_output(sample) for i in range(1, len(self.layers)): self.layers[i].calc_output() return map(lambda node: node.output, self.layers[-1].nodes[:-1]) def dump(self): ''' 打印网络信息 ''' for layer in self.layers: layer.dump() def gradient_check(network, sample_feature, sample_label): ''' 梯度检查 network: 神经网络对象 sample_feature: 样本的特征 sample_label: 样本的标签 ''' # 计算网络误差 network_error = lambda vec1, vec2: \ 0.5 * reduce(lambda a, b: a + b, map(lambda v: (v[0] - v[1]) * (v[0] - v[1]), zip(vec1, vec2))) # 获取网络在当前样本下每个连接的梯度 network.get_gradient(sample_feature, sample_label) # 对每个权重做梯度检查 for conn in network.connections.connections: # 获取指定连接的梯度 actual_gradient = conn.get_gradient() # 增加一个很小的值,计算网络的误差 epsilon = 0.0001 conn.weight += epsilon error1 = network_error(network.predict(sample_feature), sample_label) # 减去一个很小的值,计算网络的误差 conn.weight -= 2 * epsilon # 刚才加过了一次,因此这里需要减去2倍 error2 = network_error(network.predict(sample_feature), sample_label) # 根据式6计算期望的梯度值 expected_gradient = (error2 - error1) / (2 * epsilon) # 打印 print 'expected gradient: \t%f\nactual gradient: \t%f' % ( expected_gradient, actual_gradient)
def testFindCameraSpacePointsFour(): v = Connections() srcPts01 = np.array([[1, 1], [8, 8], [17, 17]]) dstPts01 = np.array([[2, 2], [9, 9], [18, 18]]) v.addConnection(0, 1, None, None, srcPts01, dstPts01) srcPts02 = np.array([[5, 5], [19, 19]]) dstPts02 = np.array([[7, 7], [20, 20]]) v.addConnection(0, 2, None, None, srcPts02, dstPts02) srcPts03 = np.array([[8, 8], [11, 11]]) dstPts03 = np.array([[10, 10], [13, 13]]) v.addConnection(0, 3, None, None, srcPts03, dstPts03) srcPts12 = np.array([[2, 2], [6, 6], [14, 14], [21, 21]]) dstPts12 = np.array([[3, 3], [7, 7], [15, 15], [22, 22]]) v.addConnection(1, 2, None, None, srcPts12, dstPts12) srcPts13 = np.array([[14, 14], [23, 23]]) dstPts13 = np.array([[16, 16], [24, 24]]) v.addConnection(1, 3, None, None, srcPts13, dstPts13) srcPts23 = np.array([[3, 3], [12, 12], [25, 25]]) dstPts23 = np.array([[4, 4], [13, 13], [26, 26]]) v.addConnection(2, 3, None, None, srcPts23, dstPts23) # v.debugConnections() listOfMatches = [] discovered = [dict(), dict(), dict(), dict()] v.findMatches(listOfMatches, discovered) [match.debugViews() for match in listOfMatches]
def ConstructTreeConvolution(phrase_1, phrase_2, word_dict, numFea,numLeft, numRight, numJoint, numDis, numOut, \ Wleft, Wright, Bleft, Bright, Wjoint_left, Wjoint_right, Bjoint, Wdis, Wout, Bdis, Bout ): # nodes # numFea: # of the word/symbol feature size phrase_1_len = len(phrase_1) phrase_2_len = len(phrase_2) layer_left = Lay.layer('left', Bleft, numLeft) layer_right = Lay.layer('right', Bright, numRight) #embedding layer 1 emb_layer1 = [None] * phrase_1_len layers = [] for idx in xrange(phrase_1_len): word = phrase_1[idx] if word in word_dict.keys(): # get index of the word in dictionary bidx = word_dict[word] * numFea emb_layer1[idx] = Lay.layer('vec_' + word + '_', \ range(bidx, bidx + numFea), \ numFea ) emb_layer1[idx].act = 'embedding' con_left = Con.connection(emb_layer1[idx], layer_left, numFea, numLeft, Wleft) layers.append(emb_layer1[idx]) #embedding layer 2 emb_layer2 = [None] * phrase_2_len for idx in xrange(phrase_2_len): word = phrase_2[idx] if word in word_dict.keys(): # get index of the word in dictionary if word in word_dict: bidx = word_dict[word] * numFea else: bidx = 0 emb_layer2[idx] = Lay.layer('vec_' + word + '_', \ range(bidx, bidx + numFea), \ numFea ) emb_layer2[idx].act = 'embedding' con_right = Con.connection(emb_layer2[idx], layer_right, numFea, numRight, Wright) layers.append(emb_layer2[idx]) layers.append(layer_left) layers.append(layer_right) joint = Lay.layer('joint', Bjoint, numJoint) con_left = Con.connection(layer_left, joint, numLeft, numJoint, Wjoint_left) con_right = Con.connection(layer_right, joint, numRight, numJoint, Wjoint_right) layers.append(joint) discriminative = Lay.layer('discriminative', Bdis, numDis) discriminative.act = 'hidden' discon = Con.connection(joint, discriminative, numJoint, numDis, Wdis) output = Lay.layer('outputlayer', Bout, numOut) output.act = 'softmax' outcon = Con.connection(discriminative, output, numDis, numOut, Wout) if numOut > 1: output._activate = Activation.softmax output._activatePrime = None layers.append(discriminative) layers.append(output) # add successive connections numlayers = len(layers) for idx in xrange(numlayers): if idx > 0: layers[idx].successiveLower = layers[idx - 1] if idx < numlayers - 1: layers[idx].successiveUpper = layers[idx + 1] return layers
def where_users(): return (Connections.LocalUsers())
def __init__(self): self.complete = False self.db = Connections.Connections()
def ConstructTreeConvolution(nodes, numFea, numCon, numDis, numOut,\ Wleft, Wright, Bconstruct,\ Wcomb_ae, Wcomb_orig, \ Wconv_root, Wconv_left, Wconv_right, Wconv_sib, Bconv,\ Wdis, Woutput, Bdis, Boutput,\ poolCutoff ): # nodes # numFea: # of the word/symbol feature size # numCon: # of the convolution size # Wleft: left weights of continous binary tree autoencoder # Wright: right weights of continous binary tree autoencoder # Bconstruct: the biase for the autoencoder # Wcomb_ae, Wcomb_orig: the weights for the combination of # autoencoder and the original vector # (no biase for this sate) # Wconv_root, Wconv_left, Wconv_right, Bconv: the weights for covolution # Bconv: Biases for covolution numNodes = len(nodes) layers = [None] * numNodes # construct layers for each node # layers = |---leaf---|---non_leaf---| numLeaf = 0 for idx in xrange(numNodes): node = nodes[idx] if len(node.children) == 0: numLeaf += 1 layers[idx] = Lay.layer('vec_'+str(idx)+'_' + node.word,\ range( node.bidx, node.bidx + numFea),\ numFea ) layers[idx].act = 'embedding' # auto encoding # layers = |---leaf---|---non_leaf(autoencoded)---| (numNodes) # |---non_leaf(original)---| ( numNonLeaf) numNonLeaf = numNodes - numLeaf layers.extend([None] * (2 * numNonLeaf)) for idx in xrange(numLeaf, numNodes): node = nodes[idx] layers[idx + numNonLeaf] = layers[idx] tmplayer = Lay.layer('ae_'+str(idx)+'_'+node.word,\ Bconstruct, numFea) tmplayer.act = 'autoencoding' layers[idx] = tmplayer # add reconstruction connections for idx in xrange(0, numNodes): node = nodes[idx] if node.parent == None: continue tmplayer = layers[idx] parent = layers[node.parent] if node.leftRate != 0: leftcon = Con.connection(tmplayer, parent,\ numFea, numFea, Wleft, Wcoef = node.leftRate * node.leafNum/nodes[node.parent].leafNum) if node.rightRate != 0: rightcon = Con.connection(tmplayer, parent,\ numFea, numFea, Wright, Wcoef = node.rightRate * node.leafNum/nodes[node.parent].leafNum) # combinition of the constructed and original value # layers = |---leaf---|---non_leaf(combinition)---| (numNodes) # |---non_leaf(original)---|---non_leaf(ae)---| (2 * numNonLeaf) for idx in xrange(numLeaf, numNodes): aelayer = layers[idx] origlayer = layers[idx + numNonLeaf] layers[idx + numNonLeaf * 2] = aelayer comlayer = Lay.layer('comb_' + str(idx) + '_' + nodes[idx].word, None, numFea) comlayer.act = 'combination' layers[idx] = comlayer # connecton auto encoded vector and original vector con_ae = Con.connection(aelayer, comlayer, numFea, numFea, Wcomb_ae) con_orig = Con.connection(origlayer, comlayer, numFea, numFea, Wcomb_orig) # CONVOLVE!!! and POOOOL!!! # layers = |---leaf---|---non_leaf(combition)---| => (numNodes) # |---non_leaf(original)---|---non_leaf(ae)---| => (2 * numNonLeaf) # |------------convolution----------| queue = [(numNodes - 1, None)] poolTop = Lay.PoolLayer('poolTop', numCon) poolLeft = Lay.PoolLayer('poolLeft', numCon) poolRight = Lay.PoolLayer('poolRight', numCon) layerCnt = 0 rootChildrenNum = len(nodes[-1].children) - 1 while True: curLen = len(queue) #layerCnt.append( curLen ) if curLen == 0: break nextQueue = [] for (nodeidx, info) in queue: curLayer = layers[nodeidx] curNode = nodes[nodeidx] conLayer = Lay.layer('Convolve_' + curLayer.name, \ Bconv, numCon) conLayer.act = 'convolution' layers.append(conLayer) # add root connection rootCon = Con.connection(curLayer, conLayer, numFea, numCon, Wconv_root) # add sibling connections for sib in curNode.siblings: sibNode = nodes[sib] sibLayer = layers[sib] sib_childrenNum = len(sibNode.children) if sib_childrenNum == 0: sib_childrenNum = 1 sib_Weight = 1.0 * sib_childrenNum / len(curNode.siblings) sibCon = Con.connection(sibLayer, conLayer, \ numFea, numCon, Wconv_sib, sib_Weight) childNum = len(curNode.children) #print curLayer.name, info # pooling if layerCnt < poolCutoff: poolCon = Con.PoolConnection(conLayer, poolTop) else: # TODO if layerCnt >= poolCutoff if info == 'l' or info == 'lr': poolCon = Con.PoolConnection(conLayer, poolLeft) if info == 'r' or info == 'lr': poolCon = Con.PoolConnection(conLayer, poolRight) # for each child of the current node for child in curNode.children: childNode = nodes[child] childLayer = layers[child] if layerCnt != 0 and info != 'u': childinfo = info else: rootChildrenNum = len(curNode.children) - 1 if rootChildrenNum == 0: childinfo = 'u' elif childNode.pos <= rootChildrenNum / 2.0: childinfo = 'l' else: # childNode.pos > rootChildrenNum/2.0: childinfo = 'r' #else: # childinfo = 'lr' nextQueue.append((child, childinfo)) # add to child if childNum == 1: leftWeight = .5 rightWeight = .5 else: rightWeight = childNode.pos / (childNum - 1.0) leftWeight = 1 - rightWeight if leftWeight != 0: leftCon = Con.connection(childLayer, conLayer,\ numFea, numCon, Wconv_left, leftWeight) if rightWeight != 0: rightCon = Con.connection(childLayer, conLayer,\ numFea, numCon, Wconv_right, rightWeight) # end of each child of the current node queue = nextQueue layerCnt += 1 # end of current layer layers.append(poolTop) layers.append(poolLeft) layers.append(poolRight) # reorder # layers = |---leaf---|---non_leaf(ae)---| => (numNodes) # |---non_leaf(original)---|---non_leaf(comb)---| => (2 * numNonLeaf) # |------------convolution----------| for idx in xrange(numLeaf, numLeaf + numNonLeaf): tmp = layers[idx] layers[idx] = layers[idx + 2 * numNonLeaf] layers[idx + 2 * numNonLeaf] = tmp # discrimitive layer # layers = |---leaf---|---non_leaf(ae)---| => (numNodes) # |---non_leaf(original)---|---non_leaf(comb)---| => (2 * numNonLeaf) # |------------convolution----------| # |---3 pools---| ## POOOOOOOOOOOL # # pool all # poolLayer = Lay.PoolLayer('pool', numCon) # for idx in xrange( numNodes + 2* numNonLeaf, len(layers) ): # poolCon = Con.PoolConnection(layers[idx], poolLayer) # poolLayer.connectDown.append(poolCon) # layers[idx].connectUp.append(poolCon) # layers.append(poolLayer) # discriminative layer # layers = |---leaf---|---non_leaf(ae)---| => (numNodes) # |---non_leaf(original)---|---non_leaf(comb)---| => (2 * numNonLeaf) # |------------convolution----------| # |---3 pools---| # |---discriminative layer-----| # |--output--| numPool = 3 lenlayer = len(layers) conbegin = lenlayer - numPool discriminative = Lay.layer('discriminative', Bdis, numDis) discriminative.act = 'hidden' output = Lay.layer('outputlayer', Boutput, numOut) output.act = 'softmax' #One Weight Size ows = numDis * numCon for idx in xrange(numPool): poollayer = layers[idx + conbegin] con = Con.connection(poollayer, discriminative, numCon, numDis, Wdis[idx * ows:(idx * ows + ows)]) outcon = Con.connection(discriminative, output, numDis, numOut, Woutput) if numOut > 1: output._activate = Activation.softmax output._activatePrime = None layers.append(discriminative) layers.append(output) # add successive connections numlayers = len(layers) for idx in xrange(numlayers): if idx > 0: layers[idx].successiveLower = layers[idx - 1] if idx < numlayers - 1: layers[idx].successiveUpper = layers[idx + 1] return layers
def ConstructTreeConvolution(nodes, numFea, numRecur, numDis, numOut, \ Wleft, Wright, Bconstruct, \ Wcomb_ae, Wcomb_orig, \ Wrecur_root, Wrecur_left, Wrecur_right, Wrecur_sib, Brecur, \ Wdis, Woutput, Bdis, Boutput, \ poolCutoff ): # nodes # numFea: # of the word/symbol feature size # numCon: # of the convolution size # Wleft: left weights of continous binary tree autoencoder # Wright: right weights of continous binary tree autoencoder # Bconstruct: the biase for the autoencoder # Wcomb_ae, Wcomb_orig: the weights for the combination of # autoencoder and the original vector # (no biase for this sate) # Wconv_root, Wconv_left, Wconv_right, Bconv: the weights for covolution # Bconv: Biases for covolution numNodes = len(nodes) layers = [None] * numNodes # construct layers for each node # layers = |---leaf---|---non_leaf---| numLeaf = 0 for idx in xrange(numNodes): node = nodes[idx] if len(node.children) == 0: numLeaf += 1 layers[idx] = Lay.layer('vec_' + str(idx) + '_' + node.word, \ range(node.bidx, node.bidx + numFea), \ numFea ) layers[idx].act = 'embedding' # auto encoding # layers = |---leaf---|---non_leaf(autoencoded)---| (numNodes) # |---non_leaf(original)---| ( numNonLeaf) numNonLeaf = numNodes - numLeaf layers.extend([None] * (2 * numNonLeaf)) for idx in xrange(numLeaf, numNodes): node = nodes[idx] layers[idx + numNonLeaf] = layers[idx] tmplayer = Lay.layer('ae_' + str(idx) + '_' + node.word, \ Bconstruct, numFea) tmplayer.act = 'autoencoding' layers[idx] = tmplayer # add reconstruction connections for idx in xrange(0, numNodes): node = nodes[idx] if node.parent == None: continue tmplayer = layers[idx] parent = layers[node.parent] if node.leftRate != 0: leftcon = Con.connection(tmplayer, parent, \ numFea, numFea, Wleft, Wcoef=node.leftRate * node.leafNum / nodes[node.parent].leafNum) if node.rightRate != 0: rightcon = Con.connection(tmplayer, parent, \ numFea, numFea, Wright, Wcoef=node.rightRate * node.leafNum / nodes[node.parent].leafNum) # combinition of the constructed and original value # layers = |---leaf---|---non_leaf(combinition)---| (numNodes) # |---non_leaf(original)---|---non_leaf(ae)---| (2 * numNonLeaf) for idx in xrange(numLeaf, numNodes): aelayer = layers[idx] origlayer = layers[idx + numNonLeaf] layers[idx + numNonLeaf * 2] = aelayer comlayer = Lay.layer('comb_' + str(idx) + '_' + nodes[idx].word, None, numFea) comlayer.act = 'combination' layers[idx] = comlayer # connecton auto encoded vector and original vector con_ae = Con.connection(aelayer, comlayer, numFea, numFea, Wcomb_ae) con_orig = Con.connection(origlayer, comlayer, numFea, numFea, Wcomb_orig) # CONVOLVE!!! and POOOOL!!! # layers = |---leaf---|---non_leaf(combition)---| => (numNodes) # |---non_leaf(original)---|---non_leaf(ae)---| => (2 * numNonLeaf) # |------------convolution----------| queue = [(numNodes - 1, None)] rootChildrenNum = len(nodes[-1].children) - 1 recurLayers = {} # the map of recursive layer #copy leaf for idx in xrange( 0, numLeaf): # leaf ---> recursive leaf: in numFea, out numRecur recurLayers[idx] = Lay.layer('Recur_' + str(idx) + '_' + nodes[idx].word, \ Brecur, numRecur) Con.connection(layers[idx], recurLayers[idx], numFea, numRecur, Wrecur_root) while True: curLen = len(queue) # layerCnt.append( curLen ) if curLen == 0: break nextQueue = [] for (nodeidx, info) in queue: curLayer = layers[nodeidx] curNode = nodes[nodeidx] childNum = len(curNode.children) if childNum == 0: # leaf node queue = nextQueue continue # create recursive node if nodeidx not in recurLayers.keys(): recurLayer = Lay.layer('Recur_' + str(nodeidx) + '_'+ curNode.word, \ Brecur, numRecur) recurLayer.act = 'recursive' #layers.append(recurLayer) recurLayers[nodeidx] = recurLayer recurLayer = recurLayers[nodeidx] # add root connection from Combination layer rootCon = Con.connection(curLayer, recurLayer, numFea, numRecur, Wrecur_root) # add connection from one previous sibling sibs_idx = curNode.siblings sibs_idx = [i for i in sibs_idx if i < nodeidx] if len(sibs_idx) > 0: sibs_idx.sort(reverse=True) sib_idx = sibs_idx[0] sibNode = nodes[sib_idx] if sib_idx not in recurLayers.keys(): sibLayer = Lay.layer('Recur_' + str(sib_idx) + '_' + sibNode.word, \ Brecur, numRecur) recurLayers[sib_idx] = sibLayer sibLayer = recurLayers[sib_idx] sib_childrenNum = len(sibNode.children) if sib_childrenNum == 0: sib_childrenNum = 1 sib_Weight = 1.0 * sib_childrenNum / len(curNode.siblings) sibCon = Con.connection(sibLayer, recurLayer, \ numRecur, numRecur, Wrecur_sib, sib_Weight) # for each child of the current node for child in curNode.children: childNode = nodes[child] if child not in recurLayers.keys(): childLayer = Lay.layer('Recur_' + str(child) + '_' + childNode.word, \ Brecur, numFea) #layers.append(childLayer) recurLayers[child] = childLayer childLayer = recurLayers[child] nextQueue.append((child, '')) # add to child if childNum == 1: leftWeight = .5 rightWeight = .5 else: rightWeight = childNode.pos / (childNum - 1.0) leftWeight = 1 - rightWeight if leftWeight != 0: leftCon = Con.connection(childLayer, recurLayer, \ numRecur, numRecur, Wrecur_left, leftWeight) if rightWeight != 0: rightCon = Con.connection(childLayer, recurLayer, \ numRecur, numRecur, Wrecur_right, rightWeight) # end of each child of the current node queue = nextQueue # end of current layer # add recursive layer for idx in xrange(0, numNodes): layers.append(recurLayers[idx]) # reorder # layers = |---leaf---|---non_leaf(ae)---| => (numNodes) # |---non_leaf(original)---|---non_leaf(comb)---| => (2 * numNonLeaf) # |------------convolution----------| for idx in xrange(numLeaf, numLeaf + numNonLeaf): tmp = layers[idx] layers[idx] = layers[idx + 2 * numNonLeaf] layers[idx + 2 * numNonLeaf] = tmp # discriminative layer # layers = |---leaf---|---non_leaf(ae)---| => (numNodes) # |---non_leaf(original)---|---non_leaf(comb)---| => (2 * numNonLeaf) # |------------recursive----------| # |---discriminative layer-----| # |--output--| lenlayer = len(layers) rootRecur = recurLayers[numNodes - 1] discriminative = Lay.layer('discriminative', Bdis, numDis) discriminative.act = 'hidden' output = Lay.layer('outputlayer', Boutput, numOut) output.act = 'softmax' # One Weight Size con = Con.connection(rootRecur, discriminative, numFea, numDis, Wdis) outcon = Con.connection(discriminative, output, numDis, numOut, Woutput) if numOut > 1: output._activate = Activation.softmax output._activatePrime = None layers.append(discriminative) layers.append(output) # add successive connections numlayers = len(layers) for idx in xrange(numlayers): if idx > 0: layers[idx].successiveLower = layers[idx - 1] if idx < numlayers - 1: layers[idx].successiveUpper = layers[idx + 1] return layers