Esempio n. 1
0
def load_model(filename):
    model = DQN()

    checkpoint = torch.load(filename)
    model.load_state_dict(checkpoint['state_dict'])

    return model
Esempio n. 2
0
    """
    def __init__(self, size):
        self.size = size
        self.memory = deque(maxlen=self.size)

    def update(self, SARS):
        self.memory.append(SARS)

    def sample(self, batch_size):
        return zip(*random.sample(self.memory, batch_size))


r_memory = ReplayMemory(memory_size)
agent = DQN(12, 12, 16)
target = DQN(12, 12, 16)
target.load_state_dict(agent.state_dict())
optimizer = Adam(agent.parameters())


def update_target():
    if len(r_memory.memory) < batch_size:
        return
    observation, action, reward, observation_next, done = r_memory.sample(
        batch_size)
    observations = torch.cat(observation)
    observation_next = torch.cat(observation_next)
    actions = index_action(torch.LongTensor(action))
    rewards = torch.LongTensor(reward)
    done = torch.FloatTensor(done)
    q_values = agent(observations)
    p_q_values_next = agent(observation_next)