def regularization_loss(w, lambda_): """ Calculates the regularization loss of the output weights. Regularization loss is used to favor smaller magnitudes of weights. :param w: The weight matrix of the output layer of form (H x K), where H is the size of the previous layer and K is the number of classes. :param lambda_: A hyperparameter used to control the magnitude of the weight. :return: The regularization loss. """ return .5 * lambda_ * np.sum(np.square(w))
def calc_match_rate(self): if self.match_rate > 0: return self.match_rate self.match_rate = 0 self.img = Image.new('RGBA', self.size) draw = ImageDraw.Draw(self.img) draw.polygon([(0, 0), (0, 255), (255, 255), (255, 0)], fill=(255, 255, 255, 255)) for triangle in self.triangles: self.img = Image.alpha_composite(self.img, triangle.img_t or triangle.draw_it(self.size)) # 与下方代码功能相同,此版本便于理解但效率低 # pixels = [self.img.getpixel((x, y)) for x in range(0, self.size[0], 2) for y in range(0, self.size[1], 2)] # for i in range(0, min(len(pixels), len(self.target_pixels))): # delta_red = pixels[i][0] - self.target_pixels[i][0] # delta_green = pixels[i][1] - self.target_pixels[i][1] # delta_blue = pixels[i][2] - self.target_pixels[i][2] # self.match_rate += delta_red * delta_red + \ # delta_green * delta_green + \ # delta_blue * delta_blue arrs = [np.array(x) for x in list(self.img.split())] # 分解为RGBA四通道 for i in range(3): # 对RGB通道三个矩阵分别与目标图片相应通道作差取平方加和评估相似度 self.match_rate += np.sum(np.square(arrs[i]-self.target_pixels[i]))[0]
def update_parameter(x, dx, epoch, learning_rate, m, v, beta1, beta2, eps): """ Updates the parameter x using its gradient dx. :param x: The parameter to be updated. :param dx: The gradient of the parameter to be updated. :param epoch: The current training epoch. :param learning_rate: The learning rate of the network. Indicates the size of learning steps. :param m: The current momentum. :param v: The current velocity. :param beta1: Hyperparameter for the Adam parameter update. Recommended to be .9. :param beta2: Hyperparameter for the Adam parameter update. Recommended to be .999. :param eps: Hyperparameter for the Adam parameter update. Recommended to be 1e-8. :return: The updated parameter of the same type as x. """ m = beta1 * m + (1 - beta1) * dx mt = m / (1 - beta1**epoch) v = beta2 * v + (1 - beta2) * (np.square(dx)) vt = v / (1 - beta2**epoch) update = -learning_rate * mt / (np.sqrt(vt) + eps) #print(np.mean(update / x)) x += update # x += - learning_rate * dx return x, m, v
def garchLLH(y, par): h = garchSim(np.square(y), par) T = y.shape[0] llh = -0.5 * (T - 1) * math.log( 2 * math.pi) - 0.5 * np.sum(np.log(h) + (y / np.sqrt(h))**2) return llh[0]
def test_ufunc(): x = np.array([-1.2, 1.2]) np.absolute(x) np.absolute(1.2 + 1j) x = np.linspace(start=-10, stop=10, num=101) np.add(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.add(x1, x2) np.arccos([1, -1]) x = np.linspace(-1, 1, num=100) np.arccosh([np.e, 10.0]) np.arccosh(1) np.arcsin(0) np.arcsinh(np.array([np.e, 10.0])) np.arctan([0, 1]) np.pi / 4 x = np.linspace(-10, 10) x = np.array([-1, +1, +1, -1]) y = np.array([-1, -1, +1, +1]) np.arctan2(y, x) * 180 / np.pi np.arctan2([1., -1.], [0., 0.]) np.arctan2([0., 0., np.inf], [+0., -0., np.inf]) np.arctanh([0, -0.5]) np.bitwise_and(13, 17) np.bitwise_and(14, 13) # np.binary_repr(12) return str np.bitwise_and([14, 3], 13) np.bitwise_and([11, 7], [4, 25]) np.bitwise_and(np.array([2, 5, 255]), np.array([3, 14, 16])) np.bitwise_and([True, True], [False, True]) np.bitwise_or(13, 16) # np.binary_repr(29) np.bitwise_or(32, 2) np.bitwise_or([33, 4], 1) np.bitwise_or([33, 4], [1, 2]) np.bitwise_or(np.array([2, 5, 255]), np.array([4, 4, 4])) # np.array([2, 5, 255]) | np.array([4, 4, 4]) np.bitwise_or(np.array([2, 5, 255, 2147483647], dtype=np.int32), np.array([4, 4, 4, 2147483647], dtype=np.int32)) np.bitwise_or([True, True], [False, True]) np.bitwise_xor(13, 17) # np.binary_repr(28) np.bitwise_xor(31, 5) np.bitwise_xor([31, 3], 5) np.bitwise_xor([31, 3], [5, 6]) np.bitwise_xor([True, True], [False, True]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.ceil(a) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.trunc(a) np.cos(np.array([0, np.pi / 2, np.pi])) np.cosh(0) x = np.linspace(-4, 4, 1000) rad = np.arange(12.) * np.pi / 6 np.degrees(rad) out = np.zeros((rad.shape)) r = np.degrees(rad, out) # np.all(r == out) return bool np.rad2deg(np.pi / 2) np.divide(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.divide(2, 4) np.divide(2, 4.) np.equal([0, 1, 3], np.arange(3)) np.equal(1, np.ones(1)) x = np.linspace(-2 * np.pi, 2 * np.pi, 100) np.exp2([2, 3]) np.expm1(1e-10) np.exp(1e-10) - 1 np.fabs(-1) np.fabs([-1.2, 1.2]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.floor(a) np.floor_divide(7, 3) np.floor_divide([1., 2., 3., 4.], 2.5) np.fmod([-3, -2, -1, 1, 2, 3], 2) np.remainder([-3, -2, -1, 1, 2, 3], 2) np.fmod([5, 3], [2, 2.]) a = np.arange(-3, 3).reshape(3, 2) np.fmod(a, [2, 2]) np.greater([4, 2], [2, 2]) a = np.array([4, 2]) b = np.array([2, 2]) a > b np.greater_equal([4, 2, 1], [2, 2, 2]) np.hypot(3 * np.ones((3, 3)), 4 * np.ones((3, 3))) np.hypot(3 * np.ones((3, 3)), [4]) np.bitwise_not is np.invert np.invert(np.array([13], dtype=np.uint8)) # np.binary_repr(242, width=8) np.invert(np.array([13], dtype=np.uint16)) np.invert(np.array([13], dtype=np.int8)) # np.binary_repr(-14, width=8) np.invert(np.array([True, False])) # np.isfinite(1) # np.isfinite(0) # np.isfinite(np.nan) # np.isfinite(np.inf) # np.isfinite(np.NINF) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) np.isfinite(x, y) # np.isinf(np.inf) # np.isinf(np.nan) # np.isinf(np.NINF) # np.isinf([np.inf, -np.inf, 1.0, np.nan]) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) # np.isinf(x, y) # np.isnan(np.nan) # np.isnan(np.inf) # np.binary_repr(5) np.left_shift(5, 2) # np.binary_repr(20) np.left_shift(5, [1, 2, 3]) np.less([1, 2], [2, 2]) np.less_equal([4, 2, 1], [2, 2, 2]) x = np.array([0, 1, 2, 2**4]) xi = np.array([0 + 1.j, 1, 2 + 0.j, 4.j]) np.log2(xi) prob1 = np.log(1e-50) prob2 = np.log(2.5e-50) prob12 = np.logaddexp(prob1, prob2) prob12 np.exp(prob12) prob1 = np.log2(1e-50) prob2 = np.log2(2.5e-50) prob12 = np.logaddexp2(prob1, prob2) prob1, prob2, prob12 2**prob12 np.log1p(1e-99) np.log(1 + 1e-99) # np.logical_and(True, False) # np.logical_and([True, False], [False, False]) x = np.arange(5) # np.logical_and(x>1, x<4) # np.logical_not(3) # np.logical_not([True, False, 0, 1]) x = np.arange(5) # np.logical_not(x<3) # np.logical_or(True, False) # np.logical_or([True, False], [False, False]) x = np.arange(5) # np.logical_or(x < 1, x > 3) # np.logical_xor(True, False) # np.logical_xor([True, True, False, False], [True, False, True, False]) x = np.arange(5) # np.logical_xor(x < 1, x > 3) # np.logical_xor(0, np.eye(2)) np.maximum([2, 3, 4], [1, 5, 2]) # np.maximum([np.nan, 0, np.nan], [0, np.nan, np.nan]) # np.maximum(np.Inf, 1) np.minimum([2, 3, 4], [1, 5, 2]) # np.minimum([np.nan, 0, np.nan],[0, np.nan, np.nan]) # np.minimum(-np.Inf, 1) np.fmax([2, 3, 4], [1, 5, 2]) np.fmax(np.eye(2), [0.5, 2]) # np.fmax([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.fmin([2, 3, 4], [1, 5, 2]) np.fmin(np.eye(2), [0.5, 2]) # np.fmin([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.modf([0, 3.5]) np.modf(-0.5) np.multiply(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.multiply(x1, x2) np.negative([1., -1.]) np.not_equal([1., 2.], [1., 3.]) np.not_equal([1, 2], [[1, 3], [1, 4]]) x1 = range(6) np.power(x1, 3) x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0] np.power(x1, x2) x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]]) np.power(x1, x2) deg = np.arange(12.) * 30. np.radians(deg) out = np.zeros((deg.shape)) ret = np.radians(deg, out) ret is out np.deg2rad(180) np.reciprocal(2.) np.reciprocal([1, 2., 3.33]) np.remainder([4, 7], [2, 3]) np.remainder(np.arange(7), 5) # np.binary_repr(10) np.right_shift(10, 1) # np.binary_repr(5) np.right_shift(10, [1, 2, 3]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.rint(a) np.sign([-5., 4.5]) np.sign(0) # np.sign(5-2j) # np.signbit(-1.2) np.signbit(np.array([1, -2.3, 2.1])) np.copysign(1.3, -1) np.copysign([-1, 0, 1], -1.1) np.copysign([-1, 0, 1], np.arange(3) - 1) np.sin(np.pi / 2.) np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.) x = np.linspace(-np.pi, np.pi, 201) np.sinh(0) # np.sinh(np.pi*1j/2) np.sqrt([1, 4, 9]) np.sqrt([4, -1, -3 + 4J]) np.cbrt([1, 8, 27]) np.square([-1j, 1]) np.subtract(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.subtract(x1, x2) np.tan(np.array([-pi, pi / 2, pi])) np.tanh((0, np.pi * 1j, np.pi * 1j / 2)) x = np.arange(5) np.true_divide(x, 4) x = np.arange(9) y1, y2 = np.frexp(x) y1 * 2**y2 np.ldexp(5, np.arange(4)) x = np.arange(6) np.ldexp(*np.frexp(x))
for i in range(0, current_x.shape[0], 100): current_x_batch = current_x[i:i + 100] current_label_batch = np.expand_dims(current_label[i:i + 100], axis=1) layer_1 = current_x_batch.dot(w1) layer_1_act = tanh(layer_1) layer_2 = layer_1_act.dot(w2) layer_2_act = tanh(layer_2) layer_3 = layer_2_act.dot(w3) layer_3_act = sigmoid(layer_3) cost = np.square(current_label_batch - layer_3_act) / (current_x.shape[0] * 2) grad_3_part_1 = current_label_batch - layer_3_act grad_3_part_2 = d_sigmoid(layer_3) grad_3_part_3 = layer_2_act grad_3 = grad_3_part_3.T.dot(grad_3_part_1 * grad_3_part_2) grad_2_part_1 = (grad_3_part_1 * grad_3_part_2).dot(w3.T) grad_2_part_2 = d_tanh(layer_2) grad_2_part_3 = layer_1_act grad_2 = grad_2_part_3.T.dot(grad_2_part_1 * grad_2_part_2) grad_1_part_1 = (grad_2_part_1 * grad_2_part_2).dot(w2.T) grad_1_part_2 = d_tanh(layer_1) grad_1_part_3 = current_x_batch grad_1 = grad_1_part_3.T.dot(grad_1_part_1 * grad_1_part_2)
def test_ufunc(): x = np.array([-1.2, 1.2]) np.absolute(x) np.absolute(1.2 + 1j) x = np.linspace(start=-10, stop=10, num=101) np.add(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.add(x1, x2) np.arccos([1, -1]) x = np.linspace(-1, 1, num=100) np.arccosh([np.e, 10.0]) np.arccosh(1) np.arcsin(0) np.arcsinh(np.array([np.e, 10.0])) np.arctan([0, 1]) np.pi/4 x = np.linspace(-10, 10) x = np.array([-1, +1, +1, -1]) y = np.array([-1, -1, +1, +1]) np.arctan2(y, x) * 180 / np.pi np.arctan2([1., -1.], [0., 0.]) np.arctan2([0., 0., np.inf], [+0., -0., np.inf]) np.arctanh([0, -0.5]) np.bitwise_and(13, 17) np.bitwise_and(14, 13) # np.binary_repr(12) return str np.bitwise_and([14,3], 13) np.bitwise_and([11,7], [4,25]) np.bitwise_and(np.array([2,5,255]), np.array([3,14,16])) np.bitwise_and([True, True], [False, True]) np.bitwise_or(13, 16) # np.binary_repr(29) np.bitwise_or(32, 2) np.bitwise_or([33, 4], 1) np.bitwise_or([33, 4], [1, 2]) np.bitwise_or(np.array([2, 5, 255]), np.array([4, 4, 4])) # np.array([2, 5, 255]) | np.array([4, 4, 4]) np.bitwise_or(np.array([2, 5, 255, 2147483647], dtype=np.int32), np.array([4, 4, 4, 2147483647], dtype=np.int32)) np.bitwise_or([True, True], [False, True]) np.bitwise_xor(13, 17) # np.binary_repr(28) np.bitwise_xor(31, 5) np.bitwise_xor([31,3], 5) np.bitwise_xor([31,3], [5,6]) np.bitwise_xor([True, True], [False, True]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.ceil(a) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.trunc(a) np.cos(np.array([0, np.pi/2, np.pi])) np.cosh(0) x = np.linspace(-4, 4, 1000) rad = np.arange(12.)*np.pi/6 np.degrees(rad) out = np.zeros((rad.shape)) r = np.degrees(rad, out) # np.all(r == out) return bool np.rad2deg(np.pi/2) np.divide(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.divide(2, 4) np.divide(2, 4.) np.equal([0, 1, 3], np.arange(3)) np.equal(1, np.ones(1)) x = np.linspace(-2*np.pi, 2*np.pi, 100) np.exp2([2, 3]) np.expm1(1e-10) np.exp(1e-10) - 1 np.fabs(-1) np.fabs([-1.2, 1.2]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.floor(a) np.floor_divide(7,3) np.floor_divide([1., 2., 3., 4.], 2.5) np.fmod([-3, -2, -1, 1, 2, 3], 2) np.remainder([-3, -2, -1, 1, 2, 3], 2) np.fmod([5, 3], [2, 2.]) a = np.arange(-3, 3).reshape(3, 2) np.fmod(a, [2,2]) np.greater([4,2],[2,2]) a = np.array([4,2]) b = np.array([2,2]) a > b np.greater_equal([4, 2, 1], [2, 2, 2]) np.hypot(3*np.ones((3, 3)), 4*np.ones((3, 3))) np.hypot(3*np.ones((3, 3)), [4]) np.bitwise_not is np.invert np.invert(np.array([13], dtype=np.uint8)) # np.binary_repr(242, width=8) np.invert(np.array([13], dtype=np.uint16)) np.invert(np.array([13], dtype=np.int8)) # np.binary_repr(-14, width=8) np.invert(np.array([True, False])) # np.isfinite(1) # np.isfinite(0) # np.isfinite(np.nan) # np.isfinite(np.inf) # np.isfinite(np.NINF) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) np.isfinite(x, y) # np.isinf(np.inf) # np.isinf(np.nan) # np.isinf(np.NINF) # np.isinf([np.inf, -np.inf, 1.0, np.nan]) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) # np.isinf(x, y) # np.isnan(np.nan) # np.isnan(np.inf) # np.binary_repr(5) np.left_shift(5, 2) # np.binary_repr(20) np.left_shift(5, [1,2,3]) np.less([1, 2], [2, 2]) np.less_equal([4, 2, 1], [2, 2, 2]) x = np.array([0, 1, 2, 2**4]) xi = np.array([0+1.j, 1, 2+0.j, 4.j]) np.log2(xi) prob1 = np.log(1e-50) prob2 = np.log(2.5e-50) prob12 = np.logaddexp(prob1, prob2) prob12 np.exp(prob12) prob1 = np.log2(1e-50) prob2 = np.log2(2.5e-50) prob12 = np.logaddexp2(prob1, prob2) prob1, prob2, prob12 2**prob12 np.log1p(1e-99) np.log(1 + 1e-99) # np.logical_and(True, False) # np.logical_and([True, False], [False, False]) x = np.arange(5) # np.logical_and(x>1, x<4) # np.logical_not(3) # np.logical_not([True, False, 0, 1]) x = np.arange(5) # np.logical_not(x<3) # np.logical_or(True, False) # np.logical_or([True, False], [False, False]) x = np.arange(5) # np.logical_or(x < 1, x > 3) # np.logical_xor(True, False) # np.logical_xor([True, True, False, False], [True, False, True, False]) x = np.arange(5) # np.logical_xor(x < 1, x > 3) # np.logical_xor(0, np.eye(2)) np.maximum([2, 3, 4], [1, 5, 2]) # np.maximum([np.nan, 0, np.nan], [0, np.nan, np.nan]) # np.maximum(np.Inf, 1) np.minimum([2, 3, 4], [1, 5, 2]) # np.minimum([np.nan, 0, np.nan],[0, np.nan, np.nan]) # np.minimum(-np.Inf, 1) np.fmax([2, 3, 4], [1, 5, 2]) np.fmax(np.eye(2), [0.5, 2]) # np.fmax([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.fmin([2, 3, 4], [1, 5, 2]) np.fmin(np.eye(2), [0.5, 2]) # np.fmin([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.modf([0, 3.5]) np.modf(-0.5) np.multiply(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.multiply(x1, x2) np.negative([1.,-1.]) np.not_equal([1.,2.], [1., 3.]) np.not_equal([1, 2], [[1, 3],[1, 4]]) x1 = range(6) np.power(x1, 3) x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0] np.power(x1, x2) x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]]) np.power(x1, x2) deg = np.arange(12.) * 30. np.radians(deg) out = np.zeros((deg.shape)) ret = np.radians(deg, out) ret is out np.deg2rad(180) np.reciprocal(2.) np.reciprocal([1, 2., 3.33]) np.remainder([4, 7], [2, 3]) np.remainder(np.arange(7), 5) # np.binary_repr(10) np.right_shift(10, 1) # np.binary_repr(5) np.right_shift(10, [1,2,3]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.rint(a) np.sign([-5., 4.5]) np.sign(0) # np.sign(5-2j) # np.signbit(-1.2) np.signbit(np.array([1, -2.3, 2.1])) np.copysign(1.3, -1) np.copysign([-1, 0, 1], -1.1) np.copysign([-1, 0, 1], np.arange(3)-1) np.sin(np.pi/2.) np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180. ) x = np.linspace(-np.pi, np.pi, 201) np.sinh(0) # np.sinh(np.pi*1j/2) np.sqrt([1,4,9]) np.sqrt([4, -1, -3+4J]) np.cbrt([1,8,27]) np.square([-1j, 1]) np.subtract(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.subtract(x1, x2) np.tan(np.array([-pi,pi/2,pi])) np.tanh((0, np.pi*1j, np.pi*1j/2)) x = np.arange(5) np.true_divide(x, 4) x = np.arange(9) y1, y2 = np.frexp(x) y1 * 2**y2 np.ldexp(5, np.arange(4)) x = np.arange(6) np.ldexp(*np.frexp(x))