基于类脑神经元的推理系统探索

Abstract

This study constructs a symbolic reasoning model based on a recurrent spiking neural network (RSNN) designed to learn transitive inference rules (e.g., A→B, B→C ⇒ A→C). The model employs learnable entity embeddings (8‑dimensional) to map symbolic entities into a continuous semantic space and uses a genetic algorithm (GA) to optimise network weights and embeddings. Experimental results show that the model not only correctly classifies entity chains seen during training but also generalises to unseen entities (e.g., “tiger”, “wolf”) based on their semantic features, demonstrating semantic generalisation. From a cognitive neuroscience perspective, we analyse the model’s correspondence with biological mechanisms of logical reasoning and discuss its potential as a neural‑symbolic system.

1. Introduction

The human brain efficiently performs symbolic reasoning. For example, from “All humans are mortal” and “Socrates is human”, we deduce “Socrates is mortal”. This ability is thought to depend on the coordinated activity of the prefrontal and parietal cortices. However, traditional spiking neural networks (SNNs) have mostly been applied to perceptual tasks or simple pattern recognition and lack the ability to learn abstract rules. This study attempts to bridge this gap by proposing an interpretable SNN that implicitly encodes logical rules in its synaptic weights and recurrent dynamics.

2. Model Architecture

2.1 Input Encoding

Each fact (subject–predicate–object) is encoded as a fixed‑length vector: [subject_embedding, predicate_fixed, object_embedding, validity_flag], where the embedding dimension is set to 8. Three facts are concatenated to form the network input (dimension = 3×(2×8+2) = 54). This encoding preserves the structural relations among entities.

2.2 Recurrent Spiking Neural Network

  • LIF neurons: The membrane potential v decays over time (LEAK=0.95), accumulates input current, and when it reaches a threshold THRESH=1.0, a spike is emitted (using a continuous approximation spike_fn), after which the potential is reset.
  • Recurrent connections: The hidden layer has self‑recurrent weights (Whh), allowing the network to integrate information across multiple time steps and simulate the temporal unfolding of a reasoning chain.
  • Output layer: A softmax layer outputs three classes (corresponding to three different reasoning conclusions).

2.3 Learnable Entity Embeddings

Each entity (e.g., “cat”, “dog”) is assigned an 8‑dimensional real‑valued vector that is optimised together with the network weights via the genetic algorithm. This enables the network to automatically discover semantic similarities (e.g., “Persian cat” becomes close to “cat” in embedding space), thereby achieving generalisation.

2.4 Genetic Algorithm Training

  • Genotype: Includes all weight matrices (input→hidden, hidden→hidden, hidden→output) and all entity embeddings, totalling about 1096 parameters.
  • Fitness: Classification accuracy on six training samples.
  • Operators: Tournament selection, single‑point crossover, Gaussian mutation, and elite preservation.
  • Convergence: Typically reaches 100% accuracy within ten generations.

3. Experimental Results

3.1 Training Samples

Training samples included: cat→mammal, mammal→animal, animal→living_thing (label 0), dog→… (label 1), human→… (label 2), as well as similar chains for “Persian cat”, “hound”, and “pet”. The model correctly classified all training samples.

3.2 Generalisation Test

Unseen entities:

  • tiger→mammal, mammal→animal, animal→living_thing → predicted label 0 (feline class) – correct.
  • wolf→mammal, mammal→animal, animal→living_thing → predicted label 1 (canine class) – correct.

This indicates that the network learned the abstract transitive rule rather than memorising entity IDs.

3.3 Probability Distributions

For an ambiguous entity such as “pet”, the output probabilities were nearly uniform (0.360, 0.311, 0.329), reflecting its semantic ambiguity.

4. Cognitive Neuroscience Discussion

4.1 Correspondence with Biological Reasoning Mechanisms

  • Prefrontal cortex: Believed to be responsible for rule maintenance and working memory. The recurrent connections (Whh) in our model are analogous to cortical recurrent circuits that sustain information across time steps.
  • Basal ganglia: Involved in gating and selection. The elite selection and crossover in the genetic algorithm can be seen as “selection pressure” at the evolutionary level.
  • Hippocampus: Memory encoding. Entity embeddings can be viewed as distributed memory traces, similar to grid‑cell representations in the entorhinal cortex.

4.2 Spiking Codes and Neural Oscillations

The firing rate of LIF neurons in the model encodes the activation strength of concepts, which is consistent with rate coding theory in biological neurons. The evolution of membrane potentials over time steps can be interpreted as phase coding within theta‑rhythm oscillations.

4.3 Neural Basis of Semantic Generalisation

Learnable embeddings map symbols (e.g., “cat”) into a high‑dimensional space such that “cat” and “Persian cat” are close together. This resembles the “conceptual space” of semantic representation in the brain – for example, the anterior temporal lobe exhibits similar response patterns for different animals. Through the genetic algorithm, the network automatically forms such a topological structure.

4.4 Genetic Algorithm as a Learning Mechanism

Biological learning primarily relies on synaptic plasticity (e.g., STDP) rather than global evolution. However, the genetic algorithm can be viewed as a meta‑learning framework that mimics the innate reasoning structures shaped over evolutionary time (e.g., core knowledge systems). In our model, the “innate” part is the hand‑crafted initial embeddings (semantic features) and the network topology, while “acquired” learning fine‑tunes the weights via GA.

4.5 Limitations and Future Directions

  • Biological plausibility of time steps: Each discrete time step in the model represents a “reasoning step”, whereas reasoning in the brain is continuous. Continuous‑time LIF models could be introduced.
  • Lack of explicit rule extraction: Currently, rules are implicitly stored in weights and not readable. Future work could incorporate a neural logic machine (NLM) to output explicit rules.
  • Generalisation depth: For longer inference chains (four or more steps), the current model may require more hidden layers or greater recurrent depth.

5. Conclusion

This study implements a learnable‑embedding recurrent spiking neural network that successfully learns transitive inference rules and exhibits semantic generalisation. The model provides a computational framework for understanding the brain’s symbolic reasoning mechanisms and lays a foundation for building neuromorphic reasoning systems.

c++演示程序

参数snn_learnable_embed_improved.txt

