ltntorch 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/AGENTS.md +34 -0
- data/CONTRIBUTING.md +66 -0
- data/changelog.md +68 -0
- data/code_of_conduct.md +84 -0
- data/examples/1_binary_classification.rb +108 -0
- data/examples/2_multi_class_single_label_classification.rb +130 -0
- data/examples/3_multi_class_multi_label_classification.rb +158 -0
- data/examples/4_semi_supervised_pattern_recognition.rb +249 -0
- data/examples/5_regression.rb +103 -0
- data/examples/6_clustering.rb +126 -0
- data/examples/7_learning_embeddings_with_ltn.rb +166 -0
- data/examples/datasets/crabs.dat +201 -0
- data/examples/datasets/iris_test.csv +31 -0
- data/examples/datasets/iris_training.csv +121 -0
- data/examples/datasets/readme.md +9 -0
- data/examples/datasets/real-estate.csv +415 -0
- data/examples/support/example.rb +92 -0
- data/examples/support/mnist.rb +77 -0
- data/lib/ltn/connective.rb +56 -0
- data/lib/ltn/constant.rb +30 -0
- data/lib/ltn/diag.rb +54 -0
- data/lib/ltn/function.rb +28 -0
- data/lib/ltn/fuzzy_ops.rb +428 -0
- data/lib/ltn/lambda_model.rb +24 -0
- data/lib/ltn/ltn_object.rb +47 -0
- data/lib/ltn/model_wrapper.rb +72 -0
- data/lib/ltn/predicate.rb +38 -0
- data/lib/ltn/process_ltn_objects.rb +58 -0
- data/lib/ltn/quantifier.rb +176 -0
- data/lib/ltn/variable.rb +49 -0
- data/lib/ltn/version.rb +8 -0
- data/lib/ltn.rb +45 -0
- data/license.md +22 -0
- data/readme.md +166 -0
- data/sig/ltn.rbs +207 -0
- data/tutorials/1_grounding_non_logical_symbols.rb +146 -0
- data/tutorials/2_grounding_connectives.rb +126 -0
- data/tutorials/2b_operators_and_gradients.rb +78 -0
- data/tutorials/3_knowledgebase_and_learning.rb +124 -0
- metadata +94 -0
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
No,X1 transaction date,X2 house age,X3 distance to the nearest MRT station,X4 number of convenience stores,X5 latitude,X6 longitude,Y house price of unit area
|
|
2
|
+
1,2012.917,32,84.87882,10,24.98298,121.54024,37.9
|
|
3
|
+
2,2012.917,19.5,306.5947,9,24.98034,121.53951,42.2
|
|
4
|
+
3,2013.583,13.3,561.9845,5,24.98746,121.54391,47.3
|
|
5
|
+
4,2013.500,13.3,561.9845,5,24.98746,121.54391,54.8
|
|
6
|
+
5,2012.833,5,390.5684,5,24.97937,121.54245,43.1
|
|
7
|
+
6,2012.667,7.1,2175.03,3,24.96305,121.51254,32.1
|
|
8
|
+
7,2012.667,34.5,623.4731,7,24.97933,121.53642,40.3
|
|
9
|
+
8,2013.417,20.3,287.6025,6,24.98042,121.54228,46.7
|
|
10
|
+
9,2013.500,31.7,5512.038,1,24.95095,121.48458,18.8
|
|
11
|
+
10,2013.417,17.9,1783.18,3,24.96731,121.51486,22.1
|
|
12
|
+
11,2013.083,34.8,405.2134,1,24.97349,121.53372,41.4
|
|
13
|
+
12,2013.333,6.3,90.45606,9,24.97433,121.5431,58.1
|
|
14
|
+
13,2012.917,13,492.2313,5,24.96515,121.53737,39.3
|
|
15
|
+
14,2012.667,20.4,2469.645,4,24.96108,121.51046,23.8
|
|
16
|
+
15,2013.500,13.2,1164.838,4,24.99156,121.53406,34.3
|
|
17
|
+
16,2013.583,35.7,579.2083,2,24.9824,121.54619,50.5
|
|
18
|
+
17,2013.250,0,292.9978,6,24.97744,121.54458,70.1
|
|
19
|
+
18,2012.750,17.7,350.8515,1,24.97544,121.53119,37.4
|
|
20
|
+
19,2013.417,16.9,368.1363,8,24.9675,121.54451,42.3
|
|
21
|
+
20,2012.667,1.5,23.38284,7,24.96772,121.54102,47.7
|
|
22
|
+
21,2013.417,4.5,2275.877,3,24.96314,121.51151,29.3
|
|
23
|
+
22,2013.417,10.5,279.1726,7,24.97528,121.54541,51.6
|
|
24
|
+
23,2012.917,14.7,1360.139,1,24.95204,121.54842,24.6
|
|
25
|
+
24,2013.083,10.1,279.1726,7,24.97528,121.54541,47.9
|
|
26
|
+
25,2013.000,39.6,480.6977,4,24.97353,121.53885,38.8
|
|
27
|
+
26,2013.083,29.3,1487.868,2,24.97542,121.51726,27
|
|
28
|
+
27,2012.667,3.1,383.8624,5,24.98085,121.54391,56.2
|
|
29
|
+
28,2013.250,10.4,276.449,5,24.95593,121.53913,33.6
|
|
30
|
+
29,2013.500,19.2,557.478,4,24.97419,121.53797,47
|
|
31
|
+
30,2013.083,7.1,451.2438,5,24.97563,121.54694,57.1
|
|
32
|
+
31,2013.500,25.9,4519.69,0,24.94826,121.49587,22.1
|
|
33
|
+
32,2012.750,29.6,769.4034,7,24.98281,121.53408,25
|
|
34
|
+
33,2012.750,37.9,488.5727,1,24.97349,121.53451,34.2
|
|
35
|
+
34,2013.250,16.5,323.655,6,24.97841,121.54281,49.3
|
|
36
|
+
35,2012.750,15.4,205.367,7,24.98419,121.54243,55.1
|
|
37
|
+
36,2013.500,13.9,4079.418,0,25.01459,121.51816,27.3
|
|
38
|
+
37,2012.917,14.7,1935.009,2,24.96386,121.51458,22.9
|
|
39
|
+
38,2013.167,12,1360.139,1,24.95204,121.54842,25.3
|
|
40
|
+
39,2012.667,3.1,577.9615,6,24.97201,121.54722,47.7
|
|
41
|
+
40,2013.167,16.2,289.3248,5,24.98203,121.54348,46.2
|
|
42
|
+
41,2013.000,13.6,4082.015,0,24.94155,121.50381,15.9
|
|
43
|
+
42,2013.500,16.8,4066.587,0,24.94297,121.50342,18.2
|
|
44
|
+
43,2013.417,36.1,519.4617,5,24.96305,121.53758,34.7
|
|
45
|
+
44,2012.750,34.4,512.7871,6,24.98748,121.54301,34.1
|
|
46
|
+
45,2013.583,2.7,533.4762,4,24.97445,121.54765,53.9
|
|
47
|
+
46,2013.083,36.6,488.8193,8,24.97015,121.54494,38.3
|
|
48
|
+
47,2013.417,21.7,463.9623,9,24.9703,121.54458,42
|
|
49
|
+
48,2013.583,35.9,640.7391,3,24.97563,121.53715,61.5
|
|
50
|
+
49,2013.417,24.2,4605.749,0,24.94684,121.49578,13.4
|
|
51
|
+
50,2012.667,29.4,4510.359,1,24.94925,121.49542,13.2
|
|
52
|
+
51,2013.417,21.7,512.5487,4,24.974,121.53842,44.2
|
|
53
|
+
52,2013.083,31.3,1758.406,1,24.95402,121.55282,20.7
|
|
54
|
+
53,2013.583,32.1,1438.579,3,24.97419,121.5175,27
|
|
55
|
+
54,2013.083,13.3,492.2313,5,24.96515,121.53737,38.9
|
|
56
|
+
55,2013.083,16.1,289.3248,5,24.98203,121.54348,51.7
|
|
57
|
+
56,2012.833,31.7,1160.632,0,24.94968,121.53009,13.7
|
|
58
|
+
57,2013.417,33.6,371.2495,8,24.97254,121.54059,41.9
|
|
59
|
+
58,2012.917,3.5,56.47425,7,24.95744,121.53711,53.5
|
|
60
|
+
59,2013.500,30.3,4510.359,1,24.94925,121.49542,22.6
|
|
61
|
+
60,2013.083,13.3,336.0532,5,24.95776,121.53438,42.4
|
|
62
|
+
61,2013.417,11,1931.207,2,24.96365,121.51471,21.3
|
|
63
|
+
62,2013.500,5.3,259.6607,6,24.97585,121.54516,63.2
|
|
64
|
+
63,2012.917,17.2,2175.877,3,24.96303,121.51254,27.7
|
|
65
|
+
64,2013.583,2.6,533.4762,4,24.97445,121.54765,55
|
|
66
|
+
65,2013.333,17.5,995.7554,0,24.96305,121.54915,25.3
|
|
67
|
+
66,2013.417,40.1,123.7429,8,24.97635,121.54329,44.3
|
|
68
|
+
67,2013.000,1,193.5845,6,24.96571,121.54089,50.7
|
|
69
|
+
68,2013.500,8.5,104.8101,5,24.96674,121.54067,56.8
|
|
70
|
+
69,2013.417,30.4,464.223,6,24.97964,121.53805,36.2
|
|
71
|
+
70,2012.833,12.5,561.9845,5,24.98746,121.54391,42
|
|
72
|
+
71,2013.583,6.6,90.45606,9,24.97433,121.5431,59
|
|
73
|
+
72,2013.083,35.5,640.7391,3,24.97563,121.53715,40.8
|
|
74
|
+
73,2013.583,32.5,424.5442,8,24.97587,121.53913,36.3
|
|
75
|
+
74,2013.167,13.8,4082.015,0,24.94155,121.50381,20
|
|
76
|
+
75,2012.917,6.8,379.5575,10,24.98343,121.53762,54.4
|
|
77
|
+
76,2013.500,12.3,1360.139,1,24.95204,121.54842,29.5
|
|
78
|
+
77,2013.583,35.9,616.4004,3,24.97723,121.53767,36.8
|
|
79
|
+
78,2012.833,20.5,2185.128,3,24.96322,121.51237,25.6
|
|
80
|
+
79,2012.917,38.2,552.4371,2,24.97598,121.53381,29.8
|
|
81
|
+
80,2013.000,18,1414.837,1,24.95182,121.54887,26.5
|
|
82
|
+
81,2013.500,11.8,533.4762,4,24.97445,121.54765,40.3
|
|
83
|
+
82,2013.000,30.8,377.7956,6,24.96427,121.53964,36.8
|
|
84
|
+
83,2013.083,13.2,150.9347,7,24.96725,121.54252,48.1
|
|
85
|
+
84,2012.917,25.3,2707.392,3,24.96056,121.50831,17.7
|
|
86
|
+
85,2013.083,15.1,383.2805,7,24.96735,121.54464,43.7
|
|
87
|
+
86,2012.750,0,338.9679,9,24.96853,121.54413,50.8
|
|
88
|
+
87,2012.833,1.8,1455.798,1,24.9512,121.549,27
|
|
89
|
+
88,2013.583,16.9,4066.587,0,24.94297,121.50342,18.3
|
|
90
|
+
89,2012.917,8.9,1406.43,0,24.98573,121.52758,48
|
|
91
|
+
90,2013.500,23,3947.945,0,24.94783,121.50243,25.3
|
|
92
|
+
91,2012.833,0,274.0144,1,24.9748,121.53059,45.4
|
|
93
|
+
92,2013.250,9.1,1402.016,0,24.98569,121.5276,43.2
|
|
94
|
+
93,2012.917,20.6,2469.645,4,24.96108,121.51046,21.8
|
|
95
|
+
94,2012.917,31.9,1146.329,0,24.9492,121.53076,16.1
|
|
96
|
+
95,2012.917,40.9,167.5989,5,24.9663,121.54026,41
|
|
97
|
+
96,2012.917,8,104.8101,5,24.96674,121.54067,51.8
|
|
98
|
+
97,2013.417,6.4,90.45606,9,24.97433,121.5431,59.5
|
|
99
|
+
98,2013.083,28.4,617.4424,3,24.97746,121.53299,34.6
|
|
100
|
+
99,2013.417,16.4,289.3248,5,24.98203,121.54348,51
|
|
101
|
+
100,2013.417,6.4,90.45606,9,24.97433,121.5431,62.2
|
|
102
|
+
101,2013.500,17.5,964.7496,4,24.98872,121.53411,38.2
|
|
103
|
+
102,2012.833,12.7,170.1289,1,24.97371,121.52984,32.9
|
|
104
|
+
103,2013.083,1.1,193.5845,6,24.96571,121.54089,54.4
|
|
105
|
+
104,2012.750,0,208.3905,6,24.95618,121.53844,45.7
|
|
106
|
+
105,2012.667,32.7,392.4459,6,24.96398,121.5425,30.5
|
|
107
|
+
106,2012.833,0,292.9978,6,24.97744,121.54458,71
|
|
108
|
+
107,2013.083,17.2,189.5181,8,24.97707,121.54308,47.1
|
|
109
|
+
108,2013.333,12.2,1360.139,1,24.95204,121.54842,26.6
|
|
110
|
+
109,2013.417,31.4,592.5006,2,24.9726,121.53561,34.1
|
|
111
|
+
110,2013.583,4,2147.376,3,24.96299,121.51284,28.4
|
|
112
|
+
111,2013.083,8.1,104.8101,5,24.96674,121.54067,51.6
|
|
113
|
+
112,2013.583,33.3,196.6172,7,24.97701,121.54224,39.4
|
|
114
|
+
113,2013.417,9.9,2102.427,3,24.96044,121.51462,23.1
|
|
115
|
+
114,2013.333,14.8,393.2606,6,24.96172,121.53812,7.6
|
|
116
|
+
115,2012.667,30.6,143.8383,8,24.98155,121.54142,53.3
|
|
117
|
+
116,2013.083,20.6,737.9161,2,24.98092,121.54739,46.4
|
|
118
|
+
117,2013.000,30.9,6396.283,1,24.94375,121.47883,12.2
|
|
119
|
+
118,2013.000,13.6,4197.349,0,24.93885,121.50383,13
|
|
120
|
+
119,2013.500,25.3,1583.722,3,24.96622,121.51709,30.6
|
|
121
|
+
120,2013.500,16.6,289.3248,5,24.98203,121.54348,59.6
|
|
122
|
+
121,2013.167,13.3,492.2313,5,24.96515,121.53737,31.3
|
|
123
|
+
122,2013.500,13.6,492.2313,5,24.96515,121.53737,48
|
|
124
|
+
123,2013.250,31.5,414.9476,4,24.98199,121.54464,32.5
|
|
125
|
+
124,2013.417,0,185.4296,0,24.9711,121.5317,45.5
|
|
126
|
+
125,2012.917,9.9,279.1726,7,24.97528,121.54541,57.4
|
|
127
|
+
126,2013.167,1.1,193.5845,6,24.96571,121.54089,48.6
|
|
128
|
+
127,2013.083,38.6,804.6897,4,24.97838,121.53477,62.9
|
|
129
|
+
128,2013.250,3.8,383.8624,5,24.98085,121.54391,55
|
|
130
|
+
129,2013.083,41.3,124.9912,6,24.96674,121.54039,60.7
|
|
131
|
+
130,2013.417,38.5,216.8329,7,24.98086,121.54162,41
|
|
132
|
+
131,2013.250,29.6,535.527,8,24.98092,121.53653,37.5
|
|
133
|
+
132,2013.500,4,2147.376,3,24.96299,121.51284,30.7
|
|
134
|
+
133,2013.167,26.6,482.7581,5,24.97433,121.53863,37.5
|
|
135
|
+
134,2012.833,18,373.3937,8,24.9866,121.54082,39.5
|
|
136
|
+
135,2012.667,33.4,186.9686,6,24.96604,121.54211,42.2
|
|
137
|
+
136,2012.917,18.9,1009.235,0,24.96357,121.54951,20.8
|
|
138
|
+
137,2012.750,11.4,390.5684,5,24.97937,121.54245,46.8
|
|
139
|
+
138,2013.500,13.6,319.0708,6,24.96495,121.54277,47.4
|
|
140
|
+
139,2013.167,10,942.4664,0,24.97843,121.52406,43.5
|
|
141
|
+
140,2012.667,12.9,492.2313,5,24.96515,121.53737,42.5
|
|
142
|
+
141,2013.250,16.2,289.3248,5,24.98203,121.54348,51.4
|
|
143
|
+
142,2013.333,5.1,1559.827,3,24.97213,121.51627,28.9
|
|
144
|
+
143,2013.417,19.8,640.6071,5,24.97017,121.54647,37.5
|
|
145
|
+
144,2013.500,13.6,492.2313,5,24.96515,121.53737,40.1
|
|
146
|
+
145,2013.083,11.9,1360.139,1,24.95204,121.54842,28.4
|
|
147
|
+
146,2012.917,2.1,451.2438,5,24.97563,121.54694,45.5
|
|
148
|
+
147,2012.750,0,185.4296,0,24.9711,121.5317,52.2
|
|
149
|
+
148,2012.750,3.2,489.8821,8,24.97017,121.54494,43.2
|
|
150
|
+
149,2013.500,16.4,3780.59,0,24.93293,121.51203,45.1
|
|
151
|
+
150,2012.667,34.9,179.4538,8,24.97349,121.54245,39.7
|
|
152
|
+
151,2013.250,35.8,170.7311,7,24.96719,121.54269,48.5
|
|
153
|
+
152,2013.500,4.9,387.7721,9,24.98118,121.53788,44.7
|
|
154
|
+
153,2013.333,12,1360.139,1,24.95204,121.54842,28.9
|
|
155
|
+
154,2013.250,6.5,376.1709,6,24.95418,121.53713,40.9
|
|
156
|
+
155,2013.500,16.9,4066.587,0,24.94297,121.50342,20.7
|
|
157
|
+
156,2013.167,13.8,4082.015,0,24.94155,121.50381,15.6
|
|
158
|
+
157,2013.583,30.7,1264.73,0,24.94883,121.52954,18.3
|
|
159
|
+
158,2013.250,16.1,815.9314,4,24.97886,121.53464,35.6
|
|
160
|
+
159,2013.000,11.6,390.5684,5,24.97937,121.54245,39.4
|
|
161
|
+
160,2012.667,15.5,815.9314,4,24.97886,121.53464,37.4
|
|
162
|
+
161,2012.917,3.5,49.66105,8,24.95836,121.53756,57.8
|
|
163
|
+
162,2013.417,19.2,616.4004,3,24.97723,121.53767,39.6
|
|
164
|
+
163,2012.750,16,4066.587,0,24.94297,121.50342,11.6
|
|
165
|
+
164,2013.500,8.5,104.8101,5,24.96674,121.54067,55.5
|
|
166
|
+
165,2012.833,0,185.4296,0,24.9711,121.5317,55.2
|
|
167
|
+
166,2012.917,13.7,1236.564,1,24.97694,121.55391,30.6
|
|
168
|
+
167,2013.417,0,292.9978,6,24.97744,121.54458,73.6
|
|
169
|
+
168,2013.417,28.2,330.0854,8,24.97408,121.54011,43.4
|
|
170
|
+
169,2013.083,27.6,515.1122,5,24.96299,121.5432,37.4
|
|
171
|
+
170,2013.417,8.4,1962.628,1,24.95468,121.55481,23.5
|
|
172
|
+
171,2013.333,24,4527.687,0,24.94741,121.49628,14.4
|
|
173
|
+
172,2013.083,3.6,383.8624,5,24.98085,121.54391,58.8
|
|
174
|
+
173,2013.583,6.6,90.45606,9,24.97433,121.5431,58.1
|
|
175
|
+
174,2013.083,41.3,401.8807,4,24.98326,121.5446,35.1
|
|
176
|
+
175,2013.417,4.3,432.0385,7,24.9805,121.53778,45.2
|
|
177
|
+
176,2013.083,30.2,472.1745,3,24.97005,121.53758,36.5
|
|
178
|
+
177,2012.833,13.9,4573.779,0,24.94867,121.49507,19.2
|
|
179
|
+
178,2013.083,33,181.0766,9,24.97697,121.54262,42
|
|
180
|
+
179,2013.500,13.1,1144.436,4,24.99176,121.53456,36.7
|
|
181
|
+
180,2013.083,14,438.8513,1,24.97493,121.5273,42.6
|
|
182
|
+
181,2012.667,26.9,4449.27,0,24.94898,121.49621,15.5
|
|
183
|
+
182,2013.167,11.6,201.8939,8,24.98489,121.54121,55.9
|
|
184
|
+
183,2013.500,13.5,2147.376,3,24.96299,121.51284,23.6
|
|
185
|
+
184,2013.500,17,4082.015,0,24.94155,121.50381,18.8
|
|
186
|
+
185,2012.750,14.1,2615.465,0,24.95495,121.56174,21.8
|
|
187
|
+
186,2012.750,31.4,1447.286,3,24.97285,121.5173,21.5
|
|
188
|
+
187,2013.167,20.9,2185.128,3,24.96322,121.51237,25.7
|
|
189
|
+
188,2013.000,8.9,3078.176,0,24.95464,121.56627,22
|
|
190
|
+
189,2012.917,34.8,190.0392,8,24.97707,121.54312,44.3
|
|
191
|
+
190,2012.917,16.3,4066.587,0,24.94297,121.50342,20.5
|
|
192
|
+
191,2013.500,35.3,616.5735,8,24.97945,121.53642,42.3
|
|
193
|
+
192,2013.167,13.2,750.0704,2,24.97371,121.54951,37.8
|
|
194
|
+
193,2013.167,43.8,57.58945,7,24.9675,121.54069,42.7
|
|
195
|
+
194,2013.417,9.7,421.479,5,24.98246,121.54477,49.3
|
|
196
|
+
195,2013.500,15.2,3771.895,0,24.93363,121.51158,29.3
|
|
197
|
+
196,2013.333,15.2,461.1016,5,24.95425,121.5399,34.6
|
|
198
|
+
197,2013.000,22.8,707.9067,2,24.981,121.54713,36.6
|
|
199
|
+
198,2013.250,34.4,126.7286,8,24.96881,121.54089,48.2
|
|
200
|
+
199,2013.083,34,157.6052,7,24.96628,121.54196,39.1
|
|
201
|
+
200,2013.417,18.2,451.6419,8,24.96945,121.5449,31.6
|
|
202
|
+
201,2013.417,17.4,995.7554,0,24.96305,121.54915,25.5
|
|
203
|
+
202,2013.417,13.1,561.9845,5,24.98746,121.54391,45.9
|
|
204
|
+
203,2012.917,38.3,642.6985,3,24.97559,121.53713,31.5
|
|
205
|
+
204,2012.667,15.6,289.3248,5,24.98203,121.54348,46.1
|
|
206
|
+
205,2013.000,18,1414.837,1,24.95182,121.54887,26.6
|
|
207
|
+
206,2013.083,12.8,1449.722,3,24.97289,121.51728,21.4
|
|
208
|
+
207,2013.250,22.2,379.5575,10,24.98343,121.53762,44
|
|
209
|
+
208,2013.083,38.5,665.0636,3,24.97503,121.53692,34.2
|
|
210
|
+
209,2012.750,11.5,1360.139,1,24.95204,121.54842,26.2
|
|
211
|
+
210,2012.833,34.8,175.6294,8,24.97347,121.54271,40.9
|
|
212
|
+
211,2013.500,5.2,390.5684,5,24.97937,121.54245,52.2
|
|
213
|
+
212,2013.083,0,274.0144,1,24.9748,121.53059,43.5
|
|
214
|
+
213,2013.333,17.6,1805.665,2,24.98672,121.52091,31.1
|
|
215
|
+
214,2013.083,6.2,90.45606,9,24.97433,121.5431,58
|
|
216
|
+
215,2013.583,18.1,1783.18,3,24.96731,121.51486,20.9
|
|
217
|
+
216,2013.333,19.2,383.7129,8,24.972,121.54477,48.1
|
|
218
|
+
217,2013.250,37.8,590.9292,1,24.97153,121.53559,39.7
|
|
219
|
+
218,2012.917,28,372.6242,6,24.97838,121.54119,40.8
|
|
220
|
+
219,2013.417,13.6,492.2313,5,24.96515,121.53737,43.8
|
|
221
|
+
220,2012.750,29.3,529.7771,8,24.98102,121.53655,40.2
|
|
222
|
+
221,2013.333,37.2,186.5101,9,24.97703,121.54265,78.3
|
|
223
|
+
222,2013.333,9,1402.016,0,24.98569,121.5276,38.5
|
|
224
|
+
223,2013.583,30.6,431.1114,10,24.98123,121.53743,48.5
|
|
225
|
+
224,2013.250,9.1,1402.016,0,24.98569,121.5276,42.3
|
|
226
|
+
225,2013.333,34.5,324.9419,6,24.97814,121.5417,46
|
|
227
|
+
226,2013.250,1.1,193.5845,6,24.96571,121.54089,49
|
|
228
|
+
227,2013.000,16.5,4082.015,0,24.94155,121.50381,12.8
|
|
229
|
+
228,2012.917,32.4,265.0609,8,24.98059,121.53986,40.2
|
|
230
|
+
229,2013.417,11.9,3171.329,0,25.00115,121.51776,46.6
|
|
231
|
+
230,2013.583,31,1156.412,0,24.9489,121.53095,19
|
|
232
|
+
231,2013.500,4,2147.376,3,24.96299,121.51284,33.4
|
|
233
|
+
232,2012.833,16.2,4074.736,0,24.94235,121.50357,14.7
|
|
234
|
+
233,2012.917,27.1,4412.765,1,24.95032,121.49587,17.4
|
|
235
|
+
234,2013.333,39.7,333.3679,9,24.98016,121.53932,32.4
|
|
236
|
+
235,2013.250,8,2216.612,4,24.96007,121.51361,23.9
|
|
237
|
+
236,2012.750,12.9,250.631,7,24.96606,121.54297,39.3
|
|
238
|
+
237,2013.167,3.6,373.8389,10,24.98322,121.53765,61.9
|
|
239
|
+
238,2013.167,13,732.8528,0,24.97668,121.52518,39
|
|
240
|
+
239,2013.083,12.8,732.8528,0,24.97668,121.52518,40.6
|
|
241
|
+
240,2013.500,18.1,837.7233,0,24.96334,121.54767,29.7
|
|
242
|
+
241,2013.083,11,1712.632,2,24.96412,121.5167,28.8
|
|
243
|
+
242,2013.500,13.7,250.631,7,24.96606,121.54297,41.4
|
|
244
|
+
243,2012.833,2,2077.39,3,24.96357,121.51329,33.4
|
|
245
|
+
244,2013.417,32.8,204.1705,8,24.98236,121.53923,48.2
|
|
246
|
+
245,2013.083,4.8,1559.827,3,24.97213,121.51627,21.7
|
|
247
|
+
246,2013.417,7.5,639.6198,5,24.97258,121.54814,40.8
|
|
248
|
+
247,2013.417,16.4,389.8219,6,24.96412,121.54273,40.6
|
|
249
|
+
248,2013.333,21.7,1055.067,0,24.96211,121.54928,23.1
|
|
250
|
+
249,2013.000,19,1009.235,0,24.96357,121.54951,22.3
|
|
251
|
+
250,2012.833,18,6306.153,1,24.95743,121.47516,15
|
|
252
|
+
251,2013.167,39.2,424.7132,7,24.97429,121.53917,30
|
|
253
|
+
252,2012.917,31.7,1159.454,0,24.9496,121.53018,13.8
|
|
254
|
+
253,2012.833,5.9,90.45606,9,24.97433,121.5431,52.7
|
|
255
|
+
254,2012.667,30.4,1735.595,2,24.96464,121.51623,25.9
|
|
256
|
+
255,2012.667,1.1,329.9747,5,24.98254,121.54395,51.8
|
|
257
|
+
256,2013.417,31.5,5512.038,1,24.95095,121.48458,17.4
|
|
258
|
+
257,2012.667,14.6,339.2289,1,24.97519,121.53151,26.5
|
|
259
|
+
258,2013.250,17.3,444.1334,1,24.97501,121.5273,43.9
|
|
260
|
+
259,2013.417,0,292.9978,6,24.97744,121.54458,63.3
|
|
261
|
+
260,2013.083,17.7,837.7233,0,24.96334,121.54767,28.8
|
|
262
|
+
261,2013.250,17,1485.097,4,24.97073,121.517,30.7
|
|
263
|
+
262,2013.167,16.2,2288.011,3,24.95885,121.51359,24.4
|
|
264
|
+
263,2012.917,15.9,289.3248,5,24.98203,121.54348,53
|
|
265
|
+
264,2013.417,3.9,2147.376,3,24.96299,121.51284,31.7
|
|
266
|
+
265,2013.167,32.6,493.657,7,24.96968,121.54522,40.6
|
|
267
|
+
266,2012.833,15.7,815.9314,4,24.97886,121.53464,38.1
|
|
268
|
+
267,2013.250,17.8,1783.18,3,24.96731,121.51486,23.7
|
|
269
|
+
268,2012.833,34.7,482.7581,5,24.97433,121.53863,41.1
|
|
270
|
+
269,2013.417,17.2,390.5684,5,24.97937,121.54245,40.1
|
|
271
|
+
270,2013.000,17.6,837.7233,0,24.96334,121.54767,23
|
|
272
|
+
271,2013.333,10.8,252.5822,1,24.9746,121.53046,117.5
|
|
273
|
+
272,2012.917,17.7,451.6419,8,24.96945,121.5449,26.5
|
|
274
|
+
273,2012.750,13,492.2313,5,24.96515,121.53737,40.5
|
|
275
|
+
274,2013.417,13.2,170.1289,1,24.97371,121.52984,29.3
|
|
276
|
+
275,2013.167,27.5,394.0173,7,24.97305,121.53994,41
|
|
277
|
+
276,2012.667,1.5,23.38284,7,24.96772,121.54102,49.7
|
|
278
|
+
277,2013.000,19.1,461.1016,5,24.95425,121.5399,34
|
|
279
|
+
278,2013.417,21.2,2185.128,3,24.96322,121.51237,27.7
|
|
280
|
+
279,2012.750,0,208.3905,6,24.95618,121.53844,44
|
|
281
|
+
280,2013.417,2.6,1554.25,3,24.97026,121.51642,31.1
|
|
282
|
+
281,2013.250,2.3,184.3302,6,24.96581,121.54086,45.4
|
|
283
|
+
282,2013.333,4.7,387.7721,9,24.98118,121.53788,44.8
|
|
284
|
+
283,2012.917,2,1455.798,1,24.9512,121.549,25.6
|
|
285
|
+
284,2013.417,33.5,1978.671,2,24.98674,121.51844,23.5
|
|
286
|
+
285,2012.917,15,383.2805,7,24.96735,121.54464,34.4
|
|
287
|
+
286,2013.167,30.1,718.2937,3,24.97509,121.53644,55.3
|
|
288
|
+
287,2012.917,5.9,90.45606,9,24.97433,121.5431,56.3
|
|
289
|
+
288,2013.000,19.2,461.1016,5,24.95425,121.5399,32.9
|
|
290
|
+
289,2013.583,16.6,323.6912,6,24.97841,121.5428,51
|
|
291
|
+
290,2013.333,13.9,289.3248,5,24.98203,121.54348,44.5
|
|
292
|
+
291,2013.083,37.7,490.3446,0,24.97217,121.53471,37
|
|
293
|
+
292,2012.833,3.4,56.47425,7,24.95744,121.53711,54.4
|
|
294
|
+
293,2013.083,17.5,395.6747,5,24.95674,121.534,24.5
|
|
295
|
+
294,2012.667,12.6,383.2805,7,24.96735,121.54464,42.5
|
|
296
|
+
295,2013.500,26.4,335.5273,6,24.9796,121.5414,38.1
|
|
297
|
+
296,2013.167,18.2,2179.59,3,24.96299,121.51252,21.8
|
|
298
|
+
297,2012.750,12.5,1144.436,4,24.99176,121.53456,34.1
|
|
299
|
+
298,2012.833,34.9,567.0349,4,24.97003,121.5458,28.5
|
|
300
|
+
299,2013.333,16.7,4082.015,0,24.94155,121.50381,16.7
|
|
301
|
+
300,2013.167,33.2,121.7262,10,24.98178,121.54059,46.1
|
|
302
|
+
301,2013.083,2.5,156.2442,4,24.96696,121.53992,36.9
|
|
303
|
+
302,2012.750,38,461.7848,0,24.97229,121.53445,35.7
|
|
304
|
+
303,2013.500,16.5,2288.011,3,24.95885,121.51359,23.2
|
|
305
|
+
304,2013.500,38.3,439.7105,0,24.97161,121.53423,38.4
|
|
306
|
+
305,2013.417,20,1626.083,3,24.96622,121.51668,29.4
|
|
307
|
+
306,2013.083,16.2,289.3248,5,24.98203,121.54348,55
|
|
308
|
+
307,2013.500,14.4,169.9803,1,24.97369,121.52979,50.2
|
|
309
|
+
308,2012.833,10.3,3079.89,0,24.9546,121.56627,24.7
|
|
310
|
+
309,2013.417,16.4,289.3248,5,24.98203,121.54348,53
|
|
311
|
+
310,2013.250,30.3,1264.73,0,24.94883,121.52954,19.1
|
|
312
|
+
311,2013.583,16.4,1643.499,2,24.95394,121.55174,24.7
|
|
313
|
+
312,2013.167,21.3,537.7971,4,24.97425,121.53814,42.2
|
|
314
|
+
313,2013.583,35.4,318.5292,9,24.97071,121.54069,78
|
|
315
|
+
314,2013.333,8.3,104.8101,5,24.96674,121.54067,42.8
|
|
316
|
+
315,2013.250,3.7,577.9615,6,24.97201,121.54722,41.6
|
|
317
|
+
316,2013.083,15.6,1756.411,2,24.9832,121.51812,27.3
|
|
318
|
+
317,2013.250,13.3,250.631,7,24.96606,121.54297,42
|
|
319
|
+
318,2012.750,15.6,752.7669,2,24.97795,121.53451,37.5
|
|
320
|
+
319,2013.333,7.1,379.5575,10,24.98343,121.53762,49.8
|
|
321
|
+
320,2013.250,34.6,272.6783,5,24.95562,121.53872,26.9
|
|
322
|
+
321,2012.750,13.5,4197.349,0,24.93885,121.50383,18.6
|
|
323
|
+
322,2012.917,16.9,964.7496,4,24.98872,121.53411,37.7
|
|
324
|
+
323,2013.000,12.9,187.4823,1,24.97388,121.52981,33.1
|
|
325
|
+
324,2013.417,28.6,197.1338,6,24.97631,121.54436,42.5
|
|
326
|
+
325,2012.667,12.4,1712.632,2,24.96412,121.5167,31.3
|
|
327
|
+
326,2013.083,36.6,488.8193,8,24.97015,121.54494,38.1
|
|
328
|
+
327,2013.500,4.1,56.47425,7,24.95744,121.53711,62.1
|
|
329
|
+
328,2013.417,3.5,757.3377,3,24.97538,121.54971,36.7
|
|
330
|
+
329,2012.833,15.9,1497.713,3,24.97003,121.51696,23.6
|
|
331
|
+
330,2013.000,13.6,4197.349,0,24.93885,121.50383,19.2
|
|
332
|
+
331,2013.083,32,1156.777,0,24.94935,121.53046,12.8
|
|
333
|
+
332,2013.333,25.6,4519.69,0,24.94826,121.49587,15.6
|
|
334
|
+
333,2013.167,39.8,617.7134,2,24.97577,121.53475,39.6
|
|
335
|
+
334,2012.750,7.8,104.8101,5,24.96674,121.54067,38.4
|
|
336
|
+
335,2012.917,30,1013.341,5,24.99006,121.5346,22.8
|
|
337
|
+
336,2013.583,27.3,337.6016,6,24.96431,121.54063,36.5
|
|
338
|
+
337,2012.833,5.1,1867.233,2,24.98407,121.51748,35.6
|
|
339
|
+
338,2012.833,31.3,600.8604,5,24.96871,121.54651,30.9
|
|
340
|
+
339,2012.917,31.5,258.186,9,24.96867,121.54331,36.3
|
|
341
|
+
340,2013.333,1.7,329.9747,5,24.98254,121.54395,50.4
|
|
342
|
+
341,2013.333,33.6,270.8895,0,24.97281,121.53265,42.9
|
|
343
|
+
342,2013.000,13,750.0704,2,24.97371,121.54951,37
|
|
344
|
+
343,2012.667,5.7,90.45606,9,24.97433,121.5431,53.5
|
|
345
|
+
344,2013.000,33.5,563.2854,8,24.98223,121.53597,46.6
|
|
346
|
+
345,2013.500,34.6,3085.17,0,24.998,121.5155,41.2
|
|
347
|
+
346,2012.667,0,185.4296,0,24.9711,121.5317,37.9
|
|
348
|
+
347,2013.417,13.2,1712.632,2,24.96412,121.5167,30.8
|
|
349
|
+
348,2013.583,17.4,6488.021,1,24.95719,121.47353,11.2
|
|
350
|
+
349,2012.833,4.6,259.6607,6,24.97585,121.54516,53.7
|
|
351
|
+
350,2012.750,7.8,104.8101,5,24.96674,121.54067,47
|
|
352
|
+
351,2013.000,13.2,492.2313,5,24.96515,121.53737,42.3
|
|
353
|
+
352,2012.833,4,2180.245,3,24.96324,121.51241,28.6
|
|
354
|
+
353,2012.833,18.4,2674.961,3,24.96143,121.50827,25.7
|
|
355
|
+
354,2013.500,4.1,2147.376,3,24.96299,121.51284,31.3
|
|
356
|
+
355,2013.417,12.2,1360.139,1,24.95204,121.54842,30.1
|
|
357
|
+
356,2013.250,3.8,383.8624,5,24.98085,121.54391,60.7
|
|
358
|
+
357,2012.833,10.3,211.4473,1,24.97417,121.52999,45.3
|
|
359
|
+
358,2013.417,0,338.9679,9,24.96853,121.54413,44.9
|
|
360
|
+
359,2013.167,1.1,193.5845,6,24.96571,121.54089,45.1
|
|
361
|
+
360,2013.500,5.6,2408.993,0,24.95505,121.55964,24.7
|
|
362
|
+
361,2012.667,32.9,87.30222,10,24.983,121.54022,47.1
|
|
363
|
+
362,2013.083,41.4,281.205,8,24.97345,121.54093,63.3
|
|
364
|
+
363,2013.417,17.1,967.4,4,24.98872,121.53408,40
|
|
365
|
+
364,2013.500,32.3,109.9455,10,24.98182,121.54086,48
|
|
366
|
+
365,2013.417,35.3,614.1394,7,24.97913,121.53666,33.1
|
|
367
|
+
366,2012.917,17.3,2261.432,4,24.96182,121.51222,29.5
|
|
368
|
+
367,2012.750,14.2,1801.544,1,24.95153,121.55254,24.8
|
|
369
|
+
368,2012.833,15,1828.319,2,24.96464,121.51531,20.9
|
|
370
|
+
369,2013.417,18.2,350.8515,1,24.97544,121.53119,43.1
|
|
371
|
+
370,2012.667,20.2,2185.128,3,24.96322,121.51237,22.8
|
|
372
|
+
371,2012.750,15.9,289.3248,5,24.98203,121.54348,42.1
|
|
373
|
+
372,2013.500,4.1,312.8963,5,24.95591,121.53956,51.7
|
|
374
|
+
373,2013.000,33.9,157.6052,7,24.96628,121.54196,41.5
|
|
375
|
+
374,2013.083,0,274.0144,1,24.9748,121.53059,52.2
|
|
376
|
+
375,2013.250,5.4,390.5684,5,24.97937,121.54245,49.5
|
|
377
|
+
376,2013.250,21.7,1157.988,0,24.96165,121.55011,23.8
|
|
378
|
+
377,2013.417,14.7,1717.193,2,24.96447,121.51649,30.5
|
|
379
|
+
378,2013.333,3.9,49.66105,8,24.95836,121.53756,56.8
|
|
380
|
+
379,2013.333,37.3,587.8877,8,24.97077,121.54634,37.4
|
|
381
|
+
380,2013.333,0,292.9978,6,24.97744,121.54458,69.7
|
|
382
|
+
381,2013.333,14.1,289.3248,5,24.98203,121.54348,53.3
|
|
383
|
+
382,2013.417,8,132.5469,9,24.98298,121.53981,47.3
|
|
384
|
+
383,2013.000,16.3,3529.564,0,24.93207,121.51597,29.3
|
|
385
|
+
384,2012.667,29.1,506.1144,4,24.97845,121.53889,40.3
|
|
386
|
+
385,2012.750,16.1,4066.587,0,24.94297,121.50342,12.9
|
|
387
|
+
386,2013.000,18.3,82.88643,10,24.983,121.54026,46.6
|
|
388
|
+
387,2012.833,0,185.4296,0,24.9711,121.5317,55.3
|
|
389
|
+
388,2013.250,16.2,2103.555,3,24.96042,121.51462,25.6
|
|
390
|
+
389,2013.500,10.4,2251.938,4,24.95957,121.51353,27.3
|
|
391
|
+
390,2013.250,40.9,122.3619,8,24.96756,121.5423,67.7
|
|
392
|
+
391,2013.500,32.8,377.8302,9,24.97151,121.5435,38.6
|
|
393
|
+
392,2013.583,6.2,1939.749,1,24.95155,121.55387,31.3
|
|
394
|
+
393,2013.083,42.7,443.802,6,24.97927,121.53874,35.3
|
|
395
|
+
394,2013.000,16.9,967.4,4,24.98872,121.53408,40.3
|
|
396
|
+
395,2013.500,32.6,4136.271,1,24.95544,121.4963,24.7
|
|
397
|
+
396,2012.917,21.2,512.5487,4,24.974,121.53842,42.5
|
|
398
|
+
397,2012.667,37.1,918.6357,1,24.97198,121.55063,31.9
|
|
399
|
+
398,2013.417,13.1,1164.838,4,24.99156,121.53406,32.2
|
|
400
|
+
399,2013.417,14.7,1717.193,2,24.96447,121.51649,23
|
|
401
|
+
400,2012.917,12.7,170.1289,1,24.97371,121.52984,37.3
|
|
402
|
+
401,2013.250,26.8,482.7581,5,24.97433,121.53863,35.5
|
|
403
|
+
402,2013.083,7.6,2175.03,3,24.96305,121.51254,27.7
|
|
404
|
+
403,2012.833,12.7,187.4823,1,24.97388,121.52981,28.5
|
|
405
|
+
404,2012.667,30.9,161.942,9,24.98353,121.53966,39.7
|
|
406
|
+
405,2013.333,16.4,289.3248,5,24.98203,121.54348,41.2
|
|
407
|
+
406,2012.667,23,130.9945,6,24.95663,121.53765,37.2
|
|
408
|
+
407,2013.167,1.9,372.1386,7,24.97293,121.54026,40.5
|
|
409
|
+
408,2013.000,5.2,2408.993,0,24.95505,121.55964,22.3
|
|
410
|
+
409,2013.417,18.5,2175.744,3,24.9633,121.51243,28.1
|
|
411
|
+
410,2013.000,13.7,4082.015,0,24.94155,121.50381,15.4
|
|
412
|
+
411,2012.667,5.6,90.45606,9,24.97433,121.5431,50
|
|
413
|
+
412,2013.250,18.8,390.9696,7,24.97923,121.53986,40.6
|
|
414
|
+
413,2013.000,8.1,104.8101,5,24.96674,121.54067,52.5
|
|
415
|
+
414,2013.500,6.5,90.45606,9,24.97433,121.5431,63.9
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Released under the MIT License.
|
|
4
|
+
# Copyright, 2026, by Mauro Tortonesi.
|
|
5
|
+
|
|
6
|
+
require "ltn"
|
|
7
|
+
|
|
8
|
+
# Shared helpers of the examples, which are ports of LTNtorch's
|
|
9
|
+
# (https://github.com/tommasocarraro/LTNtorch/tree/main/examples).
|
|
10
|
+
#
|
|
11
|
+
# Each example is a class whose #run trains the model and returns its final
|
|
12
|
+
# metrics, so that benchmark/examples.rb and test/examples.rb can run it with
|
|
13
|
+
# fewer epochs; run as a script, it trains with LTNtorch's settings and
|
|
14
|
+
# prints its progress.
|
|
15
|
+
module LTNExamples
|
|
16
|
+
DATASETS = File.expand_path("../datasets", __dir__)
|
|
17
|
+
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
# Mini-batches of rows of `tensors`, which share their first dimension, as
|
|
21
|
+
# LTNtorch's examples' DataLoader yields them.
|
|
22
|
+
#
|
|
23
|
+
# @parameter tensors [Array(Torch::Tensor)] the data, e.g. features and labels.
|
|
24
|
+
# @parameter batch_size [Integer] the number of rows per batch (the last batch may be smaller).
|
|
25
|
+
# @parameter shuffle [Boolean] whether to shuffle the rows, with torch's RNG.
|
|
26
|
+
# @returns [Array(Array(Torch::Tensor))] one array of tensors per batch.
|
|
27
|
+
def batches(tensors, batch_size:, shuffle:)
|
|
28
|
+
n = tensors.first.shape[0]
|
|
29
|
+
order = shuffle ? Torch.randperm(n) : Torch.arange(n)
|
|
30
|
+
(0...n).step(batch_size).map do |start|
|
|
31
|
+
indices = order[start...[start + batch_size, n].min]
|
|
32
|
+
tensors.map { |tensor| tensor[indices] }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Reads a table of numbers from a text file with a header line.
|
|
37
|
+
#
|
|
38
|
+
# @parameter name [String] the file name, in examples/datasets.
|
|
39
|
+
# @parameter separator [String, Regexp] the column separator.
|
|
40
|
+
# @returns [Array(Array(String), Array(Array(String)))] the header and the rows, as strings.
|
|
41
|
+
def read_table(name, separator: ",")
|
|
42
|
+
header, *rows = File.readlines(File.join(DATASETS, name), chomp: true).reject(&:empty?)
|
|
43
|
+
split = ->(line) { line.strip.split(separator) }
|
|
44
|
+
[split.call(header), rows.map(&split)]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Seeds Ruby's and torch's random number generators.
|
|
48
|
+
def seed(seed)
|
|
49
|
+
srand(seed)
|
|
50
|
+
Torch.manual_seed(seed)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Fraction of equal elements of two tensors.
|
|
54
|
+
def accuracy(predictions, labels)
|
|
55
|
+
predictions.eq(labels).float.mean.item
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# A scatter plot in plain text, each series drawn with its own character,
|
|
59
|
+
# so it reads the same with or without colors.
|
|
60
|
+
#
|
|
61
|
+
# @parameter series [Hash(String, Array)] for each series name, its marker
|
|
62
|
+
# character and an n x 2 tensor of points, e.g. `{"A" => ["#", points]}`.
|
|
63
|
+
# @parameter title [String] the title.
|
|
64
|
+
# @parameter xlim [Array(Float)] the range of x.
|
|
65
|
+
# @parameter ylim [Array(Float)] the range of y.
|
|
66
|
+
# @returns [String] the plot.
|
|
67
|
+
def scatter(series, title:, xlim: [0.0, 1.0], ylim: [0.0, 1.0], width: 48, height: 18)
|
|
68
|
+
grid = Array.new(height) { Array.new(width, " ") }
|
|
69
|
+
series.each_value do |marker, points|
|
|
70
|
+
points.to_a.each do |x, y|
|
|
71
|
+
column = ((x - xlim[0]) / (xlim[1] - xlim[0]) * (width - 1)).round
|
|
72
|
+
row = ((ylim[1] - y) / (ylim[1] - ylim[0]) * (height - 1)).round
|
|
73
|
+
grid[row][column] = marker if row.between?(0, height - 1) && column.between?(0, width - 1)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
legend = series.map { |name, (marker, _)| "#{marker} #{name}" }.join(", ")
|
|
77
|
+
border = "+#{"-" * width}+"
|
|
78
|
+
[title, border, *grid.map { |row| "|#{row.join}|" }, border,
|
|
79
|
+
format("x: %.1f..%.1f, y: %.1f..%.1f; %s", *xlim, *ylim, legend)].join("\n")
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Runs `example` with the defaults of LTNtorch's version when the file is
|
|
83
|
+
# executed as a script, then yields it (e.g. to plot its results).
|
|
84
|
+
def main(example, file, **options)
|
|
85
|
+
return unless File.expand_path($PROGRAM_NAME) == File.expand_path(file)
|
|
86
|
+
|
|
87
|
+
epochs = ENV["EPOCHS"]&.then { |value| Integer(value) }
|
|
88
|
+
metrics = epochs ? example.run(epochs: epochs, **options) : example.run(**options)
|
|
89
|
+
puts "Final metrics: #{metrics.transform_values { |v| v.is_a?(Float) ? v.round(3) : v }}"
|
|
90
|
+
yield example if block_given?
|
|
91
|
+
end
|
|
92
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Released under the MIT License.
|
|
4
|
+
# Copyright, 2026, by Mauro Tortonesi.
|
|
5
|
+
|
|
6
|
+
require "fileutils"
|
|
7
|
+
require "net/http"
|
|
8
|
+
require "zlib"
|
|
9
|
+
require "torch"
|
|
10
|
+
|
|
11
|
+
module LTNExamples
|
|
12
|
+
# Downloads MNIST into examples/datasets/MNIST on first use (from the
|
|
13
|
+
# mirror torchvision uses), and loads it as tensors: images as uint8
|
|
14
|
+
# tensors of shape n x 28 x 28, labels as int64 tensors of size n. The
|
|
15
|
+
# tensors are cached as a .pth file, since converting the raw bytes takes
|
|
16
|
+
# a while.
|
|
17
|
+
module MNIST
|
|
18
|
+
MIRROR = "https://ossci-datasets.s3.amazonaws.com/mnist/"
|
|
19
|
+
FILES = {
|
|
20
|
+
train: %w[train-images-idx3-ubyte.gz train-labels-idx1-ubyte.gz],
|
|
21
|
+
test: %w[t10k-images-idx3-ubyte.gz t10k-labels-idx1-ubyte.gz]
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
def directory
|
|
27
|
+
File.join(LTNExamples::DATASETS, "MNIST")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @parameter split [Symbol] :train or :test.
|
|
31
|
+
# @returns [Array(Torch::Tensor)] the images and the labels.
|
|
32
|
+
def load(split)
|
|
33
|
+
cache = File.join(directory, "#{split}.pth")
|
|
34
|
+
if File.exist?(cache)
|
|
35
|
+
tensors = Torch.load(cache)
|
|
36
|
+
return [tensors["images"], tensors["labels"]]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
images_file, labels_file = FILES.fetch(split).map { |name| download(name) }
|
|
40
|
+
images = idx_tensor(images_file)
|
|
41
|
+
labels = idx_tensor(labels_file).long
|
|
42
|
+
Torch.save({"images" => images, "labels" => labels}, cache)
|
|
43
|
+
[images, labels]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def download(name)
|
|
47
|
+
path = File.join(directory, name)
|
|
48
|
+
return path if File.exist?(path)
|
|
49
|
+
|
|
50
|
+
FileUtils.mkdir_p(directory)
|
|
51
|
+
warn "Downloading #{MIRROR}#{name}"
|
|
52
|
+
response = Net::HTTP.get_response(URI("#{MIRROR}#{name}"))
|
|
53
|
+
raise "Could not download #{name}: HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess)
|
|
54
|
+
|
|
55
|
+
File.binwrite(path, response.body)
|
|
56
|
+
path
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Parses an IDX file of unsigned bytes (the format of MNIST's files).
|
|
60
|
+
def idx_tensor(path)
|
|
61
|
+
bytes = Zlib::GzipReader.open(path, &:read)
|
|
62
|
+
magic, = bytes.unpack("N")
|
|
63
|
+
raise "#{path}: not an IDX file of unsigned bytes" unless (magic >> 8) == 0x08
|
|
64
|
+
|
|
65
|
+
dimensions = magic & 0xff
|
|
66
|
+
shape = bytes.unpack("N#{dimensions}", offset: 4)
|
|
67
|
+
data = bytes.byteslice(4 + 4 * dimensions, shape.inject(:*))
|
|
68
|
+
# Converted in chunks, to bound the size of the intermediate Ruby arrays.
|
|
69
|
+
row = shape.drop(1).inject(1, :*)
|
|
70
|
+
chunks = (0...shape[0]).step(1000).map do |start|
|
|
71
|
+
count = [1000, shape[0] - start].min
|
|
72
|
+
Torch.tensor(data.byteslice(start * row, count * row).unpack("C*"), dtype: :uint8).reshape([count] + shape.drop(1))
|
|
73
|
+
end
|
|
74
|
+
Torch.cat(chunks)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Released under the MIT License.
|
|
4
|
+
# Copyright, 2026, by Mauro Tortonesi.
|
|
5
|
+
# Ported from LTNtorch, copyright 2021-2024 by Tommaso Carraro.
|
|
6
|
+
|
|
7
|
+
module LTN
|
|
8
|
+
# An LTN connective: applies a fuzzy connective operator (see LTN::FuzzyOps)
|
|
9
|
+
# to LTN objects, after broadcasting them (see LTN.process_ltn_objects).
|
|
10
|
+
#
|
|
11
|
+
# and_ = LTN::Connective.new(LTN::FuzzyOps::AndProd.new)
|
|
12
|
+
# and_.call(p.call(x), q.call(y)) # free_vars ["x", "y"]
|
|
13
|
+
class Connective
|
|
14
|
+
# @attribute [FuzzyOps::ConnectiveOperator] the operator.
|
|
15
|
+
attr_reader :connective_op
|
|
16
|
+
|
|
17
|
+
# @parameter connective_op [FuzzyOps::ConnectiveOperator] the operator.
|
|
18
|
+
# @raises [TypeError] if `connective_op` is not a connective operator.
|
|
19
|
+
def initialize(connective_op)
|
|
20
|
+
unless connective_op.is_a?(FuzzyOps::ConnectiveOperator)
|
|
21
|
+
raise TypeError, "Connective.new: argument 'connective_op' (position 1) must be a " \
|
|
22
|
+
"LTN::FuzzyOps::ConnectiveOperator, not #{connective_op.class}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
@connective_op = connective_op
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @parameter operands [Array(LTNObject)] one operand for a unary operator, two for a binary one.
|
|
29
|
+
# @parameter kwargs [Hash] passed to the operator, e.g. `stable: false`. (LTNtorch ignores them.)
|
|
30
|
+
# @returns [LTNObject] the result, with one axis per free variable of the operands.
|
|
31
|
+
# @raises [ArgumentError] if the number of operands does not match the
|
|
32
|
+
# operator, or their values are not in [0, 1].
|
|
33
|
+
# @raises [TypeError] if an operand is not an LTNObject.
|
|
34
|
+
def call(*operands, **kwargs)
|
|
35
|
+
if connective_op.is_a?(FuzzyOps::UnaryConnectiveOperator) && operands.size != 1
|
|
36
|
+
raise ArgumentError, "Expected one operand for a unary connective, but got #{operands.size}"
|
|
37
|
+
end
|
|
38
|
+
if connective_op.is_a?(FuzzyOps::BinaryConnectiveOperator) && operands.size != 2
|
|
39
|
+
raise ArgumentError, "Expected two operands for a binary connective, but got #{operands.size}"
|
|
40
|
+
end
|
|
41
|
+
unless operands.all?(LTNObject)
|
|
42
|
+
raise TypeError, "Expected parameter 'operands' to be LTNObjects, but got #{operands.map(&:class).inspect}"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
FuzzyOps.check_values(*operands.map(&:value))
|
|
46
|
+
processed, vars, n_individuals_per_var = LTN.process_ltn_objects(operands)
|
|
47
|
+
output = connective_op.call(*processed.map(&:value), **kwargs)
|
|
48
|
+
LTNObject.new(output.reshape(n_individuals_per_var), vars)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def inspect
|
|
52
|
+
"Connective(connective_op=#{connective_op.inspect})"
|
|
53
|
+
end
|
|
54
|
+
alias_method :to_s, :inspect
|
|
55
|
+
end
|
|
56
|
+
end
|
data/lib/ltn/constant.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Released under the MIT License.
|
|
4
|
+
# Copyright, 2026, by Mauro Tortonesi.
|
|
5
|
+
# Ported from LTNtorch, copyright 2021-2024 by Tommaso Carraro.
|
|
6
|
+
|
|
7
|
+
module LTN
|
|
8
|
+
# An LTN constant: an individual of the domain, grounded as a tensor with no
|
|
9
|
+
# free variables. A trainable constant is an embedding learned along with
|
|
10
|
+
# the predicates and functions of the knowledge base.
|
|
11
|
+
class Constant < LTNObject
|
|
12
|
+
# @parameter value [Torch::Tensor] the grounding, moved to LTN.device.
|
|
13
|
+
# @parameter trainable [Boolean] whether to learn the grounding. A
|
|
14
|
+
# trainable constant is converted to float32 and requires grad.
|
|
15
|
+
# @raises [TypeError] if `value` is not a tensor.
|
|
16
|
+
def initialize(value, trainable: false)
|
|
17
|
+
super(value, [])
|
|
18
|
+
@value = @value.to(LTN.device)
|
|
19
|
+
return unless trainable
|
|
20
|
+
|
|
21
|
+
@value = @value.float
|
|
22
|
+
@value.requires_grad!(true)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def inspect
|
|
26
|
+
"Constant(value=#{value.inspect}, free_vars=#{free_vars.inspect})"
|
|
27
|
+
end
|
|
28
|
+
alias_method :to_s, :inspect
|
|
29
|
+
end
|
|
30
|
+
end
|