问题描述
所以我分配了Stoachastic梯度像样的代码,基本上我发现从多个向量中随机采样同时保持顺序不变是一个问题。我的代码如下:
import numpy as np
import matplotlib.pyplot as plt
import random
x = np.array([0.,0.,100.,300.,900.,900.])
y = np.array([0.,1.,1.])
def f(b0,b1,x,y):
vec = [y[i]*np.log(1/(1+np.exp(-b0-b1*x[i]))) + (1-y[i])*np.log(1 - (1/(1+np.exp(-b0-b1*x[i])))) for i in range(len(y))]
return sum(vec)
def dervf0(b0,y):
vec = [-y[i] + (1/(1+np.exp(-b0-b1*x[i]))) for i in range(len(y))]
return sum(vec)
def dervf1(b0,y):
vec = [-x[i]*(y[i]-(1/(1+np.exp(-b0-b1*x[i])))) for i in range(len(y))]
return sum(vec)
def SGD(v,y,tol,maxiter):
x = #random selection
y= #random selection
for i in range(maxiter):
theta_new = v - 0.001*np.array(
[dervf0(v[0],v[1],y),dervf1(v[0],y)])
if np.linalg.norm(theta_new - v) < tol:
break
else:
v = theta_new
#print('i\t{}\tv\t{}\ttheta_new\t{}'.format(i,v,theta_new))
return theta_new,i
如您所见,我有2个向量,x和y,它们是链接在一起的,例如x [0]是给我们y [0] = 0的实验。在这里,无结构地随机采样是没有意义的在我看来。我正在努力做的是在SGD函数中,我想要x的n点和y的n点,但结构正确!任何帮助表示赞赏!
Y
解决方法
您可以使用以下内容获取要采样的索引列表-
import random
x = ['This','is','a','random','sampling','example']
n = len(x)
k = 5
indices_to_sample = sorted(random.sample(range(n),k)) # Chooses k out of n indices and sorts them
for i in indices_to_sample:
print(x[i]) # Gets x at index i
更多信息,请访问random.sample docs