def gradient_check(network, sample_feature, sample_label): # 梯度检查 network_error = lambda vec1, vec2: \ 0.5 * np.reduce(lambda a, b: a + b, map(lambda v: (v[0] - v[1]) * (v[0] - v[1]), zip(vec1, vec2) ) ) # 获取网络在当前样本下每个链接的梯度 network.get_gradient(sample_feature, sample_label) # 对每个权重做梯度检查 for conn in network.connections.connections: # 获取指定链接的梯度 actual_gradient = conn.get_gradient() # 增加一个很小的值,计算网络的误差 epsilon = 0.0001 conn.weight += epsilon error1 = network_error(network.predict(sample_feature), sample_label) # 减去一个很小的值,计算网络的误差 conn.weeight -= 2 * epsilon error2 = network_error(network.predict(sample_feature), sample_label) # 计算期望梯度 expected_gradient = (error2 - error1) / (2 * epsilon) print("excepted gradient: \t%f\nactual gradient: \t%f" % (expected_gradient, actual_gradient))
def stretch(a): """ Performs a histogram stretch on a gdalnumeric array image. """ hist = histogram(a) im = array_to_image(a) lut = [] for b in range(0, len(hist), 256): # step size step = np.reduce(operator.add, hist[b:b + 256]) / 255 # create equalization lookup table n = 0 for i in range(256): lut.append(n / step) n = n + hist[i + b] im = im.point(lut) return image_to_array(im)
def calc_hidden_layer_delta(self): # 节点属于隐藏层,根据下式计算delta downstream_delta = np.reduce( lambda ret, conn: ret + conn.downstream_node.delta * conn.weight, self.downstream, 0.0) self.delta = self.output * (1 - self.output) * downstream_delta
def calc_output(self): # 计算节点的输出 output = np.reduce( lambda ret, conn: ret + conn.upstrem__node.output * conn.weight, self.upstream, 0) self.output = sigmoid(output)
def mean_square_error(vec1, vec2): return 0.5 * np.reduce( lambda a, b: a + b, map(lambda v: (v[0] - v[1]) * (v[0] - v[1]), zip(vec1, vec2)))
def denorm(self, vec): binary = map(lambda i: 1 if i > 0.5 else 0, vec) for i in range(len(self.mask)): binary[i] = binary[i] * self.mask[i] return np.reduce(lambda x, y: x + y, binary)
def mdot(*args): return np.reduce(np.dot, args)
def scf(basis): #read in dat file dat = [] f = open('test.txt', 'r') for line in f: dat.append(line) #read in nuclear energy E_nuc = float(dat[1]) #read in one-electron integrals S = numpy.zeros((7, 7)) for index in range(5, 33): i = int(dat[index][0]) - 1 j = int(dat[index][6]) - 1 S[i, j] = float(dat[index][9:28]) S[j, i] = S[i][j] #end T = numpy.zeros((7, 7)) for index in range(36, 64): i = int(dat[index][0]) - 1 j = int(dat[index][6]) - 1 T[i, j] = float(dat[index][9:28]) T[j, i] = T[i, j] #end V = numpy.zeros((7, 7)) for index in range(67, 95): i = int(dat[index][0]) - 1 j = int(dat[index][6]) - 1 V[i, j] = float(dat[index][9:28]) V[j, i] = V[i, j] #end H = T + V #read in two-electron integrals tei = numpy.zeros((2401)) for ind in range(98, 326): i = int(dat[ind][0]) j = int(dat[ind][6]) k = int(dat[ind][12]) l = int(dat[ind][18]) ii = i - 1 jj = j - 1 kk = k - 1 ll = l - 1 ij = int(ii * (ii + 1) / 2 + jj if (ii > jj) else jj * (jj + 1) / 2 + ii) kl = int(kk * (kk + 1) / 2 + ll if (kk > ll) else ll * (ll + 1) / 2 + kk) ijkl = int(ij * (ij + 1) / 2 + kl if (ij > kl) else kl * (kl + 1) / 2 + ij) tei[ijkl] = float(dat[ind][22:41]) #end #build orthogonalization matrix S_eig = numpy.linalg.eigh(S) S_evec = S_eig[1] S_eval_diag = S_eig[0] S_eval = numpy.zeros((7, 7)) for i in range(7): S_eval[i, i] = S_eval_diag[i] #end ortho = numpy.zeros((7, 7)) #ortho = S_evec*numpy.linalg.inv((scipy.linalg.sqrtm(S_eval)))*numpy.transpose(S_evec) ortho_tmp = numpy.matmul(S_evec, numpy.linalg.inv(scipy.linalg.sqrtm(S_eval))) ortho = numpy.matmul(ortho_tmp, numpy.transpose(S_evec)) #build initial Fock matrix fock_tmp = numpy.matmul(numpy.transpose(ortho), H) fock = numpy.matmul(fock_tmp, ortho) #initial iteration F_eig = numpy.linalg.eigh(fock) F_eval = F_eig[0] F_evec = F_eig[1] F_evec = F_evec[:, numpy.argsort(F_eval)] coeff = numpy.matmul(ortho, F_evec) density = numpy.zeros((7, 7)) for i in range(7): for j in range(i + 1): density[i, j] = numpy.dot(coeff[i, 0:4], coeff[j, 0:4]) density[j, i] = density[i, j] #end #end #no idea why, but this element is the only element wrong in the above density matrix #hack to fix density[4, 4] = 1.0 #initial scf energy E_elec = numpy.reduce(numpy.dot(density, H + fock)) E_tot = E_elec + E_nuc #first iteration of fock build fock_a = twoei_kl(fock, density, 7, tei) fock_a += H return fock_a