def __transform_circle(pattern, material, material_parent, Gx, Gy, omega_index): position = pattern.position * MICRON radius = pattern.radius * MICRON Gx_r, Gx_l = np.meshgrid(Gx, Gx) Gy_r, Gy_l = np.meshgrid(Gy, Gy) Gx_mat = Gx_l - Gx_r Gy_mat = Gy_l - Gy_r phase = np.exp(1j*(Gx_mat*position[0] + Gy_mat*position[1])) geometry_mat = self.__transform_circle_element(Gx_mat, Gy_mat, radius) eps_parent = material_parent.get_epsilon_at_index(omega_index) mu_parent = material_parent.get_mu_at_index(omega_index) eps = material.get_epsilon_at_index(omega_index) mu = material.get_mu_at_index(omega_index) dimension = self.__lattice.get_dimension() if dimension == "one": area = self.__lattice.get_area() * MICRON else: area = self.__lattice.get_area() * np.sqaure(MICRON) dval = list() dval_mu = list() for i in range(3): for j in range(3): eps_mat = (eps[i,j] - eps_parent[i,j])*phase*geometry_mat dval.append(eps_mat) mu_mat = (mu[i,j] - mu_parent[i,j])*phase*geometry_mat dval_mu.append(mu_mat) dval.extend(dval_mu) return dval
def __transform_grating(pattern, material, material_parent, Gx, Gy, omega_index): center = pattern.center * MICRON width = pattern.width *MICRON Gx_r, Gx_l = np.meshgrid(Gx, Gx) Gmat = Gx_l - Gx_r phase = np.exp(1j*Gmat*center) eps_parent = material_parent.get_epsilon_at_index(omega_index) mu_parent = material_parent.get_mu_at_index(omega_index) eps = material.get_epsilon_at_index(omega_index) mu = material.get_mu_at_index(omega_index) geometry_mat = self.__transform_grating_element(G_mat, width) dimension = self.__lattice.get_dimension() if dimension == "one": area = self.__lattice.get_area() * MICRON else: area = self.__lattice.get_area() * np.sqaure(MICRON) dval = list() dval_mu = list() for i in range(3): for j in range(3): eps_mat = (eps[i,j] - eps_parent[i,j])*phase*geometry_mat dval.append(eps_mat) mu_mat = (mu[i,j] - mu_parent[i,j])*phase*geometry_mat dval_mu.append(mu_mat) dval.extend(dval_mu) return dval
def test(testImg, noComp, layerNo): alpha = 0.01 X = T.dmatrix() lambdaC = T.dscalar() D = pickle.load(open('../weights/dictionary_theano_{}.sav'.format(layerNo), 'rb')) W = shared(np.zeros(X.shape[0], noComp)) gen = np.dot(W, D) genFunc = function([], gen) cost = np.sum(np.sqaure(X - np.dot(W, D))) + lambdaC * np.sum(np.square(W)) costFunc = function([X, lambdaC], cost, updates = [(W, W - alpha * T.grad(cost, W))]) for i in range(1000): cost = costFunc(testImg, 0.1) image = np.reshape(gen(), (32, 32)) plt.imshow(image) plt.savefig('../output/test_theano_{}.png'.format(layerNo)) print("=> Weights learnt and Image Saved for Test-Layer {}.".format(layerNo))
def train(data, noComp, layerNo): alpha = 0.01 X = T.dmatrix() lambdaC = T.dscalar() D = shared(np.zeros((noComp, X.shape[1]))) W = shared(np.zeros(X.shape[0], noComp)) gen = np.dot(W, D) genFunc = function([], gen) cost = np.sum(np.sqaure(X - np.dot(W, D))) + lambdaC * np.sum(np.square(W)) costFunc = function([X, lambdaC], cost, updates = [(W, W - alpha * T.grad(cost, W)), (D, D - alpha * T.grad(cost, D))]) for i in range(1000): cost = costFunc(data, 0.1) if i%50 == 0: print("== Cost : {}".format(cost)) pickle.dump(D, open('../weights/dictionary_theano_{}.sav'.format(layerNo), 'wb')) print("=> Dictionary Learnt and Saved.") pickle.dump(W, open('../weights/weights_theano_{}.sav'.format(layerNo), 'wb')) print("=> Weights Learnt and Saved.")
def __transform_polygon(pattern, material, material_parent, Gx, Gy, omega_index): position = pattern.position * MICRON angle = pattern.angle * np.pi/180 edge_list = pattern.edge_list * MICRON Gx_r, Gx_l = np.meshgrid(Gx, Gx) Gy_r, Gy_l = np.meshgrid(Gy, Gy) Gx_mat = Gx_l - Gx_r Gy_mat = Gy_l - Gy_r phase = np.exp(1j*(Gx_mat*position[0] + Gy_mat*position[1])) G_temp = Gx_mat * np.cos(angle) + Gy_mat * np.sin(angle) Gy_mat = -Gx_mat * np.sin(angle) + Gy_mat * np.cos(angle) Gx_mat = G_temp geometry_mat = self.__transform_polygon_element(Gx_mat, Gy_mat, edge_list) eps_parent = material_parent.get_epsilon_at_index(omega_index) mu_parent = material_parent.get_mu_at_index(omega_index) eps = material.get_epsilon_at_index(omega_index) mu = material.get_mu_at_index(omega_index) dimension = self.__lattice.get_dimension() if dimension == "one": area = self.__lattice.get_area() * MICRON else: area = self.__lattice.get_area() * np.sqaure(MICRON) dval = list() dval_mu = list() for i in range(3): for j in range(3): eps_mat = (eps[i,j] - eps_parent[i,j])*phase*geometry_mat dval.append(eps_mat) mu_mat = (mu[i,j] - mu_parent[i,j])*phase*geometry_mat dval_mu.append(mu_mat) dval.extend(dval_mu) return dval
def compute_distances_one_loop(self, X): """ Compute the distance between each test point in X and each training point in self.X_train using a single loop over the test data. Input / Output: Same as compute_distances_two_loops """ num_test = X.shape[0] num_train = self.X_train.shape[0] dists = np.zeros((num_test, num_train)) for i in range(num_test): ####################################################################### # TODO: # # Compute the l2 distance between the ith test point and all training # # points, and store the result in dists[i, :]. # # Do not use np.linalg.norm(). # ####################################################################### # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** dists[i:] = np.sqrt(np.sum(np.sqaure(X[i:] - self.X_train), axis=1)) # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** return dists
def func(x1, x2, x3): return np.square(x1 + x2 - 1.0) + np.square(x1 - x2 - 2.0) + np.sqaure( float(x1 - x3))
def MSE(W,X,Y): minval = np.amin(Y) err = np.sum(np.sqaure(np.matmul(X,W) - Y)) denom = np.sum(np.sqaure(Y-minval)) return (err/denom)
def __transform_ellipse_element(Gx_mat, Gy_mat, halfwidths): a, b = halfwidths rho = np.square(np.sqaure(a*Gx_mat) + np.square(b*Gy_mat)) jinc_mat = geometry.jinc(rho) geometry_mat = 2*np.pi*a*b*jinc_mat return geometry_mat
def __transform_circle_element(Gx_mat, Gy_mat, radius): rho = np.sqrt(np.sqaure(Gx_mat) + np.sqaure(Gy_mat)) * radius jinc_mat = geometry.jinc(rho) geometry_mat = 2*np.pi*np.square(radius) * jinc_mat return geometry_mat