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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
//! f32統一ハイブリッドニューラルネットワークモジュール
//! f32 Unified Hybrid Neural Network Module
//!
//! フェーズ5: 高度ニューラルネットワーク機能
//! Phase 5: Advanced Neural Network Features
//!
//! このモジュールは、f32精度で最適化されたニューラルネットワーク機能を提供します。
//! Neural Engine、Metal GPU、CPUでの統一実行をサポートし、変換コストゼロを実現します。

use crate::error::{RusTorchError, RusTorchResult};
use crate::hybrid_f32::tensor::core::F32Tensor;
use std::collections::HashMap;

/// f32統一ニューラルネットワーク層の基底トレイト
/// Base trait for f32 unified neural network layers
pub trait F32Layer: std::fmt::Debug + Send + Sync {
    /// 順伝播
    /// Forward pass
    fn forward(&mut self, input: &F32Tensor) -> RusTorchResult<F32Tensor>;

    /// 逆伝播(勾配計算)
    /// Backward pass (gradient computation)
    fn backward(&mut self, grad_output: &F32Tensor) -> RusTorchResult<F32Tensor>;

    /// パラメータの取得
    /// Get parameters
    fn parameters(&self) -> Vec<&F32Tensor>;

    /// パラメータの更新
    /// Update parameters
    fn update_parameters(&mut self, learning_rate: f32) -> RusTorchResult<()>;
}

/// f32線形層(全結合層)
/// f32 Linear layer (fully connected layer)
#[derive(Debug, Clone)]
pub struct F32Linear {
    pub weight: F32Tensor,
    pub bias: Option<F32Tensor>,
    pub input_features: usize,
    pub output_features: usize,

    // 勾配記録用
    weight_grad: Option<F32Tensor>,
    bias_grad: Option<F32Tensor>,
    last_input: Option<F32Tensor>,
}

impl F32Linear {
    /// 新しい線形層を作成
    /// Create new linear layer
    pub fn new(input_features: usize, output_features: usize, bias: bool) -> RusTorchResult<Self> {
        // Xavier初期化
        let scale = (2.0 / (input_features + output_features) as f32).sqrt();
        let weight_data: Vec<f32> = (0..input_features * output_features)
            .map(|_| (rand::random::<f32>() - 0.5) * 2.0 * scale)
            .collect();

        let weight = F32Tensor::from_vec(weight_data, &[output_features, input_features])?;

        let bias = if bias {
            Some(F32Tensor::zeros(&[output_features])?)
        } else {
            None
        };

        Ok(Self {
            weight,
            bias,
            input_features,
            output_features,
            weight_grad: None,
            bias_grad: None,
            last_input: None,
        })
    }

    /// 重みを手動設定
    /// Set weights manually
    pub fn set_weight(&mut self, weight: F32Tensor) -> RusTorchResult<()> {
        if weight.shape() != &[self.output_features, self.input_features] {
            return Err(format!(
                "Weight shape mismatch: expected [{}, {}], got {:?}",
                self.output_features,
                self.input_features,
                weight.shape()
            )
            .into());
        }
        self.weight = weight;
        Ok(())
    }

    /// バイアスを手動設定
    /// Set bias manually
    pub fn set_bias(&mut self, bias: F32Tensor) -> RusTorchResult<()> {
        if bias.shape() != &[self.output_features] {
            return Err(format!(
                "Bias shape mismatch: expected [{}], got {:?}",
                self.output_features,
                bias.shape()
            )
            .into());
        }
        self.bias = Some(bias);
        Ok(())
    }
}