-0.3945578210
-0.1059294641
0.0749153119
-0.4289440439
-0.2927494922
-0.0969305024
-0.0707249101
0.2763244648
-0.0196847829
-0.0792303507
0.1016015766
0.1299029137
0.1115735198
-0.0705238894
-0.0548891218
-0.3094649550
0.0609698155
0.1293727518
0.1575739001
-0.0673775725
-0.0337269636
-0.0926982567
-0.1157115566
-0.1713861087
0.2273824714
-0.7003737048
-0.3320104342
0.2180180713
-0.0618512200
0.0642505674
0.0814825383
-0.1642414079
0.0647888343
0.0142227151
-0.0718258400
0.1934871980
-0.2388979240
-0.0325300619
-0.0373144883
0.1472789706
0.3479511409
0.0051238711
-0.4499954787
0.0474659407
0.1084964280
-0.0757522466
0.5635462572
0.0703031943
-0.0937042173
-0.4424582607
-0.1306168066
-0.1631215005
0.5354425072
-0.4616707459
-0.0907410309
0.0145890742
0.2112942565
0.2124016617
0.3052596748
-0.2976223732
0.0430262228
-0.1416703774
-0.6530632162
0.0520663684
0.2043708606
-0.1996990794
-0.0427402842
0.0686914418
-0.0867593203
-0.2669752439
0.2576700876
0.1474542674
-0.1532734995
0.0102404771
-0.2160355503
-0.1799034509
-0.0372994852
-0.1760065140
-0.0452358017
-0.2875510859
0.0152391865
-0.1683303486
0.0882537084
-0.0540896014
-0.3562355411
0.1435040270
-0.1221143698
0.2823859718
-0.3256552147
0.0594263353
0.0424723619
-0.4049525296
0.3701126857
0.2953880999
-0.3667823842
-0.1649002853
0.5941356320
-0.2314172170
0.2989556309
-0.0498620035
0.1561963910
-0.0170684140
0.2087439502
0.7549644931
0.0866949337
0.2940365952
-0.0342906785
-0.0648774219
-0.1439805240
0.1393736991
0.4151659953
-0.1880143104
-0.0910236341
-0.0776026625
-0.1920053467
-0.1337252490
0.0253204900
0.1619283842
0.0047438639
-0.5256372184
-0.0511940437
-0.0317850710
0.2159935789
0.1004268576
0.0535235522
-0.1402071602
-0.3781422395
0.3786770224
-0.0184506799
0.1581361114
0.1822857435
0.2186341710
0.0749712480
0.0089965755
-0.0075183754
0.1752021250
0.5623772543
0.2385636445
-0.1338131009
-0.2050428460
0.2905272209
0.2810911461
0.1828889467
0.0910951497
0.0990811049
-0.3781970779
0.3264119780
0.0818412050
0.2244791563
0.2135220049
0.0087098315
-0.1868942382
0.0538639970
0.3107962104
0.1102974673
-0.1297023875
-0.2423280604
-0.0246144374
-0.3781493785
0.0196530782
-0.0703370469
-0.1028111788
-0.1136856081
0.2072712139
-0.2377952637
-0.3393316079
0.4688913592
0.0009577098
-0.2186978287
0.1236016282
-0.1842186083
0.2851870388
-0.4688425834
-0.0890947608
0.1199392236
-0.1417186438
-0.1162054495
-0.0201335275
-0.0473020929
0.2759691063
-0.2741356350
0.3475213989
-0.2266697558
0.5662621486
0.2830137236
0.0256672648
-0.3568724127
-0.2610293707
0.2179610203
0.0359565903
0.1876485619
-0.0181159002
0.1764344014
-0.1371584183
-0.2871569164
-0.0713593378
0.0761369473
0.1160249758
-0.0010171304
-0.0128926486
-0.0056326819
0.0241423677
0.0762184628
0.0164417419
0.1161081858
-0.0701216087
0.2082109927
0.0336377285
0.2457113174
0.2079437817
0.0990723917
0.2136406404
0.1791653999
-0.1947372639
-0.1325368452
-0.4289433686
0.3374148518
-0.1681304315
0.0249831001
-0.1935413183
0.0649901876
-0.2717048630
0.2071076038
-0.3024623272
-0.2929239487
-0.4842112065
0.4003654912
0.0930384719
-0.0102904174
-0.0548348784
0.2452625429
0.0028366204
0.1189480736
-0.0467670784
0.1746881693
0.4868778837
0.1107198037
0.1133633147
-0.2953603770
-0.2414686133
-0.1393618970
-0.3503519588
-0.1015986914
-0.1423782397
0.1718683685
-0.0089115845
-0.0764479100
-0.2556590075
-0.1403957886
0.2192447445
0.2206172077
0.1036792354
-0.1148182345
0.2336233013
0.1497077054
-0.0878890515
-0.3561560727
-0.0719979223
-0.1294551185
-0.2711689442
-0.0327135547
0.0577387215
-0.1020821063
0.1100435027
0.0422970160
0.0126451942
0.2625507801
-0.0644271224
0.0055986485
0.1452422017
-0.3504903962
-0.0743455918
-0.2563566251
0.0237056364
-0.1101553610
0.1448885122
0.0175407765
-0.0647768837
0.1285000742
-0.0591461782
-0.0121768549
-0.2085543939
0.2920335895
0.0486537285
0.0914141555
0.2397499506
0.0774166231
0.1206827788
0.1091739749
-0.0637527734
0.0771098515
-0.0417913304
-0.0387854612
-0.0202311173
0.0300642289
-0.1570684626
-0.2649069476
-0.3325160744
0.1420836661
-0.1568059920
-0.0205459280
0.1229585129
-0.0638930135
-0.2732295178
-0.1394898111
-0.0161093027
0.0743650834
0.1884785261
-0.3824759350
-0.0797086492
-0.0847468623
-0.2131910092
-0.1114516198
0.3267990656
0.1413213775
0.1747792663
-0.1655089707
-0.1755867459
0.0408507196
0.1327305223
-0.1066818479
-0.4732399955
0.0139735126
-0.6487458142
0.4009611384
-0.3810788061
0.4627206560
-0.0767957999
-0.1156389754
0.1826805452
0.2035492626
0.2552788232
0.2789408816
-0.0188507795
-0.1128578163
0.1071955842
-0.0726994906
-0.2718075910
0.0553161797
-0.1021286251
-0.0817976032
0.1960171854
-0.1659457908
0.3135167187
-0.0024258268
0.4994184713
-0.4383238588
-0.2374562069
-0.0849803612
-0.0975851004
-0.0842232463
0.1357508485
-0.0663695009
0.2048348784
0.0053136447
0.1200992462
0.0270087526
0.5975119457
0.3505393333
-0.3879904372
-0.0931111013
-0.0187921224
0.3728556045
0.4332212881
0.3075961426
0.1877137890
0.1924342753
0.0222027059
-0.2094854412
0.4572577528
-0.1876291765
-0.0597193788
-0.1439921807
-0.1263773157
0.5702601949
0.0356117176
-0.2770327285
0.4198969449
-0.4699391069
0.1058545621
-0.3105280352
0.1239082941
-0.2761392165
0.0433571566
-0.1877520206
-0.2614235615
-0.2505790290
-0.0354560818
0.1978725166
-0.0251576351
-0.1709344030
0.1588845543
-0.1925321354
-0.0829746583
0.0956585169
0.0965401991
0.0907044317
-0.1462267127
-0.0284986802
-0.1193884055
0.3105831403
-0.1842263742
0.0429747650
-0.1363744032
-0.1837296475
-0.1647838377
-0.1650123760
-0.1489319488
0.3275452991
0.4665973951
-0.0839791254
0.0024595516
-0.0656389642
0.0780845836
-0.2657622229
-0.0671912775
0.1675234592
0.0403094016
-0.0133802039
-0.1189068383
0.1149711698
0.1066941764
0.3027186713
0.1041356927
-0.0081333440
0.3796995406
0.0475670511
0.4088434300
-0.0624252535
-0.1070354839
-0.3459116802
0.1283148625
0.1275362248
-0.2086075027
-0.4887095918
0.2297135706
-0.1898909760
0.1322670514
0.1359697096
0.2542903603
-0.1147281525
0.1118825685
0.2752060403
0.2515456709
0.2110940676
0.0102343201
-0.2016472219
0.3363869063
-0.0712837309
0.1655083294
-0.1845952695
-0.0802380807
-0.2355427400
0.0314323021
-0.2979533669
0.0901445900
0.0500840729
0.2061586408
-0.0643158074
0.0122502049
-0.6288547124
-0.0356426288
0.0495439215
0.1174087996
-0.1724964070
-0.2727069787
0.0003659977
-0.0006786962
0.1407483200
0.3415463726
-0.2147424869
0.0153563284
-0.2034709629
0.2832035187
0.1532179608
0.1441974281
0.1348608478
0.1708103248
-0.0757771760
-0.0594334803
0.2055494635
0.1064891354
0.1442971858
-0.0315358430
0.2302845921
-0.2090357401
-0.2263486954
0.0397062616
-0.0998931912
-0.2608788030
0.1445854826
-0.1786430491
-0.1864831864
-0.3871009961
0.0856849856
-0.6473404378
0.0940162231
-0.6737318503
-0.2168369261
-0.2145125861
0.1493923251
0.2850490895
0.1018539736
-0.0891240266
0.1042968425
0.0263845661
0.0247409221
-0.1286518875
0.0910049207
-0.1064200748
0.4360836445
-0.5032103955
0.3037488700
-0.3112458670
-0.0198653736
0.0526904798
0.4361718342
0.1786312047
-0.2765431574
-0.2801741131
0.1439413657
-0.0917379443
0.4717405408
0.4099116602
0.4407609354
-0.1568674379
0.1752903868
-0.1453199942
0.3940196862
-0.0443038837
0.0956818769
-0.0360960825
0.0051862475
0.0043893028
-0.0415568434
-0.0169426182
0.1639688586
-0.3911968417
0.1770401528
0.4987245552
-0.0549591251
-0.5580892005
-0.1895485423
-0.1036321035
-0.1295560277
0.0961986303
0.2340258576
0.0297705488
-0.3433587847
-0.1700489601
-0.0665292029
-0.5148908483
0.1937696356
-0.1914822368
-0.0904042636
-0.2531410597
-0.5701902985
-0.0920009937
-0.5209147359
0.5982195001
-0.0582562267
0.1077798798
0.4962260430
0.0214670764
0.1572988247
-0.3046492318
0.2327250723
-0.1514154066
-0.1607613592
0.0795711257
-0.0760136614
-0.0845599470
0.0567303432
0.1983776742
-0.1600992024
0.1163836710
0.2442317519
0.3992087665
0.4822946381
0.0115539422
0.9357652107
-0.0465952129
0.0348912645
0.1011170425
0.3831783070
-0.6153016240
0.1315690332
0.1364083114
0.2268678588
0.2156112489
-0.4514043250
-0.2016163135
-0.0063926095
0.0855819553
0.4190691884
-0.3222450586
-0.4793952009
0.2302971392
-0.2692652862
0.1866285104
0.7205484198
-0.2127178284
0.1234895106
-0.1958050674
-0.0542067847
0.0018768273
-0.1164707551
-0.2114518417
-0.4696048582
0.0598346868
0.0352003739
-0.1301166299
-0.2453477683
0.0131929371
-0.1624954528
0.5335960053
-0.1468382080
0.1270030165
-0.1714958102
-0.2141041992
0.0336437335
0.2227118478
0.4391727695
-0.0972796868
-0.0681049615
-0.0849351928
-0.0486254002
-0.0707377713
0.2414102458
0.1006394365
-0.0402799898
0.1074792058
-0.4904529625
0.0100917500
-0.0948896082
-0.0508141183
0.1525390094
0.4442552581
0.0713089998
-0.2101671528
-0.0630384155
0.0031302540
0.0704571434
0.0014961878
0.1416205500
-0.1656629205
-0.5165853664
0.0275354195
0.5688012244
0.0427846314
0.0053896161
-0.1637741966
-0.0017865193
0.1211922550
-0.1999583217
0.0671642474
-0.4188704253
-0.2091406306
0.2356171229
0.1095898038
-0.0586413977
0.0527461058
-0.0588238346
-0.4222796137
0.1951477773
0.0450746959
0.2472102031
0.5465710181
0.2080222687
-0.1842820513
-0.1560798718
0.3393594890
0.0737992898
0.3048386288
-0.1812623221
-0.5480840338
-0.1807247068
0.0022344074
0.0335716845
0.1520501724
-0.1622871728
-0.3440147344
-0.0339406850
0.1723515949
-0.2425557106
0.0787363495
-0.0855951108
0.1225195639
-0.2028250494
-0.0974849951
0.1636852528
0.0693504010
0.0637677931
-0.1408899032
0.5731997360
-0.1851615537
-0.8671762128
0.1189052946
-0.1346511179
-0.1430494277
0.1904043987
0.0826675750
0.0981276107
-0.3865289825
0.2518110957
-0.1348945752
-0.0354841830
-0.4602645096
0.0635595549
0.5060472835
0.3077998812
-0.0608937764
0.0651910771
0.2065464685
-0.1659221295
-0.1128232532
-0.0457550123
-0.1592158190
-0.0364934152
-0.1888546683
-0.4430452590
-0.3523627974
-0.0616552843
-0.2126247632
-0.0890220544
-0.1759095235
-0.1652823392
0.1554113444
0.1149873079
-0.1141504747
-0.0206180668
-0.1741165289
-0.1225809565
-0.4191825120
-0.6137641624
-0.0230728245
-0.7612972443
0.0155130420
0.2543718209
0.0492292304
-0.1015821672
-0.0718784635
-0.0407007464
0.1137007692
0.0888285984
0.0343922513
-0.0835594881
-0.1297436351
-0.0989018853
0.3248490171
0.5306229521
0.3920991344
-0.1953321905
0.0437209424
-0.1321365886
-0.0328605430
-0.3223604337
-0.0621599615
0.2146692080
-0.7703260426
0.0510117786
-0.0324366572
-0.2206749316
-0.1488047573
0.3600722244
-0.0449706248
-0.1374538907
0.1242148315
0.0640430520
0.2641125341
0.1102031831
0.1250605036
-0.5006048039
0.2382653204
0.1274003350
0.1015341465
-0.2506109092
0.0968497905
-0.2463049819
0.4021429320
-0.1243578909
-0.0160847137
-0.0826000599
-0.1712471766
-0.0377892610
0.8245469954
-0.1064297136
0.5088577708
-0.0820631966
-0.0217278818
0.4973417413
-0.1025921398
-0.2847704458
0.3047838206
-0.2441911998
0.0626088647
-0.2513969950
-0.1072458188
0.2020463718
0.0767695363
-0.4744010917
0.1350634637
0.0599800656
-0.2291220675
0.3591817804
0.1035853551
-0.3349513754
0.1719834101
0.0283913748
-0.0535263286
0.2405027483
0.1856348424
0.2114121010
0.1293329522
0.1079559205
0.2792508115
-0.1457102140
-0.1574277108
0.0231464501
0.1514011646
0.0116896114
-0.2192082485
0.3825338815
-0.2402350862
-0.4456282911
0.0383494958
-0.5742003721
-0.0578744797
-0.0946839639
0.1954077538
-0.1655119406
-0.0939820031
0.1767317911
0.1312600964
0.0331746222
-0.3812202335
0.0316378762
-0.3767790507
-0.0904470971
-0.0953377447
0.1821564575
-0.0967401713
0.0293466672
-0.1747603014
-0.0042297712
0.7336590221
0.1286218594
-0.2623442285
-0.0655188919
-0.1886098403
-0.1802224095
-0.1358256063
0.0771356814
-0.0129393008
-0.2908945762
0.2515920314
-0.1175013778
-0.2926524798
-0.0022649018
0.0900255184
0.4176666490
-0.1588858530
0.0088890192
0.0931256000
-0.3234308811
0.1657416649
0.0348894310
0.1753490983
-0.4232056226
-0.1708579578
0.0242539400
-0.0379008282
-0.3321874032
-0.0178119342
-0.1024036608
-0.1187613784
-0.0270130670
0.3632880809
-0.1971663839
-0.2562324096
0.5284818792
0.1554898982
-0.1212866267
-0.0137815389
-0.4021219837
0.0365185937
0.0855693724
-0.1184392868
-0.3984220688
0.2428157581
-0.0309548073
0.1890959815
0.3390162241
0.0009218972
0.1810177327
-0.0979626096
0.0485801750
-0.0746573935
0.4538122913
0.0067349439
0.0019896376
-0.1082590314
0.6331607862
0.3263636247
0.4760281454
-0.1626768151
-0.2314449339
-0.1974502322
0.3558230592
0.1358862595
-0.2253680216
0.0959754858
-0.5715412445
-0.4973993327
-0.0772121103
0.2198685874
0.0808979443
-0.0149057003
0.1275556095
0.1696210029
-0.0266287361
0.0355730951
0.1495034471
0.2855456272
-0.2120556751
-0.2336528588
-0.1643969941
-0.3064692590
0.1308505116
0.3191186679
0.1617707198
-0.1350788369
0.2576916677
-0.2964773639
-0.0090074937
0.0764927774
-0.7813205685
-0.5541040882
0.0032039972
0.0137420918
-0.0624129720
0.1040997423
-0.1192326118
-0.0323724508
-0.0419885820
-0.0635676635
0.0720754862
0.4560133207
-0.2517019923
-0.0792901517
0.0363699027
0.0181680850
0.0970740188
0.1890772135
-0.3442430696
0.1371770017
0.0858856633
-0.1005397577
-0.4284215240
-0.1954787364
-0.2671960067
-0.0002309090
0.3848590040
-0.0709852842
-0.2146771966
0.1800123952
-0.0708421804
-0.2132416503
0.0802765840
0.1807338850
-0.1790294602
-0.4509983593
-0.1088853501
-0.0131693969
-0.0392837030
-0.3249287079
-0.4356724771
0.2344343422
0.0531319237
0.0747881613
0.4683044689
-0.2502960791
0.4016105326
0.2411484873
-0.0322954868
0.0226020719
0.5347079387
-0.0709238087
0.0158781780
-0.5175118773
-0.0533218959
-0.0641616537
-0.4094791870
0.0601607674
-0.0194395672
-0.0320877426
-0.1191778675
0.1172131702
-0.0341178097
0.2498783247
-0.1492005218
0.3552134511
0.2281949422
0.0692396517
0.3802955268
-0.1500459693
-0.0699477823
0.1876233952
-0.2865970909
0.5943750634
0.1788948918
0.3920369708
0.1159736363
-0.1397718840
0.9735261392
-0.1287136555
0.3932111502
0.1953027195
0.5613530721
-0.1784012147
0.3874213166
-1.0000000000
-0.6207493677
-0.2976038736
-0.1282523869
-0.0672970810
0.1095895682
-0.2536252133
0.0106510876
0.3985823626
0.4640038498
-0.1347901761
-0.3766446445
0.3543439265
-0.1674867935
-0.1634457859
0.0449103947
-0.0826201501
0.0134371825
-0.0541297229
-0.2287654354
0.4079240878
-0.2579078419
-0.2133940003
0.1021030305
-0.0012439434
-0.1312318118
0.0467326550
0.0096457078
0.1205167666
0.0906823565
-0.0740580055
0.3712330012
0.1335313327
-0.0555359642
0.1856830629
-0.0916400081
-0.1340193301
0.2569788040
0.0150963943
-0.6874474047
-0.0441827046
-0.4000813292
0.1465333371
0.0343454817
0.1082361768
0.1182370347
-0.0069926418
0.0796203165
0.0975994258
0.0613616593
0.1400521105
0.3995540843
0.1429343639
-0.0194904156
0.2016208834
-0.0319913124
0.2166031259
-0.3310620660
-0.2701362657
0.0774482725
0.1438689859
-0.4643766399
0.0626790464
0.3066293648
-0.1597799577
-0.1528455821
0.0076780366
0.1488656844
-0.0913983706
-0.2094680585
-0.2026615628
0.4788073914
0.6629939795
-0.2397707636
-0.0152406832
-0.2662693507
-0.0976003279
-0.1701785057
-0.2532377146
-0.2843977339
0.2457480598
-0.1769683323
0.3023618394
-0.2782112731
-0.3351941095
0.1710581292
-0.0667136356
-0.1234879694
-0.0061211441
0.0041794995
-0.1534372487
0.1409552409
-0.3494397299
-0.3762880229
0.1982531077
-0.1512607172
0.1525824995
0.0740293717
0.2563398046
0.1448601356
-0.1088111443
-0.0839989441
0.0741363877
0.0624333802
0.1183435777
0.1633594426
0.0197663963
-0.1230710897
0.0438487062
-0.7811158568
0.3809213311
-0.1468708988
0.6375345587
-0.5112496677
0.0086293032
-0.1135095391
0.1317354929
-0.1017776309
-0.1123899971
-0.0800946044
0.5938836059
-0.1728895761
0.3366626269
-0.1651436619
-0.0941264492
0.1915707743
-0.1249222412
0.2584833506
0.0623901730
-0.0308972934
0.1927421424
0.5160758351
0.1092104960
0.3660808816
0.0109065117
-0.0923401041
-0.0698911079
0.2880729038
-0.2375749551
-0.2349628461
-0.1442889874
0.1553965230
0.3043882516
-0.0822552501
0.4662302808
0.1027923398
-0.0428912977
0.1646836850
0.0551851647
-0.7820841418
0.0798082963
-0.0631626917
-0.1916945305
-0.3341465388
-0.0818548500
-0.0002086063
-0.1417389490
-0.2437851668
-0.0241800345
-0.2998077692
-0.0491971859
0.2471425286
0.2627315165
0.0276023069
0.1184845694
-0.2228952232
-0.4085447332
-0.1059677207
-0.6437247028
0.2769619593
-0.1751381348
0.4619304313
0.2101326946
-0.0690562781
-0.1479617059
0.1320691369
0.2770559869
0.1966384351
-0.0211838710
-0.0188501103
-0.0069043489
-0.0544262425
-0.2453350775
-0.2328920524
0.0676640896
0.0233761813
0.4091787473
0.1026626209
0.1503154243
0.4414530643
0.0994458728
0.1140266027
0.2314407851
-0.0313837699
0.1999962947
0.1776424515
0.4200463692
-0.3579145331
-0.1361300181
-0.6944943847
-0.2830032011
0.0858161748
0.2353054490
-0.1940543653
-0.3983463528
0.1449338519
0.6228906921
-0.2634257755
0.5734298499
0.0687550380
-0.1241850660
-0.0778360859
0.2431978945
-0.0824850559
-0.2212299950
-0.0963919423
-0.4683815819
0.0537892715
-0.1131566395
-0.0670639338
0.3082683939
-0.1053171319
0.2155218231
-0.1029420790
-0.1886103785
0.0646211915
0.3532295451
0.2436489996
-0.0767078191
0.0004379522
-0.0705883650
-0.2009700732
-0.0954805983
0.1395844801
0.0733816072
0.1294243675
0.2782206127
0.2026304561
-0.0664101874
-0.0264455847
0.0681240304
-0.0949616028
-0.0558648352
0.2695595342
0.5292338884
-0.3001303569
0.0255762635
-0.0280180019
-0.0682608516
0.3296119407
-0.0992515767
-0.0236759890
0.8558183873
-0.2220886160
0.0893127228
0.3131920205
-0.0336568255
0.0598834166
-0.0489077033
-0.3794560581
0.2075735240
0.2745832996
0.0485220130
0.0436654004
-0.1431105202
-0.0145363348
-0.0729274053
-0.0593213417
0.1169600521
0.5955138575
-0.2871467874
0.1093996036
0.2235672564
-0.1629355979
0.1083883562
-0.8474763974
0.0103985243
0.2217866486
-0.2316028955
0.2121707672
-0.2671811989
0.2764251909
-0.1170719393
-0.1216883443
0.0699128060
-0.1540002399
-0.4383524228
0.2924940287
-0.0040547031
0.2820873823
0.2652750767
0.5999248498
-0.1714584453
-0.2436458271
0.1565191722
-0.0802009161
0.0818457239
0.6133964626
0.0132003701
0.0553433553
0.3113286791
0.2157534446
0.0009067604
-0.4099134548
-0.3479686646
0.1100686404
0.3392448345
0.4096463481
-0.3363529315
-0.1892902665
0.1105834543
-0.1390402431
-0.1763546717
-0.2670633301
0.0156024114
-0.0953921025
0.0833196681
-0.2118857114
-0.1964694977
0.1278714379
0.1692539408
0.2762164871
0.1123774834
-0.0916194119
-0.3334275809
-0.1706929104
0.0803772798
0.1563596383
-0.6485735708
0.6876606538
-0.4679424461
0.2075960044
-0.2225356430
0.1478134744
-0.0284035447
0.1683599786
-0.0716017177
0.2736930779
0.1287793352
-0.1815243359
0.0795904273
0.0334499211
-0.1509210653
-0.1747845490
-0.3941409393
0.0843987505
0.2450423936
-0.4399749137
-0.0910743724
0.3115391672
0.1479575544
0.1950882155
0.1167864120
-0.4006289413
0.0248979809
0.4263109047
-0.1511765977
-0.2364827221
-0.5262378111
-0.0404683986
0.1086991777
-0.3208430301
-0.3739411676
0.4711898671
-0.3088615015
-0.1265920002
-0.0504628331
0.1288364573
-0.3456796262
-0.2289205742
-0.1556936026
-0.1974698301
-0.2572320896
-0.1654557061
0.3255708953
0.1060558162
0.1559350643
-0.2316341651
0.1191268690
-0.0440302716
-0.2951484770
-0.2366468768
0.0136991333
0.1423669339
-0.4831753193
0.1353146225
0.2635846917
-0.1456287518
-0.0475541582
0.3224284926
0.4570806979
0.1841738447
-0.1704971023
0.1280724529
0.0660797155
0.1982156284
0.0534178492
-0.4796457802
0.0428591560
0.0576122142
0.1069114415
-1.0000000000
0.0374269112
0.5782232497
-0.0803168871
0.1687664010
-0.0948454501
0.0968402656
0.4243929895
0.0605392357
-0.0662905602
0.2812179013
-0.1351725758
-0.1165147086
0.0792297422
0.0024801792
0.0040015211
0.6081724366
-0.0200450581
-0.2632864260
0.1680147250
0.0279121572
0.0774502812
-0.2457886997
0.4083617567
-0.0530664960
-0.1129464795
0.0650711957
-0.0918226086
-0.1321624398
-0.1267382877
-0.1377499137
-0.4800186724
-0.1854301621
0.1093364031
0.0909685336
-0.0597031784
0.2172195872
-0.0544012688
-0.1403435962
0.0626117859
-0.3503916845
0.3104105435
-0.2458757549
0.0680370527
-0.2148922613
0.0992976393
-0.0499322451
-0.0510714520
-0.2986690005
0.2330242960
-0.0440758918
-0.1581974042
0.2991694503
0.0653422376
0.0037152098
-0.0261257157
-0.1281615836
-0.1840582088
-0.5452818638
-0.2179245973
-0.1470946882
0.3823988195
0.3559468332
-0.0791762166
0.4167313892
-0.1517863820
0.0227376131
0.2824627786
0.1215229401
-0.2028539683
0.0752766088
-0.0477852516
0.6540760253
-0.1910078416
-0.0589273330
-0.2076810982
-0.0517766347
-0.0636568329
0.1167966502
0.0759175091
-0.4156608806
0.3687946922
-0.0039385246
-0.2288682311
0.2243470724
-0.2681091636
-0.0656929640
-0.0273018510
-0.0931718760
-0.3517102698
0.0858343143
0.0810485359
-0.1503788248
0.2552785509
-0.0431703761
0.0441491155
0.3323259538
-0.0941743246
-0.3326987368
-0.0522705495
-0.2167122712
0.1757862810
-0.2589754847
-0.2206955618
-0.1149003899
-0.4981661852
0.0709515132
-0.3678683532
0.0083288243
-0.2833679060
0.3786404260
0.6047247422
0.0974557763
-0.0955957372
0.0197715090
-0.0741201521
0.2414523352
-0.0708132128
0.0910951736
-0.6306357732
-0.1823118282
-0.1251217934
0.0168564884
-0.1509841114
0.2354311723
0.0493238030
-0.2363248065
-0.0139668075
-0.5170665240
0.1247764041
-0.0257916429
-0.0699915861
0.1611130334
-0.4353398686
0.0466520787
-0.1940513754
0.2692211096
0.0320785231
0.2619228151
0.0300543554
-0.3479926360
-0.4370511005
-0.4474761524
-0.1951044523
0.1386193968
0.1694709819
-0.0842842006
0.0555362371
-0.1197409818
0.4043776434
0.3132117779
0.4321266411
0.3564819327
-0.1768324147
-0.2050723188
-0.6992860011
-0.1068877933
0.1739188488
-0.1371316127
0.4858202667
0.1791137144
0.5098743622
-0.0202874667
0.2495449694
0.0359758669
0.1378328217
-0.0609130922
-0.0049227729
-0.1432893282
-0.1467612986
-0.1215133802
0.2691917762
-0.1000711329
0.2398899676
-0.0524328896
0.4707972535
-0.1092995637
0.1494831481
0.2393975382
-0.2396021806
0.1174404174
-0.1443807059
-0.1861134174
0.1957593525
0.0599204265
-0.1362480588
0.0741099593
-0.0020990999
-0.1083195684
-0.4405525007
-0.2266593870
-0.5585798118
0.0794119144
-0.1486224116
0.2879698634
0.1773582553
0.2723616574
-0.1251760132
0.0758336303
0.3687081578
-0.2685000882
0.0291817357
0.1750051065
0.2049010163
0.1094373646
-0.1645699151
-0.0933594873
-0.2528801775
0.2829715333
0.0630071743
0.3044733504
0.1280068126
0.2564593073
0.2215167675
0.5925292454
-0.0581826487
0.1414703172
-0.3341490818
0.2759672338
0.2414943343
0.3219172915
0.0919199394
0.0341152195
0.0566433883
-0.6735828176
-0.2416258746
-0.3034206917
0.3102492832
0.8385133819
-0.1485222201
0.4107759936
0.2533572818
-0.2989493149
-0.2004841015
0.0903432486
-0.4330861393
0.1592258720
-0.4235022430
0.4194693091
0.2428250853
0.1347988524
-0.1103181435
-0.0003287241
0.3869058015
0.1793715708
0.4473847330
-0.6644528483
0.4676019186
-0.4779757597
-0.1169323150
-0.0274739191
-0.1248468792
0.0924140746
-0.1458634694
-0.1940532562
0.0762559240
0.3271248221
-0.0947989376
-0.1857212084
-0.5720933884
-0.1949334593
-0.0078115475
-0.1110030355
-0.1568766189
-0.1863219142
0.4589733607
0.1930821546
-0.0884719026
-0.4077793820
-0.3256533633
0.0785590967
-0.1054180271
-0.0386145487
0.1386361174
-0.0343240693
-0.4527138213
-0.3028474268
-0.1441734441
0.0070199939
0.3425682467
-0.0619123393
-0.0168081469
0.3663816655
0.1408348000
0.0108519089
-0.1592297226
-0.0633259678
0.3632910782
0.0143816042
-0.1859561656
-0.3066773025
0.0166772122
0.1296599413
0.2453285088
-0.0775007767
-0.4243016805
-0.2058625624
-0.1813243716
-0.4933031368
-0.1063116500
-0.3268296379
-0.0646706790
0.2483441181
0.2905937488
0.1302238881
-0.2373816606
0.1244302226
0.1197419638
-0.1852381238
-0.0644335027
0.0318133395
0.1410837474
0.0882082220
-0.0657622132
-0.2109222238
-0.2451001707
0.2631417383
0.0274133214
0.3463563780
-0.0468723073
0.5156053444
0.0780869268
0.1756784694
-0.2476509455
0.1807681469
0.0667869117
0.4610025830
0.3959111544
0.1032156927
0.1389604439
0.1105109233
-0.0484124091
-0.0765225862
0.2286097894
-0.0762351728
0.2170731662
-0.2450840947
-0.0104233939
0.2084010444
0.3349297431
0.1954620224
0.1802740639
-0.2746632983
0.1273725822
0.2781248386
0.2931025110
0.3390557682
0.3018915114
-0.0171738064
0.1587709456
-0.2078866400
0.2306170847
0.2655293195
-0.2062271511
-0.5489120340
-0.0704897234
-0.1316003023
0.0061550157
0.0296857676
-0.0015992748
-0.4834191598
-0.2911566608
-0.4034593638
-0.2051110961
0.0209142119
-0.1672817268
-0.3085803593
0.6102428026
-0.0961386452
-0.2374754772
0.0979164063
-0.0445451958
0.2276891121
-0.0164194392
0.2838075949
0.0017644863
0.2733601880
0.0470135068
0.2978380152
0.1140542339
0.3537482602
0.4772676675
0.5151500579
0.1641837563
-0.4833451350
-0.0510263691
-0.2165550067
-0.4553692626
0.3299874421
0.1713323755
0.1342234128
-0.5450956645
0.0518416685
0.1849813526
0.1268658135
-0.1017601133
-0.1203759023
-0.3607799050
0.0813641291
-0.0627322806
-0.1861465587
-0.1785247624
0.1495551215
0.2180753938
0.5541403041
0.0663963499
-0.2509381942
0.5473754610
0.0920269804
-0.1471355124
0.1705118700
-0.0047174565
-0.0465934573
0.6133633479
0.1433655724
-0.3255102825
0.0336823101
0.1509525951
-0.5490187831
0.3252228525
-0.2453878433
0.2303094241
0.0896674059
-0.4279765070
-0.3827218579
0.1324531382
0.0979184542
0.1535225691
-0.3005204231
0.1746084423
-0.1589898658
0.3530391317
-0.1901842733
-0.2876813272
-0.5208450798
0.1803496463
-0.0850514906
-0.3186623496
-0.5756884990
0.3408320353
0.5896432151
0.1907509317
-0.2645393593
0.0143998458
0.1015745464
0.0329156482
-0.0053406816
0.3347783284
0.0043877868
-0.3286276782
-0.1405648321
0.0708676877
-0.3079620490
0.1451566842
-0.0604395911
-0.0805782570
0.1394374513
0.2901957529
-0.0259367401
-0.5817881752
-0.1779255701
-0.2181355758
-0.0327709961
0.0980059717
-0.0435560286
0.0616802236
0.5940674394
-0.3449317586
-0.2774784885
-0.5604349722
-0.0775053670
-0.1589084405
0.0985917457
0.0797834993
0.0137779841
-0.3641619825
-0.1167433610
0.1764676363
0.0784036739
0.1611942899
0.0875917148
0.2307474078
0.1143199521
0.1171988007
-0.1101959582
-0.0090765495
0.0933186301
-0.1042966151
0.0220949178
0.0922935996
-0.1585282581
0.0856246708
-0.2992589300
-0.0968752420
-0.0721077688
0.0449855614
-0.1448226435
0.0525964239
0.2381224310
0.1964177405
-0.1291339557
-0.0072083782
0.3628242637
-0.2700478716
0.0231724249
0.0238268666
0.0894575302
0.4280526984
-0.2585678913
-0.1297719837
-0.0917813072
0.4195539038
0.1476131054
-0.0844230263
-0.4556147055
-0.1069771906
-0.0069799359
-0.0678310539
-0.2092580708
-0.0315832238
0.0208755305
-0.3303208034
1.0000000000
1.0000000000
0.9897117806
0.0804550770
0.0526430194
0.1717201096
-0.0840901645
-0.1162551231
0.8393958017
0.9869420880
-0.0109537105
1.0000000000
0.1607640183
0.0967238769
-0.2070221668
0.1867310689
0.8744442179
-0.1673667425
0.1706082315
-0.1487195314
0.9529573786
0.1640173474
0.3248248461
0.2882960812
1.0000000000
0.0854625998
-0.0036064062
-0.3496923579
0.0576907185
0.3736387368
0.5523497729
0.4486411121
1.0000000000
-0.5025750190
0.3153673577
-0.0878607863
-0.1469380258
-0.2413246703
-0.3371638766
0.1993898460
0.8372260435
0.1504959405
-0.3872757046
0.1972375430
0.1477676032
0.0546361830
-0.4872899215
0.3187917051
0.9709018286
0.7431887565
0.9611820994
-0.3329383243
-0.0034858209
0.0803826136
0.0331884631
-0.0013717268
0.9356248090
0.7674339737
0.2679354673
0.8829200400
0.1419457592
0.1132836796
-0.2668251511
0.1673902485
0.8309767696
0.9455581794
0.6137267861
0.3468436526
-0.1826239990
-0.0590934308
0.3216018112
0.0283366530
0.9497795002
-0.1688667997
1.0000000000
0.3800020464
-0.2075083603
-0.1001369051
-0.1805396911
-0.2130164446
0.9441811789
0.2704273062
-0.1701210980
0.6805360055
-0.1585494012
-0.2695446665
-0.1327393715
-0.0091627763

