-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_generators.py
More file actions
35 lines (25 loc) · 1.16 KB
/
Copy pathdata_generators.py
File metadata and controls
35 lines (25 loc) · 1.16 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
import numpy as np
import torch
class PeriodicLinearParameterFlip:
def __init__(self, period=20, num_total_components=20, num_flipping_components=5,
noise_level=0.0, seed=0):
self.period = period
self.num_total_components = num_total_components
self.num_flipping_components = num_flipping_components
self.noise_level = noise_level
self.random_state = np.random.RandomState(seed)
self.weights = np.zeros(num_total_components)
weight_sample = self.random_state.choice([-1, 1], num_flipping_components)
self.weights[:num_flipping_components] = weight_sample
self.time_step = 0
def sample_training_example(self):
x = self.random_state.randn(self.num_total_components)
y = np.dot(self.weights, x)
y += self.random_state.randn() * self.noise_level
self.time_step += 1
if self.time_step % self.period == 0:
self._flip_random_weight_components()
return x, y
def _flip_random_weight_components(self):
flip_component = self.random_state.choice(self.num_flipping_components)
self.weights[flip_component] *= -1