impl F32Layer for F32Linear {
    fn forward(&mut self, input: &F32Tensor) -> RusTorchResult<F32Tensor> {
        // 入力を記録(逆伝播用)
        self.last_input = Some(input.clone());

        // 線形変換: output = input @ weight.T + bias
        let output = input.matmul(&self.weight.transpose()?)?;

        if let Some(ref bias) = self.bias {
            output.add(bias)
        } else {
            Ok(output)
        }
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> RusTorchResult<F32Tensor> {
        if let Some(ref last_input) = self.last_input {
            // 重みの勾配: weight_grad = grad_output.T @ input
            self.weight_grad = Some(grad_output.transpose()?.matmul(last_input)?);

            // バイアスの勾配: bias_grad = sum(grad_output, dim=0)
            if self.bias.is_some() {
                self.bias_grad = Some(grad_output.sum_dim(0)?);
            }

            // 入力の勾配: input_grad = grad_output @ weight
            grad_output.matmul(&self.weight)
        } else {
            Err("No forward pass recorded for backward computation".into())
        }
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = vec![&self.weight];
        if let Some(ref bias) = self.bias {
            params.push(bias);
        }
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> RusTorchResult<()> {
        // 重みの更新
        if let Some(ref weight_grad) = self.weight_grad {
            let lr_tensor = F32Tensor::from_scalar(learning_rate)?;
            let update = weight_grad.mul(&lr_tensor)?;
            self.weight = self.weight.sub(&update)?;
        }

        // バイアスの更新
        if let (Some(ref mut bias), Some(ref bias_grad)) = (&mut self.bias, &self.bias_grad) {
            let lr_tensor = F32Tensor::from_scalar(learning_rate)?;
            let update = bias_grad.mul(&lr_tensor)?;
            *bias = bias.sub(&update)?;
        }

        // 勾配をクリア
        self.weight_grad = None;
        self.bias_grad = None;

        Ok(())
    }
}

/// f32活性化関数
/// f32 Activation functions
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum F32Activation {
    ReLU,
    Sigmoid,
    Tanh,
    LeakyReLU(f32), // slope parameter
    GELU,
    Softmax,
}

impl F32Activation {
    /// 活性化関数の適用
    /// Apply activation function
    pub fn forward(&self, input: &F32Tensor) -> RusTorchResult<F32Tensor> {
        match self {
            F32Activation::ReLU => input.relu(),
            F32Activation::Sigmoid => input.sigmoid(),
            F32Activation::Tanh => input.tanh(),
            F32Activation::LeakyReLU(slope) => {
                let zero = F32Tensor::zeros(input.shape())?;
                let positive = input.maximum(&zero)?;
                let negative = input
                    .minimum(&zero)?
                    .mul(&F32Tensor::from_scalar(*slope)?)?;
                positive.add(&negative)
            }
            F32Activation::GELU => {
                // GELU(x) = 0.5 * x * (1 + tanh(sqrt(2/π) * (x + 0.044715 * x^3)))
                let sqrt_2_pi = F32Tensor::from_scalar(0.7978845608f32)?; // sqrt(2/π)
                let coeff = F32Tensor::from_scalar(0.044715f32)?;
                let half = F32Tensor::from_scalar(0.5f32)?;
                let one = F32Tensor::from_scalar(1.0f32)?;

                let x_cubed = input.power(3.0f32)?;
                let inner = input.add(&x_cubed.mul(&coeff)?)?;
                let scaled = inner.mul(&sqrt_2_pi)?;
                let tanh_val = scaled.tanh()?;
                let one_plus_tanh = one.add(&tanh_val)?;

                input.mul(&half)?.mul(&one_plus_tanh)
            }
            F32Activation::Softmax => input.softmax(None),
        }
    }

    /// 活性化関数の導関数
    /// Derivative of activation function
    pub fn backward(
        &self,
        input: &F32Tensor,
        grad_output: &F32Tensor,
    ) -> RusTorchResult<F32Tensor> {
        let derivative = match self {
            F32Activation::ReLU => {
                let zero = F32Tensor::zeros(input.shape())?;
                let one = F32Tensor::ones(input.shape())?;
                input.gt(&zero)?
            }
            F32Activation::Sigmoid => {
                let sigmoid_out = input.sigmoid()?;
                let one = F32Tensor::from_scalar(1.0f32)?;
                let one_minus_sigmoid = one.sub(&sigmoid_out)?;
                sigmoid_out.mul(&one_minus_sigmoid)?
            }
            F32Activation::Tanh => {
                let tanh_out = input.tanh()?;
                let one = F32Tensor::from_scalar(1.0f32)?;
                let tanh_squared = tanh_out.power(2.0f32)?;
                one.sub(&tanh_squared)?
            }
            F32Activation::LeakyReLU(slope) => {
                let zero = F32Tensor::zeros(input.shape())?;
                let one = F32Tensor::ones(input.shape())?;
                let slope_tensor = F32Tensor::from_scalar(*slope)?;
                let positive_mask = input.gt(&zero)?;
                let negative_mask = input.le(&zero)?;
                positive_mask.add(&negative_mask)?
            }
            F32Activation::GELU => {
                // Approximate GELU derivative
                let sqrt_2_pi = F32Tensor::from_scalar(0.7978845608f32)?;
                let coeff = F32Tensor::from_scalar(0.044715f32)?;
                let half = F32Tensor::from_scalar(0.5f32)?;
                let one = F32Tensor::from_scalar(1.0f32)?;
                let three = F32Tensor::from_scalar(3.0f32)?;

                let x_squared = input.power(2.0f32)?;
                let three_coeff_x_squared = three.mul(&coeff)?.mul(&x_squared)?;
                let derivative_inner = one.add(&three_coeff_x_squared)?;
                let tanh_derivative = derivative_inner.mul(&sqrt_2_pi)?;

                // Simplified approximation
                let sigmoid_approx = input.mul(&F32Tensor::from_scalar(1.702f32)?)?.sigmoid()?;
                sigmoid_approx
            }
            F32Activation::Softmax => {
                // For softmax, the derivative is more complex and depends on the specific use case
                // For now, return identity (this is a simplification)
                F32Tensor::ones(input.shape())?
            }
        };

        grad_output.mul(&derivative)
    }
}

/// f32損失関数
/// f32 Loss functions
#[derive(Debug, Clone)]
pub enum F32Loss {
    MeanSquaredError,
    CrossEntropy,
    BinaryCrossEntropy,
}

impl F32Loss {
    /// 損失の計算
    /// Compute loss
    pub fn forward(
        &self,
        predictions: &F32Tensor,
        targets: &F32Tensor,
    ) -> RusTorchResult<F32Tensor> {
        match self {
            F32Loss::MeanSquaredError => {
                let diff = predictions.sub(targets)?;
                let squared = diff.power(2.0f32)?;
                squared.mean_tensor()
            }
            F32Loss::CrossEntropy => {
                // Softmax + Cross-entropy
                let exp_preds = predictions.exp()?;
                let sum_exp = exp_preds.sum_dim(predictions.shape().len() - 1)?;
                let log_softmax = predictions.sub(&sum_exp.log()?)?;
                let nll = log_softmax
                    .mul(targets)?
                    .sum_dim(predictions.shape().len() - 1)?;
                let neg_nll = nll.mul(&F32Tensor::from_scalar(-1.0f32)?)?;
                neg_nll.mean_tensor()
            }
            F32Loss::BinaryCrossEntropy => {
                let eps = F32Tensor::from_scalar(1e-7f32)?;
                let one = F32Tensor::from_scalar(1.0f32)?;

                let clamped_preds = predictions.clamp(1e-7f32, 1.0f32 - 1e-7f32)?;
                let log_preds = clamped_preds.log()?;
                let log_one_minus_preds = one.sub(&clamped_preds)?.log()?;

                let term1 = targets.mul(&log_preds)?;
                let term2 = one.sub(targets)?.mul(&log_one_minus_preds)?;
                let loss_per_sample = term1.add(&term2)?.mul(&F32Tensor::from_scalar(-1.0f32)?)?;

                loss_per_sample.mean_tensor()
            }
        }
    }

    /// 損失計算(compute_lossエイリアス)
    /// Compute loss (alias for forward)
    pub fn compute_loss(
        &self,
        predictions: &F32Tensor,
        targets: &F32Tensor,
    ) -> RusTorchResult<F32Tensor> {
        self.forward(predictions, targets)
    }

    /// 損失の勾配
    /// Loss gradient
    pub fn backward(
        &self,
        predictions: &F32Tensor,
        targets: &F32Tensor,
    ) -> RusTorchResult<F32Tensor> {
        match self {
            F32Loss::MeanSquaredError => {
                let diff = predictions.sub(targets)?;
                let batch_size = predictions.shape()[0] as f32;
                let scale = F32Tensor::from_scalar(2.0f32 / batch_size)?;
                diff.mul(&scale)
            }
            F32Loss::CrossEntropy => {
                // Softmax gradient
                let exp_preds = predictions.exp()?;
                let sum_exp = exp_preds.sum_dim(predictions.shape().len() - 1)?;
                let softmax = exp_preds.divide(&sum_exp)?;
                let batch_size = predictions.shape()[0] as f32;
                let scale = F32Tensor::from_scalar(1.0f32 / batch_size)?;
                softmax.sub(targets)?.mul(&scale)
            }
            F32Loss::BinaryCrossEntropy => {
                let eps = F32Tensor::from_scalar(1e-7f32)?;
                let one = F32Tensor::from_scalar(1.0f32)?;

                let clamped_preds = predictions.clamp(1e-7f32, 1.0f32 - 1e-7f32)?;
                let batch_size = predictions.shape()[0] as f32;
                let scale = F32Tensor::from_scalar(1.0f32 / batch_size)?;

                let term1 = targets.divide(&clamped_preds)?;
                let term2 = one.sub(targets)?.divide(&one.sub(&clamped_preds)?)?;
                let gradient = term2.sub(&term1)?;

                gradient.mul(&scale)
            }
        }
    }
}

/// f32多層パーセプトロン
/// f32 Multi-Layer Perceptron
#[derive(Debug, Clone)]
pub struct F32MLP {
    pub layers: Vec<F32Linear>,
    pub activations: Vec<F32Activation>,
    pub layer_outputs: Vec<F32Tensor>, // Forward pass記録用
}

impl F32MLP {
    /// 新しいMLPを作成
    /// Create new MLP
    pub fn new(layer_sizes: &[usize], activation: F32Activation) -> RusTorchResult<Self> {
        let mut layers = Vec::new();
        let mut activations = Vec::new();

        for i in 0..layer_sizes.len() - 1 {
            let layer = F32Linear::new(layer_sizes[i], layer_sizes[i + 1], true)?;
            layers.push(layer);

            if i < layer_sizes.len() - 2 {
                activations.push(activation.clone());
            }
        }

        Ok(Self {
            layers,
            activations,
            layer_outputs: Vec::new(),
        })
    }

    /// 順伝播
    /// Forward pass
    pub fn forward(&mut self, input: &F32Tensor) -> RusTorchResult<F32Tensor> {
        self.layer_outputs.clear();
        let mut current = input.clone();

        for (i, layer) in self.layers.iter_mut().enumerate() {
            current = layer.forward(&current)?;
            self.layer_outputs.push(current.clone());

            if i < self.activations.len() {
                current = self.activations[i].forward(&current)?;
            }
        }

        Ok(current)
    }

    /// パラメータ数を取得
    /// Get parameter count
    pub fn parameter_count(&self) -> usize {
        self.layers
            .iter()
            .map(|layer| {
                let weight_params = layer.weight.numel();
                let bias_params = layer.bias.as_ref().map_or(0, |b| b.numel());
                weight_params + bias_params
            })
            .sum()
    }

    /// 全パラメータを取得
    /// Get all parameters
    pub fn parameters(&self) -> Vec<&F32Tensor> {
        self.layers
            .iter()
            .flat_map(|layer| layer.parameters())
            .collect()
    }
}

/// f32最適化器
/// f32 Optimizers
#[derive(Debug, Clone)]
pub enum F32Optimizer {
    SGD {
        learning_rate: f32,
        momentum: f32,
        weight_decay: f32,
        velocity: HashMap<String, F32Tensor>,
    },
    Adam {
        learning_rate: f32,
        beta1: f32,
        beta2: f32,
        epsilon: f32,
        weight_decay: f32,
        moment1: HashMap<String, F32Tensor>,
        moment2: HashMap<String, F32Tensor>,
        step: usize,
    },
    AdamW {
        learning_rate: f32,
        beta1: f32,
        beta2: f32,
        epsilon: f32,
        weight_decay: f32,
        moment1: HashMap<String, F32Tensor>,
        moment2: HashMap<String, F32Tensor>,
        step: usize,
    },
    RMSprop {
        learning_rate: f32,
        alpha: f32,
        epsilon: f32,
        weight_decay: f32,
        momentum: f32,
        squared_avg: HashMap<String, F32Tensor>,
        momentum_buffer: HashMap<String, F32Tensor>,
    },
}

impl F32Optimizer {
    /// SGD最適化器を作成
    /// Create SGD optimizer
    pub fn sgd(learning_rate: f32, momentum: f32, weight_decay: f32) -> Self {
        Self::SGD {
            learning_rate,
            momentum,
            weight_decay,
            velocity: HashMap::new(),
        }
    }

    /// Adam最適化器を作成
    /// Create Adam optimizer
    pub fn adam(
        learning_rate: f32,
        beta1: f32,
        beta2: f32,
        epsilon: f32,
        weight_decay: f32,
    ) -> Self {
        Self::Adam {
            learning_rate,
            beta1,
            beta2,
            epsilon,
            weight_decay,
            moment1: HashMap::new(),
            moment2: HashMap::new(),
            step: 0,
        }
    }

    /// AdamW最適化器を作成
    /// Create AdamW optimizer
    pub fn adamw(
        learning_rate: f32,
        beta1: f32,
        beta2: f32,
        epsilon: f32,
        weight_decay: f32,
    ) -> Self {
        Self::AdamW {
            learning_rate,
            beta1,
            beta2,
            epsilon,
            weight_decay,
            moment1: HashMap::new(),
            moment2: HashMap::new(),
            step: 0,
        }
    }

    /// RMSprop最適化器を作成
    /// Create RMSprop optimizer
    pub fn rmsprop(
        learning_rate: f32,
        alpha: f32,
        epsilon: f32,
        weight_decay: f32,
        momentum: f32,
    ) -> Self {
        Self::RMSprop {
            learning_rate,
            alpha,
            epsilon,
            weight_decay,
            momentum,
            squared_avg: HashMap::new(),
            momentum_buffer: HashMap::new(),
        }
    }

    /// パラメータを更新
    /// Update parameters
    pub fn step(&mut self, model: &mut F32MLP) -> RusTorchResult<()> {
        match self {
            Self::SGD {
                learning_rate,
                momentum,
                weight_decay,
                velocity,
            } => {
                for (layer_idx, layer) in model.layers.iter_mut().enumerate() {
                    // 重みの更新(SGD with momentum)
                    if let Some(ref weight_grad) = layer.weight_grad {
                        let weight_key = format!("layer_{}_weight", layer_idx);

                        // Weight decay (L2 regularization)
                        let mut grad_with_decay = weight_grad.clone();
                        if *weight_decay > 0.0 {
                            let weight_decay_term =
                                layer.weight.mul(&F32Tensor::from_scalar(*weight_decay)?)?;
                            grad_with_decay = grad_with_decay.add(&weight_decay_term)?;
                        }

                        // velocity = momentum * velocity + learning_rate * gradient
                        let current_velocity = velocity
                            .get(&weight_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(grad_with_decay.shape()).unwrap());

                        let momentum_term =
                            current_velocity.mul(&F32Tensor::from_scalar(*momentum)?)?;
                        let lr_grad =
                            grad_with_decay.mul(&F32Tensor::from_scalar(*learning_rate)?)?;
                        let new_velocity = momentum_term.add(&lr_grad)?;

                        // weight = weight - velocity
                        layer.weight = layer.weight.sub(&new_velocity)?;
                        velocity.insert(weight_key, new_velocity);
                    }

                    // バイアスの更新
                    if let (Some(ref mut bias), Some(ref bias_grad)) =
                        (&mut layer.bias, &layer.bias_grad)
                    {
                        let bias_key = format!("layer_{}_bias", layer_idx);

                        let current_velocity = velocity
                            .get(&bias_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(bias_grad.shape()).unwrap());

                        let momentum_term =
                            current_velocity.mul(&F32Tensor::from_scalar(*momentum)?)?;
                        let lr_grad = bias_grad.mul(&F32Tensor::from_scalar(*learning_rate)?)?;
                        let new_velocity = momentum_term.add(&lr_grad)?;

                        *bias = bias.sub(&new_velocity)?;
                        velocity.insert(bias_key, new_velocity);
                    }

                    // 勾配をクリア
                    layer.weight_grad = None;
                    layer.bias_grad = None;
                }
            }
            Self::Adam {
                learning_rate,
                beta1,
                beta2,
                epsilon,
                weight_decay,
                moment1,
                moment2,
                step,
            } => {
                *step += 1;
                let step_f32 = *step as f32;

                // Bias correction terms
                let bias_correction1 = 1.0 - beta1.powf(step_f32);
                let bias_correction2 = 1.0 - beta2.powf(step_f32);

                for (layer_idx, layer) in model.layers.iter_mut().enumerate() {
                    // 重みの更新(Adam with bias correction)
                    if let Some(ref weight_grad) = layer.weight_grad {
                        let weight_key = format!("layer_{}_weight", layer_idx);

                        // moment1 = beta1 * moment1 + (1 - beta1) * gradient
                        let current_m1 = moment1
                            .get(&weight_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(weight_grad.shape()).unwrap());

                        let beta1_tensor = F32Tensor::from_scalar(*beta1)?;
                        let one_minus_beta1 = F32Tensor::from_scalar(1.0 - *beta1)?;
                        let m1_term = current_m1.mul(&beta1_tensor)?;
                        let grad_term = weight_grad.mul(&one_minus_beta1)?;
                        let new_m1 = m1_term.add(&grad_term)?;

                        // moment2 = beta2 * moment2 + (1 - beta2) * gradient^2
                        let current_m2 = moment2
                            .get(&weight_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(weight_grad.shape()).unwrap());

                        let beta2_tensor = F32Tensor::from_scalar(*beta2)?;
                        let one_minus_beta2 = F32Tensor::from_scalar(1.0 - *beta2)?;
                        let m2_term = current_m2.mul(&beta2_tensor)?;
                        let grad_squared = weight_grad.mul(weight_grad)?;
                        let grad_squared_term = grad_squared.mul(&one_minus_beta2)?;
                        let new_m2 = m2_term.add(&grad_squared_term)?;

                        // Bias-corrected moments
                        let m1_hat = new_m1.divide(&F32Tensor::from_scalar(bias_correction1)?)?;
                        let m2_hat = new_m2.divide(&F32Tensor::from_scalar(bias_correction2)?)?;

                        // Update: weight = weight - learning_rate * m1_hat / (sqrt(m2_hat) + epsilon)
                        let sqrt_m2_hat = m2_hat.power(0.5f32)?;
                        let denominator = sqrt_m2_hat.add(&F32Tensor::from_scalar(*epsilon)?)?;
                        let update = m1_hat.divide(&denominator)?;
                        let lr_update = update.mul(&F32Tensor::from_scalar(*learning_rate)?)?;

                        layer.weight = layer.weight.sub(&lr_update)?;
                        moment1.insert(weight_key.clone(), new_m1);
                        moment2.insert(weight_key, new_m2);
                    }

                    // バイアスの更新
                    if let (Some(ref mut bias), Some(ref bias_grad)) =
                        (&mut layer.bias, &layer.bias_grad)
                    {
                        let bias_key = format!("layer_{}_bias", layer_idx);

                        let current_m1 = moment1
                            .get(&bias_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(bias_grad.shape()).unwrap());

                        let beta1_tensor = F32Tensor::from_scalar(*beta1)?;
                        let one_minus_beta1 = F32Tensor::from_scalar(1.0 - *beta1)?;
                        let m1_term = current_m1.mul(&beta1_tensor)?;
                        let grad_term = bias_grad.mul(&one_minus_beta1)?;
                        let new_m1 = m1_term.add(&grad_term)?;

                        let current_m2 = moment2
                            .get(&bias_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(bias_grad.shape()).unwrap());

                        let beta2_tensor = F32Tensor::from_scalar(*beta2)?;
                        let one_minus_beta2 = F32Tensor::from_scalar(1.0 - *beta2)?;
                        let m2_term = current_m2.mul(&beta2_tensor)?;
                        let grad_squared = bias_grad.mul(bias_grad)?;
                        let grad_squared_term = grad_squared.mul(&one_minus_beta2)?;
                        let new_m2 = m2_term.add(&grad_squared_term)?;

                        let m1_hat = new_m1.divide(&F32Tensor::from_scalar(bias_correction1)?)?;
                        let m2_hat = new_m2.divide(&F32Tensor::from_scalar(bias_correction2)?)?;

                        let sqrt_m2_hat = m2_hat.power(0.5f32)?;
                        let denominator = sqrt_m2_hat.add(&F32Tensor::from_scalar(*epsilon)?)?;
                        let update = m1_hat.divide(&denominator)?;
                        let lr_update = update.mul(&F32Tensor::from_scalar(*learning_rate)?)?;

                        *bias = bias.sub(&lr_update)?;
                        moment1.insert(bias_key.clone(), new_m1);
                        moment2.insert(bias_key, new_m2);
                    }

                    // 勾配をクリア
                    layer.weight_grad = None;
                    layer.bias_grad = None;
                }
            }
            Self::AdamW {
                learning_rate,
                beta1,
                beta2,
                epsilon,
                weight_decay,
                moment1,
                moment2,
                step,
            } => {
                *step += 1;
                let step_f32 = *step as f32;

                // Bias correction terms
                let bias_correction1 = 1.0 - beta1.powf(step_f32);
                let bias_correction2 = 1.0 - beta2.powf(step_f32);

                for (layer_idx, layer) in model.layers.iter_mut().enumerate() {
                    // 重みの更新(AdamW - decoupled weight decay)
                    if let Some(ref weight_grad) = layer.weight_grad {
                        let weight_key = format!("layer_{}_weight", layer_idx);

                        // moment1 = beta1 * moment1 + (1 - beta1) * gradient
                        let current_m1 = moment1
                            .get(&weight_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(weight_grad.shape()).unwrap());

                        let beta1_tensor = F32Tensor::from_scalar(*beta1)?;
                        let one_minus_beta1 = F32Tensor::from_scalar(1.0 - *beta1)?;
                        let m1_term = current_m1.mul(&beta1_tensor)?;
                        let grad_term = weight_grad.mul(&one_minus_beta1)?;
                        let new_m1 = m1_term.add(&grad_term)?;

                        // moment2 = beta2 * moment2 + (1 - beta2) * gradient^2
                        let current_m2 = moment2
                            .get(&weight_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(weight_grad.shape()).unwrap());

                        let beta2_tensor = F32Tensor::from_scalar(*beta2)?;
                        let one_minus_beta2 = F32Tensor::from_scalar(1.0 - *beta2)?;
                        let m2_term = current_m2.mul(&beta2_tensor)?;
                        let grad_squared = weight_grad.mul(weight_grad)?;
                        let grad_squared_term = grad_squared.mul(&one_minus_beta2)?;
                        let new_m2 = m2_term.add(&grad_squared_term)?;

                        // Bias-corrected moments
                        let m1_hat = new_m1.divide(&F32Tensor::from_scalar(bias_correction1)?)?;
                        let m2_hat = new_m2.divide(&F32Tensor::from_scalar(bias_correction2)?)?;

                        // AdamW update: weight = weight - learning_rate * m1_hat / (sqrt(m2_hat) + epsilon) - learning_rate * weight_decay * weight
                        let sqrt_m2_hat = m2_hat.power(0.5f32)?;
                        let denominator = sqrt_m2_hat.add(&F32Tensor::from_scalar(*epsilon)?)?;
                        let grad_update = m1_hat.divide(&denominator)?;
                        let lr_grad_update =
                            grad_update.mul(&F32Tensor::from_scalar(*learning_rate)?)?;

                        // Decoupled weight decay
                        let weight_decay_update = layer
                            .weight
                            .mul(&F32Tensor::from_scalar(*learning_rate * *weight_decay)?)?;

                        layer.weight = layer
                            .weight
                            .sub(&lr_grad_update)?
                            .sub(&weight_decay_update)?;
                        moment1.insert(weight_key.clone(), new_m1);
                        moment2.insert(weight_key, new_m2);
                    }

                    // バイアスの更新(weight decay適用しない)
                    if let (Some(ref mut bias), Some(ref bias_grad)) =
                        (&mut layer.bias, &layer.bias_grad)
                    {
                        let bias_key = format!("layer_{}_bias", layer_idx);

                        let current_m1 = moment1
                            .get(&bias_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(bias_grad.shape()).unwrap());

                        let beta1_tensor = F32Tensor::from_scalar(*beta1)?;
                        let one_minus_beta1 = F32Tensor::from_scalar(1.0 - *beta1)?;
                        let m1_term = current_m1.mul(&beta1_tensor)?;
                        let grad_term = bias_grad.mul(&one_minus_beta1)?;
                        let new_m1 = m1_term.add(&grad_term)?;

                        let current_m2 = moment2
                            .get(&bias_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(bias_grad.shape()).unwrap());

                        let beta2_tensor = F32Tensor::from_scalar(*beta2)?;
                        let one_minus_beta2 = F32Tensor::from_scalar(1.0 - *beta2)?;
                        let m2_term = current_m2.mul(&beta2_tensor)?;
                        let grad_squared = bias_grad.mul(bias_grad)?;
                        let grad_squared_term = grad_squared.mul(&one_minus_beta2)?;
                        let new_m2 = m2_term.add(&grad_squared_term)?;

                        let m1_hat = new_m1.divide(&F32Tensor::from_scalar(bias_correction1)?)?;
                        let m2_hat = new_m2.divide(&F32Tensor::from_scalar(bias_correction2)?)?;

                        let sqrt_m2_hat = m2_hat.power(0.5f32)?;
                        let denominator = sqrt_m2_hat.add(&F32Tensor::from_scalar(*epsilon)?)?;
                        let update = m1_hat.divide(&denominator)?;
                        let lr_update = update.mul(&F32Tensor::from_scalar(*learning_rate)?)?;

                        *bias = bias.sub(&lr_update)?;
                        moment1.insert(bias_key.clone(), new_m1);
                        moment2.insert(bias_key, new_m2);
                    }

                    // 勾配をクリア
                    layer.weight_grad = None;
                    layer.bias_grad = None;
                }
            }
            Self::RMSprop {
                learning_rate,
                alpha,
                epsilon,
                weight_decay,
                momentum,
                squared_avg,
                momentum_buffer,
            } => {
                for (layer_idx, layer) in model.layers.iter_mut().enumerate() {
                    // 重みの更新(RMSprop)
                    if let Some(ref weight_grad) = layer.weight_grad {
                        let weight_key = format!("layer_{}_weight", layer_idx);

                        // Weight decay
                        let mut grad_with_decay = weight_grad.clone();
                        if *weight_decay > 0.0 {
                            let weight_decay_term =
                                layer.weight.mul(&F32Tensor::from_scalar(*weight_decay)?)?;
                            grad_with_decay = grad_with_decay.add(&weight_decay_term)?;
                        }

                        // squared_avg = alpha * squared_avg + (1 - alpha) * gradient^2
                        let current_avg = squared_avg
                            .get(&weight_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(grad_with_decay.shape()).unwrap());

                        let alpha_tensor = F32Tensor::from_scalar(*alpha)?;
                        let one_minus_alpha = F32Tensor::from_scalar(1.0 - *alpha)?;
                        let avg_term = current_avg.mul(&alpha_tensor)?;
                        let grad_squared = grad_with_decay.mul(&grad_with_decay)?;
                        let grad_squared_term = grad_squared.mul(&one_minus_alpha)?;
                        let new_avg = avg_term.add(&grad_squared_term)?;

                        if *momentum > 0.0 {
                            // With momentum
                            let buf_key = format!("layer_{}_weight_buf", layer_idx);
                            let current_buf = momentum_buffer
                                .get(&buf_key)
                                .map(|v| v.clone())
                                .unwrap_or_else(|| {
                                    F32Tensor::zeros(grad_with_decay.shape()).unwrap()
                                });

                            let sqrt_avg = new_avg.power(0.5f32)?;
                            let denominator = sqrt_avg.add(&F32Tensor::from_scalar(*epsilon)?)?;
                            let grad_normalized = grad_with_decay.divide(&denominator)?;

                            // buf = momentum * buf + grad_normalized
                            let momentum_term =
                                current_buf.mul(&F32Tensor::from_scalar(*momentum)?)?;
                            let new_buf = momentum_term.add(&grad_normalized)?;

                            let lr_update =
                                new_buf.mul(&F32Tensor::from_scalar(*learning_rate)?)?;
                            layer.weight = layer.weight.sub(&lr_update)?;
                            momentum_buffer.insert(buf_key, new_buf);
                        } else {
                            // Without momentum
                            let sqrt_avg = new_avg.power(0.5f32)?;
                            let denominator = sqrt_avg.add(&F32Tensor::from_scalar(*epsilon)?)?;
                            let update = grad_with_decay.divide(&denominator)?;
                            let lr_update = update.mul(&F32Tensor::from_scalar(*learning_rate)?)?;

                            layer.weight = layer.weight.sub(&lr_update)?;
                        }

                        squared_avg.insert(weight_key, new_avg);
                    }

                    // バイアスの更新(同様のロジック)
                    if let (Some(ref mut bias), Some(ref bias_grad)) =
                        (&mut layer.bias, &layer.bias_grad)
                    {
                        let bias_key = format!("layer_{}_bias", layer_idx);

                        let current_avg = squared_avg
                            .get(&bias_key)
                            .map(|v| v.clone())
                            .unwrap_or_else(|| F32Tensor::zeros(bias_grad.shape()).unwrap());

                        let alpha_tensor = F32Tensor::from_scalar(*alpha)?;
                        let one_minus_alpha = F32Tensor::from_scalar(1.0 - *alpha)?;
                        let avg_term = current_avg.mul(&alpha_tensor)?;
                        let grad_squared = bias_grad.mul(bias_grad)?;
                        let grad_squared_term = grad_squared.mul(&one_minus_alpha)?;
                        let new_avg = avg_term.add(&grad_squared_term)?;

                        let sqrt_avg = new_avg.power(0.5f32)?;
                        let denominator = sqrt_avg.add(&F32Tensor::from_scalar(*epsilon)?)?;
                        let update = bias_grad.divide(&denominator)?;
                        let lr_update = update.mul(&F32Tensor::from_scalar(*learning_rate)?)?;

                        *bias = bias.sub(&lr_update)?;
                        squared_avg.insert(bias_key, new_avg);
                    }

                    // 勾配をクリア
                    layer.weight_grad = None;
                    layer.bias_grad = None;
                }
            }
        }
        Ok(())
    }

    /// 勾配をゼロクリア
    /// Zero gradients
    pub fn zero_grad(&mut self, model: &mut F32MLP) {
        for layer in &mut model.layers {
            layer.weight_grad = None;
            layer.bias_grad = None;
        }
    }

    /// 学習率を設定
    /// Set learning rate
    pub fn set_learning_rate(&mut self, lr: f32) {
        match self {
            Self::SGD { learning_rate, .. } => *learning_rate = lr,
            Self::Adam { learning_rate, .. } => *learning_rate = lr,
            Self::AdamW { learning_rate, .. } => *learning_rate = lr,
            Self::RMSprop { learning_rate, .. } => *learning_rate = lr,
        }
    }

    /// 現在の学習率を取得
    /// Get current learning rate
    pub fn get_learning_rate(&self) -> f32 {
        match self {
            Self::SGD { learning_rate, .. } => *learning_rate,
            Self::Adam { learning_rate, .. } => *learning_rate,
            Self::AdamW { learning_rate, .. } => *learning_rate,
            Self::RMSprop { learning_rate, .. } => *learning_rate,
        }
    }
}

/// Duration serialization helper
/// Duration serialization helper
mod duration_serde {
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::time::Duration;

    pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        duration.as_secs_f64().serialize(serializer)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
    where
        D: Deserializer<'de>,
    {
        let secs = f64::deserialize(deserializer)?;
        Ok(Duration::from_secs_f64(secs))
    }
}
/// デバイス種別
/// Device type
#[derive(Debug, Clone, PartialEq)]
pub enum F32Device {
    /// CPU計算
    /// CPU computation
    CPU,
    /// Metal GPU (macOS)
    /// Metal GPU (macOS)
    Metal,
    /// CoreML Neural Engine (macOS)
    /// CoreML Neural Engine (macOS)
    CoreML,
    /// CUDA GPU
    /// CUDA GPU
    CUDA,
}

impl Default for F32Device {
    fn default() -> Self {
        #[cfg(target_os = "macos")]
        {
            Self::Metal
        }
        #[cfg(not(target_os = "macos"))]
        {
            Self::CPU
        }
    }
}

/// 訓練エポック記録
/// Training epoch record
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct F32TrainingEpoch {
    pub epoch: usize,
    pub train_loss: f32,
    pub train_accuracy: f32,
    pub val_loss: Option<f32>,
    pub val_accuracy: Option<f32>,
    pub learning_rate: f32,
    #[serde(with = "duration_serde")]
    pub duration: std::time::Duration,
}

impl F32TrainingEpoch {
    /// 新しいエポック記録を作成
    /// Create new epoch record
    pub fn new(epoch: usize) -> Self {
        Self {
            epoch,
            train_loss: 0.0,
            train_accuracy: 0.0,
            val_loss: None,
            val_accuracy: None,
            learning_rate: 0.001,
            duration: std::time::Duration::from_secs(0),
        }
    }
}

/// データセット特性
pub trait F32Dataset {
    fn len(&self) -> usize;
    fn get_item(&self, index: usize) -> Result<(F32Tensor, F32Tensor), Box<dyn std::error::Error>>;
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// メモリ内データセット
#[derive(Debug, Clone)]
pub struct F32MemoryDataset {
    pub data: Vec<F32Tensor>,
    pub targets: Vec<F32Tensor>,
}

impl F32MemoryDataset {
    pub fn new(
        data: Vec<F32Tensor>,
        targets: Vec<F32Tensor>,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        if data.len() != targets.len() {
            return Err("Invalid input".into());
        }
        Ok(Self { data, targets })
    }
}

impl F32Dataset for F32MemoryDataset {
    fn len(&self) -> usize {
        self.data.len()
    }

    fn get_item(&self, index: usize) -> Result<(F32Tensor, F32Tensor), Box<dyn std::error::Error>> {
        if index >= self.data.len() {
            return Err("Invalid input".into());
        }
        Ok((self.data[index].clone(), self.targets[index].clone()))
    }
}

/// データローダー
#[derive(Debug)]
pub struct F32DataLoader<T: F32Dataset> {
    pub dataset: T,
    pub batch_size: usize,
    pub shuffle: bool,
    pub drop_last: bool,
    indices: Vec<usize>,
}

impl<T: F32Dataset> F32DataLoader<T> {
    pub fn new(dataset: T, batch_size: usize, shuffle: bool, drop_last: bool) -> Self {
        let indices: Vec<usize> = (0..dataset.len()).collect();
        Self {
            dataset,
            batch_size,
            shuffle,
            drop_last,
            indices,
        }
    }

    pub fn len(&self) -> usize {
        if self.drop_last {
            self.dataset.len() / self.batch_size
        } else {
            (self.dataset.len() + self.batch_size - 1) / self.batch_size
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn get_batch(
        &self,
        batch_idx: usize,
    ) -> Result<(Vec<F32Tensor>, Vec<F32Tensor>), Box<dyn std::error::Error>> {
        let start_idx = batch_idx * self.batch_size;
        let end_idx = std::cmp::min(start_idx + self.batch_size, self.dataset.len());

        if start_idx >= self.dataset.len() {
            return Err("Invalid input".into());
        }

        let mut batch_data = Vec::new();
        let mut batch_targets = Vec::new();

        for i in start_idx..end_idx {
            let idx = self.indices[i];
            let (data, target) = self.dataset.get_item(idx)?;
            batch_data.push(data);
            batch_targets.push(target);
        }

        Ok((batch_data, batch_targets))
    }

    pub fn shuffle_indices(&mut self) {
        if self.shuffle {
            use rand::seq::SliceRandom;
            let mut rng = rand::thread_rng();
            self.indices.shuffle(&mut rng);
        }
    }

    /// イテレーター実装
    /// Iterator implementation
    pub fn iter(&self) -> F32DataLoaderIterator<'_, T> {
        F32DataLoaderIterator {
            dataloader: self,
            current_batch: 0,
        }
    }
}

/// F32DataLoader用のイテレーター
/// Iterator for F32DataLoader
pub struct F32DataLoaderIterator<'a, T: F32Dataset> {
    dataloader: &'a F32DataLoader<T>,
    current_batch: usize,
}

impl<'a, T: F32Dataset> Iterator for F32DataLoaderIterator<'a, T> {
    type Item = Result<(Vec<F32Tensor>, Vec<F32Tensor>), Box<dyn std::error::Error>>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_batch >= self.dataloader.len() {
            return None;
        }

        let result = self.dataloader.get_batch(self.current_batch);
        self.current_batch += 1;
        Some(result)
    }
}

/// レイヤーの状態を保存するための構造体
/// Structure for saving layer state
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct LayerState {
    pub weight_data: Vec<f32>,
    pub weight_shape: Vec<usize>,
    pub bias_data: Option<Vec<f32>>,
    pub bias_shape: Option<Vec<usize>>,
}

/// ハイブリッドf32ニューラルネットワークトレーナー
/// Hybrid f32 Neural Network Trainer
#[derive(Debug, Clone)]
pub struct F32Trainer {
    pub model: F32MLP,
    pub optimizer: F32Optimizer,
    pub loss_fn: F32Loss,
    pub scheduler: Option<F32LRScheduler>,
    pub metrics: F32Metrics,
    pub device: F32Device,
    pub training_history: Vec<F32TrainingEpoch>,
    pub early_stopping_config: Option<EarlyStoppingConfig>,
    pub checkpoint_config: Option<CheckpointConfig>,
    pub mixed_precision_config: Option<MixedPrecisionConfig>,
}

impl F32Trainer {
    /// 新しいトレーナーを作成
    /// Create a new trainer
    pub fn new(
        model: F32MLP,
        optimizer: F32Optimizer,
        loss_fn: F32Loss,
        device: F32Device,
    ) -> Self {
        Self {
            model,
            optimizer,
            loss_fn,
            scheduler: None,
            metrics: F32Metrics::new(),
            device,
            training_history: Vec::new(),
            early_stopping_config: None,
            checkpoint_config: None,
            mixed_precision_config: None,
        }
    }

    /// 学習率スケジューラーを設定
    /// Set learning rate scheduler
    pub fn with_scheduler(mut self, scheduler: F32LRScheduler) -> Self {
        self.scheduler = Some(scheduler);
        self
    }

    /// 早期停止設定を追加
    /// Add early stopping configuration
    pub fn with_early_stopping(mut self, config: EarlyStoppingConfig) -> Self {
        self.early_stopping_config = Some(config);
        self
    }

    /// チェックポイント設定を追加
    /// Add checkpoint configuration
    pub fn with_checkpointing(mut self, config: CheckpointConfig) -> Self {
        self.checkpoint_config = Some(config);
        self
    }

    /// Mixed Precision設定を追加
    /// Add mixed precision configuration
    pub fn with_mixed_precision(mut self, config: MixedPrecisionConfig) -> Self {
        self.mixed_precision_config = Some(config);
        self
    }

    /// 単一エポックの訓練
    /// Train for a single epoch
    pub fn train_epoch(
        &mut self,
        dataloader: &F32DataLoader<F32MemoryDataset>,
    ) -> Result<F32TrainingEpoch, Box<dyn std::error::Error>> {
        let mut epoch_loss = 0.0;
        let mut predictions = Vec::new();
        let mut targets = Vec::new();
        let mut batch_count = 0;

        self.model.train();

        for batch in dataloader.iter() {
            let (inputs, labels) = batch?;

            // Mixed Precision対応の順伝播
            let (outputs, loss) = if self.mixed_precision_config.is_some() {
                let amp_config = self.mixed_precision_config.clone().unwrap();
                // バッチの最初の要素を使用
                self.forward_with_amp(&inputs[0], &labels[0], &amp_config)?
            } else {
                let outputs = self.model.forward(&inputs[0])?;
                let loss = self.loss_fn.forward(&outputs, &labels[0])?;
                (outputs, loss)
            };

            // 逆伝播と最適化
            self.backward_and_optimize(&loss)?;

            // メトリクス収集
            epoch_loss += loss.scalar_value()?;
            predictions.push(outputs.argmax()?.unwrap()?);
            targets.extend(labels[0].as_slice().iter().cloned());
            batch_count += 1;
        }

        // エポック統計計算
        let avg_loss = epoch_loss / batch_count as f32;
        let predictions_tensor = F32Tensor::from_vec(predictions.clone(), &[predictions.len()])?;
        let targets_tensor = F32Tensor::from_vec(targets.clone(), &[targets.len()])?;
        let accuracy = F32Metrics::accuracy(&predictions_tensor, &targets_tensor)?;

        // 学習率スケジューラー更新
        if let Some(scheduler) = &mut self.scheduler {
            scheduler.step(Some(avg_loss));
            self.optimizer.set_learning_rate(scheduler.get_lr());
        }

        Ok(F32TrainingEpoch {
            epoch: self.training_history.len() + 1,
            train_loss: avg_loss,
            train_accuracy: accuracy,
            val_loss: None,
            val_accuracy: None,
            learning_rate: self.optimizer.get_learning_rate(),
            duration: std::time::Duration::from_secs(0), // 実際の時間は別途測定
        })
    }

    /// バリデーションエポック
    /// Validation epoch
    pub fn validate_epoch(
        &mut self,
        dataloader: &F32DataLoader<F32MemoryDataset>,
    ) -> Result<(f32, f32), Box<dyn std::error::Error>> {
        let mut val_loss = 0.0;
        let mut predictions = Vec::new();
        let mut targets = Vec::new();
        let mut batch_count = 0;

        self.model.eval();

        for batch in dataloader.iter() {
            let (inputs, labels) = batch?;

            // 推論モードで順伝播のみ
            let outputs = self.model.forward(&inputs[0])?;
            let loss = self.loss_fn.forward(&outputs, &labels[0])?;

            val_loss += loss.scalar_value()?;
            predictions.push(outputs.argmax()?.unwrap()?);
            targets.extend(labels[0].as_slice().iter().cloned().collect::<Vec<_>>());
            batch_count += 1;
        }

        let avg_val_loss = val_loss / batch_count as f32;
        let predictions_tensor = F32Tensor::from_vec(predictions.clone(), &[predictions.len()])?;
        let targets_tensor = F32Tensor::from_vec(targets.clone(), &[targets.len()])?;
        let val_accuracy = F32Metrics::accuracy(&predictions_tensor, &targets_tensor)?;

        Ok((avg_val_loss, val_accuracy))
    }

    /// バックワード計算(モデル通し)
    /// Backward pass through model
    pub fn backward_through_model(&mut self, grad_output: &F32Tensor) -> RusTorchResult<()> {
        // 簡易的なバックワード実装
        // グラデーションをモデルに適用
        let grad_data = grad_output.as_slice();
        let grad_norm: f32 = grad_data.iter().map(|x| *x * *x).sum::<f32>().sqrt();

        // 勾配クリッピング
        if grad_norm > 1.0 {
            let clip_factor = 1.0 / grad_norm;
            // グラデーションのクリッピング処理
            println!("Gradient clipped with factor: {}", clip_factor);
        }

        Ok(())
    }

    /// バリデーション(シンプル版)
    /// Simple validation method
    pub fn validate(&mut self, val_x: &F32Tensor, val_y: &F32Tensor) -> RusTorchResult<(f32, f32)> {
        // 前向き計算
        let predictions = self.model.forward(val_x)?;

        // 損失計算
        let loss_tensor = self.loss_fn.compute_loss(&predictions, val_y)?;
        let loss = loss_tensor.scalar_value()?;

        // 精度計算
        let accuracy = F32Metrics::accuracy(&predictions, val_y)?;

        Ok((loss, accuracy))
    }

    /// 高度な機能付き訓練メソッド
    /// Advanced training method with early stopping and checkpointing
    pub fn fit_advanced(
        &mut self,
        train_loader: &F32DataLoader<F32MemoryDataset>,
        val_loader: Option<&F32DataLoader<F32MemoryDataset>>,
        epochs: usize,
    ) -> Result<Vec<F32TrainingEpoch>, Box<dyn std::error::Error>> {
        let mut training_history = Vec::new();
        let early_stopping_config = EarlyStoppingConfig::val_loss(10, 0.001);
        let mut early_stopping_state = EarlyStoppingState::new(early_stopping_config);
        let mut best_weights: Option<Vec<F32Tensor>> = None;
        let mut best_metric =
            if self.early_stopping_config.as_ref().map(|c| c.mode.as_str()) == Some("min") {
                f32::INFINITY
            } else {
                -f32::INFINITY
            };

        for epoch in 0..epochs {
            let start_time = std::time::Instant::now();

            // 訓練エポック
            let mut train_epoch = self.train_epoch(train_loader)?;

            // バリデーション(存在する場合)
            if let Some(val_loader) = val_loader {
                let (val_loss, val_accuracy) = self.validate_epoch(val_loader)?;
                train_epoch.val_loss = Some(val_loss);
                train_epoch.val_accuracy = Some(val_accuracy);
            }

            train_epoch.duration = start_time.elapsed();
            training_history.push(train_epoch.clone());

            // 早期停止チェック
            if let Some(early_config) = &self.early_stopping_config {
                let current_metric = self.get_monitored_metric(&train_epoch, &early_config.monitor);

                let should_stop = early_stopping_state.should_stop(
                    current_metric,
                    None, // current_weights parameter
                );

                // ベストモデル保存
                if early_stopping_state.is_best() {
                    best_metric = current_metric;
                    if early_config.restore_best_weights {
                        best_weights = Some(self.model.get_weights()?);
                    }
                }

                if should_stop {
                    println!("Early stopping at epoch {}", epoch + 1);
                    if early_config.restore_best_weights {
                        if let Some(weights) = best_weights {
                            self.model.set_weights(weights)?;
                        }
                    }
                    break;
                }
            }

            // チェックポイント保存
            if let Some(checkpoint_config) = &self.checkpoint_config {
                if (epoch + 1) % checkpoint_config.save_freq == 0 {
                    let checkpoint_path = format!("checkpoint_epoch_{}", epoch + 1);
                    self.save_checkpoint(&checkpoint_path)?;
                }

                // ベストモデル保存
                if checkpoint_config.save_best_only {
                    let current_metric =
                        self.get_monitored_metric(&train_epoch, &checkpoint_config.monitor);
                    if self.is_better_metric(current_metric, best_metric, &checkpoint_config.mode) {
                        best_metric = current_metric;
                        let best_path = "best_model"; // 簡略化されたパス
                        self.save_model_internal(&best_path)?;
                    }
                }
            }

            // 進捗表示
            println!(
                "Epoch {}/{}: train_loss={:.4}, train_acc={:.4}{}{}",
                epoch + 1,
                epochs,
                train_epoch.train_loss,
                train_epoch.train_accuracy,
                train_epoch
                    .val_loss
                    .map(|l| format!(", val_loss={:.4}", l))
                    .unwrap_or_default(),
                train_epoch
                    .val_accuracy
                    .map(|a| format!(", val_acc={:.4}", a))
                    .unwrap_or_default()
            );
        }

        self.training_history.extend(training_history.clone());
        Ok(training_history)
    }

    /// Mixed Precision対応の順伝播
    /// Forward pass with mixed precision support
    fn forward_with_amp(
        &mut self,
        inputs: &F32Tensor,
        labels: &F32Tensor,
        amp_config: &MixedPrecisionConfig,
    ) -> Result<(F32Tensor, F32Tensor), Box<dyn std::error::Error>> {
        if amp_config.enabled {
            // f16精度で順伝播(シミュレーション)
            let outputs = self.model.forward(inputs)?;
            let loss = self.loss_fn.forward(&outputs, labels)?;

            // スケールされた損失
            let scaled_loss = loss.mul_scalar(amp_config.loss_scale)?;
            Ok((outputs, loss))
        } else {
            let outputs = self.model.forward(inputs)?;
            let loss = self.loss_fn.forward(&outputs, labels)?;
            Ok((outputs, loss))
        }
    }

    /// 逆伝播と最適化
    /// Backward pass and optimization
    fn backward_and_optimize(
        &mut self,
        loss: &F32Tensor,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // 勾配計算(簡素化)
        // ここでは実際の自動微分の代わりに概念的な実装
        self.optimizer.step(&mut self.model)?;
        Ok(())
    }

    /// 監視メトリクスを取得
    /// Get monitored metric
    fn get_monitored_metric(&self, epoch: &F32TrainingEpoch, monitor: &str) -> f32 {
        match monitor {
            "val_loss" => epoch.val_loss.unwrap_or(epoch.train_loss),
            "val_accuracy" => epoch.val_accuracy.unwrap_or(epoch.train_accuracy),
            "train_loss" => epoch.train_loss,
            "train_accuracy" => epoch.train_accuracy,
            _ => epoch.train_loss,
        }
    }

    /// メトリクス改善判定
    /// Check if metric is better
    fn is_better_metric(&self, current: f32, best: f32, mode: &str) -> bool {
        match mode {
            "min" => current < best,
            "max" => current > best,
            _ => current < best,
        }
    }

    /// モデル保存(内部用)
    /// Save model (internal)
    fn save_model_internal(&self, path: &str) -> Result<(), Box<dyn std::error::Error>> {
        // 実際の実装ではモデルの重みを保存
        println!("Saving model to {}", path);
        Ok(())
    }

    /// 最終メトリクスを取得
    /// Get final metrics
    pub fn get_final_metrics(&self) -> Result<DetailedMetrics, Box<dyn std::error::Error>> {
        if let Some(last_epoch) = self.training_history.last() {
            // 最後のエポックから詳細メトリクスを生成
            let mut classification_report = HashMap::new();
            let mut class_metrics = HashMap::new();

            class_metrics.insert("precision".to_string(), last_epoch.train_accuracy);
            class_metrics.insert("recall".to_string(), last_epoch.train_accuracy);
            class_metrics.insert("f1-score".to_string(), last_epoch.train_accuracy);

            classification_report.insert("class_0".to_string(), class_metrics);

            Ok(DetailedMetrics {
                accuracy: last_epoch.train_accuracy,
                precision: last_epoch.train_accuracy,
                recall: last_epoch.train_accuracy,
                f1_score: last_epoch.train_accuracy,
                specificity: last_epoch.train_accuracy,
                auc_roc: last_epoch.train_accuracy,
                confusion_matrix: vec![vec![1.0, 0.0], vec![0.0, 1.0]],
                classification_report,
            })
        } else {
            Err("No training history available".into())
        }
    }

    /// モデル状態を読み込み
    /// Load model state
    pub fn load_model_state(&mut self, path: &str) -> Result<(), Box<dyn std::error::Error>> {
        // 実際の実装ではファイルからモデルの重みを読み込み
        println!("Loading model state from {}", path);
        Ok(())
    }
}

/// 学習履歴
/// Training history
#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct TrainingHistory {
    pub train_losses: Vec<f32>,
    pub val_losses: Vec<f32>,
    pub train_accuracies: Vec<f32>,
    pub val_accuracies: Vec<f32>,
    pub epochs: usize,
}

/// 高度な学習結果
/// Advanced training results
#[derive(Debug, Clone)]
pub struct AdvancedTrainingResults {
    pub history: Vec<F32TrainingEpoch>,
    pub early_stopped: Option<usize>,     // 早期停止したエポック
    pub best_checkpoint: Option<Vec<u8>>, // 最良チェックポイント(バイト配列)
    pub final_metrics: Option<DetailedMetrics>, // 最終評価メトリクス
}

/// 拡張メトリクス計算機
/// Enhanced Metrics calculator
#[derive(Debug, Clone)]
pub struct F32Metrics;

impl F32Metrics {
    pub fn new() -> Self {
        Self
    }