g++ -std=c++17 -O2 demo_snn_inference.cpp -o demo_snn_inference
./demo_snn_inference

下面是程序

// demo_snn_inference.cpp
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <map>
#include <fstream>
#include <iomanip>
#include <algorithm>

// ==================== 参数(必须与训练时完全一致)====================
const int EMBED_DIM = 8;
const int FACT_DIM = 2 * EMBED_DIM + 2;
const int INPUT_DIM = 3 * FACT_DIM;
const int HIDDEN_DIM = 24;
const int OUTPUT_DIM = 3;
const int TIME_STEPS = 30;
const double DT = 0.1;
const double LEAK = 0.95;
const double THRESH = 1.0;
const double BETA = 5.0;

// 实体列表(必须与训练时的顺序一致)
const std::vector<std::string> ENTITIES = {
    "猫", "狗", "人", "哺乳动物", "动物", "生物",
    "波斯猫", "猎犬", "宠物", "老虎", "狼"
};
const int NUM_ENTITIES = ENTITIES.size();

struct Sample {
    std::string s1, o1, s2, o2, s3, o3;
    int label;
};

double spike_fn(double v) {
    return 1.0 / (1.0 + std::exp(-BETA * (v - THRESH)));
}

std::vector<double> encode_fact(const std::string& subj, const std::string& obj,
                                const std::map<std::string, std::vector<double>>& embed_table) {
    std::vector<double> vec(FACT_DIM, 0.0);
    auto it_s = embed_table.find(subj);
    auto it_o = embed_table.find(obj);
    if (it_s != embed_table.end())
        std::copy(it_s->second.begin(), it_s->second.end(), vec.begin());
    vec[EMBED_DIM] = 0.5;
    if (it_o != embed_table.end())
        std::copy(it_o->second.begin(), it_o->second.end(), vec.begin() + EMBED_DIM + 1);
    vec[2 * EMBED_DIM + 1] = 1.0;
    return vec;
}

