-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
33 lines (26 loc) · 867 Bytes
/
utils.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
import os
import random
import numpy as np
#initialize the weighs of the network for Convolutional layers and batchnorm layers
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1 and classname.find('Conv2d') == -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1 and classname.find(
'BatchNorm2d') == -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
class AverageValueMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0.0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count