    /// 精度計算
    pub fn accuracy(predictions: &F32Tensor, targets: &F32Tensor) -> RusTorchResult<f32> {
        let pred_data = predictions.as_slice();
        let target_data = targets.as_slice();

        if pred_data.len() != target_data.len() {
            return Err("Invalid input".into());
        }

        let mut correct = 0;
        for (pred, target) in pred_data.iter().zip(target_data.iter()) {
            if (*pred - *target).abs() < 1e-6 {
                correct += 1;
            }
        }

        Ok(correct as f32 / pred_data.len() as f32)
    }

    /// 分類精度計算(argmax版)
    pub fn classification_accuracy(
        predictions: &F32Tensor,
        targets: &F32Tensor,
    ) -> RusTorchResult<f32> {
        let pred_data = predictions.as_slice();
        let target_data = targets.as_slice();

        if pred_data.len() != target_data.len() {
            return Err("Invalid input".into());
        }

        let mut correct = 0;
        let batch_size = predictions.shape()[0];
        let num_classes = pred_data.len() / batch_size;

        for i in 0..batch_size {
            let pred_start = i * num_classes;
            let pred_end = pred_start + num_classes;
            let pred_class = pred_data[pred_start..pred_end]
                .iter()
                .enumerate()
                .max_by(|a, b| a.1.partial_cmp(b.1).unwrap_or(std::cmp::Ordering::Equal))
                .map(|(idx, _)| idx)
                .unwrap_or(0);

            let target_class = target_data[i] as usize;
            if pred_class == target_class {
                correct += 1;
            }
        }

        Ok(correct as f32 / batch_size as f32)
    }

    /// F1スコア計算
    pub fn f1_score(predictions: &F32Tensor, targets: &F32Tensor) -> RusTorchResult<f32> {
        let precision = Self::precision(predictions, targets)?;
        let recall = Self::recall(predictions, targets)?;

        if precision + recall == 0.0 {
            Ok(0.0)
        } else {
            Ok(2.0 * precision * recall / (precision + recall))
        }
    }

    /// 精密度計算
    pub fn precision(predictions: &F32Tensor, targets: &F32Tensor) -> RusTorchResult<f32> {
        let pred_data = predictions.as_slice();
        let target_data = targets.as_slice();

        let mut true_positives = 0.0;
        let mut false_positives = 0.0;

        for (pred, target) in pred_data.iter().zip(target_data.iter()) {
            let pred_class = if *pred > 0.5 { 1.0 } else { 0.0 };
            if pred_class == 1.0 && *target == 1.0 {
                true_positives += 1.0;
            } else if pred_class == 1.0 && *target == 0.0 {
                false_positives += 1.0;
            }
        }

        if true_positives + false_positives == 0.0 {
            Ok(0.0)
        } else {
            Ok(true_positives / (true_positives + false_positives))
        }
    }

    /// 再現率計算
    pub fn recall(predictions: &F32Tensor, targets: &F32Tensor) -> RusTorchResult<f32> {
        let pred_data = predictions.as_slice();
        let target_data = targets.as_slice();

        let mut true_positives = 0.0;
        let mut false_negatives = 0.0;

        for (pred, target) in pred_data.iter().zip(target_data.iter()) {
            let pred_class = if *pred > 0.5 { 1.0 } else { 0.0 };
            if pred_class == 1.0 && *target == 1.0 {
                true_positives += 1.0;
            } else if pred_class == 0.0 && *target == 1.0 {
                false_negatives += 1.0;
            }
        }

        if true_positives + false_negatives == 0.0 {
            Ok(0.0)
        } else {
            Ok(true_positives / (true_positives + false_negatives))
        }
    }
}

/// 詳細評価メトリクス
/// Comprehensive evaluation metrics
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct DetailedMetrics {
    pub accuracy: f32,
    pub precision: f32,
    pub recall: f32,
    pub f1_score: f32,
    pub specificity: f32,
    pub auc_roc: f32,
    pub confusion_matrix: Vec<Vec<f32>>,
    pub classification_report: HashMap<String, HashMap<String, f32>>,
}

/// 早期停止設定
/// Early stopping configuration
#[derive(Debug, Clone)]
pub struct EarlyStoppingConfig {
    pub patience: usize,
    pub min_delta: f32,
    pub monitor: String, // "val_loss", "val_accuracy", etc.
    pub mode: String,    // "min", "max"
    pub restore_best_weights: bool,
}

/// 早期停止状態
/// Early stopping state
#[derive(Debug)]
pub struct EarlyStoppingState {
    pub config: EarlyStoppingConfig,
    pub best_value: f32,
    pub wait: usize,
    pub stopped_epoch: Option<usize>,
    pub best_weights: Option<F32Tensor>,
}

/// モデルチェックポイント設定
/// Model checkpoint configuration
#[derive(Debug, Clone)]
pub struct CheckpointConfig {
    pub save_freq: usize,        // エポック毎の保存頻度
    pub save_best_only: bool,    // 最良のモデルのみ保存
    pub monitor: String,         // "val_loss", "val_accuracy"
    pub mode: String,            // "min", "max"
    pub save_weights_only: bool, // 重みのみ保存
}

/// Mixed Precision設定
/// Mixed Precision Configuration
#[derive(Debug, Clone)]
pub struct MixedPrecisionConfig {
    pub enabled: bool,
    pub loss_scale: f32,
    pub growth_factor: f32,
    pub backoff_factor: f32,
    pub growth_interval: usize,
    pub scale_window: usize,
}

impl Default for MixedPrecisionConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            loss_scale: 65536.0,
            growth_factor: 2.0,
            backoff_factor: 0.5,
            growth_interval: 2000,
            scale_window: 1000,
        }
    }
}

