我正在构建一个函数来填充缓冲区,这被多次调用。我声明rep_counter
来保持每次通过后的值,但是当我再次调用它时,它会重置为0。我尝试在self.rep_counter
内将其声明为def __init__
,并尝试声明它作为global
函数,但没有任何运气。我该怎么做?这是我的实际代码:
import gym
import cv2
import numpy as np
from collections import deque
env = gym.make('SpaceInvaders-v0')
env.reset()
action_size = env.action_space.n
state_size = env.observation_space.shape
steps = 0
step_counter = 0
total_reward = 0
i = 0
i_update = i
episodes = 100010
img_batch_size = 4
img_batch = []
state1, reward1, _, _ = env.step(0)
batch_shape = (1,80,80,4)
img_batch = [state1,state1,state1,state1]
exp_batch = []
rep_batch = []
rep_batch_size = 4
rep_counter = 0
memory_length = 2
memory = deque((), maxlen = memory_length)
rep_batch = deque((), maxlen = rep_batch_size)
class AI:
def replay(self, exp_batch, rep_counter):
if rep_counter < rep_batch_size:
rep_counter += 1
rep_batch.append(exp_batch)
else:
if len(memory) < memory_length:
memory.append(rep_batch)
else:
memory.popleft()
memory.append(rep_batch)
rep_batch.popleft()
rep_batch.append(exp_batch)
rep_counter = 0
def buffer_processing(img_batch):
processed_batch = [cv2.resize(cv2.cvtColor(x,cv2.COLOR_RGB2GRAY),(80,80)) for x in img_batch]
processed_batch = [[x/255.0]for x in processed_batch]
processed_batch = np.asarray(processed_batch)
processed_batch.shape = batch_shape
return processed_batch
a = buffer_processing(img_batch)
for i in range (episodes):
total_reward = 0
for steps in range (10000):
action = env.action_space.sample()
next_state, reward, done, _ = env.step(action)
total_reward += reward
state = next_state
exp_batch = (state, action, next_state, reward, done)
agent = AI()
agent.replay(exp_batch, rep_counter)
exp_batch = []
step_counter += 1
答案 0 :(得分:1)
在等待此处评论中指定的最小的完整工作示例时,可能是建议。
您可以全局声明它,然后在函数内部将其引用为global rep_counter
,然后再使用。
您可以将其声明为任何方法内的实例变量(最好是__init__
)为self.rep_counter
。
您可以将其声明为类变量(其值在所有实例之间共享):
class AI:
rep_counter
并命名为AI.rep_counter
。
如果我们在谈论纯函数,则可以使用一种方法来调用C / C ++静态关键字:
def myFunction():
...
myFunction.NumberOfCalls += 1
myFunction.NumberOfCalls = 0