std::vector<double> encode_input(const Sample& s,
                                 const std::map<std::string, std::vector<double>>& embed_table) {
    auto f1 = encode_fact(s.s1, s.o1, embed_table);
    auto f2 = encode_fact(s.s2, s.o2, embed_table);
    auto f3 = encode_fact(s.s3, s.o3, embed_table);
    std::vector<double> in(INPUT_DIM);
    std::copy(f1.begin(), f1.end(), in.begin());
    std::copy(f2.begin(), f2.end(), in.begin() + FACT_DIM);
    std::copy(f3.begin(), f3.end(), in.begin() + 2 * FACT_DIM);
    return in;
}

class RecurrentSNN {
public:
    std::vector<double> params;
    int w_ih_size, whh_size, who_size, embed_size;
    int total_params;

    bool load_text(const std::string& filename) {
        std::ifstream ifs(filename);
        if (!ifs.is_open()) return false;
        std::vector<double> loaded;
        double val;
        while (ifs >> val) loaded.push_back(val);
        ifs.close();
        // 需要先知道参数总数,但我们可以通过已知公式计算
        w_ih_size = INPUT_DIM * HIDDEN_DIM;
        whh_size = HIDDEN_DIM * HIDDEN_DIM;
        who_size = HIDDEN_DIM * OUTPUT_DIM;
        embed_size = NUM_ENTITIES * EMBED_DIM;
        total_params = w_ih_size + whh_size + who_size + embed_size;
        if (loaded.size() != total_params) return false;
        params = loaded;
        return true;
    }