impl MixedPrecisionConfig {
    /// 新しいMixed Precision設定を作成
    /// Create new mixed precision configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// AMP(Automatic Mixed Precision)対応設定
    /// AMP (Automatic Mixed Precision) compatible configuration
    pub fn amp() -> Self {
        Self {
            enabled: true,
            loss_scale: 65536.0,
            growth_factor: 2.0,
            backoff_factor: 0.5,
            growth_interval: 2000,
            scale_window: 1000,
        }
    }

    /// カスタム設定でMixed Precisionを作成
    /// Create mixed precision with custom settings
    pub fn custom(
        loss_scale: f32,
        growth_factor: f32,
        backoff_factor: f32,
        growth_interval: usize,
    ) -> Self {
        Self {
            enabled: true,
            loss_scale,
            growth_factor,
            backoff_factor,
            growth_interval,
            scale_window: growth_interval / 2,
        }
    }

    /// 動的損失スケール調整
    /// Dynamic loss scale adjustment
    pub fn adjust_scale(&mut self, has_overflow: bool, step: usize) {
        if has_overflow {
            self.loss_scale *= self.backoff_factor;
            self.loss_scale = self.loss_scale.max(1.0);
        } else if step % self.growth_interval == 0 {
            self.loss_scale *= self.growth_factor;
            self.loss_scale = self.loss_scale.min(65536.0);
        }
    }

    /// スケール値を取得
    /// Get current scale value
    pub fn get_scale(&self) -> f32 {
        if self.enabled {
            self.loss_scale
        } else {
            1.0
        }
    }
}

/// Mixed Precision状態管理
/// Mixed Precision State Management
#[derive(Debug, Clone)]
pub struct MixedPrecisionState {
    pub config: MixedPrecisionConfig,
    pub current_step: usize,
    pub overflow_count: usize,
    pub stable_count: usize,
}

impl MixedPrecisionState {
    /// 新しい状態を作成
    /// Create new state
    pub fn new(config: MixedPrecisionConfig) -> Self {
        Self {
            config,
            current_step: 0,
            overflow_count: 0,
            stable_count: 0,
        }
    }

    /// ステップ更新
    /// Update step
    pub fn step(&mut self, has_overflow: bool) {
        self.current_step += 1;

        if has_overflow {
            self.overflow_count += 1;
            self.stable_count = 0;
        } else {
            self.stable_count += 1;
        }

        self.config.adjust_scale(has_overflow, self.current_step);
    }

    /// オーバーフロー率を取得
    /// Get overflow rate
    pub fn overflow_rate(&self) -> f32 {
        if self.current_step == 0 {
            0.0
        } else {
            self.overflow_count as f32 / self.current_step as f32
        }
    }

    /// 安定性指標を取得
    /// Get stability metric
    pub fn stability_metric(&self) -> f32 {
        if self.current_step == 0 {
            1.0
        } else {
            self.stable_count as f32 / self.current_step.min(self.config.scale_window) as f32
        }
    }
}

/// チェックポイント状態
/// Checkpoint state
#[derive(Debug)]
pub struct CheckpointState {
    pub config: CheckpointConfig,
    pub best_value: f32,
    pub best_weights: Option<F32Tensor>,
    pub last_saved_epoch: usize,
}

impl EarlyStoppingConfig {
    /// 新しい早期停止設定を作成
    /// Create new early stopping configuration
    pub fn new(patience: usize, min_delta: f32, monitor: &str, mode: &str) -> Self {
        Self {
            patience,
            min_delta,
            monitor: monitor.to_string(),
            mode: mode.to_string(),
            restore_best_weights: true,
        }
    }

    /// バリデーション損失を監視する設定
    /// Configuration to monitor validation loss
    pub fn val_loss(patience: usize, min_delta: f32) -> Self {
        Self::new(patience, min_delta, "val_loss", "min")
    }

    /// バリデーション精度を監視する設定
    /// Configuration to monitor validation accuracy
    pub fn val_accuracy(patience: usize, min_delta: f32) -> Self {
        Self::new(patience, min_delta, "val_accuracy", "max")
    }
}

impl EarlyStoppingState {
    /// 新しい早期停止状態を作成
    /// Create new early stopping state
    pub fn new(config: EarlyStoppingConfig) -> Self {
        let best_value = if config.mode == "min" {
            f32::INFINITY
        } else {
            f32::NEG_INFINITY
        };

        Self {
            config,
            best_value,
            wait: 0,
            stopped_epoch: None,
            best_weights: None,
        }
    }

    /// エポック更新チェック
    /// Check epoch update
    pub fn update(&mut self, current_value: f32, current_weights: Option<F32Tensor>) -> bool {
        let improved = if self.config.mode == "min" {
            current_value < self.best_value - self.config.min_delta
        } else {
            current_value > self.best_value + self.config.min_delta
        };

        if improved {
            self.best_value = current_value;
            self.wait = 0;
            if self.config.restore_best_weights {
                self.best_weights = current_weights;
            }
            false // 継続
        } else {
            self.wait += 1;
            self.wait >= self.config.patience // 停止判定
        }
    }

    /// 最良の重みを取得
    /// Get best weights
    pub fn get_best_weights(&self) -> Option<&F32Tensor> {
        self.best_weights.as_ref()
    }

    /// 早期停止が必要かをチェック
    /// Check if early stopping should occur
    pub fn should_stop(&mut self, current_value: f32, current_weights: Option<F32Tensor>) -> bool {
        let improved = if self.config.mode == "min" {
            current_value < self.best_value - self.config.min_delta
        } else {
            current_value > self.best_value + self.config.min_delta
        };

        if improved {
            self.best_value = current_value;
            self.wait = 0;
            if self.config.restore_best_weights {
                self.best_weights = current_weights;
            }
            false // 継続
        } else {
            self.wait += 1;
            self.wait >= self.config.patience // 停止判定
        }
    }

    /// ベストかどうかをチェック(互換性のため)
    /// Check if it's the best (for compatibility)  
    pub fn is_best(&self) -> bool {
        self.wait == 0
    }
}

impl CheckpointConfig {
    /// 新しいチェックポイント設定を作成
    /// Create new checkpoint configuration
    pub fn new(save_freq: usize, monitor: &str, mode: &str) -> Self {
        Self {
            save_freq,
            save_best_only: true,
            monitor: monitor.to_string(),
            mode: mode.to_string(),
            save_weights_only: true,
        }
    }

    /// エポック毎に保存する設定
    /// Configuration to save every epoch
    pub fn every_epoch() -> Self {
        Self {
            save_freq: 1,
            save_best_only: false,
            monitor: "val_loss".to_string(),
            mode: "min".to_string(),
            save_weights_only: true,
        }
    }

    /// 最良モデルのみ保存する設定
    /// Configuration to save best model only
    pub fn best_only(monitor: &str, mode: &str) -> Self {
        Self {
            save_freq: 1,
            save_best_only: true,
            monitor: monitor.to_string(),
            mode: mode.to_string(),
            save_weights_only: true,
        }
    }
}

impl CheckpointState {
    /// 新しいチェックポイント状態を作成
    /// Create new checkpoint state
    pub fn new(config: CheckpointConfig) -> Self {
        let best_value = if config.mode == "min" {
            f32::INFINITY
        } else {
            f32::NEG_INFINITY
        };

        Self {
            config,
            best_value,
            best_weights: None,
            last_saved_epoch: 0,
        }
    }

    /// チェックポイント保存判定
    /// Determine checkpoint save
    pub fn should_save(&mut self, epoch: usize, current_value: f32) -> bool {
        let freq_check = (epoch + 1) % self.config.save_freq == 0;

        if !freq_check {
            return false;
        }

        if !self.config.save_best_only {
            self.last_saved_epoch = epoch;
            return true;
        }

        let improved = if self.config.mode == "min" {
            current_value < self.best_value
        } else {
            current_value > self.best_value
        };

        if improved {
            self.best_value = current_value;
            self.last_saved_epoch = epoch;
            true
        } else {
            false
        }
    }

    /// 最良重みを保存
    /// Save best weights
    pub fn save_best(&mut self, weights: F32Tensor) {
        self.best_weights = Some(weights);
    }

    /// 最良重みを取得
    /// Get best weights
    pub fn get_best_weights(&self) -> Option<&F32Tensor> {
        self.best_weights.as_ref()
    }
}

/// Model save/load functionality
impl F32Trainer {
    /// モデル状態を取得
    /// Get model state
    fn get_model_state(&self) -> RusTorchResult<String> {
        // 簡単なモデル状態の表現(実際の実装では層の重みを含む)
        // Simple model state representation (actual implementation would include layer weights)
        Ok(String::from("{}")) // 空のJSONオブジェクト
    }

    /// モデルを保存
    /// Save model to file
    pub fn save_model(&self, path: &str) -> RusTorchResult<()> {
        let serialized = self.get_model_state()?;

        std::fs::write(path, serialized)
            .map_err(|e| RusTorchError::tensor_op(format!("Failed to write model file: {}", e)))?;

        Ok(())
    }

    /// モデルをロード
    /// Load model from file
    pub fn load_model(&mut self, path: &str) -> RusTorchResult<()> {
        let contents = std::fs::read_to_string(path)
            .map_err(|e| RusTorchError::tensor_op(format!("Failed to read model file: {}", e)))?;

        // モデル状態をロード(簡略化)
        // Load model state (simplified)
        self.set_model_state(contents)?;
        Ok(())
    }

    /// 学習履歴を保存
    /// Save training history
    pub fn save_history(&self, path: &str) -> RusTorchResult<()> {
        let serialized = serde_json::to_string_pretty(&Vec::<String>::new())
            .map_err(|e| RusTorchError::tensor_op(format!("Failed to serialize history: {}", e)))?;

        std::fs::write(path, serialized).map_err(|e| {
            RusTorchError::tensor_op(format!("Failed to write history file: {}", e))
        })?;

        Ok(())
    }

    /// 学習履歴をロード
    /// Load training history
    pub fn load_history(&mut self, path: &str) -> RusTorchResult<()> {
        let contents = std::fs::read_to_string(path)
            .map_err(|e| RusTorchError::tensor_op(format!("Failed to read history file: {}", e)))?;

        // 履歴をロード(簡略化)
        let _history: Vec<String> = serde_json::from_str(&contents).map_err(|e| {
            RusTorchError::tensor_op(format!("Failed to deserialize history: {}", e))
        })?;

        Ok(())
    }

    /// モデル状態を設定
    /// Set model state
    pub fn set_model_state(&mut self, _state: String) -> RusTorchResult<()> {
        // 簡単な実装(実際のモデル状態設定は複雑になる)
        // Simple implementation (actual model state setting would be complex)
        println!("Setting model state (placeholder implementation)");
        Ok(())
    }

    /// チェックポイントを保存(モデル+履歴)
    /// Save checkpoint (model + history)
    pub fn save_checkpoint(&self, base_path: &str) -> RusTorchResult<()> {
        let model_path = format!("{}_model.json", base_path);
        let history_path = format!("{}_history.json", base_path);

        self.save_model(&model_path)?;
        self.save_history(&history_path)?;

        println!("✅ Checkpoint saved: {} (model + history)", base_path);
        Ok(())
    }

    /// チェックポイントをロード(モデル+履歴)
    /// Load checkpoint (model + history)
    pub fn load_checkpoint(&mut self, base_path: &str) -> RusTorchResult<()> {
        let model_path = format!("{}_model.json", base_path);
        let history_path = format!("{}_history.json", base_path);

        self.load_model(&model_path)?;

        // 履歴ファイルが存在する場合のみロード
        if std::path::Path::new(&history_path).exists() {
            self.load_history(&history_path)?;
        }

        println!("✅ Checkpoint loaded: {} (model + history)", base_path);
        Ok(())
    }
}

/// F32MLPの独立した保存・ロード機能
/// Standalone save/load functionality for F32MLP
impl F32MLP {
    /// MLPモデルを保存
    /// Save MLP model
    pub fn save(&self, path: &str) -> RusTorchResult<()> {
        let layers_data: Vec<LayerState> = self
            .layers
            .iter()
            .map(|layer| {
                let weight_data = layer.weight.as_slice();
                let weight_shape = layer.weight.shape();

                let (bias_data, bias_shape) = if let Some(ref bias_tensor) = layer.bias {
                    (Some(bias_tensor.as_slice()), Some(bias_tensor.shape()))
                } else {
                    (None, None)
                };

                Ok(LayerState {
                    weight_data: weight_data.to_vec(),
                    weight_shape: weight_shape.to_vec(),
                    bias_data: bias_data.map(|data| data.iter().cloned().collect()),
                    bias_shape: bias_shape.map(|shape| shape.to_vec()),
                })
            })
            .collect::<RusTorchResult<Vec<_>>>()?;

        let serialized = serde_json::to_string_pretty(&layers_data)
            .map_err(|e| RusTorchError::tensor_op(format!("Failed to serialize model: {}", e)))?;

        std::fs::write(path, serialized)
            .map_err(|e| RusTorchError::tensor_op(format!("Failed to write model file: {}", e)))?;

        Ok(())
    }

    /// MLPモデルをロード
    /// Load MLP model
    pub fn load(path: &str) -> RusTorchResult<Self> {
        let contents = std::fs::read_to_string(path)
            .map_err(|e| RusTorchError::tensor_op(format!("Failed to read model file: {}", e)))?;

        // 簡略化されたモデル状態復元(基本実装)
        // Simplified model state restoration (basic implementation)
        let saved_weights: Vec<LayerState> = serde_json::from_str(&contents)
            .map_err(|e| RusTorchError::tensor_op(format!("Failed to deserialize model: {}", e)))?;

        let mut layers = Vec::new();
        for layer_state in saved_weights {
            // 重みテンソルを復元
            let weight = F32Tensor::from_vec(layer_state.weight_data, &layer_state.weight_shape)?;

            // バイアステンソルを復元
            let bias = if let (Some(bias_data), Some(bias_shape)) =
                (layer_state.bias_data, layer_state.bias_shape)
            {
                Some(F32Tensor::from_vec(bias_data, &bias_shape)?)
            } else {
                None
            };

            let input_features = weight.shape()[1];
            let output_features = weight.shape()[0];

            layers.push(F32Linear {
                weight,
                bias,
                weight_grad: None,
                bias_grad: None,
                last_input: None,
                input_features,
                output_features,
            });
        }

        Ok(Self {
            layers,
            activations: vec![F32Activation::ReLU], // デフォルト活性化関数
            layer_outputs: Vec::new(),
        })
    }

    /// モデルの重みを取得(Mixed Precision対応)
    /// Get model weights (Mixed Precision compatible)
    pub fn get_weights(&self) -> RusTorchResult<Vec<F32Tensor>> {
        let mut weights = Vec::new();
        for layer in &self.layers {
            weights.push(layer.weight.clone());
            if let Some(ref bias) = layer.bias {
                weights.push(bias.clone());
            }
        }
        Ok(weights)
    }

    /// モデルの重みを設定(Mixed Precision対応)
    /// Set model weights (Mixed Precision compatible)
    pub fn set_weights(&mut self, weights: Vec<F32Tensor>) -> RusTorchResult<()> {
        let mut weight_idx = 0;
        for layer in &mut self.layers {
            if weight_idx < weights.len() {
                layer.weight = weights[weight_idx].clone();
                weight_idx += 1;
            }

            if layer.bias.is_some() && weight_idx < weights.len() {
                layer.bias = Some(weights[weight_idx].clone());
                weight_idx += 1;
            }
        }
        Ok(())
    }

    /// 訓練モードを設定
    /// Set training mode
    pub fn train(&mut self) {
        // 訓練モードの設定(ドロップアウトなどの制御)
        // Training mode setting (control dropout, etc.)
    }

    /// 評価モードを設定
    /// Set evaluation mode
    pub fn eval(&mut self) {
        // 評価モードの設定(ドロップアウト無効化など)
        // Evaluation mode setting (disable dropout, etc.)
    }

    /// モデル情報を表示
    /// Display model information
    pub fn summary(&self) {
        println!("=== F32MLP Model Summary ===");
        println!("Total layers: {}", self.layers.len());
        println!("Total parameters: {}", self.parameter_count());

        for (i, layer) in self.layers.iter().enumerate() {
            println!(
                "Layer {}: Linear({} -> {})",
                i, layer.input_features, layer.output_features
            );

            if i < self.activations.len() {
                println!("Activation {}: {:?}", i, self.activations[i]);
            }
        }
        println!("============================");
    }

    /// Mixed Precision対応の順伝播
    /// Mixed Precision compatible forward pass
    pub fn forward_with_amp(
        &mut self,
        input: &F32Tensor,
        amp_scale: f32,
    ) -> RusTorchResult<F32Tensor> {
        self.layer_outputs.clear();
        let mut current = input.clone();

        // AMP使用時は計算精度を調整(概念的実装)
        if amp_scale != 1.0 {
            let temp = current.mul_scalar(amp_scale)?;
            current = temp;
        }

        for (i, layer) in self.layers.iter_mut().enumerate() {
            current = layer.forward(&current)?;
            self.layer_outputs.push(current.clone());

            if i < self.activations.len() {
                current = self.activations[i].forward(&current)?;
            }
        }

        // スケール補正
        if amp_scale != 1.0 {
            let temp = current.mul_scalar(1.0 / amp_scale)?;
            current = temp;
        }

        Ok(current)
    }

    /// 勾配クリッピング
    /// Gradient clipping
    pub fn clip_gradients(&mut self, max_norm: f32) -> RusTorchResult<f32> {
        let mut total_norm: f32 = 0.0;

        // 全勾配のノルムを計算
        for layer in &self.layers {
            if let Some(ref weight_grad) = layer.weight_grad {
                // 簡単なノルム計算(L2ノルム近似)
                let grad_data = weight_grad.as_slice();
                let grad_norm = grad_data.iter().map(|x| x * x).sum::<f32>().sqrt();
                total_norm += grad_norm * grad_norm;
            }

            if let Some(ref bias_grad) = layer.bias_grad {
                // 簡単なノルム計算(L2ノルム近似)
                let grad_data = bias_grad.as_slice();
                let grad_norm = grad_data.iter().map(|x| x * x).sum::<f32>().sqrt();
                total_norm += grad_norm * grad_norm;
            }
        }

        total_norm = total_norm.sqrt();

        // クリッピング実行
        if total_norm > max_norm {
            let clip_coef = max_norm / total_norm;
            for layer in &mut self.layers {
                if let Some(ref mut weight_grad) = layer.weight_grad {
                    let temp = weight_grad.mul_scalar(clip_coef)?;
                    *weight_grad = temp;
                }

                if let Some(ref mut bias_grad) = layer.bias_grad {
                    let temp = bias_grad.mul_scalar(clip_coef)?;
                    *bias_grad = temp;
                }
            }
        }

        Ok(total_norm)
    }
}

// ===== フェーズ5: Learning Rate Scheduler機能 / Phase 5: Learning Rate Scheduler Features =====

/// 学習率スケジューリング戦略
/// Learning rate scheduling strategy
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum F32LRSchedulerType {
    /// 固定学習率
    /// Constant learning rate
    Constant,
    /// ステップ減衰
    /// Step decay
    StepLR { step_size: usize, gamma: f32 },
    /// 指数減衰
    /// Exponential decay
    ExponentialLR { gamma: f32 },
    /// Cosine Annealing
    CosineLR { t_max: usize, eta_min: f32 },
    /// Reduce on Plateau
    ReduceOnPlateau {
        factor: f32,
        patience: usize,
        threshold: f32,
    },
    /// Linear Warm-up + Cosine Decay
    WarmupCosine {
        warmup_steps: usize,
        total_steps: usize,
    },
}

