@webwriter/deep-learning-model 0.0.1 → 1.0.1
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/.github/workflows/main.yml +8 -0
- package/LICENSE +7 -0
- package/README.md +290 -0
- package/components/dataset-input/dataset-input.styles.ts +20 -0
- package/components/dataset-input/dataset-input.ts +196 -0
- package/components/inference-fields/class-input.ts +83 -0
- package/components/inference-fields/class-output.ts +62 -0
- package/components/inference-fields/image-input.ts +182 -0
- package/components/inference-fields/image.ts +82 -0
- package/components/inference-fields/inference-interfaces.ts +15 -0
- package/components/inference-fields/number-input.ts +62 -0
- package/components/inference-fields/number-output.ts +56 -0
- package/components/model-editor.ts +273 -0
- package/components/model-tests/bad-samples.ts +152 -0
- package/components/model-tests/loss-distribution.ts +174 -0
- package/components/reference-element-select.ts +105 -0
- package/custom-elements.json +1446 -0
- package/custom.d.ts +180 -0
- package/dist/widgets/webwriter-feature-engineering.css +444 -0
- package/dist/widgets/webwriter-feature-engineering.js +112241 -0
- package/dist/widgets/webwriter-model-designing.css +444 -0
- package/dist/widgets/webwriter-model-designing.js +113967 -0
- package/dist/widgets/webwriter-model-evaluation.css +444 -0
- package/dist/widgets/webwriter-model-evaluation.js +119019 -0
- package/dist/widgets/webwriter-model-prediction.css +444 -0
- package/dist/widgets/webwriter-model-prediction.js +109260 -0
- package/dist/widgets/webwriter-model-training.css +444 -0
- package/dist/widgets/webwriter-model-training.js +121391 -0
- package/editing-config.json +73 -0
- package/icon.svg +1 -0
- package/lib/dataset/class.ts +155 -0
- package/lib/dataset/dataset.ts +293 -0
- package/lib/dataset/feature.ts +110 -0
- package/lib/dataset/image.ts +312 -0
- package/lib/dataset/linear.ts +120 -0
- package/lib/layers/activation.ts +48 -0
- package/lib/layers/add.ts +50 -0
- package/lib/layers/attention.ts +74 -0
- package/lib/layers/concat.ts +50 -0
- package/lib/layers/conv.ts +83 -0
- package/lib/layers/dropout.ts +44 -0
- package/lib/layers/fcl.ts +66 -0
- package/lib/layers/flatten.ts +48 -0
- package/lib/layers/index.ts +708 -0
- package/lib/layers/input.ts +55 -0
- package/lib/layers/noise.ts +43 -0
- package/lib/layers/output.ts +64 -0
- package/lib/layers/reshape.ts +76 -0
- package/lib/layers/transposeconv.ts +90 -0
- package/lib/tflib.ts +6 -0
- package/lit-localize.json +47 -0
- package/localization/ar.xlf +532 -0
- package/localization/bg.xlf +531 -0
- package/localization/cs.xlf +532 -0
- package/localization/da.xlf +531 -0
- package/localization/de.xlf +531 -0
- package/localization/el.xlf +533 -0
- package/localization/es.xlf +532 -0
- package/localization/et.xlf +532 -0
- package/localization/fi.xlf +531 -0
- package/localization/fr.xlf +532 -0
- package/localization/generated/ar.ts +158 -0
- package/localization/generated/bg.ts +157 -0
- package/localization/generated/cs.ts +158 -0
- package/localization/generated/da.ts +157 -0
- package/localization/generated/de.ts +157 -0
- package/localization/generated/el.ts +159 -0
- package/localization/generated/es.ts +158 -0
- package/localization/generated/et.ts +158 -0
- package/localization/generated/fi.ts +157 -0
- package/localization/generated/fr.ts +158 -0
- package/localization/generated/hu.ts +157 -0
- package/localization/generated/id.ts +158 -0
- package/localization/generated/index.js +3 -0
- package/localization/generated/it.ts +158 -0
- package/localization/generated/ja.ts +157 -0
- package/localization/generated/ko.ts +157 -0
- package/localization/generated/locale-codes.js +83 -0
- package/localization/generated/lt.ts +157 -0
- package/localization/generated/lv.ts +157 -0
- package/localization/generated/nb.ts +157 -0
- package/localization/generated/nl.ts +157 -0
- package/localization/generated/pl.ts +158 -0
- package/localization/generated/pt-BR.ts +158 -0
- package/localization/generated/pt-PT.ts +158 -0
- package/localization/generated/ro.ts +158 -0
- package/localization/generated/ru.ts +158 -0
- package/localization/generated/sk.ts +158 -0
- package/localization/generated/sl.ts +158 -0
- package/localization/generated/sv.ts +157 -0
- package/localization/generated/tr.ts +157 -0
- package/localization/generated/uk.ts +157 -0
- package/localization/generated/zh-hans.ts +140 -0
- package/localization/generated/zh-hant.ts +140 -0
- package/localization/hu.xlf +531 -0
- package/localization/id.xlf +532 -0
- package/localization/it.xlf +532 -0
- package/localization/ja.xlf +531 -0
- package/localization/ko.xlf +531 -0
- package/localization/lt.xlf +531 -0
- package/localization/lv.xlf +531 -0
- package/localization/nb.xlf +531 -0
- package/localization/nl.xlf +531 -0
- package/localization/pkg/ar.xliff +7 -0
- package/localization/pkg/bg.xliff +7 -0
- package/localization/pkg/cs.xliff +7 -0
- package/localization/pkg/da.xliff +7 -0
- package/localization/pkg/de.xliff +7 -0
- package/localization/pkg/el.xliff +7 -0
- package/localization/pkg/es.xliff +7 -0
- package/localization/pkg/et.xliff +7 -0
- package/localization/pkg/fi.xliff +7 -0
- package/localization/pkg/fr.xliff +7 -0
- package/localization/pkg/hu.xliff +7 -0
- package/localization/pkg/id.xliff +7 -0
- package/localization/pkg/it.xliff +7 -0
- package/localization/pkg/ja.xliff +7 -0
- package/localization/pkg/ko.xliff +7 -0
- package/localization/pkg/lt.xliff +7 -0
- package/localization/pkg/lv.xliff +7 -0
- package/localization/pkg/nb.xliff +7 -0
- package/localization/pkg/nl.xliff +7 -0
- package/localization/pkg/pl.xliff +7 -0
- package/localization/pkg/pt-BR.xliff +7 -0
- package/localization/pkg/pt-PT.xliff +7 -0
- package/localization/pkg/ro.xliff +7 -0
- package/localization/pkg/ru.xliff +7 -0
- package/localization/pkg/sk.xliff +7 -0
- package/localization/pkg/sl.xliff +7 -0
- package/localization/pkg/sv.xliff +7 -0
- package/localization/pkg/tr.xliff +7 -0
- package/localization/pkg/uk.xliff +7 -0
- package/localization/pkg/zh-hans.xliff +7 -0
- package/localization/pkg/zh-hant.xliff +7 -0
- package/localization/pl.xlf +532 -0
- package/localization/pt-BR.xlf +532 -0
- package/localization/pt-PT.xlf +532 -0
- package/localization/ro.xlf +532 -0
- package/localization/ru.xlf +532 -0
- package/localization/sk.xlf +532 -0
- package/localization/sl.xlf +532 -0
- package/localization/snippets/ar.xliff +16 -0
- package/localization/snippets/bg.xliff +16 -0
- package/localization/snippets/cs.xliff +16 -0
- package/localization/snippets/da.xliff +16 -0
- package/localization/snippets/de.xliff +16 -0
- package/localization/snippets/el.xliff +16 -0
- package/localization/snippets/es.xliff +16 -0
- package/localization/snippets/et.xliff +16 -0
- package/localization/snippets/fi.xliff +16 -0
- package/localization/snippets/fr.xliff +16 -0
- package/localization/snippets/hu.xliff +16 -0
- package/localization/snippets/id.xliff +16 -0
- package/localization/snippets/it.xliff +16 -0
- package/localization/snippets/ja.xliff +16 -0
- package/localization/snippets/ko.xliff +16 -0
- package/localization/snippets/lt.xliff +16 -0
- package/localization/snippets/lv.xliff +16 -0
- package/localization/snippets/nb.xliff +16 -0
- package/localization/snippets/nl.xliff +16 -0
- package/localization/snippets/pl.xliff +16 -0
- package/localization/snippets/pt-BR.xliff +16 -0
- package/localization/snippets/pt-PT.xliff +16 -0
- package/localization/snippets/ro.xliff +16 -0
- package/localization/snippets/ru.xliff +16 -0
- package/localization/snippets/sk.xliff +16 -0
- package/localization/snippets/sl.xliff +16 -0
- package/localization/snippets/sv.xliff +16 -0
- package/localization/snippets/tr.xliff +16 -0
- package/localization/snippets/uk.xliff +16 -0
- package/localization/snippets/zh-hans.xliff +16 -0
- package/localization/snippets/zh-hant.xliff +16 -0
- package/localization/sv.xlf +531 -0
- package/localization/tr.xlf +531 -0
- package/localization/uk.xlf +531 -0
- package/localization/zh-hans.xlf +531 -0
- package/localization/zh-hant.xlf +531 -0
- package/package.json +104 -3
- package/snippets/MNIST.html +21 -0
- package/test.json +63 -0
- package/tsconfig.json +8 -0
- package/widgets/webwriter-feature-engineering.ts +421 -0
- package/widgets/webwriter-model-designing.ts +300 -0
- package/widgets/webwriter-model-evaluation.ts +215 -0
- package/widgets/webwriter-model-prediction.ts +307 -0
- package/widgets/webwriter-model-training.ts +684 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
".": {
|
|
3
|
+
"selectable": false,
|
|
4
|
+
"label": {
|
|
5
|
+
"_": "Deep Learning Model",
|
|
6
|
+
"ar": "نموذج التعلم العميق",
|
|
7
|
+
"cs": "Model hlubokého učení",
|
|
8
|
+
"de": "Deep-Learning-Modell",
|
|
9
|
+
"fi": "Syväoppimismalli",
|
|
10
|
+
"bg": "Модел за дълбоко обучение",
|
|
11
|
+
"da": "Deep Learning-model",
|
|
12
|
+
"fr": "Modèle d'apprentissage profond",
|
|
13
|
+
"el": "Μοντέλο βαθιάς μάθησης",
|
|
14
|
+
"nl": "Deep learning-model",
|
|
15
|
+
"hu": "Mélytanulási modell",
|
|
16
|
+
"it": "Modello di deep learning",
|
|
17
|
+
"lv": "Dziļās apmācības modelis",
|
|
18
|
+
"lt": "Gilusis mokymosi modelis",
|
|
19
|
+
"et": "Sügavõppe mudel",
|
|
20
|
+
"nb": "Dyp læringsmodell",
|
|
21
|
+
"pl": "Model głębokiego uczenia",
|
|
22
|
+
"pt-PT": "Modelo de aprendizagem profunda",
|
|
23
|
+
"ro": "Model de învățare profundă",
|
|
24
|
+
"sk": "Model hlbokého učenia",
|
|
25
|
+
"sl": "Model globokega učenja",
|
|
26
|
+
"es": "Modelo de aprendizaje profundo",
|
|
27
|
+
"sv": "Djupinlärningsmodell",
|
|
28
|
+
"ru": "Модель глубокого обучения",
|
|
29
|
+
"uk": "Модель глибокого навчання",
|
|
30
|
+
"pt-BR": "Modelo de Aprendizado Profundo",
|
|
31
|
+
"tr": "Derin Öğrenme Modeli",
|
|
32
|
+
"ja": "深層学習モデル",
|
|
33
|
+
"id": "Model Pembelajaran Mendalam",
|
|
34
|
+
"ko": "딥 러닝 모델",
|
|
35
|
+
"zh-hans": "深度学习模型",
|
|
36
|
+
"zh-hant": "深度學習模型"
|
|
37
|
+
},
|
|
38
|
+
"uninsertable": false,
|
|
39
|
+
"description": {
|
|
40
|
+
"ar": "مجموعة من الكتل لإنشاء نماذج التعلم العميق وتدريبها وتقييمها.",
|
|
41
|
+
"cs": "Sada bloků pro vytváření, trénování a vyhodnocování modelů hlubokého učení.",
|
|
42
|
+
"de": "Eine Reihe von Bausteinen zum Erstellen, Trainieren und Bewerten von Deep-Learning-Modellen.",
|
|
43
|
+
"fi": "Sarja lohkoja syväoppimismallien luomiseen, kouluttamiseen ja arviointiin.",
|
|
44
|
+
"bg": "Набор от блокове за създаване, обучение и оценка на модели за дълбоко обучение.",
|
|
45
|
+
"da": "Et sæt blokke til at oprette, træne og evaluere deep learning-modeller.",
|
|
46
|
+
"fr": "Un ensemble de blocs permettant de créer, d'entraîner et d'évaluer des modèles d'apprentissage profond.",
|
|
47
|
+
"el": "Ένα σύνολο μπλοκ για τη δημιουργία, την εκπαίδευση και την αξιολόγηση μοντέλων βαθιάς μάθησης.",
|
|
48
|
+
"nl": "Een set blokken om deep learning-modellen te creëren, te trainen en te evalueren.",
|
|
49
|
+
"hu": "Blokkok halmaza mélytanulási modellek létrehozásához, képzéséhez és értékeléséhez.",
|
|
50
|
+
"it": "Un insieme di blocchi per creare, addestrare e valutare modelli di deep learning.",
|
|
51
|
+
"lv": "Bloku kopums, lai izveidotu, apmācītu un novērtētu dziļās apmācības modeļus.",
|
|
52
|
+
"lt": "Blokų rinkinys, skirtas kurti, mokyti ir vertinti giluminio mokymosi modelius.",
|
|
53
|
+
"et": "Komplekt plokke sügavõppe mudelite loomiseks, treenimiseks ja hindamiseks.",
|
|
54
|
+
"nb": "Et sett med blokker for å lage, trene og evaluere dyp læringsmodeller.",
|
|
55
|
+
"pl": "Zestaw bloków do tworzenia, szkolenia i oceny modeli głębokiego uczenia.",
|
|
56
|
+
"pt-PT": "Um conjunto de blocos para criar, treinar e avaliar modelos de aprendizagem profunda.",
|
|
57
|
+
"ro": "Un set de blocuri pentru crearea, antrenarea și evaluarea modelelor de învățare profundă.",
|
|
58
|
+
"sk": "Súbor blokov na vytváranie, trénovanie a hodnotenie modelov hlbokého učenia.",
|
|
59
|
+
"sl": "Niz blokov za ustvarjanje, usposabljanje in ocenjevanje modelov globokega učenja.",
|
|
60
|
+
"es": "Un conjunto de bloques para crear, entrenar y evaluar modelos de aprendizaje profundo.",
|
|
61
|
+
"sv": "En uppsättning block för att skapa, träna och utvärdera djupinlärningsmodeller.",
|
|
62
|
+
"ru": "Набор блоков для создания, обучения и оценки моделей глубокого обучения.",
|
|
63
|
+
"uk": "Набір блоків для створення, навчання та оцінки моделей глибокого навчання.",
|
|
64
|
+
"pt-BR": "Um conjunto de blocos para criar, treinar e avaliar modelos de aprendizado profundo.",
|
|
65
|
+
"tr": "Derin öğrenme modellerini oluşturmak, eğitmek ve değerlendirmek için bir dizi blok.",
|
|
66
|
+
"ja": "深層学習モデルを作成、学習、評価するための一連のブロック。",
|
|
67
|
+
"id": "Sebuah kumpulan alat untuk membuat, melatih, dan mengevaluasi model pembelajaran mendalam.",
|
|
68
|
+
"ko": "딥 러닝 모델을 생성, 훈련 및 평가하기 위한 블록 세트.",
|
|
69
|
+
"zh-hans": "一套用于创建、训练和评估深度学习模型的工具集。",
|
|
70
|
+
"zh-hant": "一套用於建立、訓練及評估深度學習模型的工具組。"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
package/icon.svg
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icons-tabler-outline icon-tabler-ai"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M8 16v-6a2 2 0 1 1 4 0v6" /><path d="M8 13h4" /><path d="M16 8v8" /></svg>
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { SlSelect } from "@shoelace-style/shoelace";
|
|
2
|
+
import { html, TemplateResult } from "lit";
|
|
3
|
+
import { until } from 'lit/directives/until.js';
|
|
4
|
+
import { FeatureSettings, FeatureTransformation } from './feature';
|
|
5
|
+
|
|
6
|
+
import type * as TF from "@tensorflow/tfjs";
|
|
7
|
+
import getTF from "../../lib/tflib";
|
|
8
|
+
import { msg, str } from "@lit/localize";
|
|
9
|
+
let tf: typeof TF;
|
|
10
|
+
(async () => {tf = await getTF()})()
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This is the transformation from string values to a class encoding
|
|
14
|
+
*/
|
|
15
|
+
export default class ClassTransformation extends FeatureTransformation {
|
|
16
|
+
static {
|
|
17
|
+
this.registry.push({name: "Class Encoding", constructor:this});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The constructor runs through the samples and registers the unique classes.
|
|
22
|
+
*/
|
|
23
|
+
public constructor(samples: Array<string> | Array<any>, settings: FeatureSettings) {
|
|
24
|
+
super("Class Encoding", samples, settings);
|
|
25
|
+
|
|
26
|
+
this.aggrigatedInformation.classes = new Array<string>();
|
|
27
|
+
this.aggrigatedInformation.multiClasses = new Array<string>();
|
|
28
|
+
|
|
29
|
+
let hasMulti = false;
|
|
30
|
+
for (let sample of samples) {
|
|
31
|
+
let classes = this.aggrigatedInformation.classes;
|
|
32
|
+
if (classes.indexOf(sample) == -1)
|
|
33
|
+
classes.push(sample);
|
|
34
|
+
|
|
35
|
+
if (typeof sample =="object" || (sample.startsWith != null && sample.startsWith("["))) {
|
|
36
|
+
hasMulti = true;
|
|
37
|
+
let multiClasses = this.aggrigatedInformation.multiClasses;
|
|
38
|
+
|
|
39
|
+
let sampleClasses = sample;
|
|
40
|
+
if (typeof sample == "string")
|
|
41
|
+
sampleClasses = JSON.parse(sample);
|
|
42
|
+
|
|
43
|
+
sampleClasses.forEach(function(c:string) {
|
|
44
|
+
if (multiClasses.indexOf(c) == -1)
|
|
45
|
+
multiClasses.push(c);
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!this.settings.type)
|
|
51
|
+
this.settings.type = hasMulti ? "multi-hot" : "one-hot";
|
|
52
|
+
|
|
53
|
+
this.aggrigatedInformation.classes.sort();
|
|
54
|
+
this.aggrigatedInformation.multiClasses.sort();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public size(): [number] {
|
|
58
|
+
if (this.getSetting("type") == "one-hot")
|
|
59
|
+
return [(this.aggrigatedInformation.classes.length as number)];
|
|
60
|
+
if (this.getSetting("type") == "multi-hot")
|
|
61
|
+
return [(this.aggrigatedInformation.multiClasses.length as number)];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public async transform(value: string | Array<any>): Promise<TF.Tensor1D> {
|
|
65
|
+
if (this.getSetting("type") == "one-hot") {
|
|
66
|
+
let classes = this.aggrigatedInformation.classes;
|
|
67
|
+
let index = classes.indexOf(value);
|
|
68
|
+
if (index == -1)
|
|
69
|
+
return tf.zeros(this.size());
|
|
70
|
+
|
|
71
|
+
let tensor = new Array<number>(classes.length);
|
|
72
|
+
tensor.fill(0);
|
|
73
|
+
tensor[index] = 1;
|
|
74
|
+
return tf.tensor(tensor);
|
|
75
|
+
} else if (this.getSetting("type") == "multi-hot") {
|
|
76
|
+
let classes = this.aggrigatedInformation.multiClasses;
|
|
77
|
+
|
|
78
|
+
let sampleClasses = value;
|
|
79
|
+
if (typeof value == "string")
|
|
80
|
+
sampleClasses = JSON.parse(value);
|
|
81
|
+
let tensor = new Array<number>(classes.length);
|
|
82
|
+
tensor.fill(0);
|
|
83
|
+
|
|
84
|
+
for (let c of sampleClasses) {
|
|
85
|
+
let index = classes.indexOf(c);
|
|
86
|
+
tensor[index] = 1;
|
|
87
|
+
}
|
|
88
|
+
return tf.tensor(tensor);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public async inverse(tensor: TF.Tensor1D): Promise<string> {
|
|
93
|
+
if (this.getSetting("type") == "one-hot") {
|
|
94
|
+
return tf.tidy(() => {
|
|
95
|
+
let index = tf.argMax(tensor.reshape([1, tensor.shape[0]]), 1).arraySync()[0];
|
|
96
|
+
let clazz = this.aggrigatedInformation.classes[index];
|
|
97
|
+
return clazz;
|
|
98
|
+
})
|
|
99
|
+
} else if (this.getSetting("type") == "multi-hot") {
|
|
100
|
+
let values = tensor.arraySync();
|
|
101
|
+
let outClasses = [];
|
|
102
|
+
for (let i = 0; i < values.length; i++)
|
|
103
|
+
if (values[i] > 0.5)
|
|
104
|
+
outClasses.push(this.aggrigatedInformation.multiClasses[i])
|
|
105
|
+
return JSON.stringify(outClasses);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public getOutputElement(id: string): TemplateResult {
|
|
110
|
+
return html`<class-table id=${id}></class-table>`
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public getInputElement(id: string): TemplateResult {
|
|
114
|
+
return html`<class-input id=${id} classes=${JSON.stringify(this.getClasses())} multi=${this.settings.type == "multi-hot"}></class-input>`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public getClasses() : string[]{
|
|
118
|
+
if (this.getSetting("type") == "one-hot")
|
|
119
|
+
return this.aggrigatedInformation.classes;
|
|
120
|
+
if (this.getSetting("type") == "multi-hot")
|
|
121
|
+
return this.aggrigatedInformation.multiClasses;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public renderPredictionDifference(pred:TF.Tensor1D, label:TF.Tensor1D) {
|
|
125
|
+
return html`${until((async () => {
|
|
126
|
+
let predClass = await this.inverse(pred);
|
|
127
|
+
let labelClass = await this.inverse(label);
|
|
128
|
+
const relation = predClass != labelClass ? "≠" : "="
|
|
129
|
+
return html`${msg(str`Prediction: ${predClass} ${relation} Label: ${labelClass}`)}`;
|
|
130
|
+
})(), html`${msg("Visualizing Difference...")}`)}`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public getSettingElement(changeCallback): TemplateResult {
|
|
134
|
+
let that = this;
|
|
135
|
+
function typechange(e: Event) {
|
|
136
|
+
let element = e.target as SlSelect;
|
|
137
|
+
let type = element.value;
|
|
138
|
+
that.settings.type = type;
|
|
139
|
+
changeCallback(that.settings);
|
|
140
|
+
}
|
|
141
|
+
return html`
|
|
142
|
+
<sl-select label=${msg("Type")} @sl-change=${typechange} value=${this.settings.type}>
|
|
143
|
+
<sl-option value="one-hot">${msg("One Class per Sample")}</sl-option>
|
|
144
|
+
<sl-option value="multi-hot">${msg("Multiple Classes per Sample")}</sl-option>
|
|
145
|
+
</sl-select>`
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public renderTransformedValue(value: string): TemplateResult {
|
|
149
|
+
return html`${until(
|
|
150
|
+
(async () => {
|
|
151
|
+
let transformed = (await this.transform(value)).arraySync();
|
|
152
|
+
return html`${transformed.join(", ")}`;
|
|
153
|
+
})(), html`${msg("Loading...")}`)}`;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { FeatureSettings, FeatureTransformation } from './feature';
|
|
2
|
+
|
|
3
|
+
import type * as TF from "@tensorflow/tfjs";
|
|
4
|
+
import getTF from "../../lib/tflib";
|
|
5
|
+
let tf: typeof TF;
|
|
6
|
+
(async () => {tf = await getTF()})()
|
|
7
|
+
|
|
8
|
+
type RawSample = Record<string, string>
|
|
9
|
+
type TensorSample = Record<string, TF.Tensor>
|
|
10
|
+
|
|
11
|
+
type Constructor<T> = new (...args: any[]) => T;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* This class is a wrapper that references the original data and allows to access samples, batches and training/validation/testing splits
|
|
15
|
+
*/
|
|
16
|
+
export class Dataset {
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The original dataset
|
|
20
|
+
*/
|
|
21
|
+
protected dataset: Array<RawSample>;
|
|
22
|
+
/**
|
|
23
|
+
* The end of the training section (index excluded)
|
|
24
|
+
*/
|
|
25
|
+
public readonly trainingEnd: number;
|
|
26
|
+
/**
|
|
27
|
+
* The end of the validation section (index excluded)
|
|
28
|
+
*/
|
|
29
|
+
public readonly validationEnd: number;
|
|
30
|
+
/**
|
|
31
|
+
* The fields/column names of the dataset table
|
|
32
|
+
* (Note: This is captured only by the first entry of the datasets keys)
|
|
33
|
+
*/
|
|
34
|
+
public readonly fields: Array<string>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* This class is a wrapper that references the original data and allows to access samples, batches and training/validation/testing splits
|
|
38
|
+
*/
|
|
39
|
+
constructor(dataset: Array<RawSample>, trainingPercentile = 0.7, validationPercentile = 0.2, limit = 10000) {
|
|
40
|
+
this.dataset = dataset.slice(0, limit);
|
|
41
|
+
this.fields = Object.keys(dataset[0]);
|
|
42
|
+
tf.util.shuffle(this.dataset);
|
|
43
|
+
this.trainingEnd = Math.floor(this.dataset.length * trainingPercentile);
|
|
44
|
+
this.validationEnd = Math.floor(this.dataset.length * (trainingPercentile + validationPercentile));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns a sample from the dataset
|
|
49
|
+
* (note: the dataset is shuffled)
|
|
50
|
+
* @param id The id of the sample
|
|
51
|
+
*/
|
|
52
|
+
getSample(id: number): RawSample {
|
|
53
|
+
return this.dataset[id];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns all samples from the training portion of the dataset
|
|
58
|
+
*/
|
|
59
|
+
getSamples(): Array<RawSample> {
|
|
60
|
+
return this.dataset;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns all samples from the training portion of the dataset
|
|
65
|
+
*/
|
|
66
|
+
getTrainingSamples(): Array<RawSample> {
|
|
67
|
+
return this.dataset.slice(0, this.trainingEnd);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Returns all samples from the training portion of the dataset
|
|
72
|
+
*/
|
|
73
|
+
getValidationSamples(): Array<RawSample> {
|
|
74
|
+
return this.dataset.slice(this.trainingEnd, this.validationEnd);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Returns all samples from the training portion of the dataset
|
|
79
|
+
*/
|
|
80
|
+
getTestSamples(): Array<RawSample> {
|
|
81
|
+
return this.dataset.slice(this.validationEnd);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
length() {
|
|
85
|
+
return this.dataset.length;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* This class is a wrapper that references the original dataset and any accessed Sample will be transformed and delivered as tensors
|
|
91
|
+
*/
|
|
92
|
+
export class TransformedDataset {
|
|
93
|
+
public readonly dataset: Dataset;
|
|
94
|
+
public featureMaps: Record<string, FeatureTransformation> = {};
|
|
95
|
+
private cache: Array<TensorSample> = [];
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* This class is a wrapper that references the original dataset and any accessed Sample will be transformed and delivered as tensors
|
|
99
|
+
*/
|
|
100
|
+
constructor(dataset: Dataset, featureMaps: Record<string, Constructor<FeatureTransformation>>, settings: Record<string, FeatureSettings>) {
|
|
101
|
+
this.dataset = dataset;
|
|
102
|
+
for (let field in featureMaps) {
|
|
103
|
+
let FeatureTransformationConstructor = featureMaps[field];
|
|
104
|
+
try {
|
|
105
|
+
this.featureMaps[field] = new FeatureTransformationConstructor(dataset.getSamples().map(v => v[field]), settings[field])
|
|
106
|
+
} catch (e) {
|
|
107
|
+
console.log(e);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Transforms the raw sample into a tensor sample
|
|
114
|
+
* @param sample The sample that should be transformed
|
|
115
|
+
*/
|
|
116
|
+
async transformSample(sample: RawSample): Promise<TensorSample> {
|
|
117
|
+
let tensors: TensorSample = {}
|
|
118
|
+
let keys: string[] = [];
|
|
119
|
+
let promisses: Promise<TF.Tensor>[] = []
|
|
120
|
+
for (let key in sample) {
|
|
121
|
+
let transformation = this.featureMaps[key];
|
|
122
|
+
if (transformation) {
|
|
123
|
+
keys.push(key);
|
|
124
|
+
promisses.push(transformation.transform(sample[key]));
|
|
125
|
+
tensors[key] = await transformation.transform(sample[key])
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
let finished = await Promise.all(promisses);
|
|
129
|
+
for (let i = 0; i < keys.length; i++) {
|
|
130
|
+
tensors[keys[i]] = finished[i];
|
|
131
|
+
}
|
|
132
|
+
return tensors;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Transforms the raw sample into a tensor sample
|
|
137
|
+
* @param sample The sample that should be transformed
|
|
138
|
+
*/
|
|
139
|
+
async transformAndFilterSample(sample: RawSample, inFields: Set<string>, outFields: Set<string>): Promise<TensorSample> {
|
|
140
|
+
let tensors: TensorSample = {}
|
|
141
|
+
let keys: string[] = [];
|
|
142
|
+
let promisses: Promise<TF.Tensor>[] = []
|
|
143
|
+
for (let key in sample) {
|
|
144
|
+
if (!inFields.has(key) && !outFields.has(key))
|
|
145
|
+
continue;
|
|
146
|
+
let transformation = this.featureMaps[key];
|
|
147
|
+
if (transformation) {
|
|
148
|
+
keys.push(key);
|
|
149
|
+
promisses.push(transformation.transform(sample[key]));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
let finished = await Promise.all(promisses);
|
|
153
|
+
for (let i = 0; i < keys.length; i++) {
|
|
154
|
+
tensors[keys[i]] = finished[i];
|
|
155
|
+
}
|
|
156
|
+
return tensors;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Returns a sample from the dataset
|
|
161
|
+
* (note: the dataset is shuffled)
|
|
162
|
+
* @param id The id of the sample
|
|
163
|
+
*/
|
|
164
|
+
async getSample(id: number, enableCache=false): Promise<TensorSample> {
|
|
165
|
+
if (enableCache) {
|
|
166
|
+
function cloneSample(sample){
|
|
167
|
+
let out = {};
|
|
168
|
+
for (let field in sample) {
|
|
169
|
+
out[field] = sample[field].clone();
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (this.cache[id] != null) {
|
|
175
|
+
return cloneSample(this.cache[id])
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
let sample = this.dataset.getSample(id);
|
|
179
|
+
this.cache[id] = await this.transformSample(sample);
|
|
180
|
+
return cloneSample(this.cache[id]);
|
|
181
|
+
} else {
|
|
182
|
+
let sample = this.dataset.getSample(id);
|
|
183
|
+
return await this.transformSample(sample);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Returns a sample from the dataset
|
|
189
|
+
* (note: the dataset is shuffled)
|
|
190
|
+
* @param id The id of the sample
|
|
191
|
+
*/
|
|
192
|
+
async getSampleAsInputsOutputs(id: number, inFields: Set<string>, outFields: Set<string>, enableCache=false): Promise<{inputs:TensorSample, outputs:TensorSample}> {
|
|
193
|
+
if (enableCache) {
|
|
194
|
+
function cloneSample(sample){
|
|
195
|
+
let outputs = {};
|
|
196
|
+
let inputs = {};
|
|
197
|
+
for (let field in sample) {
|
|
198
|
+
if (inFields.has(field))
|
|
199
|
+
inputs["in_" + field] = sample[field].clone();
|
|
200
|
+
if (outFields.has(field))
|
|
201
|
+
outputs["out_" + field] = sample[field].clone();
|
|
202
|
+
}
|
|
203
|
+
return {inputs, outputs};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (this.cache[id] != null) {
|
|
207
|
+
return cloneSample(this.cache[id])
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
let sample = this.dataset.getSample(id);
|
|
211
|
+
this.cache[id] = await this.transformAndFilterSample(sample, inFields, outFields);
|
|
212
|
+
return cloneSample(this.cache[id]);
|
|
213
|
+
} else {
|
|
214
|
+
function mapSample(sample){
|
|
215
|
+
let outputs = {};
|
|
216
|
+
let inputs = {};
|
|
217
|
+
for (let field in sample) {
|
|
218
|
+
if (inFields.has(field))
|
|
219
|
+
inputs["in_" + field] = sample[field];
|
|
220
|
+
if (outFields.has(field))
|
|
221
|
+
outputs["out_" + field] = sample[field];
|
|
222
|
+
}
|
|
223
|
+
return {inputs, outputs};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let sample = this.dataset.getSample(id);
|
|
227
|
+
return mapSample(await this.transformAndFilterSample(sample, inFields, outFields));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Returns a generator for the training portion of the dataset
|
|
233
|
+
*/
|
|
234
|
+
getTrainingDataGenerator(inFields: Set<string>, outFields: Set<string>, enableCache=false): () => AsyncGenerator<{ xs: TensorSample; ys: TensorSample }> {
|
|
235
|
+
let that = this;
|
|
236
|
+
async function* dataGenerator() {
|
|
237
|
+
const numElements = that.dataset.trainingEnd;
|
|
238
|
+
let index = 0;
|
|
239
|
+
|
|
240
|
+
while (index < numElements) {
|
|
241
|
+
const sample = await that.getSampleAsInputsOutputs(index, inFields, outFields, enableCache);
|
|
242
|
+
yield {xs:sample.inputs, ys: sample.outputs};
|
|
243
|
+
index++;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return dataGenerator;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Returns a generator for the training portion of the dataset
|
|
251
|
+
*/
|
|
252
|
+
getValidationDataGenerator(inFields: Set<string>, outFields: Set<string>, enableCache=false): () => AsyncGenerator<{ xs: TensorSample; ys: TensorSample }> {
|
|
253
|
+
let that = this;
|
|
254
|
+
async function* dataGenerator() {
|
|
255
|
+
const numElements = that.dataset.validationEnd;
|
|
256
|
+
let index = that.dataset.trainingEnd;
|
|
257
|
+
while (index < numElements) {
|
|
258
|
+
const sample = await that.getSampleAsInputsOutputs(index, inFields, outFields, enableCache);
|
|
259
|
+
yield {xs:sample.inputs, ys: sample.outputs};
|
|
260
|
+
index++;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return dataGenerator;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Returns a generator for the training portion of the dataset
|
|
268
|
+
*/
|
|
269
|
+
getTestDataGenerator(inFields: Set<string>, outFields: Set<string>, enableCache=false): () => AsyncGenerator<{ xs: TensorSample; ys: TensorSample }> {
|
|
270
|
+
let that = this;
|
|
271
|
+
async function* dataGenerator() {
|
|
272
|
+
const numElements = that.dataset.length();
|
|
273
|
+
let index = that.dataset.validationEnd;
|
|
274
|
+
while (index < numElements) {
|
|
275
|
+
const sample = await that.getSampleAsInputsOutputs(index, inFields, outFields, enableCache);
|
|
276
|
+
yield {xs:sample.inputs, ys: sample.outputs};
|
|
277
|
+
index++;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return dataGenerator;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
clearCache() {
|
|
284
|
+
this.cache.forEach(function(sample) {
|
|
285
|
+
Object.values(sample).forEach(t => t.dispose())
|
|
286
|
+
})
|
|
287
|
+
this.cache = [];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
length() {
|
|
291
|
+
return this.dataset.length();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { TemplateResult } from "lit/html.js";
|
|
2
|
+
|
|
3
|
+
import type * as tf from "@tensorflow/tfjs";
|
|
4
|
+
|
|
5
|
+
export type NonEmptyArray<T> = [T, ...T[]];
|
|
6
|
+
export type FeatureSettings = Record<string, any> | null;
|
|
7
|
+
type Constructor<T> = new (...args: any[]) => T;
|
|
8
|
+
|
|
9
|
+
type FeatureTransformationRegistryEntry = {
|
|
10
|
+
name: string,
|
|
11
|
+
constructor: Constructor<FeatureTransformation>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns the available feature transformations registered by the classes
|
|
16
|
+
*/
|
|
17
|
+
export function getFeatureTransformations(): Array<FeatureTransformationRegistryEntry> {
|
|
18
|
+
return FeatureTransformation.registry;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Returns the available feature transformations registered by the classes
|
|
23
|
+
*/
|
|
24
|
+
export function getFeatureTransformation(name: string): FeatureTransformationRegistryEntry {
|
|
25
|
+
return FeatureTransformation.registry.find(e => e.name == name);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* This class describes a transformation function from a column in the dataset from string values to tensors
|
|
30
|
+
*/
|
|
31
|
+
export abstract class FeatureTransformation {
|
|
32
|
+
static registry: FeatureTransformationRegistryEntry[] = [];
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The information gathered from one run through all samples. This could be the max, avg or the list of unique classes.
|
|
36
|
+
*/
|
|
37
|
+
public aggrigatedInformation: Record<string, any> = {};
|
|
38
|
+
public readonly name: string;
|
|
39
|
+
protected settings: Record<string, any>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The constructor runs through the samples and aggregates needed information.
|
|
43
|
+
*/
|
|
44
|
+
public constructor(name: string, samples: Array<string>, settings: FeatureSettings) {
|
|
45
|
+
this.name = name;
|
|
46
|
+
if (settings)
|
|
47
|
+
this.settings = settings;
|
|
48
|
+
else
|
|
49
|
+
this.settings = {};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The size of tensor after the transformation
|
|
54
|
+
*/
|
|
55
|
+
public abstract size(): NonEmptyArray<number>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Takes a string value from the raw data and turns it into a tensor
|
|
59
|
+
* @param value The string value from the raw data that will be turned into a tensor
|
|
60
|
+
*/
|
|
61
|
+
public abstract transform(value: string): Promise<tf.Tensor>;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Will turn a tensor back into the original string
|
|
65
|
+
* (Note: Some transformations are not lossless or inversable and some approximation may occur on the inverse)
|
|
66
|
+
* @param tensor The tensor to turn into a string
|
|
67
|
+
*/
|
|
68
|
+
public abstract inverse(tensor: tf.Tensor): Promise<string>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Returns an interactive element with a getTensor() method, that will be used to input custom data for inference. It is important to set the id of the element.
|
|
72
|
+
*/
|
|
73
|
+
public abstract getInputElement(id:string): TemplateResult;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns an element with a setTensor() method, that will be used to display the output after inference. It is important to set the id of the element.
|
|
77
|
+
*/
|
|
78
|
+
public abstract getOutputElement(id:string): TemplateResult;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Returns the element that will be displayed and changes the settings of the transformation
|
|
82
|
+
*/
|
|
83
|
+
public abstract getSettingElement(changeCallback: (settings: FeatureSettings) => void, editMode: string): TemplateResult;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Returns an html element that will be displayed. Can be a simple text or an image etc.
|
|
87
|
+
*/
|
|
88
|
+
public abstract renderTransformedValue(value: string): TemplateResult;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Returns an html element that will be displayed. Can be a simple text or an image etc.
|
|
92
|
+
*/
|
|
93
|
+
public abstract renderPredictionDifference(pred: tf.Tensor, label: tf.Tensor): TemplateResult;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Returns a string representation of the original value for display in the feature engineering widget.
|
|
97
|
+
* Can be overridden by subclasses to provide custom formatting (e.g., converting base64 to array format).
|
|
98
|
+
* @param value The original value from the dataset
|
|
99
|
+
* @returns A string representation for display
|
|
100
|
+
*/
|
|
101
|
+
public renderOriginalValue(value: string | number[]): string {
|
|
102
|
+
if (typeof value != "string")
|
|
103
|
+
return JSON.stringify(value);
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public getSetting(name: string) {
|
|
108
|
+
return this.settings[name];
|
|
109
|
+
}
|
|
110
|
+
}
|