-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
69 lines (51 loc) · 1.37 KB
/
utility.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
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
Created on December 16, 2015
@author: pascal leimer
"""
from math import *
import numpy as np
import numpy.random as rand
import sys
vsign = np.vectorize(np.sign)
def vectorsignum(array):
for i in range(len(array)):
su = np.sum(array[i])
array[i] = np.sign(su) if su != 0 else 0
return array
def zStep(Z):
if Z < 1.:
return 1.
else:
return Z
def unitStep(x):
if x == 0:
return 0.
else:
return 0.5 * (np.sign(x) + 1)
vunitStep = np.vectorize(unitStep)
def flatten(array):
return [item for sublist in array for item in sublist]
def sigmoid(x, a, beta):
try:
return 1. / (1. + exp(-(x - a) / beta))
except:
sys.exit('error sigmoid: ' + str(x) + ', '+ str(a) + ', ' + str(beta))
vsigmoid = np.vectorize(sigmoid)
def dsigmoid(x, a, beta):
try:
return (1. / beta) * exp(-(x - a) / beta) / (1 + exp(-(x - a) / beta))**2
except:
sys.exit('error dsigmoid: ' + str(x) + ', '+ str(a) + ', ' + str(beta))
vdsigmoid = np.vectorize(dsigmoid)
def lowpass(tau, old, new):
if tau == 0:
return new
return old * exp(-1. / tau) + new * (1. - exp(-1. / tau))
vlowpass = np.vectorize(lowpass)
def addnoise(a, sigma):
if sigma == 0:
return a
else:
return a + rand.normal(0, sigma)
vaddnoise = np.vectorize(addnoise)