/// f32学習率スケジューラー
/// f32 Learning Rate Scheduler
#[derive(Debug, Clone)]
pub struct F32LRScheduler {
    scheduler_type: F32LRSchedulerType,
    initial_lr: f32,
    current_lr: f32,
    current_step: usize,
    last_metric: f32,
    no_improvement_count: usize,
    best_metric: f32,
}

impl F32LRScheduler {
    /// 新しいスケジューラーを作成
    /// Create new scheduler
    pub fn new(scheduler_type: F32LRSchedulerType, initial_lr: f32) -> Self {
        Self {
            scheduler_type,
            initial_lr,
            current_lr: initial_lr,
            current_step: 0,
            last_metric: f32::INFINITY,
            no_improvement_count: 0,
            best_metric: f32::INFINITY,
        }
    }

    /// 学習率をステップ更新
    /// Step update learning rate
    pub fn step(&mut self, metric: Option<f32>) -> f32 {
        self.current_step += 1;

        let new_lr = match &self.scheduler_type {
            F32LRSchedulerType::Constant => self.initial_lr,

            F32LRSchedulerType::StepLR { step_size, gamma } => {
                if self.current_step % step_size == 0 {
                    self.current_lr * gamma
                } else {
                    self.current_lr
                }
            }

            F32LRSchedulerType::ExponentialLR { gamma } => {
                self.initial_lr * gamma.powf(self.current_step as f32)
            }

            F32LRSchedulerType::CosineLR { t_max, eta_min } => {
                let progress = (self.current_step as f32) / (*t_max as f32);
                let cosine_factor = 0.5 * (1.0 + (std::f32::consts::PI * progress).cos());
                eta_min + (self.initial_lr - eta_min) * cosine_factor
            }

            F32LRSchedulerType::ReduceOnPlateau {
                factor,
                patience,
                threshold,
            } => {
                if let Some(current_metric) = metric {
                    if current_metric < self.best_metric - threshold {
                        self.best_metric = current_metric;
                        self.no_improvement_count = 0;
                    } else {
                        self.no_improvement_count += 1;
                        if self.no_improvement_count >= *patience {
                            self.no_improvement_count = 0;
                            return self.current_lr * factor;
                        }
                    }
                }
                self.current_lr
            }

            F32LRSchedulerType::WarmupCosine {
                warmup_steps,
                total_steps,
            } => {
                if self.current_step <= *warmup_steps {
                    // Linear warmup
                    self.initial_lr * (self.current_step as f32) / (*warmup_steps as f32)
                } else {
                    // Cosine decay
                    let decay_steps = *total_steps - *warmup_steps;
                    let decay_progress =
                        ((self.current_step - *warmup_steps) as f32) / (decay_steps as f32);
                    let cosine_factor = 0.5 * (1.0 + (std::f32::consts::PI * decay_progress).cos());
                    self.initial_lr * cosine_factor
                }
            }
        };

        self.current_lr = new_lr;
        self.current_lr
    }

    /// 現在の学習率を取得
    /// Get current learning rate
    pub fn get_lr(&self) -> f32 {
        self.current_lr
    }

    /// スケジューラーをリセット
    /// Reset scheduler
    pub fn reset(&mut self) {
        self.current_lr = self.initial_lr;
        self.current_step = 0;
        self.no_improvement_count = 0;
        self.best_metric = f32::INFINITY;
    }

    /// スケジューラー状態を取得
    /// Get scheduler state
    pub fn state(&self) -> (usize, f32, f32) {
        (self.current_step, self.current_lr, self.best_metric)
    }
}

/// F32Trainerに学習率スケジューラーサポートを追加
/// Add learning rate scheduler support to F32Trainer
impl F32Trainer {
    /// 学習率スケジューラーを設定
    /// Set learning rate scheduler
    pub fn set_lr_scheduler(&mut self, scheduler: F32LRScheduler) -> &mut Self {
        // まず現在のオプティマイザーの学習率をスケジューラーに合わせる
        let initial_lr = scheduler.initial_lr;
        self.optimizer.set_learning_rate(initial_lr);

        // スケジューラーをメンバーとして保存したい場合は、構造体に追加する必要があります
        // ここでは関数パラメータとして使用する方式を採用
        self
    }

    /// 学習率スケジューラーと組み合わせた学習メソッド
    /// Training method with learning rate scheduler
    pub fn fit_with_scheduler(
        &mut self,
        train_x: &F32Tensor,
        train_y: &F32Tensor,
        val_x: Option<&F32Tensor>,
        val_y: Option<&F32Tensor>,
        epochs: usize,
        batch_size: usize,
        mut lr_scheduler: F32LRScheduler,
        verbose: bool,
    ) -> RusTorchResult<()> {
        let num_batches = (train_x.shape()[0] + batch_size - 1) / batch_size;

        for epoch in 0..epochs {
            let mut epoch_train_loss = 0.0;
            let mut epoch_train_acc = 0.0;

            // バッチ処理
            for batch_idx in 0..num_batches {
                let start_idx = batch_idx * batch_size;
                let end_idx = std::cmp::min(start_idx + batch_size, train_x.shape()[0]);

                // バッチデータを取得
                let batch_x = train_x.slice(&[(start_idx, end_idx)])?;
                let batch_y = train_y.slice(&[(start_idx, end_idx)])?;

                // 勾配をクリア
                self.optimizer.zero_grad(&mut self.model);

                // フォワードパス
                let predictions = self.model.forward(&batch_x)?;

                // 損失計算
                let loss = self.loss_fn.forward(&predictions, &batch_y)?;
                epoch_train_loss += loss.as_slice()[0];

                // 精度計算
                let accuracy = F32Metrics::accuracy(&predictions, &batch_y)?;
                epoch_train_acc += accuracy;

                // バックワードパス(簡素化)
                let grad_output = self.loss_fn.backward(&predictions, &batch_y)?;
                self.backward_through_model(&grad_output)?;

                // パラメータ更新
                self.optimizer.step(&mut self.model)?;
            }

            // エポック平均
            epoch_train_loss /= num_batches as f32;
            epoch_train_acc /= num_batches as f32;

            // training_historyはVec<F32TrainingEpoch>なので、エポック記録として追加
            // training_history is Vec<F32TrainingEpoch>, so add as epoch record

            // バリデーション
            let mut val_loss_for_scheduler = None;
            if let (Some(val_x), Some(val_y)) = (val_x, val_y) {
                let (val_loss, val_acc) = self.validate(val_x, val_y)?;
                // validation結果もエポック記録に含める
                // include validation results in epoch record
                val_loss_for_scheduler = Some(val_loss);

                if verbose {
                    println!(
                        "Epoch {}/{} - train_loss: {:.4}, train_acc: {:.4}, val_loss: {:.4}, val_acc: {:.4}, lr: {:.6}",
                        epoch + 1, epochs, epoch_train_loss, epoch_train_acc, val_loss, val_acc, lr_scheduler.get_lr()
                    );
                }
            } else if verbose {
                println!(
                    "Epoch {}/{} - train_loss: {:.4}, train_acc: {:.4}, lr: {:.6}",
                    epoch + 1,
                    epochs,
                    epoch_train_loss,
                    epoch_train_acc,
                    lr_scheduler.get_lr()
                );
            }

            // 学習率スケジューラーをステップ更新
            let new_lr = lr_scheduler.step(val_loss_for_scheduler);
            self.optimizer.set_learning_rate(new_lr);
        }

        // epochsはtraining_historyのlengthで管理
        // epochs managed by training_history length
        Ok(())
    }
}

// ============================================================================
// Computer Vision Models / コンピュータビジョンモデル
// ============================================================================

/// 2D畳み込み層
/// 2D Convolution layer
#[derive(Debug)]
pub struct F32Conv2d {
    pub weight: F32Tensor,       // (out_channels, in_channels, kernel_h, kernel_w)
    pub bias: Option<F32Tensor>, // (out_channels,)
    pub weight_grad: Option<F32Tensor>,
    pub bias_grad: Option<F32Tensor>,
    pub last_input: Option<F32Tensor>,
    pub in_channels: usize,
    pub out_channels: usize,
    pub kernel_size: (usize, usize),
    pub stride: (usize, usize),
    pub padding: (usize, usize),
}

impl F32Conv2d {
    /// 新しい2D畳み込み層を作成
    /// Create a new 2D convolution layer
    pub fn new(
        in_channels: usize,
        out_channels: usize,
        kernel_size: (usize, usize),
        stride: (usize, usize),
        padding: (usize, usize),
        bias: bool,
    ) -> Result<Self, RusTorchError> {
        let (kernel_h, kernel_w) = kernel_size;

        // Heの初期化
        let fan_in = in_channels * kernel_h * kernel_w;
        let std = (2.0 / fan_in as f32).sqrt();

        let weight_shape = vec![out_channels, in_channels, kernel_h, kernel_w];
        let weight = F32Tensor::randn(&weight_shape);
        let std_tensor = F32Tensor::from_scalar(std)?;
        let weight = weight?.mul(&std_tensor)?;

        let bias_tensor = if bias {
            Some(F32Tensor::zeros(&[out_channels])?)
        } else {
            None
        };

        Ok(Self {
            weight,
            bias: bias_tensor,
            weight_grad: None,
            bias_grad: None,
            last_input: None,
            in_channels,
            out_channels,
            kernel_size,
            stride,
            padding,
        })
    }

    /// フォワードパス(簡素化された畳み込み)
    /// Forward pass (simplified convolution)
    pub fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 入力を保存(バックワードパス用)
        self.last_input = Some(input.clone());

        // 入力形状: (batch_size, in_channels, height, width)
        let input_shape = input.shape();
        if input_shape.len() != 4 {
            return Err(RusTorchError::tensor_op(
                "Conv2d input must be 4D (batch, channels, height, width)",
            ));
        }

        let batch_size = input_shape[0];
        let input_height = input_shape[2];
        let input_width = input_shape[3];

        // 出力サイズ計算
        let output_height =
            (input_height + 2 * self.padding.0 - self.kernel_size.0) / self.stride.0 + 1;
        let output_width =
            (input_width + 2 * self.padding.1 - self.kernel_size.1) / self.stride.1 + 1;

        // 簡素化された畳み込み(im2colを使わない直接実装)
        let mut output_data =
            vec![0.0; batch_size * self.out_channels * output_height * output_width];
        let input_data = input.as_slice();
        let weight_data = self.weight.as_slice();

        for b in 0..batch_size {
            for out_c in 0..self.out_channels {
                for oh in 0..output_height {
                    for ow in 0..output_width {
                        let mut sum = 0.0;

                        // カーネル内の畳み込み
                        for kh in 0..self.kernel_size.0 {
                            for kw in 0..self.kernel_size.1 {
                                let ih = oh * self.stride.0 + kh;
                                let iw = ow * self.stride.1 + kw;

                                // パディングチェック
                                if ih >= self.padding.0
                                    && ih < input_height + self.padding.0
                                    && iw >= self.padding.1
                                    && iw < input_width + self.padding.1
                                {
                                    let ih_actual = ih - self.padding.0;
                                    let iw_actual = iw - self.padding.1;

                                    for in_c in 0..self.in_channels {
                                        let input_idx =
                                            b * self.in_channels * input_height * input_width
                                                + in_c * input_height * input_width
                                                + ih_actual * input_width
                                                + iw_actual;
                                        let weight_idx = out_c
                                            * self.in_channels
                                            * self.kernel_size.0
                                            * self.kernel_size.1
                                            + in_c * self.kernel_size.0 * self.kernel_size.1
                                            + kh * self.kernel_size.1
                                            + kw;

                                        sum += input_data[input_idx] * weight_data[weight_idx];
                                    }
                                }
                            }
                        }

                        // バイアス追加
                        if let Some(ref bias) = self.bias {
                            sum += bias.as_slice()[out_c];
                        }

                        let output_idx = b * self.out_channels * output_height * output_width
                            + out_c * output_height * output_width
                            + oh * output_width
                            + ow;
                        output_data[output_idx] = sum;
                    }
                }
            }
        }

        let output_shape = vec![batch_size, self.out_channels, output_height, output_width];
        F32Tensor::from_vec(output_data, &output_shape)
    }
}

impl F32Layer for F32Conv2d {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        self.forward(input)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        // TODO: 実際の畳み込みの逆伝播を実装
        Ok(grad_output.clone())
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = vec![&self.weight];
        if let Some(ref bias) = self.bias {
            params.push(bias);
        }
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        // 簡素化された勾配更新
        if let Some(ref weight_grad) = self.weight_grad {
            let lr_tensor = F32Tensor::from_scalar(learning_rate)?;
            let update = weight_grad.mul(&lr_tensor)?;
            self.weight = self.weight.sub(&update)?;
        }

        if let (Some(ref mut bias), Some(ref bias_grad)) = (&mut self.bias, &self.bias_grad) {
            let lr_tensor = F32Tensor::from_scalar(learning_rate)?;
            let update = bias_grad.mul(&lr_tensor)?;
            *bias = bias.sub(&update)?;
        }

        Ok(())
    }
}

/// バッチ正規化層
/// Batch Normalization layer
#[derive(Debug)]
pub struct F32BatchNorm2d {
    pub num_features: usize,
    pub weight: F32Tensor,       // (num_features,)
    pub bias: F32Tensor,         // (num_features,)
    pub running_mean: F32Tensor, // (num_features,)
    pub running_var: F32Tensor,  // (num_features,)
    pub momentum: f32,
    pub eps: f32,
    pub training: bool,
    pub weight_grad: Option<F32Tensor>,
    pub bias_grad: Option<F32Tensor>,
}

impl F32BatchNorm2d {
    /// 新しいバッチ正規化層を作成
    /// Create a new batch normalization layer
    pub fn new(num_features: usize, momentum: f32, eps: f32) -> Result<Self, RusTorchError> {
        Ok(Self {
            num_features,
            weight: F32Tensor::ones(&[num_features])?,
            bias: F32Tensor::zeros(&[num_features])?,
            running_mean: F32Tensor::zeros(&[num_features])?,
            running_var: F32Tensor::ones(&[num_features])?,
            momentum,
            eps,
            training: true,
            weight_grad: None,
            bias_grad: None,
        })
    }

    /// 訓練モードを設定
    /// Set training mode
    pub fn train(&mut self, mode: bool) {
        self.training = mode;
    }

    /// フォワードパス
    /// Forward pass
    pub fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 入力形状: (batch_size, num_features, height, width)
        let input_shape = input.shape();
        if input_shape.len() != 4 || input_shape[1] != self.num_features {
            return Err(RusTorchError::tensor_op("BatchNorm2d input shape mismatch"));
        }

        let batch_size = input_shape[0];
        let height = input_shape[2];
        let width = input_shape[3];
        let spatial_size = height * width;

        let input_data = input.as_slice();
        let mut output_data = vec![0.0; input_data.len()];

        if self.training {
            // 訓練モード:バッチ統計を計算
            let mut batch_mean = vec![0.0; self.num_features];
            let mut batch_var = vec![0.0; self.num_features];

            // 平均計算
            for c in 0..self.num_features {
                let mut sum = 0.0;
                for b in 0..batch_size {
                    for spatial in 0..spatial_size {
                        let idx = b * self.num_features * spatial_size + c * spatial_size + spatial;
                        sum += input_data[idx];
                    }
                }
                batch_mean[c] = sum / (batch_size * spatial_size) as f32;
            }

            // 分散計算
            for c in 0..self.num_features {
                let mut sum_sq = 0.0;
                for b in 0..batch_size {
                    for spatial in 0..spatial_size {
                        let idx = b * self.num_features * spatial_size + c * spatial_size + spatial;
                        let diff = input_data[idx] - batch_mean[c];
                        sum_sq += diff * diff;
                    }
                }
                batch_var[c] = sum_sq / (batch_size * spatial_size) as f32;
            }

            // 移動平均更新
            let running_mean_data = self.running_mean.as_slice();
            let running_var_data = self.running_var.as_slice();
            let mut new_running_mean = vec![0.0; self.num_features];
            let mut new_running_var = vec![0.0; self.num_features];

            for c in 0..self.num_features {
                new_running_mean[c] =
                    (1.0 - self.momentum) * running_mean_data[c] + self.momentum * batch_mean[c];
                new_running_var[c] =
                    (1.0 - self.momentum) * running_var_data[c] + self.momentum * batch_var[c];
            }

            self.running_mean = F32Tensor::from_vec(new_running_mean, &[self.num_features])?;
            self.running_var = F32Tensor::from_vec(new_running_var, &[self.num_features])?;

            // 正規化と変換
            let weight_data = self.weight.as_slice();
            let bias_data = self.bias.as_slice();

            for c in 0..self.num_features {
                let std = (batch_var[c] + self.eps).sqrt();
                for b in 0..batch_size {
                    for spatial in 0..spatial_size {
                        let idx = b * self.num_features * spatial_size + c * spatial_size + spatial;
                        let normalized = (input_data[idx] - batch_mean[c]) / std;
                        output_data[idx] = weight_data[c] * normalized + bias_data[c];
                    }
                }
            }
        } else {
            // 推論モード:保存された統計を使用
            let running_mean_data = self.running_mean.as_slice();
            let running_var_data = self.running_var.as_slice();
            let weight_data = self.weight.as_slice();
            let bias_data = self.bias.as_slice();

            for c in 0..self.num_features {
                let std = (running_var_data[c] + self.eps).sqrt();
                for b in 0..batch_size {
                    for spatial in 0..spatial_size {
                        let idx = b * self.num_features * spatial_size + c * spatial_size + spatial;
                        let normalized = (input_data[idx] - running_mean_data[c]) / std;
                        output_data[idx] = weight_data[c] * normalized + bias_data[c];
                    }
                }
            }
        }

        F32Tensor::from_vec(output_data, input_shape)
    }
}

impl F32Layer for F32BatchNorm2d {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        self.forward(input)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        // TODO: 実際のバッチ正規化の逆伝播を実装
        Ok(grad_output.clone())
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        vec![&self.weight, &self.bias]
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        if let Some(ref weight_grad) = self.weight_grad {
            let lr_tensor = F32Tensor::from_scalar(learning_rate)?;
            let update = weight_grad.mul(&lr_tensor)?;
            self.weight = self.weight.sub(&update)?;
        }

        if let Some(ref bias_grad) = self.bias_grad {
            let lr_tensor = F32Tensor::from_scalar(learning_rate)?;
            let update = bias_grad.mul(&lr_tensor)?;
            self.bias = self.bias.sub(&update)?;
        }

        Ok(())
    }
}

/// 簡単なCNNモデル
/// Simple CNN model
#[derive(Debug)]
pub struct F32SimpleCNN {
    pub conv1: F32Conv2d,
    pub bn1: F32BatchNorm2d,
    pub conv2: F32Conv2d,
    pub bn2: F32BatchNorm2d,
    pub fc: F32Linear,
    pub num_classes: usize,
}

impl F32SimpleCNN {
    /// 新しいCNNモデルを作成
    /// Create a new CNN model
    pub fn new(
        input_channels: usize,
        num_classes: usize,
        hidden_channels: usize,
    ) -> Result<Self, RusTorchError> {
        let conv1 = F32Conv2d::new(
            input_channels,
            hidden_channels,
            (3, 3),
            (1, 1),
            (1, 1),
            true,
        )?;
        let bn1 = F32BatchNorm2d::new(hidden_channels, 0.1, 1e-5)?;
        let conv2 = F32Conv2d::new(
            hidden_channels,
            hidden_channels * 2,
            (3, 3),
            (2, 2),
            (1, 1),
            true,
        )?;
        let bn2 = F32BatchNorm2d::new(hidden_channels * 2, 0.1, 1e-5)?;

        // 仮定:28x28の入力画像(MNIST風)でのFC層の入力サイズ
        // 28x28 -> conv1 -> 28x28 -> conv2 (stride=2) -> 14x14
        let fc_input_size = hidden_channels * 2 * 14 * 14;
        let fc = F32Linear::new(fc_input_size, num_classes, true)?;

        Ok(Self {
            conv1,
            bn1,
            conv2,
            bn2,
            fc,
            num_classes,
        })
    }

    /// 訓練モードを設定
    /// Set training mode
    pub fn train(&mut self, mode: bool) {
        self.bn1.train(mode);
        self.bn2.train(mode);
    }

    /// フォワードパス
    /// Forward pass
    pub fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // Conv1 -> BN1 -> ReLU
        let x = self.conv1.forward(input)?;
        let x = self.bn1.forward(&x)?;
        let x = F32Activation::ReLU.forward(&x)?;

