-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_approximator.py
More file actions
169 lines (123 loc) · 5.87 KB
/
function_approximator.py
File metadata and controls
169 lines (123 loc) · 5.87 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
Author: Muhamed Cicak
"""
import random
from abc import ABC, abstractmethod
from typing import Callable, Generic, List, TypeVar
import numpy as np
S = TypeVar('S')
A = TypeVar('A')
class FunctionApproximator(Generic[S, A], ABC):
@abstractmethod
def predict(self, state: S, action: A) -> float:
pass
@abstractmethod
def gradient(self, state: S, action: A) -> List[float]:
pass
@abstractmethod
def update(self, state: S, action: A, error: float, step_size: float) -> None:
pass
@abstractmethod
def get_weights(self) -> List[float]:
pass
@abstractmethod
def set_weights(self, weights: List[float]) -> None:
pass
class LinearFunctionApproximator(FunctionApproximator[S, A]):
def __init__(self, feature_dim: int, feature_extractor: Callable[[S, A], List[float]]):
self.weights = np.zeros(feature_dim)
self.feature_extractor = feature_extractor
for i in range(feature_dim):
self.weights[i] = random.uniform(-0.1, 0.1)
def predict(self, state: S, action: A) -> float:
features = np.array(self.feature_extractor(state, action))
return float(np.dot(self.weights, features))
def gradient(self, state: S, action: A) -> List[float]:
return self.feature_extractor(state, action)
def update(self, state: S, action: A, error: float, step_size: float) -> None:
features = np.array(self.feature_extractor(state, action))
self.weights += step_size * error * features
def get_weights(self) -> List[float]:
return self.weights.tolist()
def set_weights(self, weights: List[float]) -> None:
if len(weights) != len(self.weights):
raise ValueError(f"Expected weights of length {len(self.weights)}, got {len(weights)}")
self.weights = np.array(weights)
class NeuralNetworkFunctionApproximator(FunctionApproximator[S, A]):
def __init__(self, feature_extractor: Callable[[S, A], List[float]],
architecture: List[int]):
if len(architecture) < 2:
raise ValueError("Network architecture must have at least input and output layers")
self.feature_extractor = feature_extractor
self.layer_sizes = architecture
self.weights = []
for i in range(len(architecture) - 1):
input_size = architecture[i]
output_size = architecture[i + 1]
for _ in range(output_size):
self.weights.append(random.uniform(-0.1, 0.1))
for _ in range(output_size * input_size):
self.weights.append(random.uniform(-0.1, 0.1))
def _forward(self, features: List[float]) -> List[List[float]]:
activations = [features]
weight_idx = 0
for l in range(len(self.layer_sizes) - 1):
input_size = self.layer_sizes[l]
output_size = self.layer_sizes[l + 1]
layer_output = [0.0] * output_size
for j in range(output_size):
layer_output[j] = self.weights[weight_idx]
weight_idx += 1
for i in range(input_size):
layer_output[j] += self.weights[weight_idx] * activations[-1][i]
weight_idx += 1
if l < len(self.layer_sizes) - 2:
layer_output[j] = max(0.0, layer_output[j])
activations.append(layer_output)
return activations
def predict(self, state: S, action: A) -> float:
features = self.feature_extractor(state, action)
activations = self._forward(features)
return activations[-1][0]
def gradient(self, state: S, action: A) -> List[float]:
features = self.feature_extractor(state, action)
activations = self._forward(features)
gradients = [0.0] * len(self.weights)
deltas = [[] for _ in range(len(self.layer_sizes))]
deltas[-1] = [1.0]
for l in range(len(self.layer_sizes) - 2, -1, -1):
current_size = self.layer_sizes[l]
next_size = self.layer_sizes[l + 1]
deltas[l] = [0.0] * current_size
weight_idx = 0
for i in range(l):
weight_idx += self.layer_sizes[i + 1] * (self.layer_sizes[i] + 1)
weight_idx += next_size
for i in range(current_size):
for j in range(next_size):
weight = self.weights[weight_idx + j * current_size + i]
deltas[l][i] += weight * deltas[l + 1][j]
if l > 0:
if activations[l][i] <= 0:
deltas[l][i] = 0.0
weight_idx = 0
for l in range(len(self.layer_sizes) - 1):
input_size = self.layer_sizes[l]
output_size = self.layer_sizes[l + 1]
for j in range(output_size):
gradients[weight_idx] = deltas[l + 1][j]
weight_idx += 1
for i in range(input_size):
gradients[weight_idx] = deltas[l + 1][j] * activations[l][i]
weight_idx += 1
return gradients
def update(self, state: S, action: A, error: float, step_size: float) -> None:
grads = self.gradient(state, action)
for i in range(len(self.weights)):
self.weights[i] += step_size * error * grads[i]
def get_weights(self) -> List[float]:
return self.weights
def set_weights(self, weights: List[float]) -> None:
if len(weights) != len(self.weights):
raise ValueError(f"Expected weights of length {len(self.weights)}, got {len(weights)}")
self.weights = weights