def __init__(self, train=True, transform=None, denoising=True): super(Dataset, self).__init__() self.transform = transform self.train = train self.denoising = denoising if self.train: h5f = h5py.File('train.h5', 'r') else: h5f = h5py.File('val.h5', 'r') self.keys = list(h5f.keys()) sf(self.keys) h5f.close()
def generate(): """Generates a simple list with lineraly number representing the heights of rectangles""" global height_list, rect_list height_list.clear() rect_list.clear() rectangle_pane.delete("all") max_height = screen_width - 4 height_list = [ int(max_height - (max_height - 1) * (number - i) / number) for i in range(number) ] sf(height_list) draw(height_list) [x.config(state="enabled") for x in button_list]
def shuffle(self): index = np.arange(len(self.mols)) sf(index) X = [self.mols[int(i)] for i in index] Y = [self.propertys[int(i)] for i in index] self.train_mols = X[:int(len(X)*0.9)] self.train_pro = Y[:int(len(Y)*0.9)] self.test_mols = X[int(len(X)*0.9):] self.test_pro = Y[int(len(Y)*0.9):] with open(self.path + 'data/information.txt', 'a') as f: f.write('Molecule numbers: \nTrain:%d, test:%d \n' %(len(self.train_mols), len(self.test_mols))) with open(self.path + 'data/train_mols.json', 'w') as f: json.dump(self.train_mols, f) with open(self.path + 'data/train_pro.json', 'w') as f: json.dump(self.train_pro, f) with open(self.path + 'data/test_mols.json', 'w') as f: json.dump(self.test_mols, f) with open(self.path + 'data/test_pro.json', 'w') as f: json.dump(self.test_pro, f)
def on_epoch_end(self): self.restant_classes_pedestrians = dict([ (ped, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) for ped in self.list_IDs ]) #Al terminar una época la lista de restances debería de volver a ser reasignada self.list_IDs_epoch = list() #Obtención de una lista con las instancias que se van a utilizar para entrenar la primera epoca while len(self.list_IDs_epoch) != (len(self.list_IDs) * 12): aux = self.list_IDs.copy() sf(aux) for ped in aux: if self.restant_classes_pedestrians[ped] != []: ped_noext = splitext(ped)[0] # Del peatón seleccionado se selecciona de manera aleatoria una de sus clases pos_class = np.random.randint( len(self.restant_classes_pedestrians[ped])) class1 = self.restant_classes_pedestrians[ped][pos_class] self.restant_classes_pedestrians[ped].remove(class1) pos_class = np.random.randint( len(self.restant_classes_pedestrians[ped])) class2 = self.restant_classes_pedestrians[ped][pos_class] self.restant_classes_pedestrians[ped].remove(class2) self.list_IDs_epoch.append( (ped_noext + '_' + str(class1) + 'p.pkl')) self.list_IDs_epoch.append( (ped_noext + '_' + str(class2) + 'p.pkl'))
def eval(gakuseki): # gakuseki = 115114 # gakuseki = 114999 p = 19 q = 31 n = p * q # print(p, "p") # print(q, "q") # print(n, "n") m = gakuseki % (n - 2) + 2 ln = lcm(pfac(p - 1), pfac(q - 1)) pfac18 = " \\times ".join(map(str, pfac(p - 1))) pfac30 = " \\times ".join(map(str, pfac(q - 1))) pfaclast = " \\times ".join(map(str, ar)) # e = [x for x in range(ln + 1) if gcd(x, ln) == 1] gcdlist = [gcd(x, ln) for x in range(ln + 1)] e = [i for i, x in enumerate(gcdlist) if x == 1] es = ", ".join(map(str, e)) gcdlist = "".join([ "GCD(%d, \\lambda(n))&= %d\\\\\n" % (i, x) for i, x in enumerate(gcdlist) ]) gcdlist = gcdlist[:-3] # print(", ".join(map(str, e)), "eの候補") e = [x for x in e if 1 < x < 20] sf(e) e = e[0] # e = 7 # print(e, "e") d = 1 while (d * e - 1) % ln: d += 1 de = d * e - 1 # print(d, "d") # print(hs) # tmp = 333 # for i in range(13): # hs, tmp = hissan.kakezan(tmp, m) # print(hs) c = m**e % n me = m**e hsk, val = hissan.ruizyo(m, e) hsd, val = hissan.warizan(me, n) hukugo = c**d % n nz = "" result = c for i in range(d): string = "$" + str(c) + "^{" + str(i + 1) + "}$ = " + str(result) string = "\\\\".join( [string[x * 98:x * 98 + 98] for x in range(len(string) // 98 + 1)]) nz += (string[:-2] if string.endswith("\\\\") else string) + "\\\\\n" result *= c nzstr = str(result // c) nz += "$%s \\bmod 589 = %d$\n" % ("\\\\".join( [nzstr[x * 98:x * 98 + 98] for x in range(len(nzstr) // 98 + 1)]), hukugo) cd = c**d # print(m**e, "m**e") # print(c**d, "c**d") # print(m, "平文") # print(c, "暗号") # print(hukugo, "復号") # hissand(132451, 589) return locals()
f = open( "C:/Users/Capp/Documents/Università/Magistrale/1-ADM/Homework4/wine.data", "r") df = pd.DataFrame(f, columns=['chara']) l = [] for row in df['chara']: l.append(row.strip('\n').split(',')) xy = [ ] #A list containing all points pf the scatter plot (Ash, Alcanity of ash) for el in l: p = [] p.append(float(el[4])) p.append(int(el[5])) xy.append(p) sf(xy) plt.figure(figsize=(20, 10)) for el in xy: plt.scatter(el[0], el[1], c='black') plt.title('WINES', fontsize=30) plt.xlabel('Ash', fontsize=20) plt.ylabel('Alcalinity of ash', fontsize=20) plt.show() def dist(x, y): distance = math.sqrt(sum([(a - b)**2 for a, b in zip(x, y)])) #Euclidean distance return distance
# import just one or two functions or constants rather than a whole module # easier for coding, but need to beware names don't conflict from math import pi print(pi) from random import randint, shuffle print(randint(1, 5)) x = ['a', 'b', 'c'] shuffle(x) print(x) # can rename an imported function if you want x = ['a', 'b', 'c'] from random import shuffle as sf sf(x) print(x) # can also import whole module using * from random import * print(randint(1, 5)) # FILE READ & WRITE # ------------------------------------------------ filename = 'city_data.txt' # this opens a file handle called fin, iterates the lines of the file, and prints each line with open(filename) as fin: for line in fin: print(line)
def eval(gakuseki): # gakuseki = 115114 # gakuseki = 114999 p = 19 q = 31 n = p*q # print(p, "p") # print(q, "q") # print(n, "n") m = gakuseki % (n - 2) + 2 ln = lcm(pfac(p - 1), pfac(q - 1)) pfac18 = " \\times ".join(map(str, pfac(p - 1))) pfac30 = " \\times ".join(map(str, pfac(q - 1))) pfaclast = " \\times ".join(map(str, ar)) # e = [x for x in range(ln + 1) if gcd(x, ln) == 1] gcdlist = [gcd(x, ln) for x in range(ln + 1)] e = [i for i, x in enumerate(gcdlist) if x == 1] es = ", ".join(map(str, e)) gcdlist = "".join(["GCD(%d, \\lambda(n))&= %d\\\\\n" % (i, x) for i, x in enumerate(gcdlist)]) gcdlist = gcdlist[:-3] # print(", ".join(map(str, e)), "eの候補") e = [x for x in e if 1 < x < 20] sf(e) e = e[0] # e = 7 # print(e, "e") d = 1 while (d*e - 1) % ln: d += 1 de = d*e - 1 # print(d, "d") # print(hs) # tmp = 333 # for i in range(13): # hs, tmp = hissan.kakezan(tmp, m) # print(hs) c = m**e % n me = m**e hsk, val = hissan.ruizyo(m, e) hsd, val = hissan.warizan(me, n) hukugo = c**d % n nz = "" result = c for i in range(d): string = "$" + str(c) + "^{"+ str(i + 1) + "}$ = " + str(result) string = "\\\\".join([string[x*98:x*98 + 98] for x in range(len(string) // 98 + 1)]) nz += (string[:-2] if string.endswith("\\\\") else string) + "\\\\\n" result *= c nzstr = str(result // c) nz += "$%s \\bmod 589 = %d$\n" % ("\\\\".join([nzstr[x*98:x*98 + 98] for x in range(len(nzstr) // 98 + 1)]), hukugo) cd = c**d # print(m**e, "m**e") # print(c**d, "c**d") # print(m, "平文") # print(c, "暗号") # print(hukugo, "復号") # hissand(132451, 589) return locals()
def shuffle(x, y): from random import shuffle as sf d = list(zip(x, y)) sf(d) x_, y_ = zip(*d) return np.array(x_).astype(float), np.array(y_).astype(float)
def main(): fmoment = int(time.time()) args = parse_args() norm = args.norm backbone = args.backbone pretrained = args.pretrained lossfunc = args.loss size = args.size pk = args.pk nk = args.nk n_epoch = args.n_epoch gpu = args.gpu test_every = args.test_every ckpt = args.ckpt print( 'norm=%s backbone=%s pretrained=%s lossfunc=%s size=%s pk=%d nk=%d epoch=%d gpu=%d test_every=%d ckpt=%s' % (norm, backbone, pretrained, lossfunc, size, pk, nk, n_epoch, gpu, test_every, ckpt)) if backbone == 'resnet18': model = resnet18.resnet18(norm=norm).cuda(device=gpu) if pretrained == 'pretrained': ckpt_dict = torch.load('resnet18-pretrained.pth') model_dict = model.state_dict() ckpt_dict = {k: v for k, v in ckpt_dict.items() if k in model_dict} model_dict.update(ckpt_dict) model.load_state_dict(model_dict) if lossfunc == 'CE': criterion = nn.CrossEntropyLoss().cuda(device=gpu) elif lossfunc == 'Focal': criterion = FocalLoss(class_num=2, gpu=gpu).cuda(device=gpu) for m in model.modules(): if isinstance(m, nn.Linear): nn.init.constant_(m.bias, -math.log(99)) elif lossfunc == 'BCE': criterion = BCE(class_num=2, gpu=gpu).cuda(device=gpu) optimizer = optim.Adam(model.parameters(), lr=1e-4, weight_decay=1e-4) cudnn.benchmark = True train_trans = transforms.Compose([ transforms.RandomHorizontalFlip(p=0.5), transforms.RandomVerticalFlip(p=0.5), transforms.ToTensor(), transforms.Normalize(mean=[0.2005, 0.1490, 0.1486], std=[0.1445, 0.1511, 0.0967]) ]) infer_trans = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.2005, 0.1490, 0.1486], std=[0.1445, 0.1511, 0.0967]) ]) train_dset = XDataset('train-%s.lib' % size, train_trans=train_trans, infer_trans=infer_trans) train_loader = torch.utils.data.DataLoader(train_dset, batch_size=64, shuffle=False, pin_memory=True) test_dset = XDataset('test-%s.lib' % size, train_trans=train_trans, infer_trans=infer_trans) test_loader = torch.utils.data.DataLoader(test_dset, batch_size=128, shuffle=False, pin_memory=True) if ckpt != 'none': checkpoint = torch.load(ckpt) start = checkpoint['epoch'] model.load_state_dict(checkpoint['state_dict']) best_f1 = checkpoint['best_f1'] optimizer.load_state_dict(checkpoint['optimizer']) if not os.path.exists( 'logs/Training_%s_%s_%s_%s_%s_%d_%d_%d.csv' % (norm, backbone, pretrained, lossfunc, size, pk, nk, fmoment)): fconv = open( 'logs/Training_%s_%s_%s_%s_%s_%d_%d_%d.csv' % (norm, backbone, pretrained, lossfunc, size, pk, nk, fmoment), 'w') fconv.write('time,epoch,loss,error\n') fconv.write('%d,0,0,0\n' % fmoment) fconv.close() if not os.path.exists( 'logs/Testing_%s_%s_%s_%s_%s_%d_%d_%d.csv' % (norm, backbone, pretrained, lossfunc, size, pk, nk, fmoment)): fconv = open( 'logs/Testing_%s_%s_%s_%s_%s_%d_%d_%d.csv' % (norm, backbone, pretrained, lossfunc, size, pk, nk, fmoment), 'w') fconv.write('time,epoch,loss,error,tp,tn,fp,fn,f1,S\n') fconv.write('%d,0,0,0\n' % fmoment) fconv.close() else: start = 0 best_f1 = 0 fconv = open( 'logs/Training_%s_%s_%s_%s_%s_%d_%d_%d.csv' % (norm, backbone, pretrained, lossfunc, size, pk, nk, fmoment), 'w') fconv.write('time,epoch,loss,error\n') fconv.write('%d,0,0,0\n' % fmoment) fconv.close() fconv = open( 'logs/Testing_%s_%s_%s_%s_%s_%d_%d_%d.csv' % (norm, backbone, pretrained, lossfunc, size, pk, nk, fmoment), 'w') fconv.write('time,epoch,loss,error,tp,tn,fp,fn,f1,S\n') fconv.write('%d,0,0,0\n' % fmoment) fconv.close() for epoch in range(start, n_epoch): train_dset.setmode(1) _, probs = inference(epoch, train_loader, model, criterion, gpu) # torch.save(probs,'probs/train-%d.pth'%(epoch+1)) probs1 = probs[:train_dset.plen] probs0 = probs[train_dset.plen:] topk1 = np.array( group_argtopk(np.array(train_dset.slideIDX[:train_dset.plen]), probs1, pk)) topk0 = np.array( group_argtopk(np.array(train_dset.slideIDX[train_dset.plen:]), probs0, nk)) + train_dset.plen topk = np.append(topk1, topk0).tolist() # torch.save(topk,'topk/train-%d.pth'%(epoch+1)) # maxs = group_max(np.array(train_dset.slideIDX), probs, len(train_dset.targets)) # torch.save(maxs, 'maxs/%d.pth'%(epoch+1)) sf(topk) train_dset.maketraindata(topk) train_dset.setmode(2) loss, err = train(train_loader, model, criterion, optimizer, gpu) moment = time.time() writecsv([moment, epoch + 1, loss, err], 'logs/Training_%s_%s_%s_%s_%s_%d_%d_%d.csv' % (norm, backbone, pretrained, lossfunc, size, pk, nk, fmoment)) print('Training epoch=%d, loss=%.5f, error=%.5f' % (epoch + 1, loss, err)) if (epoch + 1) % test_every == 0: test_dset.setmode(1) loss, probs = inference(epoch, test_loader, model, criterion, gpu) # torch.save(probs,'probs/test-%d.pth'%(epoch+1)) # topk = group_argtopk(np.array(test_dset.slideIDX), probs, pk) # torch.save(topk, 'topk/test-%d.pth'%(epoch+1)) maxs = group_max( np.array(test_dset.slideIDX), probs, len(test_dset.targets)) #è¿åæ¯ä¸ªåççæ大æ?ç # torch.save(maxs, 'maxs/test-%d.pth'%(epoch+1)) pred = [1 if x >= 0.5 else 0 for x in maxs] tp, tn, fp, fn = tfpn(pred, test_dset.targets) err = calc_err(pred, test_dset.targets) S, f1 = score(tp, tn, fp, fn) moment = time.time() writecsv( [moment, epoch + 1, loss, err, tp, tn, fp, fn, f1, S], 'logs/Testing_%s_%s_%s_%s_%s_%d_%d_%d.csv' % (norm, backbone, pretrained, lossfunc, size, pk, nk, fmoment)) print('Testing epoch=%d, loss=%.5f, error=%.5f' % (epoch + 1, loss, err)) #Save best model if f1 >= best_f1: best_f1 = f1 obj = { 'epoch': epoch + 1, 'state_dict': model.state_dict(), 'best_f1': best_f1, 'optimizer': optimizer.state_dict() } torch.save( obj, 'ckpt_%s_%s_%s_%s_%s_%d_%d_%d.pth' % (norm, backbone, pretrained, lossfunc, size, pk, nk, fmoment))
length.append(cnt) fnum.append([start, i - 1]) start = i + 1 cnt = 0 return fnum, length Motion_th = 10 K = cPickle.load(file('./Concatenate_Data/limb_K_ex4.pkl', 'r')) M = cPickle.load(file('./Concatenate_Data/limb_M_ex4.pkl', 'r')) vec_K = (K - np.roll(K, 1, axis=1))[:, 1:]**2 vec_M = (M - np.roll(M, 1, axis=1))[:, 1:]**2 Mo_K = np.sum((vec_K[::3, :] + vec_K[1::3, :] + vec_K[2::3, :])**0.5, 0) Mo_K = np.sum((vec_M[::3, :] + vec_M[1::3, :] + vec_M[2::3, :])**0.5, 0) fnum, length = conti_num((Mo_K > Motion_th) * 1, val=0, len_th=19) index = range(len(K[1])) del_idx = [] for i in fnum: tmp = range(i[0], i[1] + 1) sf(tmp) del_idx += tmp[:-10] index = np.setxor1d(index, del_idx)
def shuffle(*args): import random random.seed(0) for i in args: sf(i)
labelarr= [] Cols = [] index=[] timearr=[] for idx,i in enumerate(data): if (i.get('text') != 'neutral'): labelarr.append(i.get('text')) timearr.append(i.get('time')) colorarr.append(i.get('color')) index.append(idx) final = np.array(timearr) sf(final) def Convert(final): final = np.array(final) return list(-final) newarr=Convert(final) class Window(QWidget): def __init__(self): super().__init__()
def __init__(self, list_IDs, path_instances, batch_size=32, dim=(32, 32), n_channels=1, n_clases=10, shuffle=True, normalized=True, n_epochs=100): self.dim = dim self.batch_size = batch_size self.n_channels = n_channels self.n_classes = n_clases self.normalized = normalized self.shuffle = shuffle #Ruta donde se encuentran las instancias self.path_instances = path_instances #Lista con el nombre de los peatones que forman parte del conjunto de entrenamiento self.list_IDs = list_IDs #Se crea un diccionario utilizado para ver las clases que quedan por ser seleccionadas de cada peatón self.restant_classes_pedestrians = dict([ (ped, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) for ped in self.list_IDs ]) #Lista que contiene las intancias con las que se va a entrenar cada epoca self.list_IDs_epoch = list() #Obtención de una lista con las instancias que se van a utilizar para entrenar la primera epoca while len(self.list_IDs_epoch) != (len(self.list_IDs) * 12): aux = self.list_IDs.copy() sf(aux) for ped in aux: if self.restant_classes_pedestrians[ped] != []: ped_noext = splitext(ped)[0] # Del peatón seleccionado se selecciona de manera aleatoria una de sus clases pos_class = np.random.randint( len(self.restant_classes_pedestrians[ped])) class1 = self.restant_classes_pedestrians[ped][pos_class] self.restant_classes_pedestrians[ped].remove(class1) pos_class = np.random.randint( len(self.restant_classes_pedestrians[ped])) class2 = self.restant_classes_pedestrians[ped][pos_class] self.restant_classes_pedestrians[ped].remove(class2) self.list_IDs_epoch.append( (ped_noext + '_' + str(class1) + 'p.pkl')) self.list_IDs_epoch.append( (ped_noext + '_' + str(class2) + 'p.pkl'))
# import just one or two functions or constants rather than a whole module # easier for coding, but need to beware names don't conflict from math import pi print(pi) from random import randint, shuffle print(randint(1,5)) x = ['a', 'b', 'c'] shuffle(x) print(x) # can rename an imported function if you want x = ['a', 'b', 'c'] from random import shuffle as sf sf(x) print(x) # can also import whole module using * from random import * print(randint(1,5)) # FILE READ & WRITE # ------------------------------------------------ filename = 'city_data.txt' # this opens a file handle called fin, iterates the lines of the file, and prints each line with open(filename) as fin: for line in fin: print(line)
#optimizer = tf.train.GradientDescentOptimizer(0.001).minimize(cost) sess = tf.InteractiveSession() batch_size = 16 counter = 0 init_op = tf.global_variables_initializer() sess.run(init_op) f_test = h5py.File('NLdata_batch.h5', 'r')['test_data'][:].T f_telab = h5py.File('NLdata_batch.h5', 'r')['test_label'][:].T f_train = h5py.File('NLdata_batch.h5', 'r')['train_data'][:].T f_trlab = h5py.File('NLdata_batch.h5', 'r')['train_label'][:].T minmax = h5py.File('NLdata_batch.h5', 'r')['minmax'][:] idx = np.arange(f_train.shape[0]) sf(idx) for epoch in range(1000): if (counter + 1) * batch_size > f_train.shape[0]: counter = 0 idx = np.arange(f_train.shape[0]) sf(idx) batch_raw = f_trlab[idx[(counter) * batch_size:(counter + 1) * batch_size], :, :] batch_noise = f_train[idx[(counter) * batch_size:(counter + 1) * batch_size], :, :] # if epoch < 900: # if epoch%100 == 0:
yield guess def fbs(fbi): fbi = list(fbi) ln = len(fbi) t = fbi[:ln - 2] l = fbi[ln - 2] guess = fbi[ln - 1] for i in range(len(l)): if (guess[i] in l) and (t[i] != "A"): l[i] = "*" t[i] = "N" return t for i in range(12): guess = input("your guess here:") output = fbs(fbi(guess)) sf(output) output = toast(output) print(guess, " ", output) if output == "AAAA": ok = True break if ok: print("You've won!") else: print("You've lost!")
relidx = np.where( np.sum((R < Rel_th) * 1, 0) == 0)[0] # frames which have all joints reliable unrelidx = np.where(np.sum((R < Rel_th) * 1, 0) != 0)[0] Rel_M = M[:, relidx] Rel_K = K[:, relidx] Rel_R = R[:, relidx] unRel_M = M[:, unrelidx] unRel_K = K[:, unrelidx] unRel_R = R[:, unrelidx] Rel_sidx = range(len(relidx)) unRel_sidx = range(len(unrelidx)) sf(Rel_sidx) sf(unRel_sidx) rate = 0.8 dataset['Rel_train_M'] = Rel_M[:, Rel_sidx[:int(len(Rel_sidx) * 0.8)]] dataset['Rel_test_M'] = Rel_M[:, Rel_sidx[int(len(Rel_sidx) * 0.8):]] dataset['Rel_train_K'] = Rel_K[:, Rel_sidx[:int(len(Rel_sidx) * 0.8)]] dataset['Rel_test_K'] = Rel_K[:, Rel_sidx[int(len(Rel_sidx) * 0.8):]] dataset['Rel_train_R'] = Rel_R[:, Rel_sidx[:int(len(Rel_sidx) * 0.8)]] dataset['Rel_test_R'] = Rel_R[:, Rel_sidx[int(len(Rel_sidx) * 0.8):]] dataset['unRel_train_M'] = unRel_M[:, unRel_sidx[:int(len(unRel_sidx) * 0.8)]] dataset['unRel_test_M'] = unRel_M[:, unRel_sidx[int(len(unRel_sidx) * 0.8):]] dataset['unRel_train_K'] = unRel_K[:, unRel_sidx[:int(len(unRel_sidx) * 0.8)]] dataset['unRel_test_K'] = unRel_K[:, unRel_sidx[int(len(unRel_sidx) * 0.8):]]