Skip to content

Fix mario_rl_tutorial.py #2649

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

Merged
merged 3 commits into from
Nov 6, 2023
Merged
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
32 changes: 18 additions & 14 deletions intermediate_source/mario_rl_tutorial.py
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@
import numpy as np
from pathlib import Path
from collections import deque
import random, datetime, os, copy
import random, datetime, os

# Gym is an OpenAI toolkit for RL
import gym
@@ -424,20 +424,10 @@ def __init__(self, input_dim, output_dim):
if w != 84:
raise ValueError(f"Expecting input width: 84, got: {w}")

self.online = 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),
nn.ReLU(),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1),
nn.ReLU(),
nn.Flatten(),
nn.Linear(3136, 512),
nn.ReLU(),
nn.Linear(512, output_dim),
)
self.online = self.__build_cnn(c, output_dim)

self.target = copy.deepcopy(self.online)
self.target = self.__build_cnn(c, output_dim)
self.target.load_state_dict(self.online.state_dict())

# Q_target parameters are frozen.
for p in self.target.parameters():
@@ -449,6 +439,20 @@ def forward(self, input, model):
elif model == "target":
return self.target(input)

def __build_cnn(self, c, output_dim):
return 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),
nn.ReLU(),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1),
nn.ReLU(),
nn.Flatten(),
nn.Linear(3136, 512),
nn.ReLU(),
nn.Linear(512, output_dim),
)


######################################################################
# TD Estimate & TD Target