-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlive_plot.py
62 lines (51 loc) · 2.06 KB
/
live_plot.py
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
"""
Authors : inzapp
Github url : https://github.com/inzapp/sigmoid-classifier
Copyright 2021 inzapp Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"),
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import numpy as np
from matplotlib import pyplot as plt
class LivePlot:
def __init__(self, iterations, interval=20, mean=10, y_min=0.0, y_max=0.2, legend='loss'):
plt.style.use(['dark_background'])
self.fig, self.ax = plt.subplots()
pad = ((y_max - y_min) * 0.05)
self.interval = interval
self.mean = mean
self.y_min = y_min - pad
self.y_max = y_max + pad
self.ax.set_ylim(self.y_min, self.y_max)
self.data = np.array([None for _ in range(iterations)], dtype=np.float32)
self.values, = self.ax.plot(np.random.rand(iterations))
self.recent_values = []
self.interval_count = 0
self.index = 0
plt.xlabel('Iteration')
plt.legend([legend])
plt.tight_layout(pad=0.5)
def update(self, val):
if val < self.y_min:
val = self.y_min * 0.99
elif val > self.y_max:
val = self.y_max * 0.99
self.data[self.index] = self.get_recent_avg_value(val)
self.index += 1
self.interval_count += 1
if self.interval_count == self.interval:
self.interval_count = 0
self.values.set_ydata(self.data)
plt.pause(1e-9)
def get_recent_avg_value(self, val):
if len(self.recent_values) > self.mean:
self.recent_values.pop(0)
self.recent_values.append(val)
return np.mean(self.recent_values)