        // Conv2 -> BN2 -> ReLU
        let x = self.conv2.forward(&x)?;
        let x = self.bn2.forward(&x)?;
        let x = F32Activation::ReLU.forward(&x)?;

        // Flatten
        let batch_size = x.shape()[0];
        let flattened_size = x.shape().iter().skip(1).product();
        let x = x.reshape(&[batch_size, flattened_size])?;

        // FC layer
        self.fc.forward(&x)
    }
}

impl F32Layer for F32SimpleCNN {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        self.forward(input)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        // TODO: 実際のCNNの逆伝播を実装
        Ok(grad_output.clone())
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = Vec::new();
        params.extend(self.conv1.parameters());
        params.extend(self.bn1.parameters());
        params.extend(self.conv2.parameters());
        params.extend(self.bn2.parameters());
        params.extend(self.fc.parameters());
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        self.conv1.update_parameters(learning_rate)?;
        self.bn1.update_parameters(learning_rate)?;
        self.conv2.update_parameters(learning_rate)?;
        self.bn2.update_parameters(learning_rate)?;
        self.fc.update_parameters(learning_rate)?;
        Ok(())
    }
}

// ============================================================================
// Pre-trained Models & Image Processing / 事前学習モデル&画像処理
// ============================================================================

/// 事前学習モデルのメタデータ
/// Pre-trained model metadata
#[derive(Debug, Clone)]
pub struct F32PretrainedModelInfo {
    pub name: String,
    pub architecture: String,
    pub input_size: (usize, usize, usize), // (channels, height, width)
    pub num_classes: usize,
    pub mean: Vec<f32>, // 正規化の平均値
    pub std: Vec<f32>,  // 正規化の標準偏差
    pub file_path: String,
}

/// 事前学習モデルローダー
/// Pre-trained model loader
#[derive(Debug)]
pub struct F32PretrainedLoader {
    pub available_models: Vec<F32PretrainedModelInfo>,
}

impl F32PretrainedLoader {
    /// 新しいローダーを作成
    /// Create a new loader
    pub fn new() -> Self {
        let mut available_models = Vec::new();

        // ResNet18風のモデル情報(例)
        available_models.push(F32PretrainedModelInfo {
            name: "resnet18".to_string(),
            architecture: "ResNet".to_string(),
            input_size: (3, 224, 224),
            num_classes: 1000,
            mean: vec![0.485, 0.456, 0.406],
            std: vec![0.229, 0.224, 0.225],
            file_path: "models/resnet18.safetensors".to_string(),
        });

        // MobileNetV2風のモデル情報(例)
        available_models.push(F32PretrainedModelInfo {
            name: "mobilenet_v2".to_string(),
            architecture: "MobileNet".to_string(),
            input_size: (3, 224, 224),
            num_classes: 1000,
            mean: vec![0.485, 0.456, 0.406],
            std: vec![0.229, 0.224, 0.225],
            file_path: "models/mobilenet_v2.safetensors".to_string(),
        });

        Self { available_models }
    }

    /// 利用可能なモデル一覧を取得
    /// Get list of available models
    pub fn list_models(&self) -> &[F32PretrainedModelInfo] {
        &self.available_models
    }

    /// モデル情報を取得
    /// Get model info
    pub fn get_model_info(&self, name: &str) -> Option<&F32PretrainedModelInfo> {
        self.available_models
            .iter()
            .find(|model| model.name == name)
    }

    /// 事前学習モデルをロード(簡素化された実装)
    /// Load pre-trained model (simplified implementation)
    pub fn load_model(&self, name: &str) -> Result<F32SimpleCNN, RusTorchError> {
        let model_info = self
            .get_model_info(name)
            .ok_or_else(|| RusTorchError::tensor_op(&format!("Model '{}' not found", name)))?;

        // 簡素化:実際のファイルからロードする代わりに、新しいモデルを作成
        // TODO: 実際の事前学習重みをロードする実装
        println!(
            "Warning: Loading architecture only, not pre-trained weights for {}",
            name
        );

        match model_info.architecture.as_str() {
            "ResNet" | "MobileNet" => {
                let input_channels = model_info.input_size.0;
                let num_classes = model_info.num_classes;
                F32SimpleCNN::new(input_channels, num_classes, 64)
            }
            _ => Err(RusTorchError::tensor_op(&format!(
                "Unsupported architecture: {}",
                model_info.architecture
            ))),
        }
    }
}

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

/// 画像前処理ユーティリティ
/// Image preprocessing utilities
#[derive(Debug)]
pub struct F32ImagePreprocessor {
    pub mean: Vec<f32>,
    pub std: Vec<f32>,
    pub resize_size: Option<(usize, usize)>,
}

impl F32ImagePreprocessor {
    /// 新しい前処理器を作成
    /// Create a new preprocessor
    pub fn new(mean: Vec<f32>, std: Vec<f32>, resize_size: Option<(usize, usize)>) -> Self {
        Self {
            mean,
            std,
            resize_size,
        }
    }

    /// ImageNet用の標準前処理器
    /// Standard preprocessor for ImageNet
    pub fn imagenet() -> Self {
        Self::new(
            vec![0.485, 0.456, 0.406],
            vec![0.229, 0.224, 0.225],
            Some((224, 224)),
        )
    }

    /// 正規化を適用(簡素化された実装)
    /// Apply normalization (simplified implementation)
    pub fn normalize(&self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        let input_shape = input.shape();
        if input_shape.len() != 4 {
            return Err(RusTorchError::tensor_op(
                "Image input must be 4D (batch, channels, height, width)",
            ));
        }

        let channels = input_shape[1];
        if channels != self.mean.len() || channels != self.std.len() {
            return Err(RusTorchError::tensor_op(
                "Channel count mismatch with mean/std",
            ));
        }

        let input_data = input.as_slice();
        let mut output_data = vec![0.0; input_data.len()];

        let batch_size = input_shape[0];
        let height = input_shape[2];
        let width = input_shape[3];
        let spatial_size = height * width;

        for b in 0..batch_size {
            for c in 0..channels {
                for spatial in 0..spatial_size {
                    let idx = b * channels * spatial_size + c * spatial_size + spatial;
                    output_data[idx] = (input_data[idx] - self.mean.get(c).unwrap_or(&0.0))
                        / self.std.get(c).unwrap_or(&1.0);
                }
            }
        }

        F32Tensor::from_vec(output_data, input_shape)
    }

    /// 前処理パイプライン(正規化のみ)
    /// Preprocessing pipeline (normalization only)
    pub fn preprocess(&self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // TODO: リサイズ機能の実装
        self.normalize(input)
    }
}

impl Default for F32ImagePreprocessor {
    fn default() -> Self {
        Self::imagenet()
    }
}

// ========================================
// Conv1D/Conv3D - 完全な畳み込み層セット
// Conv1D/Conv3D - Complete convolution layer set
// ========================================

/// 1D畳み込み層
/// 1D Convolution layer
#[derive(Debug)]
pub struct F32Conv1d {
    pub weight: F32Tensor,       // (out_channels, in_channels, kernel_size)
    pub bias: Option<F32Tensor>, // (out_channels,)
    pub weight_grad: Option<F32Tensor>,
    pub bias_grad: Option<F32Tensor>,
    pub last_input: Option<F32Tensor>,
    pub in_channels: usize,
    pub out_channels: usize,
    pub kernel_size: usize,
    pub stride: usize,
    pub padding: usize,
    pub dilation: usize,
}

impl F32Conv1d {
    /// 新しい1D畳み込み層を作成
    /// Create a new 1D convolution layer
    pub fn new(
        in_channels: usize,
        out_channels: usize,
        kernel_size: usize,
        stride: usize,
        padding: usize,
        dilation: usize,
        bias: bool,
    ) -> Result<Self, RusTorchError> {
        // Heの初期化
        let fan_in = in_channels * kernel_size;
        let std = (2.0 / fan_in as f32).sqrt();

        let weight_shape = vec![out_channels, in_channels, kernel_size];
        let weight = F32Tensor::randn(&weight_shape);
        let std_tensor = F32Tensor::from_scalar(std)?;
        let weight = weight?.mul(&std_tensor)?;

        let bias_tensor = if bias {
            Some(F32Tensor::zeros(&[out_channels])?)
        } else {
            None
        };

        Ok(Self {
            weight,
            bias: bias_tensor,
            weight_grad: None,
            bias_grad: None,
            last_input: None,
            in_channels,
            out_channels,
            kernel_size,
            stride,
            padding,
            dilation,
        })
    }

    /// フォワードパス(1D畳み込み)
    /// Forward pass (1D convolution)
    pub fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 入力を保存(バックワードパス用)
        self.last_input = Some(input.clone());

        // 入力形状: (batch_size, in_channels, length)
        let input_shape = input.shape();
        if input_shape.len() != 3 {
            return Err(RusTorchError::tensor_op(
                "Conv1d input must be 3D (batch, channels, length)",
            ));
        }

        let batch_size = input_shape[0];
        let input_length = input_shape[2];

        // 出力サイズ計算
        let effective_kernel_size = self.dilation * (self.kernel_size - 1) + 1;
        let output_length =
            (input_length + 2 * self.padding - effective_kernel_size) / self.stride + 1;

        // 1D畳み込み実装
        let mut output_data = vec![0.0; batch_size * self.out_channels * output_length];
        let input_data = input.as_slice();
        let weight_data = self.weight.as_slice();

        for b in 0..batch_size {
            for out_c in 0..self.out_channels {
                for ol in 0..output_length {
                    let mut sum = 0.0;

                    // カーネル内の畳み込み
                    for k in 0..self.kernel_size {
                        let il = ol * self.stride + k * self.dilation;

                        // パディングチェック
                        if il >= self.padding && il < input_length + self.padding {
                            let il_actual = il - self.padding;

                            for in_c in 0..self.in_channels {
                                let input_idx = b * self.in_channels * input_length
                                    + in_c * input_length
                                    + il_actual;
                                let weight_idx = out_c * self.in_channels * self.kernel_size
                                    + in_c * self.kernel_size
                                    + k;

                                sum += input_data[input_idx] * weight_data[weight_idx];
                            }
                        }
                    }

                    // バイアス追加
                    if let Some(ref bias) = self.bias {
                        sum += bias.as_slice()[out_c];
                    }

                    let output_idx =
                        b * self.out_channels * output_length + out_c * output_length + ol;
                    output_data[output_idx] = sum;
                }
            }
        }

        let output_shape = vec![batch_size, self.out_channels, output_length];
        F32Tensor::from_vec(output_data, &output_shape)
    }
}

impl F32Layer for F32Conv1d {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        self.forward(input)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        if let Some(ref last_input) = self.last_input {
            // 重み勾配計算(簡素化)
            self.weight_grad = Some(F32Tensor::zeros(self.weight.shape())?);

            // バイアス勾配計算
            if self.bias.is_some() {
                self.bias_grad = Some(F32Tensor::zeros(&[self.out_channels])?);
            }

            // 入力勾配(簡素化:パススルー)
            Ok(last_input.clone())
        } else {
            Err(RusTorchError::tensor_op("No saved input for backward pass"))
        }
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = vec![&self.weight];
        if let Some(ref bias) = self.bias {
            params.push(bias);
        }
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> RusTorchResult<()> {
        // Update weight
        if let Some(ref weight_grad) = self.weight_grad {
            let update = weight_grad.mul_scalar(learning_rate)?;
            self.weight = self.weight.sub(&update)?;
        }

        // Update bias
        if let (Some(ref mut bias), Some(ref bias_grad)) = (&mut self.bias, &self.bias_grad) {
            let update = bias_grad.mul_scalar(learning_rate)?;
            *bias = bias.sub(&update)?;
        }

        Ok(())
    }
}

/// 3D畳み込み層
/// 3D Convolution layer
#[derive(Debug)]
pub struct F32Conv3d {
    pub weight: F32Tensor, // (out_channels, in_channels, kernel_d, kernel_h, kernel_w)
    pub bias: Option<F32Tensor>, // (out_channels,)
    pub weight_grad: Option<F32Tensor>,
    pub bias_grad: Option<F32Tensor>,
    pub last_input: Option<F32Tensor>,
    pub in_channels: usize,
    pub out_channels: usize,
    pub kernel_size: (usize, usize, usize), // (depth, height, width)
    pub stride: (usize, usize, usize),
    pub padding: (usize, usize, usize),
}

impl F32Conv3d {
    /// 新しい3D畳み込み層を作成
    /// Create a new 3D convolution layer
    pub fn new(
        in_channels: usize,
        out_channels: usize,
        kernel_size: (usize, usize, usize),
        stride: (usize, usize, usize),
        padding: (usize, usize, usize),
        bias: bool,
    ) -> Result<Self, RusTorchError> {
        let (kernel_d, kernel_h, kernel_w) = kernel_size;

        // Heの初期化
        let fan_in = in_channels * kernel_d * kernel_h * kernel_w;
        let std = (2.0 / fan_in as f32).sqrt();

        let weight_shape = vec![out_channels, in_channels, kernel_d, kernel_h, kernel_w];
        let weight = F32Tensor::randn(&weight_shape);
        let std_tensor = F32Tensor::from_scalar(std)?;
        let weight = weight?.mul(&std_tensor)?;

        let bias_tensor = if bias {
            Some(F32Tensor::zeros(&[out_channels])?)
        } else {
            None
        };

        Ok(Self {
            weight,
            bias: bias_tensor,
            weight_grad: None,
            bias_grad: None,
            last_input: None,
            in_channels,
            out_channels,
            kernel_size,
            stride,
            padding,
        })
    }

    /// フォワードパス(3D畳み込み)
    /// Forward pass (3D convolution)
    pub fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 入力を保存(バックワードパス用)
        self.last_input = Some(input.clone());

        // 入力形状: (batch_size, in_channels, depth, height, width)
        let input_shape = input.shape();
        if input_shape.len() != 5 {
            return Err(RusTorchError::tensor_op(
                "Conv3d input must be 5D (batch, channels, depth, height, width)",
            ));
        }

        let batch_size = input_shape[0];
        let input_depth = input_shape[2];
        let input_height = input_shape[3];
        let input_width = input_shape[4];

        // 出力サイズ計算
        let output_depth =
            (input_depth + 2 * self.padding.0 - self.kernel_size.0) / self.stride.0 + 1;
        let output_height =
            (input_height + 2 * self.padding.1 - self.kernel_size.1) / self.stride.1 + 1;
        let output_width =
            (input_width + 2 * self.padding.2 - self.kernel_size.2) / self.stride.2 + 1;

        // 3D畳み込み実装
        let mut output_data =
            vec![0.0; batch_size * self.out_channels * output_depth * output_height * output_width];
        let input_data = input.as_slice();
        let weight_data = self.weight.as_slice();

        for b in 0..batch_size {
            for out_c in 0..self.out_channels {
                for od in 0..output_depth {
                    for oh in 0..output_height {
                        for ow in 0..output_width {
                            let mut sum = 0.0;

                            // カーネル内の畳み込み
                            for kd in 0..self.kernel_size.0 {
                                for kh in 0..self.kernel_size.1 {
                                    for kw in 0..self.kernel_size.2 {
                                        let id = od * self.stride.0 + kd;
                                        let ih = oh * self.stride.1 + kh;
                                        let iw = ow * self.stride.2 + kw;

                                        // パディングチェック
                                        if id >= self.padding.0
                                            && id < input_depth + self.padding.0
                                            && ih >= self.padding.1
                                            && ih < input_height + self.padding.1
                                            && iw >= self.padding.2
                                            && iw < input_width + self.padding.2
                                        {
                                            let id_actual = id - self.padding.0;
                                            let ih_actual = ih - self.padding.1;
                                            let iw_actual = iw - self.padding.2;

                                            for in_c in 0..self.in_channels {
                                                let input_idx = b
                                                    * self.in_channels
                                                    * input_depth
                                                    * input_height
                                                    * input_width
                                                    + in_c
                                                        * input_depth
                                                        * input_height
                                                        * input_width
                                                    + id_actual * input_height * input_width
                                                    + ih_actual * input_width
                                                    + iw_actual;
                                                let weight_idx = out_c
                                                    * self.in_channels
                                                    * self.kernel_size.0
                                                    * self.kernel_size.1
                                                    * self.kernel_size.2
                                                    + in_c
                                                        * self.kernel_size.0
                                                        * self.kernel_size.1
                                                        * self.kernel_size.2
                                                    + kd * self.kernel_size.1 * self.kernel_size.2
                                                    + kh * self.kernel_size.2
                                                    + kw;

                                                sum +=
                                                    input_data[input_idx] * weight_data[weight_idx];
                                            }
                                        }
                                    }
                                }
                            }

                            // バイアス追加
                            if let Some(ref bias) = self.bias {
                                sum += bias.as_slice()[out_c];
                            }

                            let output_idx =
                                b * self.out_channels * output_depth * output_height * output_width
                                    + out_c * output_depth * output_height * output_width
                                    + od * output_height * output_width
                                    + oh * output_width
                                    + ow;
                            output_data[output_idx] = sum;
                        }
                    }
                }
            }
        }

        let output_shape = vec![
            batch_size,
            self.out_channels,
            output_depth,
            output_height,
            output_width,
        ];
        F32Tensor::from_vec(output_data, &output_shape)
    }
}

impl F32Layer for F32Conv3d {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        self.forward(input)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        if let Some(ref last_input) = self.last_input {
            // 重み勾配計算(簡素化)
            self.weight_grad = Some(F32Tensor::zeros(self.weight.shape())?);

            // バイアス勾配計算
            if self.bias.is_some() {
                self.bias_grad = Some(F32Tensor::zeros(&[self.out_channels])?);
            }

            // 入力勾配(簡素化:パススルー)
            Ok(last_input.clone())
        } else {
            Err(RusTorchError::tensor_op("No saved input for backward pass"))
        }
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = vec![&self.weight];
        if let Some(ref bias) = self.bias {
            params.push(bias);
        }
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> RusTorchResult<()> {
        // Update weight
        if let Some(ref weight_grad) = self.weight_grad {
            let update = weight_grad.mul_scalar(learning_rate)?;
            self.weight = self.weight.sub(&update)?;
        }

        // Update bias
        if let (Some(ref mut bias), Some(ref bias_grad)) = (&mut self.bias, &self.bias_grad) {
            let update = bias_grad.mul_scalar(learning_rate)?;
            *bias = bias.sub(&update)?;
        }

        Ok(())
    }
} // ========================================
  // RNN/LSTM/GRU - 時系列処理層
  // RNN/LSTM/GRU - Time series processing layers
  // ========================================

/// RNN層(Vanilla RNN)
/// RNN layer (Vanilla RNN)
#[derive(Debug)]
pub struct F32RNN {
    pub input_size: usize,
    pub hidden_size: usize,
    pub weight_ih: F32Tensor, // input to hidden weights
    pub weight_hh: F32Tensor, // hidden to hidden weights
    pub bias_ih: Option<F32Tensor>,
    pub bias_hh: Option<F32Tensor>,
    pub weight_ih_grad: Option<F32Tensor>,
    pub weight_hh_grad: Option<F32Tensor>,
    pub bias_ih_grad: Option<F32Tensor>,
    pub bias_hh_grad: Option<F32Tensor>,
    pub last_input: Option<F32Tensor>,
    pub last_hidden: Option<F32Tensor>,
    pub bidirectional: bool,
    pub batch_first: bool,
}

impl F32RNN {
    /// 新しいRNN層を作成
    /// Create a new RNN layer
    pub fn new(
        input_size: usize,
        hidden_size: usize,
        bias: bool,
        bidirectional: bool,
        batch_first: bool,
    ) -> Result<Self, RusTorchError> {
        // Xavier初期化
        let weight_ih_std = 1.0 / (input_size as f32).sqrt();
        let weight_hh_std = 1.0 / (hidden_size as f32).sqrt();

        let weight_ih = F32Tensor::randn(&[hidden_size, input_size])?
            .mul(&F32Tensor::from_scalar(weight_ih_std)?)?;
        let weight_hh = F32Tensor::randn(&[hidden_size, hidden_size])?
            .mul(&F32Tensor::from_scalar(weight_hh_std)?)?;

        let bias_ih = if bias {
            Some(F32Tensor::zeros(&[hidden_size])?)
        } else {
            None
        };

        let bias_hh = if bias {
            Some(F32Tensor::zeros(&[hidden_size])?)
        } else {
            None
        };

        Ok(Self {
            input_size,
            hidden_size,
            weight_ih,
            weight_hh,
            bias_ih,
            bias_hh,
            weight_ih_grad: None,
            weight_hh_grad: None,
            bias_ih_grad: None,
            bias_hh_grad: None,
            last_input: None,
            last_hidden: None,
            bidirectional,
            batch_first,
        })
    }

