未验证 提交 740cfa94 编写于 作者: P piotrekobiIntel 提交者: GitHub

Add loss conversion from uint16 to float in ProgressBar class (#39231)

* Add loss conversion from uint16 to float in progressbar class

* Fix test coverage

* Actually fix coverage

* Fix format error
上级 a863b32e
......@@ -20,6 +20,7 @@ import os
import sys
import time
import numpy as np
import struct
from collections import namedtuple
__all__ = []
......@@ -79,6 +80,20 @@ class ProgressBar(object):
def update(self, current_num, values={}):
now = time.time()
def convert_uint16_to_float(in_list):
in_list = np.asarray(in_list)
out = np.vectorize(
lambda x: struct.unpack('<f', struct.pack('<I', x << 16))[0],
otypes=[np.float32])(in_list.flat)
return np.reshape(out, in_list.shape)
for i, (k, val) in enumerate(values):
if k == "loss":
val = val if isinstance(val, list) or isinstance(
val, np.ndarray) else [val]
if isinstance(val[0], np.uint16):
values[i] = ("loss", list(convert_uint16_to_float(val)))
if current_num:
time_per_unit = (now - self._start) / current_num
else:
......
......@@ -41,6 +41,7 @@ class TestProgressBar(unittest.TestCase):
progbar.update(1, [['loss', 1e-4]])
progbar.update(1, [['loss', np.array([1.])]])
progbar.update(1, [['loss', np.array([1e-4])]])
progbar.update(1, [['loss', np.array([1]).astype(np.uint16)]])
progbar.start()
progbar.update(0, values)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册