Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
apollon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Blaß, Michael
apollon
Commits
292fd4c6
Commit
292fd4c6
authored
4 years ago
by
Blaß, Michael
Browse files
Options
Downloads
Patches
Plain Diff
Updated utilities.sample_stm.
parent
080d7bf3
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/apollon/som/utilities.py
+17
-17
17 additions, 17 deletions
src/apollon/som/utilities.py
with
17 additions
and
17 deletions
src/apollon/som/utilities.py
+
17
−
17
View file @
292fd4c6
...
...
@@ -159,39 +159,39 @@ def sample_rnd(data: Array, shape: Shape) -> Array:
np
.
random
.
uniform
(
*
data_limits
[
1
],
n_units
)))
def
sample_stm
(
n_features
,
n_units
):
"""
Initialize the weights with stochastic matrices.
def
sample_stm
(
data
:
Array
,
shape
:
Shape
):
"""
Compute initial SOM weights by sampling stochastic matrices from
Dirichlet distribution.
The rows of each n by n stochastic matrix are sampes drawn from the
Dirichlet distribution, where n is the number of rows and cols of the
matrix. The diagonal elemets of the matrices are set to twice the
probability of the remaining elements.
The square root n of the weight vectors
'
size must be element of the
natural numbers, so that the weight vector is reshapeable to a square
matrix.
The square root of the weight vectors
'
size must be a real integer.
Args:
n_features: Number of features in each vector
.
n_units: Number of units on the
SOM.
data: Input data set
.
shape: Shape of
SOM.
Returns:
Two-dimensional array of shape (n_units, n_features), in which each
row is a flattened random stochastic matrix.
Array of SOM weights.
Notes:
Each row of the output array is to be considered a flattened
stochastic matrix, such that each ``N = sqrt(data.shape[1])`` values
are a discrete probability distribution forming the ``N``th row of
the matrix.
"""
# check for square matrix
n_rows
=
np
.
sqrt
(
n_features
)
n_rows
=
np
.
sqrt
(
data
.
shape
[
1
])
if
bool
(
n_rows
-
int
(
n_rows
)):
msg
=
(
f
'
Weight vector
(len=
{
n_features
}
)
is not
'
msg
=
(
f
'
Weight vector
with
{
n_rows
}
elements
is not
'
'
reshapeable to square matrix.
'
)
raise
ValueError
(
msg
)
else
:
n_rows
=
int
(
n_rows
)
# set alpha
n_rows
=
int
(
n_rows
)
n_units
=
np
.
prod
(
shape
)
alpha
=
np
.
full
((
n_rows
,
n_rows
),
500
)
np
.
fill_diagonal
(
alpha
,
1000
)
# sample from dirichlet distributions
st_matrix
=
np
.
hstack
([
_stats
.
dirichlet
.
rvs
(
alpha
=
a
,
size
=
n_units
)
for
a
in
alpha
])
return
st_matrix
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment