nodecommons-esm-ai 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/helpers/commons-ai-cross-validator.d.mts +4 -0
- package/dist/helpers/commons-ai-cross-validator.mjs +59 -0
- package/dist/helpers/commons-ai-cross-validator.mjs.map +1 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.mjs +3 -0
- package/dist/index.mjs.map +1 -0
- package/dist/interfaces/icommons-ai-cross-train-kable.d.mts +4 -0
- package/dist/interfaces/icommons-ai-cross-train-kable.mjs +3 -0
- package/dist/interfaces/icommons-ai-cross-train-kable.mjs.map +1 -0
- package/dist/interfaces/icommons-ai-early-stopping.d.mts +5 -0
- package/dist/interfaces/icommons-ai-early-stopping.mjs +3 -0
- package/dist/interfaces/icommons-ai-early-stopping.mjs.map +1 -0
- package/package.json +32 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ICommonsAiModelTrainable, TCommonsAiCrossValidationK, TCommonsAiGenericTrainingData, TCommonsAiModelWithQuality, TCommonsAiTrainingStats } from 'tscommons-esm-ai';
|
|
2
|
+
import { ICommonsAiEarlyStopping } from '../interfaces/icommons-ai-early-stopping.mjs';
|
|
3
|
+
import { ICommonsAiCrossTrainKable } from '../interfaces/icommons-ai-cross-train-kable.mjs';
|
|
4
|
+
export declare function commonsAiCrossValidationTrain<InputT, OutputT, ModelT extends ICommonsAiModelTrainable<InputT, OutputT, StatsT> & ICommonsAiCrossTrainKable, StatsT extends TCommonsAiTrainingStats>(crossValidationKs: TCommonsAiCrossValidationK<InputT, OutputT>[], testingData: TCommonsAiGenericTrainingData<InputT, OutputT>[], modelConstructorCallback: (training: TCommonsAiGenericTrainingData<InputT, OutputT>[], epochs: number, validation: TCommonsAiGenericTrainingData<InputT, OutputT>[] | undefined, earlyStopping: ICommonsAiEarlyStopping | undefined) => Promise<ModelT>, epochs: number, earlyStopping?: ICommonsAiEarlyStopping, dumpAttemptQualitiesCallback?: (qualities: number[]) => void): Promise<TCommonsAiModelWithQuality<InputT, OutputT, ModelT>>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { commonsLogDebug, commonsLogDoing, commonsLogInfo } from 'nodecommons-esm-log';
|
|
2
|
+
export async function commonsAiCrossValidationTrain(crossValidationKs, testingData, modelConstructorCallback, epochs, earlyStopping, dumpAttemptQualitiesCallback) {
|
|
3
|
+
const iterations = crossValidationKs.length;
|
|
4
|
+
const attempts = [];
|
|
5
|
+
for (const crossValidationK of crossValidationKs) {
|
|
6
|
+
if (crossValidationK.training.length === 0)
|
|
7
|
+
throw new Error(`Not enough data to train for ${crossValidationKs.length} Ks.`);
|
|
8
|
+
if (crossValidationK.validation.length === 0)
|
|
9
|
+
throw new Error(`Not enough data to validate for ${crossValidationKs.length} Ks.`);
|
|
10
|
+
commonsLogInfo(`K iteration ${attempts.length + 1} of ${iterations}`);
|
|
11
|
+
const model = await modelConstructorCallback(crossValidationK.training, epochs, crossValidationK.validation, earlyStopping);
|
|
12
|
+
model.k = attempts.length + 1;
|
|
13
|
+
{ // scope
|
|
14
|
+
const log = commonsLogDoing('commonsAiCrossValidationTrain', 'Building data');
|
|
15
|
+
await model.build();
|
|
16
|
+
log.success();
|
|
17
|
+
}
|
|
18
|
+
{ // scope
|
|
19
|
+
const log = commonsLogDoing('commonsAiCrossValidationTrain', 'Training model');
|
|
20
|
+
const stats = await model.train();
|
|
21
|
+
if (stats) {
|
|
22
|
+
log.result(`loss=${stats.loss}, acc=${stats.acc}, epochs=${stats.epochs}`);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
log.success();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
{ // scope
|
|
29
|
+
const log = commonsLogDoing('commonsAiCrossValidationTrain', 'Testing model quality using testing data');
|
|
30
|
+
const quality = await model.test(testingData);
|
|
31
|
+
const delta = quality.positive - quality.negative;
|
|
32
|
+
log.result(`${quality.positive} positive, ${quality.negative} negative, overall ${delta}`);
|
|
33
|
+
attempts.push({
|
|
34
|
+
model: model,
|
|
35
|
+
quality: quality,
|
|
36
|
+
delta: delta
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
attempts
|
|
41
|
+
.sort((a, b) => {
|
|
42
|
+
if (a.delta < b.delta)
|
|
43
|
+
return -1;
|
|
44
|
+
if (b.delta > a.delta)
|
|
45
|
+
return 1;
|
|
46
|
+
return 0;
|
|
47
|
+
})
|
|
48
|
+
.reverse();
|
|
49
|
+
const qualities = attempts
|
|
50
|
+
.map((attempt) => attempt.delta);
|
|
51
|
+
commonsLogDebug(`Attempt delta qualities are ${qualities.join(', ')}`);
|
|
52
|
+
if (dumpAttemptQualitiesCallback)
|
|
53
|
+
dumpAttemptQualitiesCallback(qualities);
|
|
54
|
+
return {
|
|
55
|
+
model: attempts[0].model,
|
|
56
|
+
quality: attempts[0].quality
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=commons-ai-cross-validator.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commons-ai-cross-validator.mjs","sourceRoot":"","sources":["../../src/helpers/commons-ai-cross-validator.mts"],"names":[],"mappings":"AASA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,cAAc,EAAsB,MAAM,qBAAqB,CAAC;AAK3G,MAAM,CAAC,KAAK,UAAU,6BAA6B,CAMjD,iBAAgE,EAChE,WAA6D,EAC7D,wBAKoB,EACpB,MAAc,EACd,aAAuC,EACvC,4BAA4D;IAE7D,MAAM,UAAU,GAAW,iBAAiB,CAAC,MAAM,CAAC;IAQpD,MAAM,QAAQ,GAAe,EAAE,CAAC;IAChC,KAAK,MAAM,gBAAgB,IAAI,iBAAiB,EAAE,CAAC;QAClD,IAAI,gBAAgB,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,iBAAiB,CAAC,MAAM,MAAM,CAAC,C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Q,CAAC;YAC1D,GAAG,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,QAAQ,cAAc,OAAO,CAAC,QAAQ,sBAAsB,KAAK,EAAE,CAAC,CAAC;YAE3F,QAAQ,CAAC,IAAI,CAAC;gBACZ,KAAK,EAAE,KAAK;gBACZ,OAAO,EAAE,OAAO;gBAChB,KAAK,EAAE,KAAK;aACb,CAAC,CAAC;QACJ,CAAC;IACF,CAAC;IAED,QAAQ;SACL,IAAI,CAAC,CAAC,CAAW,EAAE,CAAW,EAAU,EAAE;QAC1C,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK;YAAE,OAAO,CAAC,CAAC,CAAC;QACjC,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK;YAAE,OAAO,CAAC,CAAC;QAChC,OAAO,CAAC,CAAC;IACV,CAAC,CAAC;SACD,OAAO,EAAE,CAAC;IAEb,MAAM,SAAS,GAAa,QAAQ;SACjC,GAAG,CAAC,CAAC,OAAiB,EAAU,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACrD,eAAe,CAAC,+BAA+B,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAEvE,IAAI,4BAA4B;QAAE,4BAA4B,CAAC,SAAS,CAAC,CAAC;IAE1E,OAAO;QACL,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK;QACxB,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO;KAC7B,CAAC;AACH,CAAC"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { commonsAiCrossValidationTrain } from './helpers/commons-ai-cross-validator.mjs';
|
|
2
|
+
import { ICommonsAiCrossTrainKable } from './interfaces/icommons-ai-cross-train-kable.mjs';
|
|
3
|
+
import { ICommonsAiEarlyStopping } from './interfaces/icommons-ai-early-stopping.mjs';
|
|
4
|
+
export { commonsAiCrossValidationTrain, ICommonsAiCrossTrainKable, ICommonsAiEarlyStopping };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAE,MAAM,0CAA0C,CAAC;AAGzF,OAAO,EACN,6BAA6B,EAG7B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"icommons-ai-cross-train-kable.mjs","sourceRoot":"","sources":["../../src/interfaces/icommons-ai-cross-train-kable.mts"],"names":[],"mappings":"AAAA,2BAA2B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"icommons-ai-early-stopping.mjs","sourceRoot":"","sources":["../../src/interfaces/icommons-ai-early-stopping.mts"],"names":[],"mappings":"AAAA,2BAA2B"}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nodecommons-esm-ai",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"tsc": "./node_modules/typescript/bin/tsc",
|
|
7
|
+
"preprepare": "rm -rf ./dist; php ~/Dev/etim.php src/ && npm run tsc",
|
|
8
|
+
"publish-major": "rm -rf dist; npm run tsc && npx eslint . && npm run preprepare && npm version major && npm install && npm publish && git add . && git commit -m 'publish'",
|
|
9
|
+
"publish-minor": "rm -rf dist; npm run tsc && npx eslint . && npm run preprepare && npm version minor && npm install && npm publish && git add . && git commit -m 'publish'",
|
|
10
|
+
"publish-patch": "rm -rf dist; npm run tsc && npx eslint . && npm run preprepare && npm version patch && npm install && npm publish && git add . && git commit -m 'publish'"
|
|
11
|
+
},
|
|
12
|
+
"main": "dist/index.mjs",
|
|
13
|
+
"types": "dist/index.d.mjs",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"author": "Pete Morris",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@stylistic/eslint-plugin-ts": "^2.11.0",
|
|
19
|
+
"@types/node": "^22.10.0",
|
|
20
|
+
"eslint-plugin-import": "^2.31.0",
|
|
21
|
+
"eslint-plugin-prefer-arrow-functions": "^3.4.1",
|
|
22
|
+
"typescript": "^5.7.2",
|
|
23
|
+
"typescript-eslint": "^8.16.0"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist/**/*"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"nodecommons-esm-log": "^0.0.6",
|
|
30
|
+
"tscommons-esm-ai": "^0.0.6"
|
|
31
|
+
}
|
|
32
|
+
}
|