-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataBlock.ts
320 lines (269 loc) · 12.5 KB
/
DataBlock.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import * as tf from '@tensorflow/tfjs-node'
import * as csv from 'fast-csv';
import * as fs from 'fs';
import { createClient } from 'redis';
interface IdatasetInfo {
size: number; usersNum: number; itemsNum: number;
userToModelMap: Map<any, number>, itemToModelMap: Map<any, number>
}
interface Idataset {
xs: {
user: tf.Tensor;
item: tf.Tensor;
}
ys: {
rating: tf.Tensor;
}
}
interface Idataset2 {
xs: {
[user_item: string]: any;
}
ys: {
rating: any;
}
}
interface optionsDataBlockCsv {
userColumn: string; itemColumn: string, ratingColumn: string; batchSize?: number;
ratingRange?: number[]; validationPercentage?: number; delimiter?: string;
seed?: number;
}
interface optionsDataBlockArray {
batchSize?: number; ratingRange?: number[];
validationPercentage?: number; seed?: number;
}
/**
DataBlock is an api which allows you to generate and manupilate your dataset.
To be used in the Learner API
*/
export class DataBlock {
trainingDataset: tf.data.Dataset<any>;
validationDataset: tf.data.Dataset<any>;
datasetInfo: IdatasetInfo;
usersMovies: any;
batchSize: number;
client: any;
ratingRange?: number[];
constructor(redisUrl?: string) {
this.datasetInfo = { size: 0, usersNum: 0, itemsNum: 0, userToModelMap: new Map(), itemToModelMap: new Map() }
this.redisConfig(redisUrl).then(e => console.log("connected"))
}
/**
Create a datablock from a csv file.
You should define the name of the columns which contain the corresponding data
*/
async fromCsv(path: string, options: optionsDataBlockCsv) {
let myPath = "file://" + path;
options.delimiter = (options.delimiter == null) ? ',' : options?.delimiter;
this.datasetInfo = await this.getInfoOnCsv(path, options.userColumn, options.itemColumn, options.delimiter)
this.ratingRange = options.ratingRange;
let csvDataset: tf.data.Dataset<any> = (tf.data.csv(
myPath, {
configuredColumnsOnly: true,
delimiter: options.delimiter,
columnConfigs: {
[options.userColumn]: {
required: true,
// dtype: "float32"
},
[options.itemColumn]: {
required: true,
// dtype: "float32"
},
[options.ratingColumn]: {
isLabel: true,
// dtype: "float32"
}
}
})).shuffle( //shuffle the dataset
(this.datasetInfo.size > 1e5) ? 1e5 : this.datasetInfo.size,
(options?.seed == null) ? undefined : options?.seed.toString(),
false
).map((x: any) => (
{ xs: { user: this.datasetInfo.userToModelMap.get(`${x.xs[options.userColumn]}`), item: this.datasetInfo.itemToModelMap.get(`${x.xs[options.itemColumn]}`) }, ys: { rating: Number(x.ys[options.ratingColumn]) } }
))
//split the dataset into train and valid set
let validationPercentage = (options?.validationPercentage == null) ? 0.1 : options?.validationPercentage
this.batchSize = (options?.batchSize == null) ? 64 : options?.batchSize
let trainSize = Math.round((1 - validationPercentage) * this.datasetInfo.size)
this.trainingDataset = csvDataset.take(trainSize).batch(this.batchSize);
this.trainingDataset = this.trainingDataset.map((x: Idataset) => ({ xs: { user: x.xs.user.reshape([-1, 1]), item: x.xs.item.reshape([-1, 1]) }, ys: { rating: x.ys.rating.reshape([-1, 1]) } }))
if (validationPercentage > 0) {
this.validationDataset = csvDataset.skip(trainSize)
.batch(this.batchSize);
this.validationDataset = this.validationDataset.map((x: Idataset) => ({ xs: { user: x.xs.user.reshape([-1, 1]), item: x.xs.item.reshape([-1, 1]) }, ys: { rating: x.ys.rating.reshape([-1, 1]) } }))
}
return this;
}
/**
Create a datablock from a tensors.
input the item, users, and ratings tensors
*/
async fromArray(items: number[], users: number[], ratings: number[], options?: optionsDataBlockArray) {
this.datasetInfo.itemsNum = new Set(items).size;
this.datasetInfo.usersNum = new Set(users).size;
this.datasetInfo.size = ratings.length;
this.batchSize = (options?.batchSize == null) ? 64 : options?.batchSize
let validationPercentage = options?.validationPercentage ? options?.validationPercentage : 0.1
// shuffle the dataset
let randomTen = Array.from(tf.util.createShuffledIndices(this.datasetInfo.size));
items = randomTen.map(i => items[i]);
users = randomTen.map(i => users[i]);
ratings = randomTen.map(i => ratings[i]);
//train valid splitting
if (validationPercentage > 0) {
this.splitTrainValidTensor(items, users, ratings, validationPercentage)
}
else {
let psuedoTrainingDataset: tf.TensorContainer[] = []
for (let i = 0; i < items.length; i++) {
psuedoTrainingDataset.push({ xs: { user: users[i], item: items[i] }, ys: { rating: ratings[i] } })
}
this.trainingDataset = tf.data.array(psuedoTrainingDataset)
}
this.trainingDataset = this.trainingDataset.batch(this.batchSize);
this.validationDataset = this.validationDataset.batch(this.batchSize);
this.trainingDataset = this.trainingDataset.map((x: Idataset) => ({ xs: { user: x.xs.user.reshape([-1, 1]), item: x.xs.item.reshape([-1, 1]) }, ys: { rating: x.ys.rating.reshape([-1, 1]) } }))
this.validationDataset = this.validationDataset.map((x: Idataset) => ({ xs: { user: x.xs.user.reshape([-1, 1]), item: x.xs.item.reshape([-1, 1]) }, ys: { rating: x.ys.rating.reshape([-1, 1]) } }))
return this;
}
/**
Get some stats about a csv file.
mainly used in fromCsv method
returns datasetInfo object
*/
getInfoOnCsv(path: string, userColumn: string, itemColumn: string, delimiter: string): Promise<IdatasetInfo> {
let client = this.client
let datasetInfo_ = new Promise<IdatasetInfo>(function (resolve, reject) {
let csvInfo = { size: 0, usersNum: 0, itemsNum: 0, userToModelMap: new Map(), itemToModelMap: new Map() };
let usersIndex: number = 0;
let itemsIndex: number = 0;
//using the fast-csv parse
csv.parseFile(path, { headers: true, delimiter: delimiter })
.on('error', error => console.error(error))
.on('data', (data) => {
if (!csvInfo.userToModelMap.has(`${data[userColumn]}`)) {
csvInfo.userToModelMap.set(`${data[userColumn]}`, usersIndex);
usersIndex += 1;
}
if (!csvInfo.itemToModelMap.has(`${data[itemColumn]}`)) {
csvInfo.itemToModelMap.set(`${data[itemColumn]}`, itemsIndex);
itemsIndex += 1;
}
client.SADD(
csvInfo.userToModelMap.get(`${data[userColumn]}`).toString(),
csvInfo.itemToModelMap.get(`${data[itemColumn]}`).toString()
);
})
.on('end', (rowCount: number) => {
csvInfo.size = rowCount;
csvInfo.usersNum = usersIndex
csvInfo.itemsNum = itemsIndex
return resolve(csvInfo);
})
});
return datasetInfo_;
}
/**
Split the tensors into training and validation set.
mainly used in fromTensor method
*/
splitTrainValidTensor(items: number[], users: number[], ratings: number[], validationPercentage: number): void {
let trainSize: number = Math.round((1 - validationPercentage) * this.datasetInfo.size);
let usersIndex = 0;
let itemsIndex = 0;
// splitting
let trainingItems = items.slice(0, trainSize);
let validationItems = items.slice(trainSize);
let trainingUsers = users.slice(0, trainSize);
let validationUsers = users.slice(trainSize);
let trainingRatings = ratings.slice(0, trainSize)
let validationRatings = ratings.slice(trainSize)
let psuedoTrainingDataset: tf.TensorContainer[] = []
for (let i = 0; i < trainingRatings.length; i++) {
psuedoTrainingDataset.push({ xs: { user: trainingUsers[i], item: trainingItems[i] }, ys: { rating: trainingRatings[i] } })
if (!this.datasetInfo.userToModelMap.has(`${trainingUsers[i]}`)) {
this.datasetInfo.userToModelMap.set(`${trainingUsers[i]}`, usersIndex);
usersIndex += 1;
}
if (!this.datasetInfo.itemToModelMap.has(`${trainingItems[i]}`)) {
this.datasetInfo.itemToModelMap.set(`${trainingItems[i]}`, itemsIndex);
itemsIndex += 1;
}
this.client.SADD(
(this.datasetInfo.userToModelMap.get(`${trainingUsers[i]}`) as number).toString(),
(this.datasetInfo.itemToModelMap.get(`${trainingItems[i]}`) as number).toString()
);
}
this.trainingDataset = tf.data.array((psuedoTrainingDataset))
let psuedoValidationDataset: tf.TensorContainer[] = []
for (let i = 0; i < validationRatings.length; i++) {
psuedoValidationDataset.push({ xs: { user: validationUsers[i], item: validationItems[i] }, ys: { rating: validationRatings[i] } })
if (!this.datasetInfo.userToModelMap.has(`${validationUsers[i]}`)) {
this.datasetInfo.userToModelMap.set(`${validationUsers[i]}`, usersIndex);
usersIndex += 1;
}
if (!this.datasetInfo.itemToModelMap.has(`${validationItems[i]}`)) {
this.datasetInfo.itemToModelMap.set(`${validationItems[i]}`, itemsIndex);
itemsIndex += 1;
}
this.client.SADD(
(this.datasetInfo.userToModelMap.get(`${validationUsers[i]}`) as number).toString(),
(this.datasetInfo.itemToModelMap.get(`${validationItems[i]}`) as number).toString()
);
}
this.validationDataset = tf.data.array((psuedoValidationDataset))
}
/**
save the datablock in a path (training + validation).
In case you wanted to save the validation data in different file, write the validation file name in the second argument "validationFileName"
*/
async save(outputFile: string, validationFileName?: string): Promise<void> {
let writeStream = fs.createWriteStream(outputFile);
let stream = csv.format({ headers: ['user', 'item', 'rating'] });
await this.trainingDataset.forEachAsync(
function (e: Idataset) {
let users_ = e.xs.user.dataSync()
let items_ = e.xs.item.dataSync()
let ratings_ = e.ys.rating.dataSync()
for (let i = 0; i < ratings_.length; i++)
stream.write([users_[i], items_[i], ratings_[i]])
}
);
if (validationFileName != null) {
console.log("validation");
stream.end();
stream.pipe(writeStream);
writeStream = fs.createWriteStream(validationFileName);
stream = csv.format({ headers: ['user', 'item', 'rating'] });
}
if (this.validationDataset != null)
await this.validationDataset.forEachAsync(
function (e: Idataset) {
let users_ = e.xs.user.dataSync()
let items_ = e.xs.item.dataSync()
let ratings_ = e.ys.rating.dataSync()
for (let i = 0; i < ratings_.length; i++)
stream.write([users_[i], items_[i], ratings_[i]])
}
);
stream.end();
stream.pipe(writeStream);
}
/**
return the size of the dataset (training + validation)
*/
size(): number {
return this.datasetInfo.size;
}
async redisConfig(url) {
if (url == null)
this.client = createClient();
else
this.client = createClient({
url: url
});
this.client.on('error', (err) => console.log('Redis Client Error', err));
await this.client.connect()
}
}