def print_corr(xk): jns, jds = [], [] corr_sum = [] for tsp in args['timbre_spaces']: dissimil_mat_tsp = load.timbrespace_dismatrix(tsp, load.database()) tab_red = np.loadtxt(log_foldername+'/../input_data/{}_{}_input_data.txt'.format(tsp, rs)) ndims, ninstrus = tab_red.shape[0], tab_red.shape[1] no_samples = ninstrus * (ninstrus - 1) / 2 idx_triu = np.triu_indices(dissimil_mat_tsp.shape[0], k=1) target_v = dissimil_mat_tsp[idx_triu] mean_target = np.mean(target_v) std_target = np.std(target_v) kernel = np.zeros((ninstrus, ninstrus)) for i in range(ninstrus): for j in range(i + 1, ninstrus): kernel[i, j] = -np.sum( np.power( np.divide(tab_red[:, i] - tab_red[:, j], (xk + np.finfo(float).eps)), 2)) kernel_v = kernel[idx_triu] mean_kernel = np.mean(kernel_v) std_kernel = np.std(kernel_v) Jn = np.sum(np.multiply(kernel_v - mean_kernel, target_v - mean_target)) Jd = no_samples * std_target * std_kernel corr_sum.append(Jn/Jd) if not os.path.isfile(os.path.join(log_foldername, 'tmp.pkl')): loop_cpt = 1 pickle.dump({'loop': loop_cpt, 'correlation': [np.mean(corr_sum)]}, open(os.path.join(log_foldername, 'tmp.pkl'), 'wb')) correlations = [np.mean(corr_sum)] pickle.dump({ 'sigmas': xk, 'kernel': kernel, 'Jn': Jn, 'Jd': Jd, 'corr_sum': corr_sum, 'correlations': correlations, }, open(os.path.join(log_foldername, 'optim_process_l={}.pkl'.format(loop_cpt)), 'wb')) else: last_loop = pickle.load(open(os.path.join(log_foldername,'tmp.pkl'), 'rb')) loop_cpt = last_loop['loop'] + 1 correlations = last_loop['correlation'] correlations.append(np.mean(corr_sum)) monitoring_step = 50 if (loop_cpt % monitoring_step == 0): corr_sum_str = ' '.join(['{:.2f}'.format(c) for c in corr_sum]) print(' |_ loop={} J={:.6f} ({})'.format(loop_cpt, np.mean(corr_sum), corr_sum_str)) pickle.dump({ 'sigmas': xk, 'kernel': kernel, 'Jn': Jn, 'Jd': Jd, 'corr_sum': corr_sum, 'correlations': correlations, }, open(os.path.join(log_foldername, 'optim_process_l={}.pkl'.format(loop_cpt)), 'wb')) pickle.dump({'loop': loop_cpt, 'correlation': correlations, 'sigmas': xk}, open(os.path.join(log_foldername, 'tmp.pkl'), 'wb'))
def corr(x): corr_sum = 0 for tsp in args['timbre_spaces']: dissimil_mat_tsp = load.timbrespace_dismatrix(tsp, load.database()) tab_red = np.loadtxt(log_foldername+'/../input_data/{}_{}_input_data.txt'.format(tsp, rs)) # tab_red = np.loadtxt('input_data/{}_{}_input_data.txt'.format(tsp, rs)) ndims, ninstrus = tab_red.shape[0], tab_red.shape[1] no_samples = ninstrus * (ninstrus - 1) / 2 idx_triu = np.triu_indices(dissimil_mat_tsp.shape[0], k=1) target_v = dissimil_mat_tsp[idx_triu] mean_target = np.mean(target_v) std_target = np.std(target_v) kernel = np.zeros((ninstrus, ninstrus)) for i in range(ninstrus): for j in range(i + 1, ninstrus): kernel[i, j] = -np.sum( np.power( np.divide(tab_red[:, i] - tab_red[:, j], (x + np.finfo(float).eps)), 2)) kernel_v = kernel[idx_triu] mean_kernel = np.mean(kernel_v) std_kernel = np.std(kernel_v) Jn = np.sum(np.multiply(kernel_v - mean_kernel, target_v - mean_target)) Jd = no_samples * std_target * std_kernel corr_sum += Jn/Jd return corr_sum
def nonopt_correlations(): corr_results = {} for i, tsp in enumerate(timbrespace_db.keys()): print('Processing', tsp) corr_results[tsp] = {} target_data = load.timbrespace_dismatrix(tsp, timbrespace_db) for rs in sorted(representations): aud_repres = load.timbrespace_features( tsp, representations=[rs], window=None, timbrespace_db=None, verbose=False)[rs] tab_red = [] rs_type = rs.split('_')[-1] if rs_type == 'strf': n_components = 1 for i in range(len(aud_repres)): # print('PCA on sound %02i' % (i + 1)) strf_reduced = pca.pca( np.absolute(aud_repres[i]), aud_repres[i].shape[1], n_components=n_components).flatten() tab_red.append(strf_reduced / np.max(strf_reduced)) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrogram' or rs_type == 'mps': for i in range(len(aud_repres)): tab_red.append(aud_repres[i].flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrum': for i in range(len(aud_repres)): tab_red.append(aud_repres[i]) # 128 x nb sounds (time or freq?) tab_red = np.transpose(np.asarray(tab_red)) input_data = tab_red / np.mean(np.std(tab_red, axis=0)) # plt.plot(input_data) # plt.show() ndims, ninstrus = input_data.shape[0], input_data.shape[1] no_samples = ninstrus * (ninstrus - 1) / 2 idx_triu = np.triu_indices(target_data.shape[0], k=1) target_v = target_data[idx_triu] mean_target = np.mean(target_v) std_target = np.std(target_v) kernel = np.zeros((ninstrus, ninstrus)) for i in range(ninstrus): for j in range(i + 1, ninstrus): kernel[i, j] = np.sum( np.power(input_data[:, i] - input_data[:, j], 2)) kernel_v = kernel[idx_triu] mean_kernel = np.mean(kernel_v) std_kernel = np.std(kernel_v) Jn = np.sum( np.multiply(kernel_v - mean_kernel, target_v - mean_target)) Jd = (no_samples - 1) * std_target * std_kernel corr_results[tsp][rs] = Jn / Jd print(' {} : {}'.format(rs, Jn / Jd)) pickle.dump(corr_results, open('correlations_results.pkl', 'wb'))
def run_once(tsp, rs, optim_args): rslog_foldername = optim_args['log_foldername'] rs = rslog_foldername.split('/')[-1].split('-')[0] dissimil_mat = load.timbrespace_dismatrix(tsp, timbrespace_db) aud_repres = load.timbrespace_features( tsp, representations=[rs], window=None, timbrespace_db=None, verbose=True)[rs] tab_red = [] rs_type = rs.split('_')[-1] mapping = [] variances = [] if rs_type == 'strf': n_components = 1 for i in range(len(aud_repres)): strf_reduced, mapping, variances = pca.pca( np.absolute(aud_repres[i]), aud_repres[i].shape[1], n_components=n_components) strf_reduced = strf_reduced.flatten() tab_red.append(strf_reduced) # tab_red.append(strf_reduced / np.max(strf_reduced)) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrogram' or rs_type == 'mps': for i in range(len(aud_repres)): tab_red.append(aud_repres[i].flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrum': for i in range(len(aud_repres)): tab_red.append(aud_repres[i]) # 128 x nb sounds (time or freq?) tab_red = np.transpose(np.asarray(tab_red)) pickle.dump({ 'data_repres': aud_repres, 'data_proj': tab_red, 'mapping': mapping, 'variances': variances, 'dissimilarities': dissimil_mat, }, open(os.path.join(rslog_foldername, 'dataset.pkl'), 'wb')) print(' data dimension:', tab_red.shape) print('* normalizing') tab_red = tab_red / np.max(np.max(np.abs(tab_red), axis=0)) # plt.plot(tab_red) # plt.show() # optimization correlations = training.kernel_optim(tab_red, dissimil_mat, **optim_args)
def run_one_tsp_rs(tsp, rs, args): rslog_foldername = args['optim_args']['log_foldername'] rs = rslog_foldername.split('/')[-1].split('-')[0] dissimil_mat = load.timbrespace_dismatrix(tsp, load.database()) aud_repres = load.timbrespace_features(tsp, representations=[rs], audio_args=args['audio_args'])[rs] tab_red = [] rs_type = rs.split('_')[-1] mapping = [] variances = [] if rs_type == 'strf': n_components = 1 for i in range(len(aud_repres)): strf_reduced, mapping_, variances = pca.pca_patil( np.absolute(aud_repres[i]), aud_repres[i].shape[1], n_components=n_components) strf_reduced = strf_reduced.flatten() tab_red.append(strf_reduced) mapping.append(mapping_) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrogram' or rs_type == 'mps': for i in range(len(aud_repres)): tab_red.append(aud_repres[i].flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrum': for i in range(len(aud_repres)): tab_red.append(aud_repres[i]) tab_red = np.transpose(np.asarray(tab_red)) pickle.dump( { 'data_repres': aud_repres, 'data_proj': tab_red, 'mapping': mapping, 'variances': variances, 'dissimilarities': dissimil_mat, 'audio_args': args['audio_args'] }, open(os.path.join(rslog_foldername, 'dataset.pkl'), 'wb')) print(' data dimension:', tab_red.shape) print('* normalizing') tab_red = tab_red / np.mean(np.max(np.abs(tab_red), axis=0)) correlations, _ = training.kernel_optim_lbfgs_log(tab_red, dissimil_mat, **args['optim_args'])
def resume_all(resumefn='./outs/'): for i, tsp in enumerate(sorted(timbrespace_db.keys())): print('Processing', tsp) dissimil_mat = load.timbrespace_dismatrix(tsp, timbrespace_db) for rs in representations: # for el in dir_names: # if el[1].split('-')[0] == tsp.lower() and el[1].split( # '-')[1] == rs: rslog_foldername = './outs/' + tsp.lower() + '/' + rs if os.path.isdir(rslog_foldername): # retrieve_foldername = os.path.join(el[0], el[1]) training.resume_kernel_optim( rslog_foldername, rslog_foldername, num_loops=100000, logging=True) else: subprocess.call(['mkdir', '-p', rslog_foldername]) run_once(tsp, rs, rslog_foldername)
def grad_corr(x): corr_sum = 0 for tsp_i, tsp in enumerate(args['timbre_spaces']): dissimil_mat_tsp = load.timbrespace_dismatrix(tsp, load.database()) tab_red = np.loadtxt(log_foldername+'/../input_data/{}_{}_input_data.txt'.format(tsp, rs)) #np.loadtxt('input_data/{}_{}_input_data.txt'.format(tsp, rs)) # tab_red = np.loadtxt('input_data/{}_{}_input_data.txt'.format(tsp, rs)) ndims, ninstrus = tab_red.shape[0], tab_red.shape[1] no_samples = ninstrus * (ninstrus - 1) / 2 idx_triu = np.triu_indices(dissimil_mat_tsp.shape[0], k=1) target_v = dissimil_mat_tsp[idx_triu] mean_target = np.mean(target_v) std_target = np.std(target_v) if tsp_i==0: gradients = np.zeros((ndims, 1)) kernel = np.zeros((ninstrus, ninstrus)) dkernel = np.zeros((ninstrus, ninstrus, ndims)) for i in range(ninstrus): for j in range(i + 1, ninstrus): kernel[i, j] = -np.sum( np.power( np.divide(tab_red[:, i] - tab_red[:, j], (x + np.finfo(float).eps)), 2)) dkernel[i, j, :] = 2 * np.power((tab_red[:, i] - tab_red[:, j]), 2) / (np.power(x, 3) + np.finfo(float).eps) kernel_v = kernel[idx_triu] mean_kernel = np.mean(kernel_v) std_kernel = np.std(kernel_v) Jn = np.sum(np.multiply(kernel_v - mean_kernel, target_v - mean_target)) Jd = no_samples * std_target * std_kernel for k in range(ndims): dkernel_k_v = dkernel[:, :, k][idx_triu] dJn = np.sum(dkernel_k_v * (target_v - mean_target)) dJd = no_samples / no_samples * \ std_target / (std_kernel + np.finfo(float).eps) * \ np.sum(dkernel_k_v * (kernel_v - mean_kernel)) gradients[k] += (Jd * dJn - Jn * dJd) / (np.power(Jd,2) + np.finfo(float).eps) return gradients
def resume_some(tsps=None, reps=None): tspaces = tsp if tsps != None else timbrespace_db.keys() some_reps = reps if reps != None else representations for i, tsp in enumerate(sorted(tspaces)): print('Processing', tsp) dissimil_mat = load.timbrespace_dismatrix(tsp, timbrespace_db) for rs in some_reps: rslog_foldername = './outs_all/' + tsp.lower() + '/' + rs if os.path.isdir(rslog_foldername): resume = {} for root, dirs, files in os.walk(rslog_foldername): loop_id = [] for name in files: if name.split('.')[-1] == 'pkl' and 'optim_process' in name.split('.')[0]: loop_id.append(int(name.split('.')[0].split('=')[-1])) optim_process = pickle.load(open(os.path.join(retrieve_foldername,'optim_process_l={}.pkl'.format(retrieved_loop)), 'rb')) optim_config = pickle.load(open(os.path.join(retrieve_foldername,'optim_config.pkl'), 'rb')) dataset = pickle.load(open(os.path.join(retrieve_foldername,'dataset.pkl'), 'rb')) input_data = dataset['data_proj'] target_data = dataset['dissimilarities'] resume['retrieved_loop'] = np.max(loop_id) resume['init_seed'] = optim_config['seed'] if verbose: print("* resuming with '{}' of size {}".format(retrieve_foldername.split('/')[-1], input_data.shape)) init_seed = optim_config['seed'] cost = optim_config['args']['cost'] init_sig_mean = optim_config['args']['init_sig_mean'] init_sig_var = optim_config['args']['init_sig_var'] training.resume_kernel_optim( rslog_foldername, rslog_foldername, num_loops=100000, logging=True)
def run_one_tsp_rs(tsp, rs, args): rslog_foldername = args['optim_args']['log_foldername'] rs = rslog_foldername.split('/')[-1].split('-')[0] dissimil_mat = load.timbrespace_dismatrix(tsp, load.database()) aud_repres = load.timbrespace_features(tsp, representations=[rs], audio_args=args['audio_args'])[rs] tab_red = [] rs_type = rs.split('_')[-1] mapping = [] variances = [] reduce_strf = args['reduce_strf'] if rs_type == 'strf': if reduce_strf == 'pca': n_components = 1 for i in range(len(aud_repres)): strf_reduced, mapping_, variances = pca.pca_patil( np.absolute(aud_repres[i]), aud_repres[i].shape[1], n_components=n_components) strf_reduced = strf_reduced.flatten() tab_red.append(strf_reduced) mapping.append(mapping_) tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time': for i in range(len(aud_repres)): strf_avgTime = np.mean(np.abs(aud_repres[i]), axis=0) freqVec = [0, 14, 28, 42, 56, 71, 85, 99, 113, 127] rateVec = [0, 3, 5, 7, 10, 11, 14, 16, 18, 21] scaleVec = [0, 2, 3, 4, 5, 6, 7, 8, 9, 10] indexedTab = strf_avgTime[freqVec, :, :][:, scaleVec, :][:, :, rateVec] tab_red.append(indexedTab.flatten()) # decimation here # [0:128:12,0:11,0:22:2] # tab_red.append(strf_avgTime[0:128:12,0:11,0:22:2].flatten()) # decimation here tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time_avg_scale_rate_freq': for i in range(len(aud_repres)): tab_red.append(strf2avgvec(aud_repres[i]).flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time_avg_freq': for i in range(len(aud_repres)): strf_avgTime = np.mean(np.abs(aud_repres[i]), axis=0) freqVec = [0, 14, 28, 42, 56, 71, 85, 99, 113, 127] rateVec = [0, 3, 5, 7, 10, 11, 14, 16, 18, 21] scaleVec = [0, 2, 3, 4, 5, 6, 7, 8, 9, 10] indexedTab = strf_avgTime[freqVec, :, :][:, scaleVec, :][:, :, rateVec] strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg( strf2avgvec(indexedTab), nbChannels=10, nbRates=10, nbScales=10) tab_red.append(strf_scale_rate.flatten()) # strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg(strf2avgvec(aud_repres[i])) # plotStrfavg(strf_scale_rate, strf_freq_rate, strf_freq_scale) # tab_red.append(np.absolute(np.mean(aud_repres[i], axis=0)).flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time_avg_scale': for i in range(len(aud_repres)): strf_avgTime = np.mean(np.abs(aud_repres[i]), axis=0) freqVec = [0, 14, 28, 42, 56, 71, 85, 99, 113, 127] rateVec = [0, 3, 5, 7, 10, 11, 14, 16, 18, 21] scaleVec = [0, 2, 3, 4, 5, 6, 7, 8, 9, 10] indexedTab = strf_avgTime[freqVec, :, :][:, scaleVec, :][:, :, rateVec] strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg( strf2avgvec(indexedTab), nbChannels=10, nbRates=10, nbScales=10) tab_red.append(strf_freq_rate.flatten()) # strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg(strf2avgvec(aud_repres[i])) # plotStrfavg(strf_scale_rate, strf_freq_rate, strf_freq_scale) # tab_red.append(np.absolute(np.mean(aud_repres[i], axis=0)).flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time_avg_rate': for i in range(len(aud_repres)): strf_avgTime = np.mean(np.abs(aud_repres[i]), axis=0) freqVec = np.array([0, 14, 28, 42, 56, 71, 85, 99, 113, 127]) rateVec = np.array([0, 3, 5, 7, 10, 11, 14, 16, 18, 21]) scaleVec = np.array([0, 2, 3, 4, 5, 6, 7, 8, 9, 10]) strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg( strf2avgvec(strf_avgTime[freqVec, scaleVec, rateVec]), nbChannels=10, nbRates=10, nbScales=10) tab_red.append(strf_freq_scale.flatten()) # strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg(strf2avgvec(aud_repres[i])) # plotStrfavg(strf_scale_rate, strf_freq_rate, strf_freq_scale) # tab_red.append(np.absolute(np.mean(aud_repres[i], axis=0)).flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time_avg_SRSFFR': for i in range(len(aud_repres)): strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg( strf2avgvec(aud_repres[i])) concat_tab = np.concatenate( (strf_scale_rate, strf_freq_rate, strf_freq_scale), axis=None) tab_red.append(concat_tab.flatten()) # strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg(strf2avgvec(aud_repres[i])) # plotStrfavg(strf_scale_rate, strf_freq_rate, strf_freq_scale) # tab_red.append(np.absolute(np.mean(aud_repres[i], axis=0)).flatten()) tab_red = np.transpose(np.asarray(tab_red)) else: for i in range(len(aud_repres)): tab_red.append(aud_repres[i].flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrogram' or rs_type == 'mps': for i in range(len(aud_repres)): tab_red.append(aud_repres[i].flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrum': for i in range(len(aud_repres)): tab_red.append(aud_repres[i]) tab_red = np.transpose(np.asarray(tab_red)) # print(tab_red) # print(aud_repres) # print(mapping) # print(variances) # print(dissimil_mat) print(args['audio_args']) # pickle.dump({ # 'data_repres': aud_repres, # 'data_proj': tab_red, # 'mapping': mapping, # 'variances': variances, # 'dissimilarities': dissimil_mat, # 'audio_args': args['audio_args'] # }, open(os.path.join(rslog_foldername, 'dataset.pkl'), 'wb')) print(' data dimension:', tab_red.shape) print('* normalizing') print(np.mean(np.max(np.abs(tab_red), axis=0))) tab_red = tab_red / np.mean(np.max(np.abs(tab_red), axis=0)) correlations, sigmas__ = training.kernel_optim_lbfgs_log( tab_red, dissimil_mat, **args['optim_args']) # strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg(sigmas__) # plotStrfavg(strf_scale_rate, strf_freq_rate, strf_freq_scale,show='false',figname=tsp+'_'+str(correlations[-1])) pickle.dump( { 'sigmas': sigmas__, 'representations': tab_red, 'dissimilarities': dissimil_mat, 'audio_args': args['audio_args'] }, open( os.path.join(args['log_foldername'] + 'resultsOptims_' + rs_type + '_' + reduce_strf + '_' + tsp + '.pkl'), 'wb'))
def run_one_tsp_rs_crossval(tsp, rs, args): rslog_foldername = args['optim_args']['log_foldername'] rs = rslog_foldername.split('/')[-1].split('-')[0] dissimil_mat = load.timbrespace_dismatrix(tsp, load.database()) aud_repres = load.timbrespace_features(tsp, representations=[rs], audio_args=args['audio_args'])[rs] tab_red = [] rs_type = rs.split('_')[-1] mapping = [] variances = [] reduce_strf = args['reduce_strf'] if rs_type == 'strf': if reduce_strf == 'pca': n_components = 1 for i in range(len(aud_repres)): strf_reduced, mapping_, variances = pca.pca_patil( np.absolute(aud_repres[i]), aud_repres[i].shape[1], n_components=n_components) strf_reduced = strf_reduced.flatten() tab_red.append(strf_reduced) mapping.append(mapping_) tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time': for i in range(len(aud_repres)): tab_red.append( np.absolute(np.mean(aud_repres[i], axis=0)).flatten()) tab_red = np.transpose(np.asarray(tab_red)) else: for i in range(len(aud_repres)): tab_red.append(aud_repres[i].flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrogram' or rs_type == 'mps': for i in range(len(aud_repres)): tab_red.append(aud_repres[i].flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrum': for i in range(len(aud_repres)): tab_red.append(aud_repres[i]) tab_red = np.transpose(np.asarray(tab_red)) pickle.dump( { 'data_repres': aud_repres, 'data_proj': tab_red, 'mapping': mapping, 'variances': variances, 'dissimilarities': dissimil_mat, 'audio_args': args['audio_args'] }, open(os.path.join(rslog_foldername, 'dataset.pkl'), 'wb')) print(' data dimension:', tab_red.shape) print('* normalizing') tab_red = tab_red / np.mean(np.max(np.abs(tab_red), axis=0)) ninstrus = tab_red.shape[1] correlations_training = [] sigmas_training = [] correlations_testing = [] correlations_testing_pearson = [] correlations_testing_spearman = [] sigmas_testing = [] distances_testing = [] target_testing = [] print('* cross-validation tests') org_fname = args['optim_args']['log_foldername'] for fold in range(ninstrus): # training data train_idx = [i for i in range(tab_red.shape[1]) if i != fold] sorted_train_idx = sorted(train_idx) input_data_training = tab_red[:, train_idx] target_data_training = dissimil_mat[train_idx, :] target_data_training = target_data_training[:, train_idx] # testing data test_idx = [fold] input_data_testing = tab_red[:, test_idx[0]] target_data_testing = np.zeros((ninstrus - 1, 1)) cpt_i = 0 for i in range(ninstrus): if i > test_idx[0]: target_data_testing[cpt_i] = dissimil_mat[test_idx[0], i] cpt_i += 1 elif i < test_idx[0]: target_data_testing[cpt_i] = dissimil_mat[i, test_idx[0]] cpt_i += 1 target_testing.append(target_data_testing.reshape(1, -1)) mean_target_test = np.mean(target_data_testing) std_target_test = np.std(target_data_testing) # optimisation on fold i print('* Fold {} - {} {}'.format(fold + 1, train_idx, test_idx)) args['optim_args']['allow_resume'] = False args['optim_args']['log_foldername'] = org_fname + '/fold{}'.format( fold + 1) subprocess.call(['mkdir', '-p', args['optim_args']['log_foldername']]) correlations, sigmas = training.kernel_optim_lbfgs_log( input_data=input_data_training, target_data=target_data_training, test_data=(input_data_testing, target_data_testing), **args['optim_args']) correlations_training.append(correlations) sigmas_training.append(sigmas) distances = np.zeros((ninstrus - 1, 1)) for i in range(ninstrus - 1): distances[i, 0] = -np.sum( np.power( np.divide( tab_red[:, test_idx[0]] - tab_red[:, sorted_train_idx[i]], (sigmas + np.finfo(float).eps)), 2)) distances_testing.append(distances.reshape(1, -1)) mean_distances = np.mean(distances) stddev_distances = np.std(distances) Jn = np.sum( np.multiply(distances - mean_distances, target_data_testing - mean_target_test)) Jd = std_target_test * stddev_distances * (ninstrus - 1) correlations_testing.append(Jn / Jd) sigmas_testing.append(sigmas) pearsr = pearsonr(distances, target_data_testing) correlations_testing_pearson.append(pearsr) spear_rho, spear_p_val = spearmanr(distances, target_data_testing) spear = [spear_rho, spear_p_val] correlations_testing_spearman.append(spear) print('\tFold={} test_corr={:.3f} (PE {} | SP {}) (train_corr={:.3f})'. format(fold + 1, Jn / Jd, pearsr, spear, correlations[-1])) pickle.dump( { 'correlations_training': correlations_training, 'sigmas_training': sigmas_training, 'correlations_testing': correlations_testing, 'sigmas_testing': sigmas_testing, 'correlations_testing_pearson': correlations_testing_pearson, 'correlations_testing_spearman': correlations_testing_spearman, 'distances_testing': distances_testing, 'target_data_testing': target_testing }, open(os.path.join(rslog_foldername, 'crossval_intrats_res.pkl'), 'wb')) mean_correlations_training = [ correlations_training[i][-1] for i in range(len(correlations_training)) ] mean_correlations_training = np.mean(mean_correlations_training) distances_testing = np.squeeze(np.array(distances_testing)).reshape(-1, 1) target_testing = np.squeeze(np.array(target_testing)).reshape(-1, 1) print( '\tAll Folds: mean_training_corr={} | corr_testing_pears={} | corr_testing_spear={}' .format(mean_correlations_training, pearsonr(distances_testing[:, 0], target_testing[:, 0]), spearmanr(distances_testing[:, 0], target_testing[:, 0])))
def run_one_tsp_rs_crossval(tsp, rs, args): rslog_foldername = args['optim_args']['log_foldername'] rs = rslog_foldername.split('/')[-1].split('-')[0] dissimil_mat = load.timbrespace_dismatrix(tsp, load.database()) aud_repres = load.timbrespace_features(tsp, representations=[rs], audio_args=args['audio_args'])[rs] tab_red = [] rs_type = rs.split('_')[-1] mapping = [] variances = [] if rs_type == 'strf': n_components = 1 for i in range(len(aud_repres)): strf_reduced, mapping_, variances = pca.pca_patil( np.absolute(aud_repres[i]), aud_repres[i].shape[1], n_components=n_components) strf_reduced = strf_reduced.flatten() tab_red.append(strf_reduced) mapping.append(mapping_) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrogram' or rs_type == 'mps': for i in range(len(aud_repres)): tab_red.append(aud_repres[i].flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif rs_type == 'spectrum': for i in range(len(aud_repres)): tab_red.append(aud_repres[i]) tab_red = np.transpose(np.asarray(tab_red)) pickle.dump( { 'data_repres': aud_repres, 'data_proj': tab_red, 'mapping': mapping, 'variances': variances, 'dissimilarities': dissimil_mat, 'audio_args': args['audio_args'] }, open(os.path.join(rslog_foldername, 'dataset.pkl'), 'wb')) print(' data dimension:', tab_red.shape) print('* normalizing') tab_red = tab_red / np.mean(np.max(np.abs(tab_red), axis=0)) corr_fold = [] sigmas_fold = [] print('* cross-validation tests') for fold in range(20): idx = [i for i in range(tab_red.shape[1])] random.shuffle(idx) train_idx = idx[:int(2 * len(idx) / 3)] test_idx = idx[int(2 * len(idx) / 3):] dmat = dissimil_mat[train_idx, :] dmat = dmat[:, train_idx] print('* Fold {} - {} {}'.format(fold + 1, train_idx, test_idx)) correlations, sigmas = training.kernel_optim_lbfgs_log( tab_red[:, train_idx], dmat, **args['optim_args']) # testing test_data = tab_red[:, test_idx] dmat = dissimil_mat[test_idx, :] target_test_data = dmat[:, test_idx] idx_triu = np.triu_indices(target_test_data.shape[0], k=1) target_test_data_v = target_test_data[idx_triu] mean_target_test = np.mean(target_test_data_v) std_target_test = np.std(target_test_data_v) ninstrus = len(test_idx) no_samples = ninstrus * (ninstrus - 1) / 2 kernel = np.zeros((ninstrus, ninstrus)) for i in range(ninstrus): for j in range(i + 1, ninstrus): kernel[i, j] = -np.sum( np.power( np.divide(test_data[:, i] - test_data[:, j], (sigmas + np.finfo(float).eps)), 2)) kernel_v = kernel[idx_triu] mean_kernel = np.mean(kernel_v) std_kernel = np.std(kernel_v) Jn = np.sum( np.multiply(kernel_v - mean_kernel, target_test_data_v - mean_target_test)) Jd = no_samples * std_target_test * std_kernel corr_fold.append(Jn / Jd) sigmas_fold.append(sigmas) print('\tfold={} corr={}'.format(fold + 1, Jn / Jd)) pickle.dump({ 'corr_fold': corr_fold, 'sigmas_fold': sigmas_fold }, open(os.path.join(rslog_foldername, 'crossval_res.pkl'), 'wb')) print('\tall folds: corr={} ({})'.format(np.mean(corr_fold), np.std(corr_fold)))
def run_one_tsp_rs(tsp, rs, args): rslog_foldername = args['optim_args']['log_foldername'] # logfolder rs = rslog_foldername.split('/')[-1].split('-')[0] dissimil_mat = load.timbrespace_dismatrix( tsp, load.database()) # load dissimilarity matrix aud_repres = load.timbrespace_features( tsp, representations=[rs], audio_args=args['audio_args'])[rs] # load representations tab_red = [] rs_type = rs.split('_')[-1] mapping = [] variances = [] reduce_strf = args['reduce_strf'] if rs_type == 'strf': # parse the type of input representation if reduce_strf == 'avg_time': for i in range(len(aud_repres)): strf_avgTime = np.mean(np.abs(aud_repres[i]), axis=0) tab_red.append(strf_avgTime.flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time_avg_scale_rate_freq': for i in range(len(aud_repres)): tab_red.append(strf2avgvec(aud_repres[i]).flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time_avg_freq': for i in range(len(aud_repres)): strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg( strf2avgvec(aud_repres[i])) tab_red.append(strf_scale_rate.flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time_avg_scale': for i in range(len(aud_repres)): strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg( strf2avgvec(aud_repres[i])) tab_red.append(strf_freq_rate.flatten()) tab_red = np.transpose(np.asarray(tab_red)) elif reduce_strf == 'avg_time_avg_rate': for i in range(len(aud_repres)): strf_scale_rate, strf_freq_rate, strf_freq_scale = avgvec2strfavg( strf2avgvec(aud_repres[i])) tab_red.append(strf_freq_scale.flatten()) tab_red = np.transpose(np.asarray(tab_red)) else: for i in range(len(aud_repres)): tab_red.append(aud_repres[i].flatten()) tab_red = np.transpose(np.asarray(tab_red)) print(args['audio_args']) print(' data dimension:', tab_red.shape) print('* normalizing') print(tab_red) print(np.mean(np.max(np.abs(tab_red), axis=0)).shape) tab_red = tab_red / np.mean(np.max(np.abs(tab_red), axis=0)) correlations, sigmas__ = training.kernel_optim_lbfgs_log( tab_red, dissimil_mat, **args['optim_args']) pickle.dump( { 'sigmas': sigmas__, 'representations': tab_red, 'dissimilarities': dissimil_mat, 'audio_args': args['audio_args'] }, open( os.path.join(args['log_foldername'] + 'resultsOptims_' + rs_type + '_' + reduce_strf + '_' + tsp + '.pkl'), 'wb'))
def run_crossts(rs, args): rslog_foldername = args['optim_args']['log_foldername'] rs = rslog_foldername.split('/')[-1].split('-')[0] print('Audio representation:', rs) cost=args['optim_args']['cost'] loss=args['optim_args']['loss'] init_sig_mean=args['optim_args']['init_sig_mean'] init_sig_var=args['optim_args']['init_sig_var'] num_loops=args['optim_args']['num_loops'] method='L-BFGS-B' log_foldername=rslog_foldername logging=args['optim_args']['logging'] verbose=True tsp = args['timbre_spaces'][0] dissimil_mat_tsp = load.timbrespace_dismatrix(tsp, load.database()) print(log_foldername+'/../input_data/{}_{}_input_data.txt'.format(tsp, rs)) tab_red = np.loadtxt(log_foldername+'/../input_data/{}_{}_input_data.txt'.format(tsp, rs)) ndims = tab_red.shape[0] sigmas = np.abs(init_sig_mean + init_sig_var * np.random.randn(ndims, 1)) init_seed = sigmas correlations = [] # = np.zeros((num_loops, )) retrieved_loop = 0 # if (verbose): # print("* training sigmas of gaussian kernels with cost '{}' and method '{}'".format( # cost, method)) loop_cpt = 0 correlations = [] optim_options = {'disp': None, 'maxls': 50, 'iprint': -1, 'gtol': 1e-36, 'eps': 1e-8, 'maxiter': num_loops, 'ftol': 1e-36} optim_bounds = [(1.0*f,1e5*f) for f in np.ones((ndims,))] if logging: pickle.dump({ 'seed': init_seed, 'cost': cost, 'loss': loss, 'method': method, 'init_sig_mean': init_sig_mean, 'init_sig_var': init_sig_var, 'num_loops': num_loops, 'log_foldername': log_foldername, 'optim_options': {'options': optim_options, 'bounds': optim_bounds} }, open(os.path.join(log_foldername, 'optim_config.pkl'), 'wb')) def corr(x): corr_sum = 0 for tsp in args['timbre_spaces']: dissimil_mat_tsp = load.timbrespace_dismatrix(tsp, load.database()) tab_red = np.loadtxt(log_foldername+'/../input_data/{}_{}_input_data.txt'.format(tsp, rs)) # tab_red = np.loadtxt('input_data/{}_{}_input_data.txt'.format(tsp, rs)) ndims, ninstrus = tab_red.shape[0], tab_red.shape[1] no_samples = ninstrus * (ninstrus - 1) / 2 idx_triu = np.triu_indices(dissimil_mat_tsp.shape[0], k=1) target_v = dissimil_mat_tsp[idx_triu] mean_target = np.mean(target_v) std_target = np.std(target_v) kernel = np.zeros((ninstrus, ninstrus)) for i in range(ninstrus): for j in range(i + 1, ninstrus): kernel[i, j] = -np.sum( np.power( np.divide(tab_red[:, i] - tab_red[:, j], (x + np.finfo(float).eps)), 2)) kernel_v = kernel[idx_triu] mean_kernel = np.mean(kernel_v) std_kernel = np.std(kernel_v) Jn = np.sum(np.multiply(kernel_v - mean_kernel, target_v - mean_target)) Jd = no_samples * std_target * std_kernel corr_sum += Jn/Jd return corr_sum def grad_corr(x): corr_sum = 0 for tsp_i, tsp in enumerate(args['timbre_spaces']): dissimil_mat_tsp = load.timbrespace_dismatrix(tsp, load.database()) tab_red = np.loadtxt(log_foldername+'/../input_data/{}_{}_input_data.txt'.format(tsp, rs)) #np.loadtxt('input_data/{}_{}_input_data.txt'.format(tsp, rs)) # tab_red = np.loadtxt('input_data/{}_{}_input_data.txt'.format(tsp, rs)) ndims, ninstrus = tab_red.shape[0], tab_red.shape[1] no_samples = ninstrus * (ninstrus - 1) / 2 idx_triu = np.triu_indices(dissimil_mat_tsp.shape[0], k=1) target_v = dissimil_mat_tsp[idx_triu] mean_target = np.mean(target_v) std_target = np.std(target_v) if tsp_i==0: gradients = np.zeros((ndims, 1)) kernel = np.zeros((ninstrus, ninstrus)) dkernel = np.zeros((ninstrus, ninstrus, ndims)) for i in range(ninstrus): for j in range(i + 1, ninstrus): kernel[i, j] = -np.sum( np.power( np.divide(tab_red[:, i] - tab_red[:, j], (x + np.finfo(float).eps)), 2)) dkernel[i, j, :] = 2 * np.power((tab_red[:, i] - tab_red[:, j]), 2) / (np.power(x, 3) + np.finfo(float).eps) kernel_v = kernel[idx_triu] mean_kernel = np.mean(kernel_v) std_kernel = np.std(kernel_v) Jn = np.sum(np.multiply(kernel_v - mean_kernel, target_v - mean_target)) Jd = no_samples * std_target * std_kernel for k in range(ndims): dkernel_k_v = dkernel[:, :, k][idx_triu] dJn = np.sum(dkernel_k_v * (target_v - mean_target)) dJd = no_samples / no_samples * \ std_target / (std_kernel + np.finfo(float).eps) * \ np.sum(dkernel_k_v * (kernel_v - mean_kernel)) gradients[k] += (Jd * dJn - Jn * dJd) / (np.power(Jd,2) + np.finfo(float).eps) return gradients def print_corr(xk): jns, jds = [], [] corr_sum = [] for tsp in args['timbre_spaces']: dissimil_mat_tsp = load.timbrespace_dismatrix(tsp, load.database()) tab_red = np.loadtxt(log_foldername+'/../input_data/{}_{}_input_data.txt'.format(tsp, rs)) ndims, ninstrus = tab_red.shape[0], tab_red.shape[1] no_samples = ninstrus * (ninstrus - 1) / 2 idx_triu = np.triu_indices(dissimil_mat_tsp.shape[0], k=1) target_v = dissimil_mat_tsp[idx_triu] mean_target = np.mean(target_v) std_target = np.std(target_v) kernel = np.zeros((ninstrus, ninstrus)) for i in range(ninstrus): for j in range(i + 1, ninstrus): kernel[i, j] = -np.sum( np.power( np.divide(tab_red[:, i] - tab_red[:, j], (xk + np.finfo(float).eps)), 2)) kernel_v = kernel[idx_triu] mean_kernel = np.mean(kernel_v) std_kernel = np.std(kernel_v) Jn = np.sum(np.multiply(kernel_v - mean_kernel, target_v - mean_target)) Jd = no_samples * std_target * std_kernel corr_sum.append(Jn/Jd) if not os.path.isfile(os.path.join(log_foldername, 'tmp.pkl')): loop_cpt = 1 pickle.dump({'loop': loop_cpt, 'correlation': [np.mean(corr_sum)]}, open(os.path.join(log_foldername, 'tmp.pkl'), 'wb')) correlations = [np.mean(corr_sum)] pickle.dump({ 'sigmas': xk, 'kernel': kernel, 'Jn': Jn, 'Jd': Jd, 'corr_sum': corr_sum, 'correlations': correlations, }, open(os.path.join(log_foldername, 'optim_process_l={}.pkl'.format(loop_cpt)), 'wb')) else: last_loop = pickle.load(open(os.path.join(log_foldername,'tmp.pkl'), 'rb')) loop_cpt = last_loop['loop'] + 1 correlations = last_loop['correlation'] correlations.append(np.mean(corr_sum)) monitoring_step = 50 if (loop_cpt % monitoring_step == 0): corr_sum_str = ' '.join(['{:.2f}'.format(c) for c in corr_sum]) print(' |_ loop={} J={:.6f} ({})'.format(loop_cpt, np.mean(corr_sum), corr_sum_str)) pickle.dump({ 'sigmas': xk, 'kernel': kernel, 'Jn': Jn, 'Jd': Jd, 'corr_sum': corr_sum, 'correlations': correlations, }, open(os.path.join(log_foldername, 'optim_process_l={}.pkl'.format(loop_cpt)), 'wb')) pickle.dump({'loop': loop_cpt, 'correlation': correlations, 'sigmas': xk}, open(os.path.join(log_foldername, 'tmp.pkl'), 'wb')) res = minimize(corr, sigmas, args=(), method=method, jac=grad_corr, callback=print_corr, options=optim_options, bounds=optim_bounds) last_loop = pickle.load(open(os.path.join(log_foldername,'tmp.pkl'), 'rb')) sigmas_ = last_loop['sigmas'] return correlations, sigmas_