    std::map<std::string, std::vector<double>> get_embed_table() const {
        std::map<std::string, std::vector<double>> table;
        int offset = w_ih_size + whh_size + who_size;
        for (int i = 0; i < NUM_ENTITIES; ++i) {
            std::vector<double> emb(EMBED_DIM);
            for (int d = 0; d < EMBED_DIM; ++d)
                emb[d] = params[offset + i * EMBED_DIM + d];
            table[ENTITIES[i]] = emb;
        }
        return table;
    }

    std::vector<double> forward(const std::vector<double>& input) {
        double* p = params.data();
        double* w_ih = p;
        double* whh = p + w_ih_size;
        double* w_ho = p + w_ih_size + whh_size;

        std::vector<double> v(HIDDEN_DIM, 0.0);
        std::vector<double> s(HIDDEN_DIM, 0.0);

        for (int t = 0; t < TIME_STEPS; ++t) {
            std::vector<double> I_h(HIDDEN_DIM, 0.0);
            for (int j = 0; j < HIDDEN_DIM; ++j) {
                for (int i = 0; i < INPUT_DIM; ++i)
                    I_h[j] += w_ih[i * HIDDEN_DIM + j] * input[i];
                for (int k = 0; k < HIDDEN_DIM; ++k)
                    I_h[j] += whh[k * HIDDEN_DIM + j] * s[k];
            }
            for (int j = 0; j < HIDDEN_DIM; ++j) {
                v[j] = v[j] * LEAK + I_h[j] * DT;
                s[j] = spike_fn(v[j]);
                if (v[j] >= THRESH) v[j] -= THRESH;
                if (v[j] > 2.0) v[j] = 2.0;
                if (v[j] < -1.0) v[j] = -1.0;
            }
        }

        std::vector<double> logits(OUTPUT_DIM, 0.0);
        for (int k = 0; k < OUTPUT_DIM; ++k)
            for (int j = 0; j < HIDDEN_DIM; ++j)
                logits[k] += w_ho[j * OUTPUT_DIM + k] * s[j];

        double max_log = *std::max_element(logits.begin(), logits.end());
        std::vector<double> probs(OUTPUT_DIM);
        double sum = 0.0;
        for (int k = 0; k < OUTPUT_DIM; ++k) {
            probs[k] = std::exp(logits[k] - max_log);
            sum += probs[k];
        }
        for (int k = 0; k < OUTPUT_DIM; ++k) probs[k] /= sum;
        return probs;
    }

