def loss_fun(p, x, y):
a, b, c, d = p
m = x.shape[0]
J = ((1+a np.exp(-b (x-c)))/(1-a np.exp(-b (x-c))) * d - y) ** 2
J = sum(J)/m
return J
def func(p):
a, b, c, d = p
return loss_fun(p, x, y)
x = np.arange(1, 201)
result = []
for i in range(Array.shape[1]):
y = A[:, i]
ga = GA(func=func, n_dim=4, size_pop=50, max_iter=800, prob_mut=0.001, precision=1e-7)
weight, f = ga.run()
result.append(weight)
老师您好,我想请问下,如何对一个多个循环做优化,例如对下面得Array里头每一列都做优化(因为Array的列有几千列),如何加速? 谢谢老师!
def loss_fun(p, x, y): a, b, c, d = p m = x.shape[0] J = ((1+a np.exp(-b (x-c)))/(1-a np.exp(-b (x-c))) * d - y) ** 2 J = sum(J)/m return J
def func(p): a, b, c, d = p return loss_fun(p, x, y)
x = np.arange(1, 201) result = [] for i in range(Array.shape[1]):
y = A[:, i] ga = GA(func=func, n_dim=4, size_pop=50, max_iter=800, prob_mut=0.001, precision=1e-7) weight, f = ga.run() result.append(weight)