-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathTestClusteringTrainer.cs
170 lines (146 loc) · 6.82 KB
/
TestClusteringTrainer.cs
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BrightData;
using BrightData.Types;
using BrightWire;
using BrightWire.TrainingData.Helper;
namespace ExampleCode.DataTableTrainers
{
internal class TestClusteringTrainer
{
readonly BrightDataContext _context;
public class AaaiDocument(string title, string[] keyword, string[] topic, string @abstract, string[] group)
{
/// <summary>
/// Free text description of the document
/// </summary>
public string Title { get; } = title;
/// <summary>
/// Free text; author-generated keywords
/// </summary>
public string[] Keyword { get; } = keyword;
/// <summary>
/// Free text; author-selected, low-level keywords
/// </summary>
public string[] Topic { get; } = topic;
/// <summary>
/// Free text; paper abstracts
/// </summary>
public string Abstract { get; } = @abstract;
/// <summary>
/// Categorical; author-selected, high-level keyword(s)
/// </summary>
public string[] Group { get; } = group;
public WeightedIndexListWithLabel<string> AsClassification(StringTableBuilder stringTable)
{
var weightedIndex = new List<WeightedIndexList.Item>();
foreach (var item in Keyword)
{
weightedIndex.Add(new WeightedIndexList.Item(stringTable.GetIndex(item), 1f));
}
foreach (var item in Topic)
{
weightedIndex.Add(new WeightedIndexList.Item(stringTable.GetIndex(item), 1f));
}
var weights = WeightedIndexList.Create(weightedIndex
.GroupBy(d => d.Index)
.Select(g => (g.Key, g.Sum(d => d.Weight)))
);
return new(Title, weights);
}
public override string ToString() => $"{Title} - {Abstract}";
}
readonly StringTableBuilder _stringTable = new();
readonly IVector<float>[] _vectors;
readonly AaaiDocument[] _documents;
readonly uint _groupCount;
public TestClusteringTrainer(BrightDataContext context, AaaiDocument[] documents)
{
_context = context;
var lap = context.LinearAlgebraProvider;
var data = documents.Select(d => d.AsClassification(_stringTable)).ToArray();
var data2 = data.Bm25Plus();
_vectors = data2.Select(d => d.Data.AsDense(_stringTable.Size + 1).Create(lap)).ToArray();
_documents = documents;
var allGroups = new HashSet<string>(documents.SelectMany(d => d.Group));
_groupCount = (uint) allGroups.Count;
}
public void KMeans()
{
Console.Write("Kmeans clustering...");
var outputPath = GetOutputPath("kmeans");
WriteClusters(outputPath, _vectors.KMeansCluster(_context, _groupCount), _documents);
Console.WriteLine($"written to {outputPath}");
}
public void Nnmf()
{
Console.Write("NNMF clustering...");
var outputPath = GetOutputPath("nnmf");
WriteClusters(outputPath, _vectors.Nnmf(_context.LinearAlgebraProvider, _groupCount), _documents);
Console.WriteLine($"written to {outputPath}");
}
public void RandomProjection()
{
var lap = _context.LinearAlgebraProvider;
// create a term/document matrix with terms as columns and documents as rows
var matrix = lap.CreateMatrixFromRows(_vectors);
Console.Write("Creating random projection...");
var outputPath = GetOutputPath("projected-kmeans");
using var randomProjection = lap.CreateRandomProjection(_stringTable.Size + 1, 512);
using var projectedMatrix = randomProjection.Compute(matrix);
var vectorList2 = projectedMatrix.RowCount.AsRange().Select(projectedMatrix.GetRowVector).ToArray();
Console.Write("done...");
Console.Write("Kmeans clustering of random projection...");
WriteClusters(outputPath, vectorList2.KMeansCluster(_context, _groupCount), _documents);
vectorList2.DisposeAll();
Console.WriteLine($"written to {outputPath}");
}
public void LatentSemanticAnalysis(uint k = 256)
{
Console.Write("Building latent term/document space...");
var outputPath = GetOutputPath("latent-kmeans");
var lap = _context.LinearAlgebraProvider;
// create a term/document matrix with terms as columns and documents as rows
var matrix = lap.CreateMatrixFromRows(_vectors);
var kIndices = k.AsRange().ToList();
var matrixT = matrix.Transpose();
matrix.Dispose();
var (_, floatVector, vt) = matrixT.Svd();
matrixT.Dispose();
var s = lap.CreateDiagonalMatrix(floatVector.Segment.Values.Take((int)k).ToArray());
var v2 = vt.GetNewMatrixFromRows(kIndices);
using (var sv2 = s.Multiply(v2))
{
v2.Dispose();
s.Dispose();
var vectorList3 = sv2.AllColumnsAsReadOnly(false).Select(c => c.Create(lap)).ToArray();
Console.WriteLine("Kmeans clustering in latent document space...");
WriteClusters(outputPath, vectorList3.KMeansCluster(_context, _groupCount), _documents);
vectorList3.DisposeAll();
}
Console.WriteLine($"written to {outputPath}");
}
string GetOutputPath(string name) => Path.Combine(DataFileDirectory, "output", $"{name}.txt");
string DataFileDirectory => _context.Get<DirectoryInfo>("DataFileDirectory")?.FullName ?? throw new Exception("Data File Directory not set");
static void WriteClusters(string filePath, uint[][] clusters, AaaiDocument[] lookupTable)
{
new FileInfo(filePath).Directory?.Create();
using var writer = new StreamWriter(filePath);
foreach (var cluster in clusters)
{
foreach (var item in cluster)
{
var document = lookupTable[item];
writer.WriteLine(document.Title);
writer.WriteLine(String.Join(", ", document.Keyword));
writer.WriteLine(String.Join(", ", document.Topic));
writer.WriteLine(String.Join(", ", document.Group));
writer.WriteLine();
}
writer.WriteLine("------------------------------------------------------------------");
}
}
}
}