Skip to content

updated the code to avoid deepcopy() #2648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions intermediate_source/mario_rl_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,23 @@ def __init__(self, input_dim, output_dim):
if w != 84:
raise ValueError(f"Expecting input width: 84, got: {w}")

self.online = nn.Sequential(
self.online = self._build_cnn(input_dim, output_dim)
self.target = self._build_cnn(input_dim, output_dim)

# Q_target parameters are frozen.
for p in self.target.parameters():
p.requires_grad = False

def forward(self, input, model):
if model == "online":
return self.online(input)
elif model == "target":
return self.target(input)

def _build_cnn(self, input_dim, output_dim):
c, _, _ = input_dim

cnn = nn.Sequential(
nn.Conv2d(in_channels=c, out_channels=32, kernel_size=8, stride=4),
nn.ReLU(),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=4, stride=2),
Expand All @@ -437,17 +453,7 @@ def __init__(self, input_dim, output_dim):
nn.Linear(512, output_dim),
)

self.target = copy.deepcopy(self.online)

# Q_target parameters are frozen.
for p in self.target.parameters():
p.requires_grad = False

def forward(self, input, model):
if model == "online":
return self.online(input)
elif model == "target":
return self.target(input)
return cnn


######################################################################
Expand Down