python – Get stacked game status in NHWC format

After reading this, I decided to change my DQN code from the keras library to the tf.keras library (the code is in this repository) and my original code used the NCHW format, since it was faster with the GPUs. As I also need to run CPUs, I found that it only worked with the NHWC format in my tf version (1.12 with python 3.7, compiled with AVX2 marks).

I redesigned my code and it is working, but I stack it in the NCHW format and then I have to transpose it to each function call, which is expensive.

Minimum work code

In this snippet, there is a list of & # 39; frames & # 39; with the size of & # 39; self.nb_frames & # 39 ;, which will contain the 10.10 states. Then I expanded the borders and transposed them, returning the list in NHWC format.

Class agent:

def __init __ (self):
"" "Initialize the agent with the given attributes." ""
self.frames = []
    self.nb_frames = 4

def get_game_data (self, state):
"" "Create a list with 4 frames and add them to each one." ""
frame = state

if self.frames is None:
self.frames = [frame] * self.nb_frames
plus:
self.frames.append (frame)
self.frames.pop (0)

# of (4, 10, 10) to (1, 4, 10, 10)
expansion_frames = np.expand_dims (self.frames, 0)

# Of (1, 4, 10, 10) to (1, 10, 10, 4) | NCHW -> NHWC
expanded_frames = np.transpose (expanded_frames, [0, 3, 2, 1])

back expanded_frames


board_size = 10
state = np.zeros ((self.board_size, self.board_size))
agent = Agent ()
stacked_state = agent.get_game_data (status)

Is there a better option to create the list directly in the format (10, 10, 4)? Does my code follow the best practices?