    int predict(const std::vector<double>& input) {
        auto probs = forward(input);
        return std::max_element(probs.begin(), probs.end()) - probs.begin();
    }
};

int main() {
    RecurrentSNN snn;
    if (!snn.load_text("snn_learnable_embed_improved.txt")) {
        std::cerr << "无法加载模型文件,请先运行训练程序生成 snn_learnable_embed_improved.txt\n";
        return 1;
    }
    std::cout << "模型加载成功。\n";
    auto embed = snn.get_embed_table();

    // 测试训练样本
    std::vector<Sample> train_samples = {
        {"猫", "哺乳动物", "哺乳动物", "动物", "动物", "生物", 0},
        {"狗", "哺乳动物", "哺乳动物", "动物", "动物", "生物", 1},
        {"人", "哺乳动物", "哺乳动物", "动物", "动物", "生物", 2},
        {"波斯猫", "哺乳动物", "哺乳动物", "动物", "动物", "生物", 0},
        {"猎犬", "哺乳动物", "哺乳动物", "动物", "动物", "生物", 1},
        {"宠物", "哺乳动物", "哺乳动物", "动物", "动物", "生物", 0}
    };
    std::cout << "\n=== 训练样本测试 ===\n";
    for (const auto& samp : train_samples) {
        auto input = encode_input(samp, embed);
        int pred = snn.predict(input);
        std::cout << samp.s1 << "→" << samp.o1 << ", "
                  << samp.s2 << "→" << samp.o2 << ", "
                  << samp.s3 << "→" << samp.o3
                  << " -> pred=" << pred << " (exp=" << samp.label << ")"
                  << (pred == samp.label ? " ✓" : " ✗") << "\n";
    }

    // 泛化测试
    std::vector<Sample> test_samples = {
        {"老虎", "哺乳动物", "哺乳动物", "动物", "动物", "生物", 0},
        {"狼", "哺乳动物", "哺乳动物", "动物", "动物", "生物", 1}
    };
    std::cout << "\n=== 泛化测试(全新实体)===\n";
    for (const auto& samp : test_samples) {
        auto input = encode_input(samp, embed);
        auto probs = snn.forward(input);
        int pred = snn.predict(input);
        std::cout << samp.s1 << "→" << samp.o1 << ", "
                  << samp.s2 << "→" << samp.o2 << ", "
                  << samp.s3 << "→" << samp.o3
                  << " -> pred=" << pred << " (exp=" << samp.label << ")"
                  << (pred == samp.label ? " ✓" : " ✗");
        std::cout << " probs: [" << std::fixed << std::setprecision(3)
                  << probs[0] << "," << probs[1] << "," << probs[2] << "]\n";
    }

    return 0;
}

