implementation becomes just one line in the NumPy version. See
nRows, nCols = size
vecLen = inputs.shape
somap = numpy.random.rand(nRows, nCols, vecLen)
influence = numpy.dstack([spread]*vecLen) # One for each feature
infWidth = (len(influence)-1) // 2
makeMesh = numpy.ix_ # Ugly
for s in range(steps):
decay = numpy.exp(-s/float(steps))
for vector in inputs:
diff = somap-vector
diff2 = diff*diff
dist2 = diff2.sum(axis=2)
index = dist2.argmin()
row = index // nRows
col = index % nRows
rows = [x % nRows for x in range(row-infWidth, row+1+infWidth)]
cols = [y % nCols for y in range(col-infWidth, col+1+infWidth)]
mesh = makeMesh(rows,cols)
somap[mesh] -= diff[mesh] * influence * decay
return somap
Do'stlaringiz bilan baham: