File size: 7,162 Bytes
4c1a791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30d27e9
4c1a791
 
 
 
30d27e9
4c1a791
 
 
 
 
 
30d27e9
4c1a791
 
 
 
30d27e9
4c1a791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30d27e9
4c1a791
 
 
 
 
30d27e9
4c1a791
 
 
 
 
 
 
 
 
 
 
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
let ws;
let lossChart;
let accuracyChart;

function showTrainingForm(type) {
    const singleForm = document.getElementById('single-model-form');
    const compareForm = document.getElementById('compare-models-form');
    
    if (type === 'single') {
        singleForm.classList.remove('hidden');
        compareForm.classList.add('hidden');
    } else {
        singleForm.classList.add('hidden');
        compareForm.classList.remove('hidden');
    }
}

function initializeCharts() {
    const lossData = [{
        name: 'Training Loss',
        x: [],
        y: [],
        type: 'scatter'
    }, {
        name: 'Validation Loss',
        x: [],
        y: [],
        type: 'scatter'
    }];

    const accuracyData = [{
        name: 'Training Accuracy',
        x: [],
        y: [],
        type: 'scatter'
    }, {
        name: 'Validation Accuracy',
        x: [],
        y: [],
        type: 'scatter'
    }];

    Plotly.newPlot('loss-plot', lossData, {
        title: 'Training and Validation Loss',
        xaxis: { title: 'Iterations' },
        yaxis: { title: 'Loss' }
    });

    Plotly.newPlot('accuracy-plot', accuracyData, {
        title: 'Training and Validation Accuracy',
        xaxis: { title: 'Iterations' },
        yaxis: { title: 'Accuracy (%)' }
    });
}

function updateCharts(data) {
    const iteration = data.epoch * data.batch;
    
    Plotly.extendTraces('loss-plot', {
        x: [[iteration], [iteration]],
        y: [[data.train_loss], [data.val_loss]]
    }, [0, 1]);

    Plotly.extendTraces('accuracy-plot', {
        x: [[iteration], [iteration]],
        y: [[data.train_acc], [data.val_acc]]
    }, [0, 1]);

    // Update training logs
    const logsDiv = document.getElementById('training-logs');
    logsDiv.innerHTML = `
        <p>Epoch: ${data.epoch + 1}</p>
        <p>Training Loss: ${data.train_loss.toFixed(4)}</p>
        <p>Training Accuracy: ${data.train_acc.toFixed(2)}%</p>
        <p>Validation Loss: ${data.val_loss.toFixed(4)}</p>
        <p>Validation Accuracy: ${data.val_acc.toFixed(2)}%</p>
    `;
}

async function trainSingleModel() {
    const config = {
        kernels: [
            parseInt(document.getElementById('kernel1').value),
            parseInt(document.getElementById('kernel2').value),
            parseInt(document.getElementById('kernel3').value)
        ],
        optimizer: document.getElementById('optimizer').value,
        batch_size: parseInt(document.getElementById('batch_size').value),
        epochs: parseInt(document.getElementById('epochs').value)
    };

    // Show progress section and initialize charts
    document.getElementById('training-progress').classList.remove('hidden');
    initializeCharts();

    // Connect to WebSocket
    ws = new WebSocket(`ws://${window.location.host}/ws/train`);
    ws.onmessage = function(event) {
        const data = JSON.parse(event.data);
        updateCharts(data);
    };

    try {
        const response = await fetch('/api/train_single', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(config)
        });
        const data = await response.json();
        
        if (data.status === 'success') {
            alert('Training completed successfully!');
        }
    } catch (error) {
        console.error('Error:', error);
        alert('Error during training. Please check console for details.');
    }
}

async function compareModels() {
    const config = {
        model1: {
            kernels: [
                parseInt(document.getElementById('model1_kernel1').value),
                parseInt(document.getElementById('model1_kernel2').value),
                parseInt(document.getElementById('model1_kernel3').value)
            ],
            optimizer: document.getElementById('model1_optimizer').value,
            batch_size: parseInt(document.getElementById('model1_batch_size').value),
            epochs: parseInt(document.getElementById('model1_epochs').value)
        },
        model2: {
            kernels: [
                parseInt(document.getElementById('model2_kernel1').value),
                parseInt(document.getElementById('model2_kernel2').value),
                parseInt(document.getElementById('model2_kernel3').value)
            ],
            optimizer: document.getElementById('model2_optimizer').value,
            batch_size: parseInt(document.getElementById('model2_batch_size').value),
            epochs: parseInt(document.getElementById('model2_epochs').value)
        }
    };

    // Show comparison progress section
    document.getElementById('comparison-progress').classList.remove('hidden');
    initializeComparisonCharts();

    try {
        const response = await fetch('/api/train_compare', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(config)
        });
        const data = await response.json();
        
        if (data.status === 'success') {
            displayComparisonResults(data);
            alert('Model comparison completed successfully!');
        }
    } catch (error) {
        console.error('Error:', error);
        alert('Error during model comparison. Please check console for details.');
    }
}

function initializeComparisonCharts() {
    const lossData = [{
        name: 'Model A Loss',
        x: [],
        y: [],
        type: 'scatter'
    }, {
        name: 'Model B Loss',
        x: [],
        y: [],
        type: 'scatter'
    }];

    const accuracyData = [{
        name: 'Model A Accuracy',
        x: [],
        y: [],
        type: 'scatter'
    }, {
        name: 'Model B Accuracy',
        x: [],
        y: [],
        type: 'scatter'
    }];

    Plotly.newPlot('comparison-loss-plot', lossData, {
        title: 'Loss Comparison',
        xaxis: { title: 'Iterations' },
        yaxis: { title: 'Loss' }
    });

    Plotly.newPlot('comparison-accuracy-plot', accuracyData, {
        title: 'Accuracy Comparison',
        xaxis: { title: 'Iterations' },
        yaxis: { title: 'Accuracy (%)' }
    });
}

function displayComparisonResults(data) {
    const logsDiv = document.getElementById('comparison-logs');
    logsDiv.innerHTML = `
        <div class="comparison-model">
            <h4>Model A</h4>
            <p>Final Loss: ${data.model1_results.history.train_loss.slice(-1)[0].toFixed(4)}</p>
            <p>Final Accuracy: ${data.model1_results.history.train_acc.slice(-1)[0].toFixed(2)}%</p>
            <p>Model Name: ${data.model1_results.model_name}</p>
        </div>
        <div class="comparison-model">
            <h4>Model B</h4>
            <p>Final Loss: ${data.model2_results.history.train_loss.slice(-1)[0].toFixed(4)}</p>
            <p>Final Accuracy: ${data.model2_results.history.train_acc.slice(-1)[0].toFixed(2)}%</p>
            <p>Model Name: ${data.model2_results.model_name}</p>
        </div>
    `;
}

function displayResults(data) {
    const resultsDiv = document.getElementById('training-results');
    // Display training results
}