-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFederated_Learning
More file actions
126 lines (86 loc) · 3.08 KB
/
Copy pathFederated_Learning
File metadata and controls
126 lines (86 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
import gym
import torch
import random
from argparse import ArgumentParser
import os
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
from scipy.ndimage.filters import gaussian_filter1d
def arguments():
parser = ArgumentParser()
parser.add_argument('--env', default = 'Data_AVs')
return parser.parse_args()
def save(agent, rewards, args):
path = './runs/{}/'.format(args.env)
try:
os.makedirs(path)
except:
pass
torch.save(agent.q.state_dict(), os.path.join(path, 'model_state_dict'))
plt.cla()
plt.plot(rewards, c = 'r', alpha = 0.3)
plt.plot(gaussian_filter1d(rewards, sigma = 5), c = 'r', label = 'Rewards')
plt.xlabel('Episodes')
plt.ylabel('Cumulative reward')
plt.title('Double DQN: {}'.format(args.env))
plt.savefig(os.path.join(path, 'reward.png'))
pd.DataFrame(rewards, columns = ['Reward']).to_csv(os.path.join(path, 'rewards.csv'), index = False)
class AgentConfig:
def __init__(self,
epsilon_start = 1.,
epsilon_final = 0.01,
epsilon_decay = 8000,
gamma = 0.99,
lr = 1e-4,
target_net_update_freq = 1000,
memory_size = 100000,
batch_size = 128,
learning_starts = 5000,
max_frames = 10000000):
self.epsilon_start = epsilon_start
self.epsilon_final = epsilon_final
self.epsilon_decay = epsilon_decay
self.epsilon_by_frame = lambda i: self.epsilon_final + (self.epsilon_start - self.epsilon_final) * np.exp(-1. * i / self.epsilon_decay)
self.gamma =gamma
self.lr =lr
self.target_net_update_freq =target_net_update_freq
self.memory_size =memory_size
self.batch_size =batch_size
self.learning_starts = learning_starts
self.max_frames = max_frames
class ExperienceReplayMemory:
def __init__(self, capacity):
self.capacity = capacity
self.memory = []
def push(self, transition):
self.memory.append(transition)
if len(self.memory) > self.capacity:
del self.memory[0]
def sample(self, batch_size):
batch = random.sample(self.memory, batch_size)
states = []
actions = []
rewards = []
next_states = []
dones = []
for b in batch:
states.append(b[0])
actions.append(b[1])
rewards.append(b[2])
next_states.append(b[3])
dones.append(b[4])
return states, actions, rewards, next_states, dones
def __len__(self):
return len(self.memory)
class TensorEnv(gym.Wrapper):
def __init__(self, env_name):
super().__init__(gym.make(env_name))
def process(self, x):
return torch.tensor(x).reshape(1,-1).float()
def reset(self):
return self.process(super().reset())
def step(self, a):
ns, r, done, infos = super().step(a)
return self.process(ns), r, done, infos