def TrainNetwork(sample, lebal): sample_num = len(sample) sample_len = len(sample[0]) import math out_num = 10 hid_num = 8 w1 = 0.2 * math.random.random((sample_len, hid_num)) - 0.1 w2 = 0.2 * math.random.random((hid_num, out_num)) - 0.1 hid_offset = math.zeros(hid_num) out_offset = math.zeros(out_num) input_learnrate = 0.2 hid_learnrate = 0.2 for i in range(0, len(sample)): t_label = math.zeros(out_num) t_label[label[i]] = 1 # 前向的过程 hid_value = math.dot(sample[i], w1) + hid_offset # 隐层的输入 hid_act = get(hid_value) # 隐层对应的输出 out_value = math.dot(hid_act, w2) + out_offset out_act = get(out_value) # 输出层最后的输出 # 后向过程 err = t_label - out_act out_delta = err * out_act * (1 - out_act) # 输出层的方向梯度方向 hid_delta = hid_act * (1 - hid_act) * math.dot(w2, out_delta) for j in range(0, out_num): w2[:, j] += hid_learnrate * out_delta[j] * hid_act for k in range(0, hid_num): w1[:, k] += input_learnrate * hid_delta[k] * sample[i] out_offset += hid_learnrate * out_delta # 阈值的更新 hid_offset += input_learnrate * hid_delta return w1, w2, hid_offset, out_offset
def crosspos(self, p0): x, y = p0 x0, y0 = view.to0plane(self.x, self.y, self.z) cx, cy = x - x0, y - y0 a = math.dot((cx, cy), self.dhat) / self.d0 b = self.dhatx * cy - self.dhaty * cx return a, b
def cvt_c1c2_using_body2inertial_Q_to_v2v3pa_tuple(Q, coord1, coord2): """Given Q and a position, returns v2, v3, V3PA tuple """ Vp_eci = Vector(1., 0., 0.) Vp_eci.set_xyz_from_angs(coord1, coord2) Vp_body_pt = Q.inv_cnvrt(Vp_eci) v2 = math.atan2(Vp_body_pt.y, Vp_body_pt.x) v3 = math.asin(unit_limit(Vp_body_pt.z)) V3_body = Vector(0., 0., 1.) V3_eci_pt = self.cnvrt(V3_body) NP_eci = Vector(0., 0., 1.) V_left = math.cross(NP_eci, Vp_eci) if V_left.length() > 0.: V_left = V_left / V_left.length() NP_in_plane = math.cross(Vp_eci, V_left) x = math.dot(V3_eci_pt, NP_in_plane) y = math.dot(V3_eci_pt, V_left) pa = math.atan2(y, x) if pa < 0.: pa += PI2 return (v2, v3, pa)
def polygon(self, f=1): theta = 30 if math.dot(self.pos, (C15, -S15)) < 0 else -60 theta = -60 R = math.R(math.radians(theta)) w, h = self.size w *= f h *= f sx, sy = self.pos ds = [R(p) for p in ((0, h), (w, 0), (w, -0.8), (-w, -0.8), (-w, 0))] return [(sx + dx, sy + dy) for dx, dy in ds]
def cvt_c1c2_using_body2inertial_Q_to_v2v3pa_tuple(Q, coord1, coord2): """Given Q and a position, returns v2, v3, V3PA tuple """ Vp_eci = Vector(1., 0., 0.) Vp_eci.set_xyz_from_angs(coord1, coord2) Vp_body_pt = Q.inv_cnvrt(Vp_eci) v2 = math.atan2(Vp_body_pt.y, Vp_body_pt.x) v3 = math.asin(unit_limit(Vp_body_pt.z)) V3_body = Vector(0., 0., 1.) V3_eci_pt = self.cnvrt(V3_body) NP_eci = Vector(0., 0., 1.) V_left = math.cross(NP_eci, Vp_eci) if V_left.length()>0.: V_left = V_left / V_left.length() NP_in_plane = math.cross(Vp_eci, V_left) x = math.dot(V3_eci_pt, NP_in_plane) y = math.dot(V3_eci_pt, V_left) pa = math.atan2(y, x) if pa < 0. : pa += PI2 return (v2, v3, pa)
def dof(j0, j1, jc=None): x0, y0 = ps[j0] x1, y1 = ps[j1] f = 1 if jc is not None: xc, yc = ps[jc] a = math.clamp( math.dot(math.norm((xc - x0, yc - y0)), math.norm((x1 - xc, y1 - yc))), -0.999, 0.999) f = 1 / math.cos(math.acos(a) / 2) return math.norm((x1 - x0, y1 - y0), w * f)
def constrain(self, pos, j): if pos[1] < 0.0001: pos = pos[0], 0.0001 if pos[0] < 0: pos = 0, pos[1] if math.dot(pos, (C30, -S30)) > 0: pos, _ = ptoline(pos, (S30, C30)) a = math.length(pos) if a > 1 - self.r: pos = math.norm(pos, 1 - self.r) return pos
def constrain(self, pos, j): if pos[1] < 0.0001: pos = pos[0], 0.0001 if pos[0] < self.width: pos = self.width, pos[1] if math.dot(math.norm(pos), (C30, -S30)) > -self.width: pos, _ = ptoline(pos, (S30, C30)) pos = pos[0] - self.width * C30, pos[1] + self.width * S30 a = math.length(pos) if a > 1 - self.width: pos = math.norm(pos, 1 - self.width) return pos
def cvt_v2v3_using_body2inertial_Q_to_c1c2pa_tuple(Q, v2, v3): """Given Q and v2, v3 gives pos on sky and V3 PA """ Vp_body = Vector(0., 0., 0.) Vp_body.set_xyz_from_angs(v2, v3) Vp_eci_pt = Q.cnvrt(Vp_body) coord1 = math.atan2(Vp_eci_pt.y, Vp_eci_pt.x) if coord1 < 0.: coord1 += PI2 coord2 = math.asin(unit_limit(Vp_eci_pt.z)) V3_body = Vector(0., 0., 1.) V3_eci_pt = Q.cnvrt(V3_body) NP_eci = Vector(0., 0., 1.) V_left = math.cross(NP_eci, Vp_eci_pt) if V_left.length() > 0.: V_left = V_left / V_left.length() NP_in_plane = math.cross(Vp_eci_pt, V_left) x = math.dot(V3_eci_pt, NP_in_plane) y = math.dot(V3_eci_pt, V_left) pa = math.atan2(y, x) if pa < 0.: pa += PI2 return (coord1, coord2, pa)
def cvt_v2v3_using_body2inertial_Q_to_c1c2pa_tuple(Q, v2, v3): """Given Q and v2, v3 gives pos on sky and V3 PA """ Vp_body = Vector(0., 0., 0.) Vp_body.set_xyz_from_angs(v2, v3) Vp_eci_pt = Q.cnvrt(Vp_body) coord1 = math.atan2(Vp_eci_pt.y, Vp_eci_pt.x) if coord1 < 0. : coord1 += PI2 coord2 = math.asin(unit_limit(Vp_eci_pt.z)) V3_body = Vector(0., 0., 1.) V3_eci_pt = Q.cnvrt(V3_body) NP_eci = Vector(0., 0., 1.) V_left = math.cross(NP_eci, Vp_eci_pt) if V_left.length()>0.: V_left = V_left/V_left.length() NP_in_plane = math.cross(Vp_eci_pt, V_left) x = math.dot(V3_eci_pt, NP_in_plane) y = math.dot(V3_eci_pt, V_left) pa = math.atan2(y, x) if pa < 0. : pa += PI2 return (coord1, coord2, pa)
def Test(): train_sample = LoadFile('mnist_train.mat') train_sample = train_sample / 256.0 train_label = LoadFile('mnist_train_labels.mat') test_sample = LoadFile('mnist_test.mat') test_sample = test_sample / 256.0 test_label = LoadFile('mnist_test_labels.mat') w1, w2, hid_offset, out_offset = TrainNetwork(train_sample, train_label) import math right = math.zeros(10) numbers = math.zeros(10) for i in test_label: numbers[i] += 1 print(numbers) for i in range(0, len(test_label)): hid_value = math.dot(test_sample[i], w1) + hid_offset hid_act = get(hid_value) out_value = math.dot(hid_act, w2) + out_offset out_act = get(out_value) if math.argmax(out_act) == test_label[i]: right[test_label[i]] += 1 print(right.sum() / len(test_label))
def ptoline(p, n): pproj = math.dot(p, n) nx, ny = n proj = nx * pproj, ny * pproj return proj, math.distance(proj, p)
def constrain(self, pos, j): a = math.clamp(math.dot(pos, (S15, C15)), self.width, 1 - self.width) return a * S15, a * C15