我写了以下代码。非常奇怪的事情正在发生。我有2个变量,当我打印它们时,得到的值分别为sums[d_index][k]=[0 0]
和rewards[k]=[1]
。因此,当我为sums[d_index][k] = sums[d_index][k]+rewards[k]
执行k=0
时,我应该期望得到sums[d_index][k]=[1 0]
。但是出于某些荒谬的原因,我得到了sums[d_index][k]=[0.2 0]
。我不知道这到底怎么可能。为什么会发生这种情况,我该如何解决?
我已用注释#HERE!!!!
import numpy as np
import math
e = 0.1
np.random.seed(2)
#Initializing the parameters of the bernoulli distributions randomly
p = np.random.rand(1,2)[0]
#>>>>>>>>>>> p = np.array([ 0.26363424, 0.70255294])
suboptimality_gap = np.max(p)-p
print p
powers = [1]
cumulative_regret = np.zeros((len(powers),1,10))
for round_number in range(1):
#Initializing the arrays to store the estimate and sum of rewards, and count of each action
estimates = np.zeros((len(powers),2))
estimates[:,0] = np.random.binomial(1, p[0], 1)
estimates[:,1] = np.random.binomial(1, p[1], 1)
counts = np.ones((len(powers),2))
sums = estimates[:]
#Updating estimates for action at time t>K=2
for t in range(1,10):
rewards = np.array([np.random.binomial(1, p[0], 1),np.random.binomial(1, p[1], 1)])
for d_index,d in enumerate([1./(t**power) for power in powers]):
#print (np.asarray([(estimates[d_index][i]+((2*math.log(1/d))/(counts[d_index][i]))**0.5) for i in [0,1]]))
k = np.argmax(np.asarray([(estimates[d_index][i]+((2*math.log(1/d))/(counts[d_index][i]))**0.5) for i in [0,1]]))
counts[d_index][k] = counts[d_index][k]+1
print "rewards=",rewards[k]
print "sums=",sums[d_index]
sums[d_index][k] = sums[d_index][k]+rewards[k] #HERE!!!!
estimates[d_index] = np.true_divide(sums[d_index], counts[d_index])
cumulative_regret[d_index][round_number][t]=cumulative_regret[d_index][round_number][t-1]+suboptimality_gap[k]
#print counts
输出:
[ 0.4359949 0.02592623]
rewards= 0
sums= [ 0. 0.]
rewards= 0
sums= [ 0. 0.]
rewards= 0
sums= [ 0. 0.]
rewards= 0
sums= [ 0. 0.]
rewards= 0
sums= [ 0. 0.]
rewards= 0
sums= [ 0. 0.]
rewards= 1
sums= [ 0. 0.]
rewards= 1
sums= [ 0.2 0. ]
rewards= 0
sums= [ 0.2 0. ]
很抱歉,我的代码是没有组织的。但这是因为最近一个小时我一直在尝试调试问题。
答案 0 :(得分:1)
如您的问题注释中所述,sums = estimates
不会创建数组的新副本,只会创建指向原始对象的新引用,这可能会导致混乱。要获得理想的结果,您可以使用:
sums = estimates.copy()