Skip to content

Commit

Permalink
Added runPcaOnXUsing: method for PMTSNE
Browse files Browse the repository at this point in the history
- Added corresponding test for new method.
- Renamed `ndims` to `outputDims`
- Renamed `initialdims` to `initialDims`
- Added accessor `x`
  • Loading branch information
AtharvaKhare committed May 17, 2019
1 parent 21cea90 commit bfb017a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
70 changes: 40 additions & 30 deletions src/Math-TSNE/PMTSNE.class.st
Expand Up @@ -9,8 +9,8 @@ Class {
#name : #PMTSNE,
#superclass : #Object,
#instVars : [
'ndims',
'initialdims',
'outputDims',
'initialDims',
'perplexity',
'x',
'y',
Expand Down Expand Up @@ -122,17 +122,17 @@ PMTSNE >> epsilon: aFloat [
epsilon := aFloat
]

{ #category : #'as yet unclassified' }
{ #category : #accessing }
PMTSNE >> initialDims [
^initialdims
^ initialDims
]

{ #category : #'as yet unclassified' }
{ #category : #accessing }
PMTSNE >> initialDims: aFloat [
initialdims := aFloat
initialDims := aFloat
]

{ #category : #'as yet unclassified' }
{ #category : #accessing }
PMTSNE >> initialDimsDefaultValue [
^ 50
]
Expand All @@ -150,8 +150,8 @@ PMTSNE >> initialize [
{ #category : #initialization }
PMTSNE >> initializeUninitializedParameters [
perplexity ifNil: [ perplexity := self perplexityDefaultValue ].
ndims ifNil: [ ndims := self ndimsDefaultValue ].
initialdims ifNil: [ initialdims := self initialDimsDefaultValue ]
outputDims ifNil: [ outputDims := self outputDimsDefaultValue ].
initialDims ifNil: [ initialDims := self initialDimsDefaultValue ]
]

{ #category : #initialization }
Expand All @@ -161,7 +161,7 @@ PMTSNE >> initializeYWithRandomValues [

| a b rows columns d |
rows := x dimension x.
columns := ndims.
columns := outputDims.
d := PMNormalDistribution new:0 sigma: 1.
a := (1 to: rows)
collect: [ :row |
Expand All @@ -172,12 +172,17 @@ PMTSNE >> initializeYWithRandomValues [
]

{ #category : #accessing }
PMTSNE >> ndims [
^ ndims
PMTSNE >> outputDims [
^ outputDims
]

{ #category : #'as yet unclassified' }
PMTSNE >> ndimsDefaultValue [
{ #category : #accessing }
PMTSNE >> outputDims: anInteger [
outputDims := anInteger
]

{ #category : #accessing }
PMTSNE >> outputDimsDefaultValue [
^ 2
]

Expand All @@ -198,23 +203,23 @@ PMTSNE >> perplexityDefaultValue [

{ #category : #running }
PMTSNE >> runPcaOnX [
"Runs PCA on X in order to reduce its dimensionality to initialdims dimensions.
print ""Preprocessing the data using PCA...""
(n, d) = X.shape;
X = X - Math.tile(Math.mean(X, 0), (n, 1));
(l, M) = Math.linalg.eig(Math.dot(X.T, X));
Y = Math.dot(X, M[:,0:no_dims]);
return Y;
"
"Runs PCA on X in order to reduce its dimensionality to initialDims dimensions."

"| analyzer i |
analyzer := PMPrincipalComponentAnalyser new: initialdims.
i := 1.
x dimension x
timesRepeat: [ analyzer accumulate: (x rowAt: i).
i := i + 1 ].
^ analyzer components"
self runPcaOnXUsing: PMPrincipalComponentAnalyserJacobiTransformation.
]

{ #category : #running }
PMTSNE >> runPcaOnXUsing: aClass [
"Runs aClass PCA on X in order to reduce its dimensionality to initialDims dimensions."

| scaler pca |
aClass superclass = PMPrincipalComponentAnalyser
ifFalse: [ self
error: 'Argument must be subclass of PMPrincipalComponentAnalyser' ].
scaler := PMStandardizationScaler new.
initialDims ifNil: [ initialDims := self initialDimsDefaultValue ].
pca := aClass new componentsNumber: (initialDims min: x dimension y).
x := pca fitAndTransform: (scaler fitAndTransform: x)
]

{ #category : #accessing }
Expand All @@ -230,6 +235,11 @@ PMTSNE >> step [
self computePairwiseAffinities
]

{ #category : #accessing }
PMTSNE >> x [
^ x
]

{ #category : #'as yet unclassified' }
PMTSNE >> x2p [
| p d beta logU n betaMin betaMax|
Expand Down
11 changes: 11 additions & 0 deletions src/Math-Tests-TSNE/PMTSNETest.class.st
Expand Up @@ -45,3 +45,14 @@ PMTSNETest >> testInitialDimsSetByDefaultWithFifty [
start.
self assert: t initialDims equals: 50
]

{ #category : #tests }
PMTSNETest >> testRunPcaOnXUsing [
"Tests the message runPcaOnXUsing"

| t |
t := (PMTSNE new)
x: (PMMatrix rows: #(#(0 0) #(2 2) #(2 0))).
t runPcaOnXUsing: PMPrincipalComponentAnalyserJacobiTransformation.
self assert: (t x) closeTo: (PMMatrix rows: #(#(-0.5 -1.5) #(-0.5 1.5) #(1 0))).
]

0 comments on commit bfb017a

Please sign in to comment.