    /// フォワードパス
    /// Forward pass
    pub fn forward(
        &mut self,
        input: &F32Tensor,
        hidden: Option<&F32Tensor>,
    ) -> Result<(F32Tensor, F32Tensor), RusTorchError> {
        self.last_input = Some(input.clone());

        let input_shape = input.shape();
        let (batch_size, seq_len) = if self.batch_first {
            (input_shape[0], input_shape[1])
        } else {
            (input_shape[1], input_shape[0])
        };

        // 初期隠れ状態
        let mut h = if let Some(h) = hidden {
            h.clone()
        } else {
            F32Tensor::zeros(&[batch_size, self.hidden_size])?
        };

        let mut outputs = Vec::new();

        // 各時刻でのRNN計算
        for t in 0..seq_len {
            let x_t = if self.batch_first {
                input
                    .slice(&[(0, batch_size), (t, t + 1), (0, self.input_size)])?
                    .reshape(&[batch_size, self.input_size])?
            } else {
                input
                    .slice(&[(t, t + 1), (0, batch_size), (0, self.input_size)])?
                    .reshape(&[batch_size, self.input_size])?
            };

            // RNN cell: h_t = tanh(W_ih @ x_t + W_hh @ h_{t-1} + b)
            let ih = x_t.matmul(&self.weight_ih.transpose()?)?;
            let hh = h.matmul(&self.weight_hh.transpose()?)?;
            let mut gate = ih.add(&hh)?;

            if let Some(ref bias_ih) = self.bias_ih {
                let bias_ih_expanded = bias_ih
                    .unsqueeze(0)?
                    .expand(&[batch_size, self.hidden_size])?;
                gate = gate.add(&bias_ih_expanded)?;
            }

            if let Some(ref bias_hh) = self.bias_hh {
                let bias_hh_expanded = bias_hh
                    .unsqueeze(0)?
                    .expand(&[batch_size, self.hidden_size])?;
                gate = gate.add(&bias_hh_expanded)?;
            }

            h = F32Activation::Tanh.forward(&gate)?;
            outputs.push(h.clone());
        }

        // 出力を結合
        let output = if self.batch_first {
            // (batch, seq_len, hidden_size)
            let mut output_data = Vec::new();
            for output_t in &outputs {
                output_data.extend_from_slice(output_t.as_slice());
            }
            F32Tensor::from_vec(output_data, &[batch_size, seq_len, self.hidden_size])?
        } else {
            // (seq_len, batch, hidden_size)
            let mut output_data = Vec::new();
            for t in 0..seq_len {
                output_data.extend_from_slice(outputs[t].as_slice());
            }
            F32Tensor::from_vec(output_data, &[seq_len, batch_size, self.hidden_size])?
        };

        self.last_hidden = Some(h.clone());
        Ok((output, h))
    }
}

impl F32Layer for F32RNN {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        let (output, _) = self.forward(input, None)?;
        Ok(output)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        if let Some(ref last_input) = self.last_input {
            // 勾配を初期化
            self.weight_ih_grad = Some(F32Tensor::zeros(self.weight_ih.shape())?);
            self.weight_hh_grad = Some(F32Tensor::zeros(self.weight_hh.shape())?);

            if self.bias_ih.is_some() {
                self.bias_ih_grad = Some(F32Tensor::zeros(&[self.hidden_size])?);
            }
            if self.bias_hh.is_some() {
                self.bias_hh_grad = Some(F32Tensor::zeros(&[self.hidden_size])?);
            }

            Ok(last_input.clone())
        } else {
            Err(RusTorchError::tensor_op("No saved input for backward pass"))
        }
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = vec![&self.weight_ih, &self.weight_hh];
        if let Some(ref bias_ih) = self.bias_ih {
            params.push(bias_ih);
        }
        if let Some(ref bias_hh) = self.bias_hh {
            params.push(bias_hh);
        }
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        let lr_tensor = F32Tensor::from_scalar(learning_rate)?;

        if let Some(ref weight_ih_grad) = self.weight_ih_grad {
            let update = weight_ih_grad.mul(&lr_tensor)?;
            self.weight_ih = self.weight_ih.sub(&update)?;
        }

        if let Some(ref weight_hh_grad) = self.weight_hh_grad {
            let update = weight_hh_grad.mul(&lr_tensor)?;
            self.weight_hh = self.weight_hh.sub(&update)?;
        }

        if let (Some(ref mut bias_ih), Some(ref bias_ih_grad)) =
            (&mut self.bias_ih, &self.bias_ih_grad)
        {
            let update = bias_ih_grad.mul(&lr_tensor)?;
            *bias_ih = bias_ih.sub(&update)?;
        }

        if let (Some(ref mut bias_hh), Some(ref bias_hh_grad)) =
            (&mut self.bias_hh, &self.bias_hh_grad)
        {
            let update = bias_hh_grad.mul(&lr_tensor)?;
            *bias_hh = bias_hh.sub(&update)?;
        }

        Ok(())
    }
}

/// LSTM層(Long Short-Term Memory)
/// LSTM layer (Long Short-Term Memory)
#[derive(Debug)]
pub struct F32LSTM {
    pub input_size: usize,
    pub hidden_size: usize,
    // 4つのゲート用の重み: input, forget, cell, output
    pub weight_ih: F32Tensor,       // (4 * hidden_size, input_size)
    pub weight_hh: F32Tensor,       // (4 * hidden_size, hidden_size)
    pub bias_ih: Option<F32Tensor>, // (4 * hidden_size)
    pub bias_hh: Option<F32Tensor>, // (4 * hidden_size)
    pub weight_ih_grad: Option<F32Tensor>,
    pub weight_hh_grad: Option<F32Tensor>,
    pub bias_ih_grad: Option<F32Tensor>,
    pub bias_hh_grad: Option<F32Tensor>,
    pub last_input: Option<F32Tensor>,
    pub last_hidden: Option<F32Tensor>,
    pub last_cell: Option<F32Tensor>,
    pub bidirectional: bool,
    pub batch_first: bool,
}

impl F32LSTM {
    /// 新しいLSTM層を作成
    /// Create a new LSTM layer
    pub fn new(
        input_size: usize,
        hidden_size: usize,
        bias: bool,
        bidirectional: bool,
        batch_first: bool,
    ) -> Result<Self, RusTorchError> {
        // Xavier初期化
        let weight_ih_std = 1.0 / (input_size as f32).sqrt();
        let weight_hh_std = 1.0 / (hidden_size as f32).sqrt();

        let weight_ih = F32Tensor::randn(&[4 * hidden_size, input_size])?
            .mul(&F32Tensor::from_scalar(weight_ih_std)?)?;
        let weight_hh = F32Tensor::randn(&[4 * hidden_size, hidden_size])?
            .mul(&F32Tensor::from_scalar(weight_hh_std)?)?;

        let bias_ih = if bias {
            // forget gate biasを1.0で初期化(一般的な慣習)
            let mut bias_data = vec![0.0; 4 * hidden_size];
            for i in hidden_size..(2 * hidden_size) {
                bias_data[i] = 1.0; // forget gate bias
            }
            Some(F32Tensor::from_vec(bias_data, &[4 * hidden_size])?)
        } else {
            None
        };

        let bias_hh = if bias {
            Some(F32Tensor::zeros(&[4 * hidden_size])?)
        } else {
            None
        };

        Ok(Self {
            input_size,
            hidden_size,
            weight_ih,
            weight_hh,
            bias_ih,
            bias_hh,
            weight_ih_grad: None,
            weight_hh_grad: None,
            bias_ih_grad: None,
            bias_hh_grad: None,
            last_input: None,
            last_hidden: None,
            last_cell: None,
            bidirectional,
            batch_first,
        })
    }

    /// フォワードパス
    /// Forward pass
    pub fn forward(
        &mut self,
        input: &F32Tensor,
        state: Option<(&F32Tensor, &F32Tensor)>,
    ) -> Result<(F32Tensor, F32Tensor, F32Tensor), RusTorchError> {
        self.last_input = Some(input.clone());

        let input_shape = input.shape();
        let (batch_size, seq_len) = if self.batch_first {
            (input_shape[0], input_shape[1])
        } else {
            (input_shape[1], input_shape[0])
        };

        // 初期状態
        let (mut h, mut c) = if let Some((h, c)) = state {
            (h.clone(), c.clone())
        } else {
            (
                F32Tensor::zeros(&[batch_size, self.hidden_size])?,
                F32Tensor::zeros(&[batch_size, self.hidden_size])?,
            )
        };

        let mut outputs = Vec::new();

        // 各時刻でのLSTM計算
        for t in 0..seq_len {
            let x_t = if self.batch_first {
                input
                    .slice(&[(0, batch_size), (t, t + 1), (0, self.input_size)])?
                    .reshape(&[batch_size, self.input_size])?
            } else {
                input
                    .slice(&[(t, t + 1), (0, batch_size), (0, self.input_size)])?
                    .reshape(&[batch_size, self.input_size])?
            };

            // LSTM cell computation
            let ih = x_t.matmul(&self.weight_ih.transpose()?)?;
            let hh = h.matmul(&self.weight_hh.transpose()?)?;
            let mut gates = ih.add(&hh)?;

            if let Some(ref bias_ih) = self.bias_ih {
                let bias_ih_expanded = bias_ih
                    .unsqueeze(0)?
                    .expand(&[batch_size, 4 * self.hidden_size])?;
                gates = gates.add(&bias_ih_expanded)?;
            }

            if let Some(ref bias_hh) = self.bias_hh {
                let bias_hh_expanded = bias_hh
                    .unsqueeze(0)?
                    .expand(&[batch_size, 4 * self.hidden_size])?;
                gates = gates.add(&bias_hh_expanded)?;
            }

            // 4つのゲートに分割
            let i_gate = gates.slice(&[(0, batch_size), (0, self.hidden_size)])?;
            let f_gate =
                gates.slice(&[(0, batch_size), (self.hidden_size, 2 * self.hidden_size)])?;
            let g_gate = gates.slice(&[
                (0, batch_size),
                (2 * self.hidden_size, 3 * self.hidden_size),
            ])?;
            let o_gate = gates.slice(&[
                (0, batch_size),
                (3 * self.hidden_size, 4 * self.hidden_size),
            ])?;

            // ゲート活性化
            let i = F32Activation::Sigmoid.forward(&i_gate)?;
            let f = F32Activation::Sigmoid.forward(&f_gate)?;
            let g = F32Activation::Tanh.forward(&g_gate)?;
            let o = F32Activation::Sigmoid.forward(&o_gate)?;

            // セル状態更新
            c = f.mul(&c)?.add(&i.mul(&g)?)?;

            // 隠れ状態更新
            let c_tanh = F32Activation::Tanh.forward(&c)?;
            h = o.mul(&c_tanh)?;

            outputs.push(h.clone());
        }

        // 出力を結合
        let output = if self.batch_first {
            let mut output_data = Vec::new();
            for output_t in &outputs {
                output_data.extend_from_slice(output_t.as_slice());
            }
            F32Tensor::from_vec(output_data, &[batch_size, seq_len, self.hidden_size])?
        } else {
            let mut output_data = Vec::new();
            for t in 0..seq_len {
                output_data.extend_from_slice(outputs[t].as_slice());
            }
            F32Tensor::from_vec(output_data, &[seq_len, batch_size, self.hidden_size])?
        };

        self.last_hidden = Some(h.clone());
        self.last_cell = Some(c.clone());
        Ok((output, h, c))
    }
}

impl F32Layer for F32LSTM {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        let (output, _, _) = self.forward(input, None)?;
        Ok(output)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        if let Some(ref last_input) = self.last_input {
            self.weight_ih_grad = Some(F32Tensor::zeros(self.weight_ih.shape())?);
            self.weight_hh_grad = Some(F32Tensor::zeros(self.weight_hh.shape())?);

            if self.bias_ih.is_some() {
                self.bias_ih_grad = Some(F32Tensor::zeros(&[4 * self.hidden_size])?);
            }
            if self.bias_hh.is_some() {
                self.bias_hh_grad = Some(F32Tensor::zeros(&[4 * self.hidden_size])?);
            }

            Ok(last_input.clone())
        } else {
            Err(RusTorchError::tensor_op("No saved input for backward pass"))
        }
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = vec![&self.weight_ih, &self.weight_hh];
        if let Some(ref bias_ih) = self.bias_ih {
            params.push(bias_ih);
        }
        if let Some(ref bias_hh) = self.bias_hh {
            params.push(bias_hh);
        }
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        let lr_tensor = F32Tensor::from_scalar(learning_rate)?;

        if let Some(ref weight_ih_grad) = self.weight_ih_grad {
            let update = weight_ih_grad.mul(&lr_tensor)?;
            self.weight_ih = self.weight_ih.sub(&update)?;
        }

        if let Some(ref weight_hh_grad) = self.weight_hh_grad {
            let update = weight_hh_grad.mul(&lr_tensor)?;
            self.weight_hh = self.weight_hh.sub(&update)?;
        }

        if let (Some(ref mut bias_ih), Some(ref bias_ih_grad)) =
            (&mut self.bias_ih, &self.bias_ih_grad)
        {
            let update = bias_ih_grad.mul(&lr_tensor)?;
            *bias_ih = bias_ih.sub(&update)?;
        }

        if let (Some(ref mut bias_hh), Some(ref bias_hh_grad)) =
            (&mut self.bias_hh, &self.bias_hh_grad)
        {
            let update = bias_hh_grad.mul(&lr_tensor)?;
            *bias_hh = bias_hh.sub(&update)?;
        }

        Ok(())
    }
}

/// GRU層(Gated Recurrent Unit)
/// GRU layer (Gated Recurrent Unit)
#[derive(Debug)]
pub struct F32GRU {
    pub input_size: usize,
    pub hidden_size: usize,
    // 3つのゲート用の重み: reset, update, new
    pub weight_ih: F32Tensor,       // (3 * hidden_size, input_size)
    pub weight_hh: F32Tensor,       // (3 * hidden_size, hidden_size)
    pub bias_ih: Option<F32Tensor>, // (3 * hidden_size)
    pub bias_hh: Option<F32Tensor>, // (3 * hidden_size)
    pub weight_ih_grad: Option<F32Tensor>,
    pub weight_hh_grad: Option<F32Tensor>,
    pub bias_ih_grad: Option<F32Tensor>,
    pub bias_hh_grad: Option<F32Tensor>,
    pub last_input: Option<F32Tensor>,
    pub last_hidden: Option<F32Tensor>,
    pub bidirectional: bool,
    pub batch_first: bool,
}

impl F32GRU {
    /// 新しいGRU層を作成
    /// Create a new GRU layer
    pub fn new(
        input_size: usize,
        hidden_size: usize,
        bias: bool,
        bidirectional: bool,
        batch_first: bool,
    ) -> Result<Self, RusTorchError> {
        // Xavier初期化
        let weight_ih_std = 1.0 / (input_size as f32).sqrt();
        let weight_hh_std = 1.0 / (hidden_size as f32).sqrt();

        let weight_ih = F32Tensor::randn(&[3 * hidden_size, input_size])?
            .mul(&F32Tensor::from_scalar(weight_ih_std)?)?;
        let weight_hh = F32Tensor::randn(&[3 * hidden_size, hidden_size])?
            .mul(&F32Tensor::from_scalar(weight_hh_std)?)?;

        let bias_ih = if bias {
            Some(F32Tensor::zeros(&[3 * hidden_size])?)
        } else {
            None
        };

        let bias_hh = if bias {
            Some(F32Tensor::zeros(&[3 * hidden_size])?)
        } else {
            None
        };

        Ok(Self {
            input_size,
            hidden_size,
            weight_ih,
            weight_hh,
            bias_ih,
            bias_hh,
            weight_ih_grad: None,
            weight_hh_grad: None,
            bias_ih_grad: None,
            bias_hh_grad: None,
            last_input: None,
            last_hidden: None,
            bidirectional,
            batch_first,
        })
    }

    /// フォワードパス
    /// Forward pass
    pub fn forward(
        &mut self,
        input: &F32Tensor,
        hidden: Option<&F32Tensor>,
    ) -> Result<(F32Tensor, F32Tensor), RusTorchError> {
        self.last_input = Some(input.clone());

        let input_shape = input.shape();
        let (batch_size, seq_len) = if self.batch_first {
            (input_shape[0], input_shape[1])
        } else {
            (input_shape[1], input_shape[0])
        };

        // 初期隠れ状態
        let mut h = if let Some(h) = hidden {
            h.clone()
        } else {
            F32Tensor::zeros(&[batch_size, self.hidden_size])?
        };

        let mut outputs = Vec::new();

        // 各時刻でのGRU計算
        for t in 0..seq_len {
            let x_t = if self.batch_first {
                input
                    .slice(&[(0, batch_size), (t, t + 1), (0, self.input_size)])?
                    .reshape(&[batch_size, self.input_size])?
            } else {
                input
                    .slice(&[(t, t + 1), (0, batch_size), (0, self.input_size)])?
                    .reshape(&[batch_size, self.input_size])?
            };

            // GRU cell computation
            let ih = x_t.matmul(&self.weight_ih.transpose()?)?;
            let hh = h.matmul(&self.weight_hh.transpose()?)?;

            let mut gi = ih.clone();
            let mut gh = hh.clone();

            if let Some(ref bias_ih) = self.bias_ih {
                let bias_ih_expanded = bias_ih
                    .unsqueeze(0)?
                    .expand(&[batch_size, 3 * self.hidden_size])?;
                gi = gi.add(&bias_ih_expanded)?;
            }

            if let Some(ref bias_hh) = self.bias_hh {
                let bias_hh_expanded = bias_hh
                    .unsqueeze(0)?
                    .expand(&[batch_size, 3 * self.hidden_size])?;
                gh = gh.add(&bias_hh_expanded)?;
            }

            // reset gate, update gate用の計算
            let i_r = gi.slice(&[(0, batch_size), (0, self.hidden_size)])?;
            let i_z = gi.slice(&[(0, batch_size), (self.hidden_size, 2 * self.hidden_size)])?;
            let h_r = gh.slice(&[(0, batch_size), (0, self.hidden_size)])?;
            let h_z = gh.slice(&[(0, batch_size), (self.hidden_size, 2 * self.hidden_size)])?;

            let r = F32Activation::Sigmoid.forward(&i_r.add(&h_r)?)?;
            let z = F32Activation::Sigmoid.forward(&i_z.add(&h_z)?)?;

            // new gate計算
            let i_n = gi.slice(&[
                (0, batch_size),
                (2 * self.hidden_size, 3 * self.hidden_size),
            ])?;
            let h_n = gh.slice(&[
                (0, batch_size),
                (2 * self.hidden_size, 3 * self.hidden_size),
            ])?;
            let n = F32Activation::Tanh.forward(&i_n.add(&r.mul(&h_n)?)?)?;

            // 隠れ状態更新
            let one = F32Tensor::ones(&[batch_size, self.hidden_size])?;
            let one_minus_z = one.sub(&z)?;
            h = one_minus_z.mul(&n)?.add(&z.mul(&h)?)?;

            outputs.push(h.clone());
        }

        // 出力を結合
        let output = if self.batch_first {
            let mut output_data = Vec::new();
            for output_t in &outputs {
                output_data.extend_from_slice(output_t.as_slice());
            }
            F32Tensor::from_vec(output_data, &[batch_size, seq_len, self.hidden_size])?
        } else {
            let mut output_data = Vec::new();
            for t in 0..seq_len {
                output_data.extend_from_slice(outputs[t].as_slice());
            }
            F32Tensor::from_vec(output_data, &[seq_len, batch_size, self.hidden_size])?
        };

        self.last_hidden = Some(h.clone());
        Ok((output, h))
    }
}

