rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
Documentation
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! 学習曲線プロット機能
//! Training curve plotting functionality

use crate::error::{RusTorchError, RusTorchResult};
use crate::models::high_level::TrainingHistory;
use crate::visualization::{PlotData, PlotMetadata, Visualizable};
use num_traits::Float;
use std::path::Path;

/// プロットスタイル
/// Plot styles
#[derive(Debug, Clone, PartialEq)]
pub enum PlotStyle {
    /// 線グラフ
    Line,
    /// 点グラフ
    Scatter,
    /// 線+点グラフ
    LineScatter,
    /// 棒グラフ
    Bar,
    /// エリアグラフ
    Area,
}

/// チャートタイプ
/// Chart types
#[derive(Debug, Clone, PartialEq)]
pub enum ChartType {
    /// 単一グラフ
    Single,
    /// サブプロット
    Subplots,
    /// 重ね合わせ
    Overlay,
    /// ダッシュボード
    Dashboard,
}

/// プロット設定
/// Plot configuration
#[derive(Debug, Clone)]
pub struct PlotConfig {
    ///    /// Width
    pub width: u32,
    /// 高さ
    /// Height
    pub height: u32,
    /// DPI
    pub dpi: u32,
    /// チャートタイプ
    /// Chart type
    pub chart_type: ChartType,
    /// 背景色
    /// Background color
    pub background_color: String,
    /// フォントサイズ
    /// Font size
    pub font_size: u32,
    /// ライン幅
    /// Line width
    pub line_width: f32,
    /// マーカーサイズ
    /// Marker size
    pub marker_size: f32,
}

impl Default for PlotConfig {
    fn default() -> Self {
        Self {
            width: 800,
            height: 600,
            dpi: 300,
            chart_type: ChartType::Single,
            background_color: "#ffffff".to_string(),
            font_size: 12,
            line_width: 2.0,
            marker_size: 4.0,
        }
    }
}

/// 学習曲線プロッター
/// Training curve plotter
#[derive(Debug)]
pub struct TrainingPlotter {
    /// プロット設定
    /// Plot configuration
    pub config: PlotConfig,
    /// メタデータ
    /// Metadata
    pub metadata: PlotMetadata,
}

impl TrainingPlotter {
    /// 新しいプロッターを作成
    /// Create a new plotter
    pub fn new() -> Self {
        Self {
            config: PlotConfig::default(),
            metadata: PlotMetadata::new(),
        }
    }

    /// 設定付きプロッターを作成
    /// Create plotter with configuration
    pub fn with_config(config: PlotConfig) -> Self {
        Self {
            config,
            metadata: PlotMetadata::new(),
        }
    }

    /// メタデータを設定
    /// Set metadata
    pub fn with_metadata(mut self, metadata: PlotMetadata) -> Self {
        self.metadata = metadata;
        self
    }

    /// 学習曲線をプロット
    /// Plot training curves
    pub fn plot_training_curves<T>(&self, history: &TrainingHistory<T>) -> RusTorchResult<String>
    where
        T: Float + std::fmt::Display + std::fmt::Debug,
    {
        // プロットデータの準備
        let _plot_data = history.to_plot_data()?;

        // SVG形式でプロットを生成
        let mut svg_content = self.generate_svg_header();

        // 学習損失プロット
        if !history.train_loss.is_empty() {
            let train_loss_data =
                self.prepare_loss_data(&history.train_loss, "Training Loss", "#1f77b4")?;
            svg_content.push_str(&self.render_line_plot(&train_loss_data)?);
        }

        // 検証損失プロット
        if !history.val_loss.is_empty() {
            let val_loss_data =
                self.prepare_loss_data(&history.val_loss, "Validation Loss", "#ff7f0e")?;
            svg_content.push_str(&self.render_line_plot(&val_loss_data)?);
        }

        // メトリクスプロット
        if !history.metrics.is_empty() {
            svg_content.push_str(&self.render_metrics_plots(history)?);
        }

        svg_content.push_str("</svg>");

        Ok(svg_content)
    }

    /// 損失比較プロット
    /// Plot loss comparison
    pub fn plot_loss_comparison<T>(
        &self,
        histories: Vec<(&str, &TrainingHistory<T>)>,
    ) -> RusTorchResult<String>
    where
        T: Float + std::fmt::Display + std::fmt::Debug,
    {
        let mut svg_content = self.generate_svg_header();

        let colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd"];

        for (i, (name, history)) in histories.iter().enumerate() {
            if !history.train_loss.is_empty() {
                let color = colors.get(i % colors.len()).unwrap_or(&"#000000");
                let train_data = self.prepare_loss_data(
                    &history.train_loss,
                    &format!("{} - Training", name),
                    color,
                )?;
                svg_content.push_str(&self.render_line_plot(&train_data)?);
            }

            if !history.val_loss.is_empty() {
                let color = colors.get(i % colors.len()).unwrap_or(&"#000000");
                let val_data = self.prepare_loss_data(
                    &history.val_loss,
                    &format!("{} - Validation", name),
                    color,
                )?;
                svg_content.push_str(&self.render_line_plot(&val_data)?);
            }
        }

        svg_content.push_str("</svg>");
        Ok(svg_content)
    }

