for combo in combos: print(strength, j, combo) for key in combo: data_dict[key] = combo[key] data_dict["non_pervasive"] = j data_dict["strength"] = strength # Run the simulation n_iter times n_iter = 200 r2_tprf_lst = [] r2_pcr_lst = [] r2_pclas_lst = [] pcr_pclas = {"pcr": [], "pclas": []} for i in tqdm(range(0, n_iter)): X, y = dg.data_generator(data_dict) Z = 1 train_window = int(data_dict["T"] / 2) result = tprf_code.recursive_train(X, y, Z, train_window) r2_tprf_lst.append(result) for procedure in ("pcr", "pclas"): try: result = tprf_code.recursive_train_alternate( X, y, train_window, 5, procedure) pcr_pclas[procedure].append(result) except: print("\n\nError:", strength, j, combo, X.shape, y.shape, procedure, i, "\n\n") continue tprf_result = np.median(r2_tprf_lst) * 100 pcr_result = np.median(pcr_pclas['pcr']) * 100
# _*_ encoding: utf-8 _*_ ''' Created on 2019年1月17日 @author: ZHOUGANG880 ''' import numpy as np from data_gen import data_generator, x_test, y_test from model import Deeplabv3 import matplotlib.pyplot as plt batch_size = 10 gen = data_generator(x_test, y_test, batch_size=batch_size) x, y, _ = next(gen) print('load model...') basemodel = Deeplabv3(input_shape=(28, 28, 3), classes=11, backbone='mobilenetv2') basemodel.load_weights('checkpoints/DeepLabV3+-Weights-120.hdf5', by_name=True) print('load model done.') logits = basemodel.predict(x) logits = np.argmax(logits, axis=-1) for i in range(batch_size): img = x[i] logit = logits[i]
def SGD_step_by_step_minimization(problem, qubits, entanglement, layers, name, seed=30, epochs=3000, batch_size=20, eta=.1, err=False): """ This function creates data and minimizes whichever problem using a step by step SGD and saving all results from accuracies for training and test sets INPUT: -problem: name of the problem, to choose among ['circle', '3 circles', 'hypersphere', 'tricrown', 'non convex', 'crown', 'sphere', 'squares', 'wavy lines'] -qubits: number of qubits, must be an integer -entanglement: whether there is entanglement or not in the Ansätze, just 'y'/'n' -layers: number of layers, must be an integer. If layers == 1, entanglement is not taken in account -method: minimization method, to choose among ['SGD', another valid for function scipy.optimize.minimize] -name: a name we want for our our files to be save with -seed: seed of numpy.random, needed for replicating results -epochs: number of epochs for a 'SGD' method. If there is another method, this input has got no importance -batch_size: size of the batches for stochastic gradient descent, only for 'SGD' method -eta: learning rate, only for 'SGD' method OUTPUT: This function has got no outputs, but several files are saved in an appropiate folder. The files are -summary.txt: Saves useful information for the problem -theta.txt: saves the theta parameters as a flat array -alpha.txt: saves the alpha parameters as a flat array -error_rates: accuracies for training and test sets as flat arrays """ chi = 'fidelity_chi' method = 'SGD' np.random.seed(seed) data, drawing = data_generator(problem, err=err) if problem == 'sphere': train_data = data[:500] test_data = data[500:] elif problem == 'hypersphere': train_data = data[:1000] test_data = data[1000:] else: train_data = data[:200] test_data = data[200:] if chi == 'fidelity_chi': qubits_lab = qubits elif chi == 'weighted_fidelity_chi': qubits_lab = 1 theta, alpha, reprs = problem_generator(problem, qubits, layers, chi, qubits_lab=qubits_lab) accs_test = [] accs_train = [] chis = [] acc_test_sol = 0 acc_train_sol = 0 fid_sol = 0 best_epoch = 0 theta_sol = theta.copy() alpha_sol = alpha.copy() file_text = write_epochs_file(chi, problem, qubits, entanglement, layers, method, name) for e in range(epochs): theta, alpha, fid = fidelity_minimization(theta, alpha, train_data, reprs, entanglement, method, batch_size, eta, 1) acc_train = tester(theta, alpha, train_data, reprs, entanglement, chi) acc_test = tester(theta, alpha, test_data, reprs, entanglement, chi) accs_test.append(acc_test) accs_train.append(acc_train) chis.append(fid) write_epoch(file_text, e, theta, alpha, fid, acc_train, acc_test) if acc_test > acc_test_sol: acc_test_sol = acc_test acc_train_sol = acc_train fid_sol = fid theta_sol = theta alpha_sol = alpha best_epoch = e close_epochs_file(file_text, best_epoch) write_summary(chi, problem, qubits, entanglement, layers, method, name, theta_sol, alpha_sol, None, fid_sol, acc_train_sol, acc_test_sol, seed, epochs) write_epochs_error_rate(chi, problem, qubits, entanglement, layers, method, name, accs_train, accs_test)
import numpy as np import matplotlib.pyplot as plt import openpyxl import time #### for Boxcox limit #### from scipy.stats import anderson from scipy import stats # read excel file (already cleaned from SQL) df = pd.read_excel( 'D:\\books\\MPE_project\\Statistics\\NY_bin_statistics.xlsx') # import data file generator import data_gen as dg df_all = dg.data_generator(df) # lambda calculation limit_iter = 0 mpe_limit_list = [] lot_id_list = [] while limit_iter < 14: df_all = df_all[:-1] # create dict for lambdas lambda_dict = {} for column in df_all.columns[2:]: lambda_dict[column] = [] # lambdas for parameters for i in df_all.columns[2:]:
def minimizer(chi, problem, qubits, entanglement, layers, method, name, seed=30, epochs=3000, batch_size=20, eta=0.1): """ This function creates data and minimizes whichever problem (from the selected ones) INPUT: -chi: cost function, to choose between 'fidelity_chi' or 'weighted_fidelity_chi' -problem: name of the problem, to choose among ['circle', '3 circles', 'hypersphere', 'tricrown', 'non convex', 'crown', 'sphere', 'squares', 'wavy lines'] -qubits: number of qubits, must be an integer -entanglement: whether there is entanglement or not in the Ansätze, just 'y'/'n' -layers: number of layers, must be an integer. If layers == 1, entanglement is not taken in account -method: minimization method, to choose among ['SGD', another valid for function scipy.optimize.minimize] -name: a name we want for our our files to be save with -seed: seed of numpy.random, needed for replicating results -epochs: number of epochs for a 'SGD' method. If there is another method, this input has got no importance -batch_size: size of the batches for stochastic gradient descent, only for 'SGD' method -eta: learning rate, only for 'SGD' method OUTPUT: This function has got no outputs, but several files are saved in an appropiate folder. The files are -summary.txt: Saves useful information for the problem -theta.txt: saves the theta parameters as a flat array -alpha.txt: saves the alpha parameters as a flat array -weight.txt: saves the weights as a flat array if they exist """ np.random.seed(seed) data, drawing = data_generator(problem) if problem == 'sphere': train_data = data[:500] test_data = data[500:] elif problem == 'hypersphere': train_data = data[:1000] test_data = data[1000:] else: train_data = data[:200] test_data = data[200:] if chi == 'fidelity_chi': qubits_lab = qubits theta, alpha, reprs = problem_generator(problem, qubits, layers, chi, qubits_lab=qubits_lab) theta, alpha, f = fidelity_minimization(theta, alpha, train_data, reprs, entanglement, method, batch_size, eta, epochs) acc_train = tester(theta, alpha, train_data, reprs, entanglement, chi) acc_test = tester(theta, alpha, test_data, reprs, entanglement, chi) write_summary(chi, problem, qubits, entanglement, layers, method, name, theta, alpha, 0, f, acc_train, acc_test, seed, epochs=epochs) elif chi == 'weighted_fidelity_chi': qubits_lab = 1 theta, alpha, weight, reprs = problem_generator(problem, qubits, layers, chi, qubits_lab=qubits_lab) theta, alpha, weight, f = weighted_fidelity_minimization( theta, alpha, weight, train_data, reprs, entanglement, method) acc_train = tester(theta, alpha, train_data, reprs, entanglement, chi, weights=weight) acc_test = tester(theta, alpha, test_data, reprs, entanglement, chi, weights=weight) write_summary(chi, problem, qubits, entanglement, layers, method, name, theta, alpha, weight, f, acc_train, acc_test, seed, epochs=epochs)
def paint_world(chi, problem, qubits, entanglement, layers, method, name, seed=30, standard_test=True, samples=4000, bw=False, err=False): np.random.seed(seed) if chi == 'fidelity_chi': qubits_lab = qubits elif chi == 'weighted_fidelity_chi': qubits_lab = 1 if standard_test == True: data, drawing = data_generator(problem) if problem == 'sphere': test_data = data[500:] elif problem == 'hypersphere': test_data = data[1000:] else: test_data = data[200:] elif standard_test == False: test_data, drawing = data_generator(problem, samples=samples) if problem in [ 'circle', 'wavy circle', 'sphere', 'non convex', 'crown', 'hypersphere' ]: classes = 2 if problem in ['tricrown']: classes = 3 elif problem in ['3 circles', 'wavy lines', 'squares']: classes = 4 reprs = representatives(classes, qubits_lab) params = read_summary(chi, problem, qubits, entanglement, layers, method, name) if chi == 'fidelity_chi': theta, alpha = params sol_test, acc_test = Accuracy_test(theta, alpha, test_data, reprs, entanglement, chi) if chi == 'weighted_fidelity_chi': theta, alpha, weight = params sol_test, acc_test = Accuracy_test(theta, alpha, test_data, reprs, entanglement, chi, weights=weight) foldname = name_folder(chi, problem, qubits, entanglement, layers, method) angles = np.zeros((len(sol_test), 2)) for i, x in enumerate(sol_test[:, :2]): theta_aux = code_coords(theta, alpha, x) C = circuit(theta_aux, entanglement) angles[i, 0] = np.arccos(np.abs(C.psi[0])**2 - np.abs(C.psi[1])**2) - np.pi / 2 angles[i, 1] = np.angle(C.psi[1] / C.psi[0]) print(angles[i]) if bw == False: colors_classes = get_cmap('plasma') norm_class = Normalize(vmin=-.5, vmax=np.max(sol_test[:, -3]) + .5) colors_rightwrong = get_cmap('RdYlGn') norm_rightwrong = Normalize(vmin=-.1, vmax=1.1) if bw == True: colors_classes = get_cmap('Greys') norm_class = Normalize(vmin=-.1, vmax=np.max(sol[:, -3]) + .1) colors_rightwrong = get_cmap('Greys') norm_rightwrong = Normalize(vmin=-.1, vmax=1.1) fig, ax = plt.subplots(nrows=2) ax[0].plot(laea_x(np.pi, np.linspace(0, np.pi)), laea_y(np.pi, np.linspace(0, np.pi)), color='k') ax[0].plot(laea_x(-np.pi, np.linspace(0, -np.pi)), laea_y(-np.pi, np.linspace(0, -np.pi)), color='k') ax[1].plot(laea_x(np.pi, np.linspace(0, np.pi)), laea_y(np.pi, np.linspace(0, np.pi)), color='k') ax[1].plot(laea_x(-np.pi, np.linspace(0, -np.pi)), laea_y(-np.pi, np.linspace(0, -np.pi)), color='k') ax[0].scatter(laea_x(angles[:, 1], angles[:, 0]), laea_y(angles[:, 1], angles[:, 0]), c=sol_test[:, -2], cmap=colors_classes, s=2, norm=norm_class) ax[1].scatter(laea_x(angles[:, 1], angles[:, 0]), laea_y(angles[:, 1], angles[:, 0]), c=sol_test[:, -1], cmap=colors_rightwrong, s=2, norm=norm_rightwrong) plt.show()
def painter(chi, problem, qubits, entanglement, layers, method, name, seed=30, standard_test=True, samples=4000, bw=False, err=False): """ This function takes written text files and paint the results of the problem INPUT: -chi: cost function, to choose between 'fidelity_chi' or 'weighted_fidelity_chi' -problem: name of the problem, to choose among ['circle', '3 circles', 'hypersphere', 'tricrown', 'non convex', 'crown', 'sphere', 'squares', 'wavy lines'] -qubits: number of qubits, must be an integer -entanglement: whether there is entanglement or not in the Ansätze, just 'y'/'n' -layers: number of layers, must be an integer. If layers == 1, entanglement is not taken in account -method: minimization method, to choose among ['SGD', another valid for function scipy.optimize.minimize] -name: a name we want for our our files to be save with -seed: seed of numpy.random, needed for replicating results -standard_test: Whether we want to paint the set test used for checking when minimizing. If True, seed and samples are not taken in account -samples: number of samples of the test set -bw: painting in black and white OUTPUT: This function has got no outputs, but a file containing the representation of the test set is created """ np.random.seed(seed) if chi == 'fidelity_chi': qubits_lab = qubits elif chi == 'weighted_fidelity_chi': qubits_lab = 1 if standard_test == True: data, drawing = data_generator(problem) if problem == 'sphere': test_data = data[500:] elif problem == 'hypersphere': test_data = data[1000:] else: test_data = data[200:] elif standard_test == False: test_data, drawing = data_generator(problem, samples=samples) if problem in [ 'circle', 'wavy circle', 'sphere', 'non convex', 'crown', 'hypersphere' ]: classes = 2 if problem in ['tricrown']: classes = 3 elif problem in ['3 circles', 'wavy lines', 'squares']: classes = 4 reprs = representatives(classes, qubits_lab) params = read_summary(chi, problem, qubits, entanglement, layers, method, name) if chi == 'fidelity_chi': theta, alpha = params sol_test, acc_test = Accuracy_test(theta, alpha, test_data, reprs, entanglement, chi) if chi == 'weighted_fidelity_chi': theta, alpha, weight = params sol_test, acc_test = Accuracy_test(theta, alpha, test_data, reprs, entanglement, chi, weights=weight) foldname = name_folder(chi, problem, qubits, entanglement, layers, method) samples_paint(problem, drawing, sol_test, foldname, name, bw)
#Universitat de Barcelona / Barcelona Supercomputing Center/Institut de Ciències del Cosmos ########################################################################### #This file provides a classical benchmark for the same problem our quantum classifier is tackling #We use a standard classifier, a SVC by scikit learn from sklearn.neural_network import MLPClassifier from sklearn.svm import SVC from data_gen import data_generator import numpy as np problem = 'squares' print(problem) data, drawing = data_generator(problem) #name for the problem number = 200 if problem == 'sphere': number = 500 elif problem == 'hypersphere': number = 1000 train_data = data[:number] test_data = data[number:] X_train = [] Y_train = [] for d in train_data: x, y = d X_train.append(x) Y_train.append(y)
if use_gpu: parallermodel = multi_gpu_model(basemodel, gpus=gpus) checkpoint = ParallerModelCheckPoint(basemodel) else: parallermodel = basemodel checkpoint = ModelCheckpoint( r'checkpoints/DeepLabV3+-Weights-{epoch:02d}.hdf5', save_weights_only=True, verbose=1) optimizer = SGD(lr=learning_rate, momentum=0.9, clipnorm=5.0) parallermodel.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) train_gen = data_generator(x_train, y_train, batch_size=batch_size) test_gen = data_generator(x_test, y_test, batch_size=batch_size) tensorboard = TensorBoard('tflog', write_graph=True) earlystop = EarlyStopping(monitor='acc', patience=10, verbose=1) print('begin training...') parallermodel.fit_generator(train_gen, steps_per_epoch=len(x_train) // batch_size, epochs=120, verbose=1, callbacks=[earlystop, checkpoint, tensorboard], validation_data=test_gen, validation_steps=100, initial_epoch=0)