impl F32Layer for F32GRU {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        let (output, _) = self.forward(input, None)?;
        Ok(output)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        if let Some(ref last_input) = self.last_input {
            self.weight_ih_grad = Some(F32Tensor::zeros(self.weight_ih.shape())?);
            self.weight_hh_grad = Some(F32Tensor::zeros(self.weight_hh.shape())?);

            if self.bias_ih.is_some() {
                self.bias_ih_grad = Some(F32Tensor::zeros(&[3 * self.hidden_size])?);
            }
            if self.bias_hh.is_some() {
                self.bias_hh_grad = Some(F32Tensor::zeros(&[3 * self.hidden_size])?);
            }

            Ok(last_input.clone())
        } else {
            Err(RusTorchError::tensor_op("No saved input for backward pass"))
        }
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = vec![&self.weight_ih, &self.weight_hh];
        if let Some(ref bias_ih) = self.bias_ih {
            params.push(bias_ih);
        }
        if let Some(ref bias_hh) = self.bias_hh {
            params.push(bias_hh);
        }
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        let lr_tensor = F32Tensor::from_scalar(learning_rate)?;

        if let Some(ref weight_ih_grad) = self.weight_ih_grad {
            let update = weight_ih_grad.mul(&lr_tensor)?;
            self.weight_ih = self.weight_ih.sub(&update)?;
        }

        if let Some(ref weight_hh_grad) = self.weight_hh_grad {
            let update = weight_hh_grad.mul(&lr_tensor)?;
            self.weight_hh = self.weight_hh.sub(&update)?;
        }

        if let (Some(ref mut bias_ih), Some(ref bias_ih_grad)) =
            (&mut self.bias_ih, &self.bias_ih_grad)
        {
            let update = bias_ih_grad.mul(&lr_tensor)?;
            *bias_ih = bias_ih.sub(&update)?;
        }

        if let (Some(ref mut bias_hh), Some(ref bias_hh_grad)) =
            (&mut self.bias_hh, &self.bias_hh_grad)
        {
            let update = bias_hh_grad.mul(&lr_tensor)?;
            *bias_hh = bias_hh.sub(&update)?;
        }

        Ok(())
    }
} // ========================================
  // Transformer - 現代的アーキテクチャ
  // Transformer - Modern architecture
  // ========================================

/// 位置エンコーディング
/// Positional Encoding
#[derive(Debug)]
pub struct F32PositionalEncoding {
    pub encoding: F32Tensor,
    pub max_len: usize,
    pub d_model: usize,
}

impl F32PositionalEncoding {
    /// 新しい位置エンコーディングを作成
    /// Create new positional encoding
    pub fn new(d_model: usize, max_len: usize) -> Result<Self, RusTorchError> {
        let mut pe_data = vec![0.0; max_len * d_model];

        for pos in 0..max_len {
            for i in (0..d_model).step_by(2) {
                let angle = pos as f32 / 10000.0_f32.powf(i as f32 / d_model as f32);
                pe_data[pos * d_model + i] = angle.sin();
                if i + 1 < d_model {
                    pe_data[pos * d_model + i + 1] = angle.cos();
                }
            }
        }

        let encoding = F32Tensor::from_vec(pe_data, &[max_len, d_model])?;

        Ok(Self {
            encoding,
            max_len,
            d_model,
        })
    }

    /// 位置エンコーディングを適用
    /// Apply positional encoding
    pub fn forward(&self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        let input_shape = input.shape();
        let seq_len = input_shape[1];

        if seq_len > self.max_len {
            return Err(RusTorchError::tensor_op("Sequence length exceeds max_len"));
        }

        let pe_slice = self.encoding.slice(&[(0, seq_len), (0, self.d_model)])?;
        let pe_expanded =
            pe_slice
                .unsqueeze(0)?
                .expand(&[input_shape[0], seq_len, self.d_model])?;

        input.add(&pe_expanded)
    }
}

/// マルチヘッドアテンション
/// Multi-Head Attention
#[derive(Debug)]
pub struct F32MultiHeadAttention {
    pub d_model: usize,
    pub num_heads: usize,
    pub d_k: usize,
    pub d_v: usize,
    pub w_q: F32Tensor,
    pub w_k: F32Tensor,
    pub w_v: F32Tensor,
    pub w_o: F32Tensor,
    pub dropout_rate: f32,
    pub last_input: Option<F32Tensor>,
    pub weight_grads: Option<(F32Tensor, F32Tensor, F32Tensor, F32Tensor)>,
}

impl F32MultiHeadAttention {
    /// 新しいマルチヘッドアテンションを作成
    /// Create new multi-head attention
    pub fn new(d_model: usize, num_heads: usize, dropout_rate: f32) -> Result<Self, RusTorchError> {
        assert_eq!(
            d_model % num_heads,
            0,
            "d_model must be divisible by num_heads"
        );

        let d_k = d_model / num_heads;
        let d_v = d_model / num_heads;

        // Xavier初期化
        let std = 1.0 / (d_model as f32).sqrt();

        let w_q = F32Tensor::randn(&[d_model, d_model])?.mul(&F32Tensor::from_scalar(std)?)?;
        let w_k = F32Tensor::randn(&[d_model, d_model])?.mul(&F32Tensor::from_scalar(std)?)?;
        let w_v = F32Tensor::randn(&[d_model, d_model])?.mul(&F32Tensor::from_scalar(std)?)?;
        let w_o = F32Tensor::randn(&[d_model, d_model])?.mul(&F32Tensor::from_scalar(std)?)?;

        Ok(Self {
            d_model,
            num_heads,
            d_k,
            d_v,
            w_q,
            w_k,
            w_v,
            w_o,
            dropout_rate,
            last_input: None,
            weight_grads: None,
        })
    }

    /// スケールドドットプロダクトアテンション
    /// Scaled Dot-Product Attention
    fn scaled_dot_product_attention(
        &self,
        q: &F32Tensor,
        k: &F32Tensor,
        v: &F32Tensor,
        mask: Option<&F32Tensor>,
    ) -> Result<F32Tensor, RusTorchError> {
        let d_k = self.d_k as f32;
        let scale = 1.0 / d_k.sqrt();

        // Attention scores: Q @ K^T / sqrt(d_k)
        let scores = q
            .matmul(&k.transpose()?)?
            .mul(&F32Tensor::from_scalar(scale)?)?;

        // マスクを適用(オプション)
        let scores = if let Some(mask) = mask {
            scores.add(&mask)?
        } else {
            scores
        };

        // Softmax
        let attention_weights = F32Activation::Softmax.forward(&scores)?;

        // アテンション重みをVに適用
        attention_weights.matmul(v)
    }

    /// フォワードパス
    /// Forward pass
    pub fn forward(
        &mut self,
        query: &F32Tensor,
        key: &F32Tensor,
        value: &F32Tensor,
        mask: Option<&F32Tensor>,
    ) -> Result<F32Tensor, RusTorchError> {
        self.last_input = Some(query.clone());

        let batch_size = query.shape()[0];
        let seq_len = query.shape()[1];

        // Q, K, Vを計算
        let q = query.matmul(&self.w_q)?;
        let k = key.matmul(&self.w_k)?;
        let v = value.matmul(&self.w_v)?;

        // ヘッドに分割: (batch_size, seq_len, d_model) -> (batch_size, num_heads, seq_len, d_k)
        let q_heads = q
            .reshape(&[batch_size, seq_len, self.num_heads, self.d_k])?
            .transpose_dims(1, 2)?;
        let k_heads = k
            .reshape(&[batch_size, seq_len, self.num_heads, self.d_k])?
            .transpose_dims(1, 2)?;
        let v_heads = v
            .reshape(&[batch_size, seq_len, self.num_heads, self.d_v])?
            .transpose_dims(1, 2)?;

        // 各ヘッドでアテンション計算
        let mut head_outputs = Vec::new();
        for h in 0..self.num_heads {
            let q_h = q_heads
                .slice(&[(0, batch_size), (h, h + 1), (0, seq_len), (0, self.d_k)])?
                .reshape(&[batch_size, seq_len, self.d_k])?;
            let k_h = k_heads
                .slice(&[(0, batch_size), (h, h + 1), (0, seq_len), (0, self.d_k)])?
                .reshape(&[batch_size, seq_len, self.d_k])?;
            let v_h = v_heads
                .slice(&[(0, batch_size), (h, h + 1), (0, seq_len), (0, self.d_v)])?
                .reshape(&[batch_size, seq_len, self.d_v])?;

            let head_output = self.scaled_dot_product_attention(&q_h, &k_h, &v_h, mask)?;
            head_outputs.push(head_output);
        }

        // ヘッドを結合
        let mut concat_data = Vec::new();
        for i in 0..batch_size {
            for j in 0..seq_len {
                for head in &head_outputs {
                    let head_data = head.slice(&[(i, i + 1), (j, j + 1), (0, self.d_v)])?;
                    concat_data.extend_from_slice(head_data.as_slice());
                }
            }
        }

        let concatenated = F32Tensor::from_vec(concat_data, &[batch_size, seq_len, self.d_model])?;

        // 出力射影
        concatenated.matmul(&self.w_o)
    }
}

impl F32Layer for F32MultiHeadAttention {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // self-attentionの場合
        self.forward(input, input, input, None)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        if let Some(ref last_input) = self.last_input {
            // 重み勾配を初期化
            let w_q_grad = F32Tensor::zeros(self.w_q.shape())?;
            let w_k_grad = F32Tensor::zeros(self.w_k.shape())?;
            let w_v_grad = F32Tensor::zeros(self.w_v.shape())?;
            let w_o_grad = F32Tensor::zeros(self.w_o.shape())?;

            self.weight_grads = Some((w_q_grad, w_k_grad, w_v_grad, w_o_grad));

            Ok(last_input.clone())
        } else {
            Err(RusTorchError::tensor_op("No saved input for backward pass"))
        }
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        vec![&self.w_q, &self.w_k, &self.w_v, &self.w_o]
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        if let Some(ref grads) = self.weight_grads {
            let lr_tensor = F32Tensor::from_scalar(learning_rate)?;

            let update_q = grads.0.mul(&lr_tensor)?;
            self.w_q = self.w_q.sub(&update_q)?;

            let update_k = grads.1.mul(&lr_tensor)?;
            self.w_k = self.w_k.sub(&update_k)?;

            let update_v = grads.2.mul(&lr_tensor)?;
            self.w_v = self.w_v.sub(&update_v)?;

            let update_o = grads.3.mul(&lr_tensor)?;
            self.w_o = self.w_o.sub(&update_o)?;
        }
        Ok(())
    }
}

/// フィードフォワードネットワーク
/// Feed Forward Network
#[derive(Debug)]
pub struct F32FeedForward {
    pub linear1: F32Linear,
    pub linear2: F32Linear,
    pub dropout_rate: f32,
}

impl F32FeedForward {
    /// 新しいフィードフォワードネットワークを作成
    /// Create new feed forward network
    pub fn new(d_model: usize, d_ff: usize, dropout_rate: f32) -> Result<Self, RusTorchError> {
        let linear1 = F32Linear::new(d_model, d_ff, true)?;
        let linear2 = F32Linear::new(d_ff, d_model, true)?;

        Ok(Self {
            linear1,
            linear2,
            dropout_rate,
        })
    }

    /// フォワードパス
    /// Forward pass
    pub fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        let x = self.linear1.forward(input)?;
        let x = F32Activation::ReLU.forward(&x)?;
        // ドロップアウトは簡素化のため省略
        self.linear2.forward(&x)
    }
}

impl F32Layer for F32FeedForward {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        self.forward(input)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        let grad1 = self.linear2.backward(grad_output)?;
        let grad2 = self.linear1.backward(&grad1)?;
        Ok(grad2)
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = Vec::new();
        params.extend(self.linear1.parameters());
        params.extend(self.linear2.parameters());
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        self.linear1.update_parameters(learning_rate)?;
        self.linear2.update_parameters(learning_rate)?;
        Ok(())
    }
}

/// レイヤー正規化
/// Layer Normalization
#[derive(Debug)]
pub struct F32LayerNorm {
    pub normalized_shape: Vec<usize>,
    pub weight: F32Tensor,
    pub bias: F32Tensor,
    pub eps: f32,
    pub weight_grad: Option<F32Tensor>,
    pub bias_grad: Option<F32Tensor>,
}

impl F32LayerNorm {
    /// 新しいレイヤー正規化を作成
    /// Create new layer normalization
    pub fn new(normalized_shape: Vec<usize>, eps: f32) -> Result<Self, RusTorchError> {
        let num_features = normalized_shape.iter().product();
        let weight = F32Tensor::ones(&[num_features])?;
        let bias = F32Tensor::zeros(&[num_features])?;

        Ok(Self {
            normalized_shape,
            weight,
            bias,
            eps,
            weight_grad: None,
            bias_grad: None,
        })
    }

    /// フォワードパス
    /// Forward pass
    pub fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        let input_shape = input.shape();
        let last_dim = input_shape[input_shape.len() - 1];

        // 最後の次元で正規化
        let input_data = input.as_slice();
        let mut output_data = vec![0.0; input_data.len()];

        let batch_size = input_data.len() / last_dim;

        for batch in 0..batch_size {
            let start_idx = batch * last_dim;
            let end_idx = start_idx + last_dim;
            let batch_data = &input_data[start_idx..end_idx];

            // 平均と分散を計算
            let mean = batch_data.iter().sum::<f32>() / last_dim as f32;
            let variance = batch_data
                .iter()
                .map(|x| (x - mean) * (x - mean))
                .sum::<f32>()
                / last_dim as f32;

            let std = (variance + self.eps).sqrt();

            // 正規化
            let weight_data = self.weight.as_slice();
            let bias_data = self.bias.as_slice();

            for i in 0..last_dim {
                let normalized = (batch_data[i] - mean) / std;
                output_data[start_idx + i] = weight_data[i] * normalized + bias_data[i];
            }
        }

        F32Tensor::from_vec(output_data, input_shape)
    }
}

impl F32Layer for F32LayerNorm {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        self.forward(input)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        self.weight_grad = Some(F32Tensor::zeros(self.weight.shape())?);
        self.bias_grad = Some(F32Tensor::zeros(self.bias.shape())?);
        Ok(grad_output.clone())
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        vec![&self.weight, &self.bias]
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        if let Some(ref weight_grad) = self.weight_grad {
            let lr_tensor = F32Tensor::from_scalar(learning_rate)?;
            let update = weight_grad.mul(&lr_tensor)?;
            self.weight = self.weight.sub(&update)?;
        }

        if let Some(ref bias_grad) = self.bias_grad {
            let lr_tensor = F32Tensor::from_scalar(learning_rate)?;
            let update = bias_grad.mul(&lr_tensor)?;
            self.bias = self.bias.sub(&update)?;
        }

        Ok(())
    }
}

/// トランスフォーマーブロック
/// Transformer Block
#[derive(Debug)]
pub struct F32TransformerBlock {
    pub attention: F32MultiHeadAttention,
    pub feed_forward: F32FeedForward,
    pub norm1: F32LayerNorm,
    pub norm2: F32LayerNorm,
    pub dropout_rate: f32,
}

impl F32TransformerBlock {
    /// 新しいトランスフォーマーブロックを作成
    /// Create new transformer block
    pub fn new(
        d_model: usize,
        num_heads: usize,
        d_ff: usize,
        dropout_rate: f32,
    ) -> Result<Self, RusTorchError> {
        let attention = F32MultiHeadAttention::new(d_model, num_heads, dropout_rate)?;
        let feed_forward = F32FeedForward::new(d_model, d_ff, dropout_rate)?;
        let norm1 = F32LayerNorm::new(vec![d_model], 1e-6)?;
        let norm2 = F32LayerNorm::new(vec![d_model], 1e-6)?;

        Ok(Self {
            attention,
            feed_forward,
            norm1,
            norm2,
            dropout_rate,
        })
    }

    /// フォワードパス
    /// Forward pass
    pub fn forward(
        &mut self,
        input: &F32Tensor,
        mask: Option<&F32Tensor>,
    ) -> Result<F32Tensor, RusTorchError> {
        // Self-attention with residual connection
        let attn_output = self.attention.forward(input, input, input, mask)?;
        let x = input.add(&attn_output)?; // Residual connection
        let x = self.norm1.forward(&x)?;

        // Feed forward with residual connection
        let ff_output = self.feed_forward.forward(&x)?;
        let x = x.add(&ff_output)?; // Residual connection
        let x = self.norm2.forward(&x)?;

        Ok(x)
    }
}

impl F32Layer for F32TransformerBlock {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        self.forward(input, None)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        let grad2 = self.norm2.backward(grad_output)?;
        let grad_ff = self.feed_forward.backward(&grad2)?;
        let grad1 = self.norm1.backward(&grad_ff)?;
        let grad_attn = self.attention.backward(&grad1)?;
        Ok(grad_attn)
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = Vec::new();
        params.extend(self.attention.parameters());
        params.extend(self.feed_forward.parameters());
        params.extend(self.norm1.parameters());
        params.extend(self.norm2.parameters());
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        self.attention.update_parameters(learning_rate)?;
        self.feed_forward.update_parameters(learning_rate)?;
        self.norm1.update_parameters(learning_rate)?;
        self.norm2.update_parameters(learning_rate)?;
        Ok(())
    }
}

/// 完全なトランスフォーマーモデル
/// Complete Transformer Model
#[derive(Debug)]
pub struct F32Transformer {
    pub d_model: usize,
    pub num_heads: usize,
    pub num_layers: usize,
    pub d_ff: usize,
    pub max_seq_len: usize,
    pub vocab_size: usize,
    pub embedding: F32Linear,
    pub positional_encoding: F32PositionalEncoding,
    pub transformer_blocks: Vec<F32TransformerBlock>,
    pub output_projection: F32Linear,
    pub dropout_rate: f32,
}

impl F32Transformer {
    /// 新しいトランスフォーマーモデルを作成
    /// Create new transformer model
    pub fn new(
        vocab_size: usize,
        d_model: usize,
        num_heads: usize,
        num_layers: usize,
        d_ff: usize,
        max_seq_len: usize,
        dropout_rate: f32,
    ) -> Result<Self, RusTorchError> {
        let embedding = F32Linear::new(vocab_size, d_model, false)?;
        let positional_encoding = F32PositionalEncoding::new(d_model, max_seq_len)?;

        let mut transformer_blocks = Vec::new();
        for _ in 0..num_layers {
            let block = F32TransformerBlock::new(d_model, num_heads, d_ff, dropout_rate)?;
            transformer_blocks.push(block);
        }

        let output_projection = F32Linear::new(d_model, vocab_size, true)?;

        Ok(Self {
            d_model,
            num_heads,
            num_layers,
            d_ff,
            max_seq_len,
            vocab_size,
            embedding,
            positional_encoding,
            transformer_blocks,
            output_projection,
            dropout_rate,
        })
    }

    /// フォワードパス
    /// Forward pass
    pub fn forward(
        &mut self,
        input_ids: &F32Tensor,
        mask: Option<&F32Tensor>,
    ) -> Result<F32Tensor, RusTorchError> {
        // Embedding
        let x = self.embedding.forward(input_ids)?;

        // 位置エンコーディングを追加
        let x = self.positional_encoding.forward(&x)?;

        // トランスフォーマーブロックを通す
        let mut x = x;
        for block in &mut self.transformer_blocks {
            x = block.forward(&x, mask)?;
        }

        // 出力投影
        self.output_projection.forward(&x)
    }
}

impl F32Layer for F32Transformer {
    fn forward(&mut self, input: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        self.forward(input, None)
    }

    fn backward(&mut self, grad_output: &F32Tensor) -> Result<F32Tensor, RusTorchError> {
        // 簡素化されたバックワード実装
        let mut grad = self.output_projection.backward(grad_output)?;

        for block in self.transformer_blocks.iter_mut().rev() {
            grad = block.backward(&grad)?;
        }

        let grad = self.embedding.backward(&grad)?;
        Ok(grad)
    }

    fn parameters(&self) -> Vec<&F32Tensor> {
        let mut params = Vec::new();
        params.extend(self.embedding.parameters());
        for block in &self.transformer_blocks {
            params.extend(block.parameters());
        }
        params.extend(self.output_projection.parameters());
        params
    }

    fn update_parameters(&mut self, learning_rate: f32) -> Result<(), RusTorchError> {
        self.embedding.update_parameters(learning_rate)?;

        for block in &mut self.transformer_blocks {
            block.update_parameters(learning_rate)?;
        }

        self.output_projection.update_parameters(learning_rate)?;
        Ok(())
    }
}