def test_sigmoid_with_scalar_ops(): with goos.OptimizationPlan() as plan: x = goos.Variable([0, 1]) y = goos.dot([1, 2], goos.Sigmoid(2 * x - 1)) np.testing.assert_allclose(y.get().array, 1.7310585786300) np.testing.assert_allclose( y.get_grad([x])[0].array_grad, [0.39322386648296, 0.7864477329659])
def test_sigmoid_array(): with goos.OptimizationPlan() as plan: x = goos.Variable([0, 1]) y = goos.dot([1, 2], goos.Sigmoid(x)) np.testing.assert_allclose(y.get().array, 1.962117) np.testing.assert_allclose( y.get_grad([x])[0].array_grad, [0.25, 0.39322386648])
def test_sum_double_array(): with goos.OptimizationPlan() as plan: x = goos.Variable([2.0, 1.0]) y = goos.Variable([3.0, -1.0]) res = goos.dot([1, 1], x + y) np.testing.assert_allclose(res.get().array, 5) np.testing.assert_allclose(res.get_grad([x])[0].array_grad, [1, 1]) np.testing.assert_allclose(res.get_grad([y])[0].array_grad, [1, 1])
def test_max_two_array(): with goos.OptimizationPlan() as plan: x = goos.Variable([2, 1]) y = goos.Variable([3, 0.5]) z = goos.dot(goos.max(x, y), [1, -1]) np.testing.assert_array_equal(z.get().array, 2) np.testing.assert_array_equal(z.get_grad([x])[0].array_grad, [0, -1]) np.testing.assert_array_equal(z.get_grad([y])[0].array_grad, [1, 0])
def test_dot(): with goos.OptimizationPlan() as plan: x = goos.Variable([3, 2, 1]) y = goos.Variable([-1, 1, 4]) res = goos.dot(x, y) assert res.get() == 3 grad = res.get_grad([x, y]) np.testing.assert_array_equal(grad[0].array_grad, [-1, 1, 4]) np.testing.assert_array_equal(grad[1].array_grad, [3, 2, 1])
def test_max_two_array_2d(): with goos.OptimizationPlan() as plan: x = goos.Variable([[1, 2], [3, 4]]) y = goos.Variable([[3, 0.5], [4.5, 6]]) z = goos.dot([[1, 2], [3, 4]], goos.max(x, y)) np.testing.assert_array_equal(z.get().array, 44.5) np.testing.assert_array_equal( z.get_grad([x])[0].array_grad, [[0, 2], [0, 0]]) np.testing.assert_array_equal( z.get_grad([y])[0].array_grad, [[1, 0], [3, 4]])
def test_optimize_ineq_constraints_arr(): with goos.OptimizationPlan() as plan: x = goos.Variable([0, 0]) obj = (goos.dot([1, 1], x) + 1)**2 + 3 goos.opt.scipy_maximize(obj, constraints_ineq=[x - np.array([2, 1])], method="SLSQP") plan.run() np.testing.assert_allclose(x.get().array, [2, 1])
def test_pixelated_multietch_grating_xz_gradient(use_edge_locs, params, pol): # Brute force calculate the gradient. # Grating defined in xz-plane. with goos.OptimizationPlan() as plan: edge_locs = goos.Variable(params) height_index = goos.Variable([0, 1, 0, 1]) grating = goos.grating.PixelatedGrating( edge_locs, height_index=height_index, height_fracs=[0.4, 1], pos=[0, 0, 0], extents=[2, 10, 0.220], material=goos.material.Material(index=1), material2=goos.material.Material(index=3.5), grating_dir=0, grating_dir_spacing=0.5, etch_dir_divs=4, etch_polarity=pol, use_edge_locs=use_edge_locs) from spins.goos_sim import maxwell shape = maxwell.RenderShape(grating, region=goos.Box3d(center=[0, 0, 0], extents=[4, 10, 0.220]), mesh=maxwell.UniformMesh(dx=0.25), wavelength=1550) np.random.seed(247) obj = goos.dot(shape, np.random.random(shape.get().array.shape)) val_orig = edge_locs.get().array step = 1e-5 grad_num = np.zeros_like(val_orig) for i in range(len(val_orig)): delta = np.zeros_like(val_orig) delta[i] = 1 edge_locs.set(val_orig + step * delta) f_plus = obj.get(run=True).array edge_locs.set(val_orig - step * delta) f_minus = obj.get(run=True).array grad_num[i] = (f_plus - f_minus) / (2 * step) grad_backprop = obj.get_grad([edge_locs])[0].array_grad np.testing.assert_allclose(grad_backprop, grad_num)
def test_render_pixelated_cont_shape_grad(): with goos.OptimizationPlan() as plan: def initializer(size): return [[1, 0], [0.5, 0.75], [0, 0.8], [1, 0.8]] var, design = goos.pixelated_cont_shape( initializer=initializer, pos=[200, 0, 0], extents=[1000, 1000, 220], material=goos.material.Material(index=2), material2=goos.material.Material(index=4), pixel_size=[250, 500, 220]) render = maxwell.RenderShape( design, region=goos.Box3d(center=[0, 0, 0], extents=[1500, 1500, 200]), mesh=maxwell.UniformMesh(dx=40), background=goos.material.Material(index=1.0), wavelength=1550) # Compute a random objective function to test gradient. np.random.seed(247) obj = goos.dot(np.random.random(render.get().array.shape), render) adjoint_grad = obj.get_grad([var])[0].array_grad # Calculate brute force gradient. var_val = var.get().array eps = 0.001 num_grad = np.zeros_like(var_val) for i in range(var_val.shape[0]): for j in range(var_val.shape[1]): temp_val = var_val.copy() temp_val[i, j] += eps var.set(temp_val) fplus = obj.get(run=True).array temp_val = var_val.copy() temp_val[i, j] -= eps var.set(temp_val) fminus = obj.get(run=True).array num_grad[i, j] = (fplus - fminus) / (2 * eps) np.testing.assert_array_almost_equal(adjoint_grad, num_grad)
def test_dot_auto_constant(): with goos.OptimizationPlan() as plan: res = goos.dot([3, 2, 1], [-1, 1, 4]) assert res.get() == 3