    /// メトリクス時系列プロット
    /// Plot metrics over time
    pub fn plot_metrics_timeline<T>(
        &self,
        history: &TrainingHistory<T>,
        metric_name: &str,
    ) -> RusTorchResult<String>
    where
        T: Float + std::fmt::Display + std::fmt::Debug,
    {
        let mut svg_content = self.generate_svg_header();

        // 指定されたメトリクスのデータを抽出
        let metric_values = self.extract_metric_values(history, metric_name)?;

        if !metric_values.is_empty() {
            let epochs: Vec<f32> = (1..=metric_values.len()).map(|i| i as f32).collect();
            let plot_data = PlotData::new(epochs, metric_values, metric_name.to_string())
                .with_color("#2ca02c".to_string())
                .with_style(PlotStyle::LineScatter);

            svg_content.push_str(&self.render_line_plot(&plot_data)?);
        }

        svg_content.push_str("</svg>");
        Ok(svg_content)
    }

    /// ファイルに保存
    /// Save to file
    pub fn save_plot<P: AsRef<Path>>(&self, content: &str, path: P) -> RusTorchResult<()> {
        std::fs::write(path, content)?;
        Ok(())
    }

    // プライベートヘルパーメソッド

    fn generate_svg_header(&self) -> String {
        format!(
            r#"<svg width="{}" height="{}" xmlns="http://www.w3.org/2000/svg">
<style>
.axis {{ stroke: #333; stroke-width: 1; }}
.grid {{ stroke: #ddd; stroke-width: 0.5; stroke-dasharray: 3,3; }}
.line {{ fill: none; stroke-width: {}; }}
.text {{ font-family: Arial, sans-serif; font-size: {}px; fill: #333; }}
.title {{ font-size: {}px; font-weight: bold; text-anchor: middle; }}
.legend {{ font-size: 10px; }}
</style>
<rect width="100%" height="100%" fill="{}"/>
"#,
            self.config.width,
            self.config.height,
            self.config.line_width,
            self.config.font_size,
            self.config.font_size + 4,
            self.config.background_color
        )
    }

    fn prepare_loss_data<T>(
        &self,
        loss_data: &[T],
        label: &str,
        color: &str,
    ) -> RusTorchResult<PlotData<f32>>
    where
        T: Float + std::fmt::Display,
    {
        let epochs: Vec<f32> = (1..=loss_data.len()).map(|i| i as f32).collect();
        let losses: Vec<f32> = loss_data
            .iter()
            .map(|&loss| loss.to_f32().unwrap_or(0.0))
            .collect();

        Ok(PlotData::new(epochs, losses, label.to_string())
            .with_color(color.to_string())
            .with_style(PlotStyle::Line))
    }

    fn render_line_plot(&self, data: &PlotData<f32>) -> RusTorchResult<String> {
        if data.x_data.len() != data.y_data.len() || data.x_data.is_empty() {
            return Err(RusTorchError::InvalidDataFormat(
                "X and Y data must have the same non-zero length".to_string(),
            )
            .into());
        }

        // データの正規化とスケーリング
        let margin = 50.0;
        let plot_width = self.config.width as f32 - 2.0 * margin;
        let plot_height = self.config.height as f32 - 2.0 * margin;

        let x_min = data.x_data.iter().cloned().fold(f32::INFINITY, f32::min);
        let x_max = data
            .x_data
            .iter()
            .cloned()
            .fold(f32::NEG_INFINITY, f32::max);
        let y_min = data.y_data.iter().cloned().fold(f32::INFINITY, f32::min);
        let y_max = data
            .y_data
            .iter()
            .cloned()
            .fold(f32::NEG_INFINITY, f32::max);

        let x_range = if x_max > x_min { x_max - x_min } else { 1.0 };
        let y_range = if y_max > y_min { y_max - y_min } else { 1.0 };

        let mut path_data = String::new();

        for (i, (&x, &y)) in data.x_data.iter().zip(data.y_data.iter()).enumerate() {
            let screen_x = margin + (x - x_min) / x_range * plot_width;
            let screen_y = margin + plot_height - (y - y_min) / y_range * plot_height;

            if i == 0 {
                path_data.push_str(&format!("M {} {}", screen_x, screen_y));
            } else {
                path_data.push_str(&format!(" L {} {}", screen_x, screen_y));
            }
        }

        let default_color = "#1f77b4".to_string();
        let color = data.color.as_ref().unwrap_or(&default_color);

        Ok(format!(
            r#"<path d="{}" class="line" stroke="{}" stroke-width="{}"/>
"#,
            path_data, color, self.config.line_width
        ))
    }

    fn render_metrics_plots<T>(&self, _history: &TrainingHistory<T>) -> RusTorchResult<String>
    where
        T: Float + std::fmt::Display + std::fmt::Debug,
    {
        // メトリクスプロットの実装(簡略化)
        Ok(String::new())
    }

    fn extract_metric_values<T>(
        &self,
        history: &TrainingHistory<T>,
        metric_name: &str,
    ) -> RusTorchResult<Vec<f32>>
    where
        T: Float + std::fmt::Display,
    {
        if let Some(metric_values) = history.metrics.get(metric_name) {
            let values: Vec<f32> = metric_values.iter().map(|&v| v as f32).collect();
            if values.is_empty() {
                return Err(RusTorchError::InvalidDataFormat(format!(
                    "Metric '{}' has no values",
                    metric_name
                ))
                .into());
            }
            Ok(values)
        } else {
            Err(RusTorchError::InvalidDataFormat(format!(
                "Metric '{}' not found in training history",
                metric_name
            ))
            .into())
        }
    }
}

impl Default for TrainingPlotter {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Float + std::fmt::Display + std::fmt::Debug> Visualizable<T> for TrainingHistory<T> {
    fn to_plot_data(&self) -> RusTorchResult<PlotData<T>> {
        if self.train_loss.is_empty() {
            return Err(RusTorchError::InvalidDataFormat(
                "Training history contains no data".to_string(),
            )
            .into());
        }

        let epochs: Vec<T> = (1..=self.train_loss.len())
            .map(|i| T::from(i).unwrap())
            .collect();

        Ok(PlotData::new(
            epochs,
            self.train_loss.clone(),
            "Training Loss".to_string(),
        ))
    }

    fn validate_config(&self, _config: &PlotConfig) -> RusTorchResult<()> {
        if self.train_loss.is_empty() {
            return Err(RusTorchError::ConfigError(
                "Cannot plot empty training history".to_string(),
            )
            .into());
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_plot_config_default() {
        let config = PlotConfig::default();
        assert_eq!(config.width, 800);
        assert_eq!(config.height, 600);
        assert_eq!(config.dpi, 300);
        assert_eq!(config.chart_type, ChartType::Single);
    }

    #[test]
    fn test_training_plotter_creation() {
        let plotter = TrainingPlotter::new();
        assert_eq!(plotter.config.width, 800);
        assert_eq!(plotter.config.height, 600);
    }

    #[test]
    fn test_plot_data_creation() {
        let x_data = vec![1.0, 2.0, 3.0];
        let y_data = vec![0.5, 0.3, 0.1];
        let plot_data = PlotData::new(x_data.clone(), y_data.clone(), "Test".to_string())
            .with_color("#ff0000".to_string())
            .with_style(PlotStyle::LineScatter);

        assert_eq!(plot_data.x_data, x_data);
        assert_eq!(plot_data.y_data, y_data);
        assert_eq!(plot_data.label, "Test");
        assert_eq!(plot_data.color, Some("#ff0000".to_string()));
        assert_eq!(plot_data.style, PlotStyle::LineScatter);
    }

    #[test]
    fn test_training_history_visualization() {
        let mut history = TrainingHistory::<f32>::new();
        history.add_epoch(0.8, Some(0.7), HashMap::new());
        history.add_epoch(0.6, Some(0.5), HashMap::new());
        history.add_epoch(0.4, Some(0.3), HashMap::new());

        let plot_data = history.to_plot_data().unwrap();
        assert_eq!(plot_data.x_data.len(), 3);
        assert_eq!(plot_data.y_data.len(), 3);
        assert_eq!(plot_data.label, "Training Loss");
    }

    #[test]
    fn test_svg_generation() {
        let plotter = TrainingPlotter::new();
        let mut history = TrainingHistory::<f32>::new();
        history.add_epoch(0.8, Some(0.7), HashMap::new());
        history.add_epoch(0.6, Some(0.5), HashMap::new());

        let svg_result = plotter.plot_training_curves(&history);
        assert!(svg_result.is_ok());

        let svg_content = svg_result.unwrap();
        assert!(svg_content.contains("<svg"));
        assert!(svg_content.contains("</svg>"));
    }
}