sengseng@192:~/ir$ ./snn_genetic_final
No saved model found, start training...
Gen 0 best fitness: 0.833333
Gen 1 best fitness: 0.833333
Gen 2 best fitness: 0.833333
Gen 3 best fitness: 0.833333
Gen 4 best fitness: 0.833333
Gen 5 best fitness: 0.833333
Gen 6 best fitness: 0.833333
Gen 7 best fitness: 0.833333
Gen 8 best fitness: 0.833333
Gen 9 best fitness: 0.833333
Gen 10 best fitness: 0.833333
Gen 11 best fitness: 0.833333
Gen 12 best fitness: 1

=== 训练样本测试 ===
猫→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=0 (exp=0) ✓
狗→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=1 (exp=1) ✓
人→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=2 (exp=2) ✓
波斯猫→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=0 (exp=0) ✓
猎犬→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=1 (exp=1) ✓
宠物→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=0 (exp=0)=== 泛化测试(全新实体)===
老虎→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=0 (exp=0) ✓ probs: [0.337,0.332,0.331]
狼→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=1 (exp=1) ✓ probs: [0.324,0.410,0.266]

=== 额外测试(宠物,已在训练集中)===
宠物→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=0 (exp=0) ✓ probs: [0.360,0.311,0.329]
sengseng@192:~/ir$ g++ -std=c++17 -O2 snn_demo_only.cpp -o snn_demo_only
sengseng@192:~/ir$ ./snn_demo_only
模型加载成功。

=== 训练样本测试 ===
猫→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=0 (exp=0) ✓
狗→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=1 (exp=1) ✓
人→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=2 (exp=2) ✓
波斯猫→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=0 (exp=0) ✓
猎犬→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=1 (exp=1) ✓
宠物→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=0 (exp=0)=== 泛化测试(全新实体)===
老虎→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=0 (exp=0) ✓ probs: [0.337,0.332,0.331]
狼→哺乳动物, 哺乳动物→动物, 动物→生物 -> pred=1 (exp=1) ✓ probs: [0.324,0.410,0.266]
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