Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Curio
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jim Wallace
Curio
Commits
d5d9c573
Commit
d5d9c573
authored
1 year ago
by
Mingchung Xia
Browse files
Options
Downloads
Patches
Plain Diff
Started on HNSWCorpus
parent
e81dc37a
No related branches found
No related tags found
1 merge request
!13
HNSW Implementation with Testcases
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Sources/SwiftNLP/1. Data Collection/HNSWCorpus + Sequence.swift
+6
-0
6 additions, 0 deletions
...s/SwiftNLP/1. Data Collection/HNSWCorpus + Sequence.swift
Sources/SwiftNLP/1. Data Collection/HNSWCorpus.swift
+48
-4
48 additions, 4 deletions
Sources/SwiftNLP/1. Data Collection/HNSWCorpus.swift
with
54 additions
and
4 deletions
Sources/SwiftNLP/1. Data Collection/HNSWCorpus + Sequence.swift
+
6
−
0
View file @
d5d9c573
...
...
@@ -21,6 +21,10 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// MARK: Sequence conformance will be done when HNSWCorpus is complete
/*
extension HNSWCorpus: Sequence {
typealias Element = [Scalar]
...
...
@@ -54,3 +58,5 @@ extension HNSWCorpus: Sequence {
return encodedDocuments.index(after: i)
}
}
*/
This diff is collapsed.
Click to expand it.
Sources/SwiftNLP/1. Data Collection/HNSWCorpus.swift
+
48
−
4
View file @
d5d9c573
...
...
@@ -28,6 +28,8 @@ import PriorityHeapAlgorithms
import
SimilarityMetric
import
HNSWAlgorithm
import
HNSWEphemeral
//import HNSWSample
import
GameplayKit
class
HNSWCorpus
<
Scalar
:
BinaryFloatingPoint
&
Codable
>
:
SNLPCorpus
{
...
...
@@ -36,18 +38,27 @@ class HNSWCorpus<Scalar: BinaryFloatingPoint & Codable>: SNLPCorpus {
var
zeroes
:
[
Scalar
]
var
count
:
Int
{
0
}
var
encodedDocuments
:
[
Int
:
[
Scalar
]]
=
[:]
// TODO: This should be replaced by HNSW
// var encodedDocuments: [Int : [Scalar]] = [:]
// MARK: typicalNeighbourhoodSize is unknown
var
encodedDocuments
:
DeterministicSampleVectorIndex
=
DeterministicSampleVectorIndex
<
[
Scalar
]
>
(
typicalNeighborhoodSize
:
20
)
init
(
_documentEncoder
:
ContextFreeEncoder
<
Scalar
>
)
{
self
.
_documentEncoder
=
_documentEncoder
zeroes
=
Array
(
repeating
:
Scalar
(
0
),
count
:
384
)
}
// TODO: Complete implementation of addUntokenizedDocument
@inlinable
func
addUntokenizedDocument
(
_
document
:
String
)
{
fatalError
(
"HNSWCorpus not implemented yet. Get on it."
)
encodedDocuments
.
insertRandom
(
document
)
}
// MARK: HNSW indexes do not support deletion - index must be rebuilt
// The following code is taken from Tests/HNSWTests/HNSWIndexTests.swift
// The test case randomly inserts and randomly queries neighbours.
//
// var index = DeterministicSampleVectorIndex(typicalNeighborhoodSize: 20)
// for _ in 0..<100 {
// index.insertRandom(range: 0...1)
...
...
@@ -65,7 +76,7 @@ class HNSWCorpus<Scalar: BinaryFloatingPoint & Codable>: SNLPCorpus {
// TODO: Continue overwriting these structures: this implementation uses the Vector instead of [Double]
public
struct
DeterministicSampleVectorIndex
<
Vector
:
Collection
&
Codable
>
where
Vector
.
Element
:
BinaryFloatingPoint
{
public
typealias
Index
=
EphemeralVectorIndex
<
Int
,
Int
,
CartesianDistanceMetric
<
[
Double
]
>
,
Void
>
...
...
@@ -75,6 +86,9 @@ public struct DeterministicSampleVectorIndex<Vector: Collection & Codable> where
base
=
.
init
(
metric
:
.
init
(),
config
:
.
unstableDefault
(
typicalNeighborhoodSize
:
typicalNeighborhoodSize
))
}
private
var
vectorRNG
=
DeterministicRandomNumberGenerator
(
seed
:
0
)
private
var
graphRNG
=
DeterministicRandomNumberGenerator
(
seed
:
1
)
public
func
find
(
near
query
:
Vector
,
limit
:
Int
,
exact
:
Bool
=
false
)
throws
->
[
Index
.
Neighbor
]
{
if
exact
{
Array
(
PriorityHeap
(
base
.
vectors
.
enumerated
()
.
map
{
...
...
@@ -86,8 +100,39 @@ public struct DeterministicSampleVectorIndex<Vector: Collection & Codable> where
}
}
// Should we be generating random Vector instead of CGPoint? How long is a Vector?
public
mutating
func
generateRandom
(
range
:
ClosedRange
<
Double
>
)
->
Vector
{
/*
CGPoint(
x: .random(in: range, using: &vectorRNG),
y: .random(in: range, using: &vectorRNG)
)
*/
}
public
mutating
func
insertRandom
(
range
:
ClosedRange
<
Double
>
)
{
base
.
insert
(
generateRandom
(
range
:
range
)
as!
[
Double
],
using
:
&
graphRNG
)
}
}
struct
DeterministicRandomNumberGenerator
:
RandomNumberGenerator
{
private
let
randomSource
:
GKMersenneTwisterRandomSource
init
(
seed
:
UInt64
)
{
randomSource
=
GKMersenneTwisterRandomSource
(
seed
:
seed
)
}
mutating
func
next
()
->
UInt64
{
let
upperBits
=
UInt64
(
UInt32
(
bitPattern
:
Int32
(
randomSource
.
nextInt
())))
<<
32
let
lowerBits
=
UInt64
(
UInt32
(
bitPattern
:
Int32
(
randomSource
.
nextInt
())))
return
upperBits
|
lowerBits
}
}
public
struct
CartesianDistanceMetric
<
Vector
:
Collection
&
Codable
>
:
SimilarityMetric
where
Vector
.
Element
:
BinaryFloatingPoint
{
public
func
similarity
(
between
someItem
:
Vector
,
_
otherItem
:
Vector
)
->
Vector
.
Element
{
// Naïve cartesian distance
...
...
@@ -98,4 +143,3 @@ public struct CartesianDistanceMetric<Vector: Collection & Codable>: SimilarityM
return
sqrt
(
squaredSum
)
}
}
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