def generate_c(self, shape, name="main", **kwargs): if kwargs.get("inp"): inp = ",".join([p for p in inp.get("inp")]) else: inp = "0" text = """ #include "ebnn.h" """ h = np.random.random([1] + list(shape)).astype(np.float32) input_size = h.size inter_sizes = [] for i, link in enumerate(self.links): if isinstance( link, Sequential): # branch off to the first branch only for now for j, link in enumerate(link.links): if hasattr(link, 'generate_c'): inter_sizes.append(link.temp_mem(h.shape)) text += link.generate_c(i + j, h.shape) h = link(h, test=True) break if hasattr(link, 'generate_c'): inter_sizes.append(link.temp_mem(h.shape)) text += link.generate_c(i, h.shape) h = link(h, test=True) inter_size = int(np.max(inter_sizes)) text += """ uint8_t temp1[{inter_size}] = {{0}}; uint8_t temp2[{inter_size}] = {{0}}; """.format(name=name, input_size=input_size, inter_size=inter_size, inp=inp) text += self.generate_call() return text
def proc(name, query, sorting, compare): print "\nProcedure: "+name print "="*15 a = time.time() print "reading & sorting sql-query into dataframe.." df = readsort(query, sorting) print "dataframe read, "+str(len(df))+" rows" print "linking.." print "progress:" matches = link(df, compare, name) print "pickling & writing matches.." picklewrite(matches, name) #print "writing sorted txt.." #writesorted(df,name) b = time.time() print str(timedelta(seconds=int(b-a))) print "\n" beep()
def addlink(): """ Check for update link """ if request.vars.lid: url = request.vars.url; desc = request.vars.description; tags = request.vars.tags vis = request.vars.vis; query = myDB.link.lid == request.vars.lid myDB(query).update(url=url, visibility=vis, description=desc, tags=tags) return dict(message=T('Link updated Successfully')) if request.vars.id: query = myDB.link.lid == request.vars.id; r = myDB(query).select(); l = link(r[0].url, r[0].lid, r[0].tags, r[0].description, r[0].created_date, r[0].visibility) return dict(link=l,update_current=1) """ Add new link else """ if request.vars.url: url = request.vars.url; desc = request.vars.description; tags = request.vars.tags vis = request.vars.vis; tm = int(time.time()) id = auth.user_id; query_1 = myDB.link.user_id == id query_2 = myDB.link.url == url rows = myDB(query_1 & query_2).select() if rows: myDB(query_1 & query_1).update(visibility=vis, description=desc, tags=tags) return dict(message=T('Link has been updated Successfully')) myDB.link.insert(user_id=auth.user_id, url=url, visibility=vis, tags=tags, description=desc, created_date=tm) return dict(message=T('Link has been added Successfully')) """ Add a link from followed user """ if request.vars.follow_lid: q1 = myDB.link.lid == request.vars.follow_lid r = myDB(q1).select() l = link(r[0].url,r[0].lid, r[0].tags, r[0].description, r[0].created_date, "private") return dict(link=l,update_current=0) return dict()
def predict_with_mask(self, x, ent_Ts=None, test=True): num = x.shape[0] bs = [] exit_i = 0 for i, link in enumerate(self.links): if isinstance(link, Sequential): b = link(x, test=test) b = b[0] y_exit, y_cont, exited = self.entropy_filter( x, b, ent_Ts[min(exit_i, len(ent_Ts) - 1)]) exit_i = exit_i + 1 b = y_exit bs.append((b, exited)) x = y_cont if len(x) == 0: break elif isinstance(link, function.dropout): x = link(x, train=not test) elif isinstance(link, chainer.links.BatchNormalization): x = link(x, test=test) elif hasattr(link, '__call__') and 'train' in inspect.getargspec( link.__call__)[0]: x = link(x, train=not test) elif hasattr(link, '__call__') and 'test' in inspect.getargspec( link.__call__)[0]: x = link(x, test=test) else: x = link(x) #if len(x) > 0: # bs.append((x,[True]*x.shape[0])) ys = [None] * num exited = [False] * num # branch exit for b, ex in bs: i = 0 j = 0 for exit in ex: while ys[i] is not None: i = i + 1 if exit: ys[i] = b[j] exited[i] = True #only count the branch exited j = j + 1 i = i + 1 # main exit if len(x) > 0: b, ex = (x, [True] * x.shape[0]) i = 0 j = 0 for exit in ex: while ys[i] is not None: i = i + 1 if exit: ys[i] = b[j] j = j + 1 i = i + 1 return F.vstack(ys), exited
def show_links(): """Retrieve links saved by the user from DB""" query_1 = myDB.link.user_id == auth.user_id rows = myDB(query_1).select() L = [] size = len(rows) for l in rows: tags = [] tags = l.tags.split(",") link_save = link(l.url, l.lid, tags, l.description, l.created_date, l.visibility) L.append(link_save) return dict(links=L)
def con_evaluate(self, x, layer_number): layer_number = int(layer_number) if (layer_number != 0): con_links = self.links[layer_number + 1:] else: con_links = self.links test = True #print "Sample shape : %s , type : %s , current_layer : %s" % (x.data.shape ,x.data.dtype , layer_number) x.to_gpu(0) layer_NO = layer_number + 1 for i, link in enumerate(con_links): if isinstance(link, Sequential): #print "layer_NO : %s , Sequential : %s" % (layer_NO,link) if self.current_stage not in link._stages: y = Variable(x.data, x.volatile) else: y = x b = link(y, test=test) layer_NO = layer_NO + 1 elif isinstance(link, function.dropout): #print "layer_NO : %s , Dropout : %s" % (layer_NO,link) x = link(x, train=not test) #print "Output shape : %s , type : %s" % (x.data.shape,x.data.dtype) elif isinstance(link, chainer.links.BatchNormalization): #print "layer_NO : %s , BatchNormalization : %s" % (layer_NO,link) x = link(x, test=test) #print "Output shape : %s , type : %s" % (x.data.shape,x.data.dtype) layer_NO = layer_NO + 1 elif hasattr(link, '__call__') and 'train' in inspect.getargspec( link.__call__)[0]: #print("train",link) x = link(x, train=not test) elif hasattr(link, '__call__') and 'test' in inspect.getargspec( link.__call__)[0]: #print "layer_NO : %s , Test : %s" % (layer_NO,link) x = link(x, test=test) #print "Output shape : %s , type : %s" % (x.data.shape,x.data.dtype) layer_NO = layer_NO + 1 else: #print "layer_NO : %s , Other : %s" % (layer_NO,link) x = link(x) #print "Output shape : %s , type : %s" % (x.data.shape,x.data.dtype) layer_NO = layer_NO + 1 #print "x result is : %s" % (x.data[0]) #print "x.data type = %s" % (type(x.data[0])) return (x.data[0].argmax())
def user(): """Home page of the user to display public links of people followed""" #print "user id", auth.user_id if not auth.user: redirect(URL("default", "login", args=["login"])); #Mrugani--- q0 = myDB.auth_user.id == auth.user_id user_info = myDB(q0).select() public_links = get_public_links_count(myDB, auth.user_id) private_links = get_private_links_count(myDB, auth.user_id) followers =get_followees(myDB, auth.user_id) following = get_followers(myDB, auth.user_id) #Taking last entry as private link logged_in = user_details(user_info[0].id, user_info[0].first_name, user_info[0].last_name, user_info[0].username, 0,followers,following,public_links,private_links); logged_in.set_joining_date(int(user_info[0].Joining_date)) #Siji------ q1 = myDB.follow.follower == auth.user_id q2 = myDB.follow.followee == myDB.link.user_id q3 = myDB.link.visibility == "public" q4 = myDB.follow.followee == myDB.auth_user.id q5 = myDB.follow.followee == myDB.auth_user.id rows = myDB(q1 & q2 & q3 & q4 & q5).select(orderby="link.created_date DESC") L=[] for l in rows: tags = [] tags = l.link.tags.split(",") q6 = myDB.link.user_id == auth.user_id q7 = myDB.link.url == l.link.url check = myDB(q6 & q7).select() if len(check)==0: exist=0 else: exist=1 #print "exist " ,exist link_display=link(l.link.url, l.link.lid, tags, l.link.description, l.link.created_date, "public") user_display=user_details(l.link.user_id,l.auth_user.first_name,l.auth_user.last_name,l.auth_user.username,exist) user_display.set_link_details(link_display) #print "user_display.exist", user_display.exist L.append(user_display) #print user_display.firstname,user_display.lastname,user_display.username,user_display.link_details.url return dict(following=L, user=logged_in)
def __call__(self, x, test=False, output_inter=False): bs = [] numlinks = len(self.links) if output_inter: interm_results = [x] for i, link in enumerate(self.links): if isinstance(link, Sequential): # detach if in different stages #if reduce(lambda x,y: x and y, [stage not in link._stages for stage in self._stages]): if self.current_stage not in link._stages: y = Variable(x.data, x.volatile) else: y = x b = link(y, test=test) bs.append(b[0]) # Currently not support branch inside a branch elif isinstance(link, function.dropout): x = link(x, train=not test) elif isinstance(link, chainer.links.BatchNormalization): x = link(x, test=test) elif hasattr(link, '__call__') and 'train' in inspect.getargspec( link.__call__)[0]: #print("train",link) x = link(x, train=not test) elif hasattr(link, '__call__') and 'test' in inspect.getargspec( link.__call__)[0]: #print("test",link) x = link(x, test=test) else: x = link(x) # do not update this branch if not the current stage if self.current_stage not in self._stages: x.unchain_backward() if output_inter: interm_results.append(x.data) bs.append(x) if output_inter: return tuple(bs), interm_results else: return tuple(bs)
def __call__(self, x, test=False, output_inter=False): print "Enter sequential call()" print "Sample shape : %s , type : %s" % (x.data.shape, x.data.dtype) #print "Sample input : %s" % (x[0][0].data) bs = [] numlinks = len(self.links) if output_inter: interm_results = [x] #print "Sample input : %s" % (x[0][0].data) layer_NO = 0 for i, link in enumerate(self.links): if isinstance(link, Sequential): # detach if in different stages #if reduce(lambda x,y: x and y, [stage not in link._stages for stage in self._stages]): #print("Sequential",link) print "layer_NO : %s , Sequential : %s" % (layer_NO, link) if self.current_stage not in link._stages: y = Variable(x.data, x.volatile) else: y = x print "Last bconv output : %s" % (x.data) b = link(y, test=test) bs.append(b[0]) layer_NO = layer_NO + 1 # Currently not support branch inside a branch elif isinstance(link, function.dropout): print("dropout", link) x = link(x, train=not test) print "Output shape : %s , type : %s" % (x.data.shape, x.data.dtype) elif isinstance(link, chainer.links.BatchNormalization): #print("BatchNormalization",link) print "layer_NO : %s , BatchNormalization : %s" % (layer_NO, link) x = link(x, test=test) print "Output shape : %s , type : %s" % (x.data.shape, x.data.dtype) layer_NO = layer_NO + 1 elif hasattr(link, '__call__') and 'train' in inspect.getargspec( link.__call__)[0]: print("train", link) x = link(x, train=not test) elif hasattr(link, '__call__') and 'test' in inspect.getargspec( link.__call__)[0]: #print("test",link) print "layer_NO : %s , test : %s" % (layer_NO, link) x = link(x, test=test) print "Output shape : %s , type : %s" % (x.data.shape, x.data.dtype) #if layer_NO < 3 : # print "Output data : %s " % (x[0][0].data) layer_NO = layer_NO + 1 else: print "layer_NO : %s , other : %s" % (layer_NO, link) x = link(x) print "Output shape : %s , type : %s" % (x.data.shape, x.data.dtype) if layer_NO == 4 or layer_NO == 5 or layer_NO == 15: print "Output data : %s " % (x[0][0].data) layer_NO = layer_NO + 1 # do not update this branch if not the current stage if self.current_stage not in self._stages: x.unchain_backward() if output_inter: interm_results.append(x.data) bs.append(x) print "x result is %s" % (x.data) if output_inter: return tuple(bs), interm_results else: return tuple(bs)
def search(): """ Search by username, tags and url """ if request.vars.keyword: keyword = request.vars.keyword option = request.vars.option """Search by user""" if option == "Users": keyword = "%"+keyword+"%" query = myDB.auth_user.username.like(keyword) rows = myDB(query).select() user_list = [] for user in rows: id = user.id; followers =get_followees(myDB, id) following = get_followers(myDB, id) query_2 = myDB.auth_user.id == id; details = myDB(query_2).select() public_links = get_public_links_count(myDB, id) follow_details = get_is_following(myDB, id, auth.user_id) if follow_details: follow = 1; else: follow = 0; #Remaining- counts user_det = user_details(id,details[0].first_name, details[0].last_name, user.username, 0, followers, following, public_links, follow) user_det.set_joining_date(int(details[0].Joining_date)) user_list.append(user_det) search_query = "Users="+keyword[1:-1] return dict(users=user_list, query=search_query) #Search by tags elif option == "Tags": keyword = "%"+keyword+"%" #check if keyword contains comma.. query_1 = myDB.link.user_id == auth.user_id query_2 = myDB.link.tags.like(keyword) rows = myDB(query_1 & query_2).select() L = [] for l in rows: tags = [] tags = l.tags.split(",") link_save = link(l.url, l.lid, tags, l.description, l.created_date, l.visibility) L.append(link_save) query = "Tags="+keyword[1:-1] return dict(links=L, query=query) #search by URL elif option == "URL": keyword = "%"+keyword+"%" #check if keyword contains comma.. query_1 = myDB.link.user_id == auth.user_id query_2 = myDB.link.url.like(keyword) rows = myDB(query_1 & query_2).select() L = [] for l in rows: tags = [] tags = l.tags.split(",") link_save = link(l.url, l.lid, tags, l.description, l.created_date, l.visibility) L.append(link_save) query = "URL="+keyword[1:-1] return dict(links=L, query=query) return dict()