def gen_fcns(dx, dy, x_pts, y_pts, input_dim_1, input_dim_2, num_fcns, session, gpu_id = 0, num_inits_per_fcn = 1, num_points_per_class = 50): fcn_objs = [] param_dims = [] if 1: for k in range(num_fcns): kk = np.random.randint(0, len(dx)) dx2 = 1/(dx[kk]*dx[kk]) dy2 = 1/(dy[kk]*dy[kk]) mx = np.max([dx2, dy2, 2*(dx2+dy2)]) input_dim = (input_dim_1[kk]-2)*(input_dim_2[kk]-2) input_dim1 = input_dim_1[kk]*input_dim_2[kk] fcn_family = QuadraticNormFcnFamily(input_dim, gpu_id = gpu_id, session = session) param_dim = fcn_family.get_total_num_dim() param_dims.append(param_dim) ind1 = [] ind2 = [] data = np.zeros([input_dim1, input_dim1]) labels = np.zeros([input_dim1,1]) k_g = np.random.rand(1)[0]+0.5 k_f = np.random.rand(1)[0]+0.5 for j in range(0, input_dim_2[kk]): for i in range(0, input_dim_1[kk]): if i == 0 or j == input_dim_2[kk] - 1 or i == input_dim_1[kk] - 1 or j == 0: ind2.append(j*input_dim_1[kk]+i) data[j*input_dim_1[kk] + i, j*input_dim_1[kk] + i] = 1.0 labels[j*input_dim_1[kk] + i] = g_fcn(x_pts[kk][i], y_pts[kk][j], k_g) else: ind1.append(j*input_dim_1[kk]+i) data[j*input_dim_1[kk] + i, j*input_dim_1[kk] + i - 1] = dx2 data[j*input_dim_1[kk] + i, j*input_dim_1[kk] + i + 1] = dx2 data[j*input_dim_1[kk] + i, (j-1)*input_dim_1[kk] + i] = dy2 data[j*input_dim_1[kk] + i, (j+1)*input_dim_1[kk] + i] = dy2 data[j*input_dim_1[kk] + i, j*input_dim_1[kk] + i] = -2*(dx2 + dy2) labels[j*input_dim_1[kk] + i] = f_fcn(x_pts[kk][i], y_pts[kk][j], k_f) #data = data/mx #labels = labels/mx data1 = data[ind1,:][:,ind1] data2 = data[ind1,:][:,ind2] labels1 = labels[ind1,:] - np.matmul(data2,labels[ind2,:]) data1 = data1/mx labels1 = labels1/mx fcn = QuadraticNormFcn(fcn_family, data1, labels1, disable_subsampling = True) for j in range(num_inits_per_fcn): fcn_objs.append(fcn) #print(fcn.param_vals) #init_locs = np.zeros([param_dim,num_fcns*num_inits_per_fcn]) #if eig < 0 and pde > 0: fcns = [{'fcn_obj': fcn_objs[k], 'dim': param_dims[k], 'init_loc': np.zeros([param_dims[k], 1])} for k in range(num_fcns*num_inits_per_fcn)] #fcns = [{'fcn_obj': fcn_objs[k], 'dim': param_dim, 'init_loc': init_locs[:,k][:,None]} for k in range(num_fcns*num_inits_per_fcn)] return fcns,fcn_family
def gen_fcns(dx, dy, x_pts, y_pts, input_dim_1, input_dim_2, num_fcns, session, gpu_id=0, num_inits_per_fcn=1, num_points_per_class=50): input_dim = (input_dim_1 - 2) * (input_dim_2 - 2) input_dim1 = input_dim_1 * input_dim_2 #if eig < 0 and pde > 0 and prec < 0: fcn_family = LagrangianFcnFamily4Eig(input_dim + 1, gpu_id=gpu_id, session=session) #elif eig < 0 and pde > 0 and prec > 0: # fcn_family = QuadraticNormCompFcnFamily(input_dim, gpu_id = gpu_id, session = session) #elif eig > 0 and pde < 0: # fcn_family = TraceNormFcnFamily(input_dim, gpu_id = gpu_id, session = session) param_dim = fcn_family.get_total_num_dim() #print(param_dim) fcn_objs = [] if 1: dx2 = 1 / (dx * dx) dy2 = 1 / (dy * dy) mx = np.max([dx2, dy2, 2 * (dx2 + dy2)]) for k in range(num_fcns): ind1 = [] ind2 = [] data = np.zeros([input_dim1, input_dim1]) labels = np.zeros([input_dim1, 1]) k_g = np.random.rand(1)[0] + 0.5 k_f = np.random.rand(1)[0] + 0.5 for j in range(0, input_dim_2): for i in range(0, input_dim_1): if i == 0 or j == input_dim_2 - 1 or i == input_dim_1 - 1 or j == 0: ind2.append(j * input_dim_1 + i) data[j * input_dim_1 + i, j * input_dim_1 + i] = 1.0 labels[j * input_dim_1 + i] = g_fcn( x_pts[i], y_pts[j], k_g) else: ind1.append(j * input_dim_1 + i) data[j * input_dim_1 + i, j * input_dim_1 + i - 1] = dx2 data[j * input_dim_1 + i, j * input_dim_1 + i + 1] = dx2 data[j * input_dim_1 + i, (j - 1) * input_dim_1 + i] = dy2 data[j * input_dim_1 + i, (j + 1) * input_dim_1 + i] = dy2 data[j * input_dim_1 + i, j * input_dim_1 + i] = -2 * (dx2 + dy2) labels[j * input_dim_1 + i] = f_fcn( x_pts[i], y_pts[j], k_f) #data = data/mx #labels = labels/mx data1 = data[ind1, :][:, ind1] data2 = data[ind1, :][:, ind2] labels1 = labels[ind1, :] - np.matmul(data2, labels[ind2, :]) data1 = -data1 / mx labels1 = -labels1 / mx #e, v = np.linalg.eig(data1) #e = e/2 #data1 = np.matmul(v, np.matmul(np.diag(e), v)) #print(mx) #if eig < 0 and pde > 0: #data1 = data1/mx #labels1 = labels1/mx fcn = LagrangianFcn4Eig(fcn_family, data1, labels1, disable_subsampling=True) #elif eig > 0 and pde < 0: #data1 = data1/mx/mx #labels1 = labels1/mx/mx # e,v = np.linalg.eig(data1) # eig = np.min(data1) # fcn = TraceNormFcn(fcn_family, data1, labels1, eig, disable_subsampling = True) for j in range(num_inits_per_fcn): fcn_objs.append(fcn) #init_locs = np.random.randn(param_dim-1,num_fcns*num_inits_per_fcn) #zeros = np.zeros([1, num_fcns*num_inits_per_fcn]) #init_locs = np.vstack((init_locs, zeros)) #norms = np.linalg.norm(init_locs, axis=0) #init_locs = init_locs/norms init_locs = np.zeros([param_dim, num_fcns * num_inits_per_fcn]) p = np.expand_dims(init_locs[:param_dim - 1, 0], axis=0) #print(p.shape) ppT = np.matmul(np.transpose(p), p) #print(ppT) #print(np.linalg.norm(data1 - np.transpose(data1))) print(np.trace(data1) - np.trace(np.matmul(data1, ppT))) #if eig < 0 and pde > 0 and prec > 0: # init_locs = np.random.randn(param_dim,num_fcns*num_inits_per_fcn) + 1j*np.random.randn(param_dim,num_fcns*num_inits_per_fcn) #if eig < 0 and pde > 0: fcns = [{ 'fcn_obj': fcn_objs[k], 'dim': param_dim, 'init_loc': init_locs[:, k][:, None] } for k in range(num_fcns * num_inits_per_fcn)] #fcns = [{'fcn_obj': fcn_objs[k], 'dim': param_dim, 'init_loc': init_locs[:,k][:,None]} for k in range(num_fcns*num_inits_per_fcn)] return fcns, fcn_family
def gen_fcns(interval1, interval2, max_dim_1, max_dim_2, num_fcns, session, gpu_id = 0, num_inits_per_fcn = 1, num_points_per_class = 50): max_dim = (max_dim_1-2)*(max_dim_2-2) max_dim1 = max_dim_1*max_dim_2 fcn_family = QuadraticNormFcnFamily(max_dim, gpu_id = gpu_id, session = session) param_dim = fcn_family.get_total_num_dim() fcn_objs = [] dims = np.random.randint(int(2*max_dim_1/3), max_dim_1, size=num_fcns) #print('dims = ', dims, max_dim_1) init_locs = np.random.randn(param_dim,num_fcns*num_inits_per_fcn) if 1: for k in range(num_fcns): dim = dims[k] dim1 = dim - 2 dx = (interval1[1]-interval1[0])/dim dy = (interval2[1]-interval2[0])/dim dx2 = 1/(dx*dx) dy2 = 1/(dy*dy) mx = np.max([dx2, dy2, 2*(dx2+dy2)]) x_pts = np.arange(interval1[0], interval1[1]+dx/2,dx) y_pts = np.arange(interval2[0], interval2[1]+dy/2,dy) ind1 = [] ind2 = [] data = np.zeros([dim*dim, dim*dim]) labels = np.zeros([dim*dim,1]) k_g = np.random.rand(1)[0]+0.5 k_f = np.random.rand(1)[0]+0.5 for j in range(0, dim): for i in range(0, dim): if i == 0 or j == dim - 1 or i == dim - 1 or j == 0: ind2.append(j*dim+i) data[j*dim + i, j*dim + i] = 1.0 labels[j*dim + i] = g_fcn(x_pts[i], y_pts[j], k_g) else: ind1.append(j*dim+i) data[j*dim + i, j*dim + i - 1] = dx2 data[j*dim + i, j*dim + i + 1] = dx2 data[j*dim + i, (j-1)*dim + i] = dy2 data[j*dim + i, (j+1)*dim + i] = dy2 data[j*dim + i, j*dim + i] = -2*(dx2 + dy2) labels[j*dim + i] = f_fcn(x_pts[i], y_pts[j], k_f) #data = data/mx #labels = labels/mx data1 = data[ind1,:][:,ind1] data2 = data[ind1,:][:,ind2] labels1 = labels[ind1,:] - np.matmul(data2,labels[ind2,:]) data1 = -data1/mx labels1 = -labels1/mx #print(type(data1), len(data1), len(data1[0])) data = np.eye(max_dim)*data1.max() labels = np.zeros([max_dim, 1]) data[0:dim1*dim1, 0:dim1*dim1] = data1 labels[0:dim1*dim1] = labels1 fcn = QuadraticNormFcn(fcn_family, data, labels, disable_subsampling = True) for j in range(num_inits_per_fcn): init_locs[:, k*num_inits_per_fcn+j] = CGM(data, labels, np.vstack((np.random.randn(dim1*dim1, 1), np.zeros([max_dim-dim1*dim1, 1]))), 8)[:,0] #print(init_locs[:, k*num_inits_per_fcn+j]) fcn_objs.append(fcn) #if eig < 0 and pde > 0: fcns = [{'fcn_obj': fcn_objs[k], 'dim': param_dim, 'init_loc': init_locs[:,k][:,None]} for k in range(num_fcns*num_inits_per_fcn)] #fcns = [{'fcn_obj': fcn_objs[k], 'dim': param_dim, 'init_loc': init_locs[:,k][:,None]} for k in range(num_fcns*num_inits_per_fcn)] return fcns,fcn_family
def gen_fcns_more(dx, dy, x_pts, y_pts, input_dim_1, input_dim_2, num_fcns, session, exp_dir, itrs, pde_flag, eig_flag, obs_flag=None, num_inits_per_fcn=1, num_points_per_class=50): input_dim = (input_dim_1 - 2) * (input_dim_2 - 2) input_dim1 = input_dim_1 * input_dim_2 #print input_dim1 #print eig, pde if eig_flag < 0 and pde_flag > 0: fcn_family = QuadraticNormFcnFamily(input_dim, gpu_id=0, session=session) elif eig_flag > 0 and pde_flag < 0: fcn_family = TraceNormFcnFamily(input_dim, gpu_id=0, session=session) param_dim = fcn_family.get_total_num_dim() fcn_objs = [] dx2 = 1 / (dx * dx) dy2 = 1 / (dy * dy) mx = np.max([dx2, dy2, 2 * (dx2 + dy2)]) for k in range(num_fcns): ind1 = [] ind2 = [] data = np.zeros([input_dim1, input_dim1]) labels = np.zeros([input_dim1, 1]) k_g = np.random.rand(1)[0] + 0.5 k_f = np.random.rand(1)[0] + 0.5 for j in range(0, input_dim_2): for i in range(0, input_dim_1): if i == 0 or j == input_dim_2 - 1 or i == input_dim_1 - 1 or j == 0: ind2.append(j * input_dim_1 + i) data[j * input_dim_1 + i, j * input_dim_1 + i] = 1.0 labels[j * input_dim_1 + i] = g_fcn( x_pts[i], y_pts[j], k_g) else: ind1.append(j * input_dim_1 + i) data[j * input_dim_1 + i, j * input_dim_1 + i - 1] = dx2 data[j * input_dim_1 + i, j * input_dim_1 + i + 1] = dx2 data[j * input_dim_1 + i, (j - 1) * input_dim_1 + i] = dy2 data[j * input_dim_1 + i, (j + 1) * input_dim_1 + i] = dy2 data[j * input_dim_1 + i, j * input_dim_1 + i] = -2 * (dx2 + dy2) labels[j * input_dim_1 + i] = f_fcn( x_pts[i], y_pts[j], k_f) data1 = data[ind1, :][:, ind1] data2 = data[ind1, :][:, ind2] labels1 = labels[ind1, :] - np.matmul(data2, labels[ind2, :]) #data1 = data1/mx #labels1 = labels1/mx if eig_flag < 0 and pde_flag > 0: data1 = data1 / mx labels1 = labels1 / mx fcn = QuadraticNormFcn(fcn_family, data1, labels1, disable_subsampling=True) elif eig_flag > 0 and pde_flag < 0: data1 = data1 / mx / mx labels1 = labels1 / mx / mx e, v = np.linalg.eig(data1) eig = np.min(e) fcn = TraceNormFcn(fcn_family, data1, labels1, eig, disable_subsampling=True) for j in range(num_inits_per_fcn): fcn_objs.append(fcn) init_locs = np.zeros([param_dim, num_fcns * num_inits_per_fcn]) #print param_dim pol = None #b = np.zeros(param_dim) #print eig, pde for k in range(num_fcns * num_inits_per_fcn): if eig_flag < 0 and pde_flag > 0: b = np.squeeze(labels1) x = np.random.rand(param_dim) for j in range(len(obs_flag)): x = pdesolver(x, b, pol, fcn_objs[k], err=1e-7, it_max=50, obs_flag=obs_flag[j]) x = x[0] elif eig_flag > 0 and pde_flag < 0: b = np.zeros(param_dim) #x = np.random.rand(param_dim) #x = x/np.linalg.norm(x) x = np.zeros(param_dim) for j in range(len(obs_flag)): x = eigfinder(x, b, pol, fcn_objs[k], err=1e-7, it_max=50, obs_flag=obs_flag[j]) x = x[0] #x = pdesolver(x[0], pol, fcn_objs[k], err = 1e-7, it_max = 50, obs_flag = obs_flag if len(x.shape) == 1: x = np.expand_dims(x, axis=1) init_locs[:, k][:, None] = x #print 2-np.matmul(np.transpose(x), x) #print (2-np.matmul(np.transpose(x), x))*(np.matmul(np.transpose(x), np.matmul(data1, x)))-eig #print np.squeeze(2-np.matmul(np.transpose(x), x))*np.matmul(data1+np.transpose(data1), x)-2*x*np.squeeze(np.matmul(np.transpose(x), np.matmul(data1, x)))-eig #print(num_fcns*num_inits_per_fcn) #print(np.size(labels),labels) #print(np.size(data),data[18,:][:,None]) if eig_flag < 0 and pde_flag > 0: fcns = [{ 'fcn_obj': fcn_objs[k], 'dim': param_dim, 'init_loc': init_locs[:, k][:, None] } for k in range(num_fcns * num_inits_per_fcn)] elif eig_flag > 0 and pde_flag < 0: fcns = [{ 'fcn_obj': fcn_objs[k], 'dim': param_dim, 'init_loc': init_locs[:, k][:, None], 'eig': eig } for k in range(num_fcns * num_inits_per_fcn)] return fcns, fcn_family