如何在函数调用之间维护变量值



我正在构建一个用于填充缓冲区的函数,该函数被调用多次。我声明rep_counter以在每次传递后保持其值,但当我再次调用它时,它会重置为0。我尝试在def __init__中声明它为self.rep_counter,尝试将它声明为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

在等待注释中指定的最小完整工作示例时,这里是可能的建议。

您可以将其声明为全局的,然后在函数内部使用前将其引用为global rep_counter

您可以将它声明为任何方法中的实例变量(如果是__init__则更好)self.rep_counter

您可以将其声明为类变量(其值在所有实例之间共享):

class AI:
rep_counter

并称之为CCD_ 8。

如果我们谈论的是纯函数,您可以使用一种方法来调用C/C++静态关键字:

def myFunction():
...
myFunction.NumberOfCalls += 1
myFunction.NumberOfCalls = 0

最新更新