Accuracy in metric learning: Angular loss

S_M: <p>Hi I have tried <a href="https://github.com/tomp11/metric_learning" rel="noopener nofollow ugc">angular loss</a>. The results have been reported by loss but I need accuracy so the following code added <a href="https://github.com/tomp11/metric_learning/blob/2a9ce38522d779e98613189db97ef539fa96ddf0/src/n_pair_train.py#L88" rel="noopener nofollow ugc">here</a></p>
<blockquote>
<pre><code>        predicy = torch.max(embedded, 1)[1].data.squeeze()
        acc = (predicy == target).sum().item() / float(target.size(0))
</code></pre>
</blockquote>
<p>The accuracy was low and it had a descending behavior. I think the accuracy has to compute in another way. How can I do that?<br>
any suggestion would be appreciated.</p>
<pre><code class="lang-auto">Train Epoch:   1 [    0/48600 (  0%)]	train_loss: 28.2820	val_loss: 8.0892	 acc:0.0000
Train Epoch:   1 [ 1600/48600 (  3%)]	train_loss: 6.9128	val_loss: 6.9078	 acc:0.8333
Train Epoch:   1 [ 3200/48600 (  7%)]	train_loss: 6.9110	val_loss: 6.9078	 acc:1.7378
Train Epoch:   1 [ 4800/48600 ( 10%)]	train_loss: 6.9079	val_loss: 6.9078	 acc:2.1107
Train Epoch:   1 [ 6400/48600 ( 13%)]	train_loss: 6.9061	val_loss: 6.9021	 acc:2.4691
Train Epoch:   1 [ 8000/48600 ( 16%)]	train_loss: 6.8372	val_loss: 6.8911	 acc:2.9455
Train Epoch:   1 [ 9600/48600 ( 20%)]	train_loss: 6.8924	val_loss: 6.8379	 acc:2.9752
Train Epoch:   1 [11200/48600 ( 23%)]	train_loss: 6.6970	val_loss: 6.5565	 acc:3.0319
Train Epoch:   1 [12800/48600 ( 26%)]	train_loss: 6.3550	val_loss: 6.4852	 acc:3.0590
Train Epoch:   1 [14400/48600 ( 30%)]	train_loss: 6.9291	val_loss: 6.4815	 acc:3.0732
Train Epoch:   1 [16000/48600 ( 33%)]	train_loss: 6.4415	val_loss: 6.3958	 acc:3.1032
Train Epoch:   1 [17600/48600 ( 36%)]	train_loss: 6.3201	val_loss: 6.4022	 acc:3.1618
Train Epoch:   1 [19200/48600 ( 40%)]	train_loss: 7.9138	val_loss: 6.2496	 acc:3.1639
Train Epoch:   1 [20800/48600 ( 43%)]	train_loss: 6.5684	val_loss: 6.3088	 acc:3.1561
Train Epoch:   1 [22400/48600 ( 46%)]	train_loss: 6.4354	val_loss: 6.5117	 acc:3.1539
Train Epoch:   1 [24000/48600 ( 49%)]	train_loss: 6.3263	val_loss: 6.3474	 acc:3.1520
Train Epoch:   1 [25600/48600 ( 53%)]	train_loss: 6.3033	val_loss: 6.2607	 acc:3.1347
Train Epoch:   1 [27200/48600 ( 56%)]	train_loss: 7.0122	val_loss: 6.6542	 acc:3.0865
Train Epoch:   1 [28800/48600 ( 59%)]	train_loss: 6.6477	val_loss: 6.7577	 acc:2.9882
Train Epoch:   1 [30400/48600 ( 63%)]	train_loss: 6.4013	val_loss: 6.7127	 acc:2.9068
Train Epoch:   1 [32000/48600 ( 66%)]	train_loss: 6.0408	val_loss: 5.8954	 acc:2.8180
Train Epoch:   1 [33600/48600 ( 69%)]	train_loss: 6.3112	val_loss: 5.9389	 acc:2.7049
Train Epoch:   1 [35200/48600 ( 72%)]	train_loss: 6.2653	val_loss: 6.3091	 acc:2.6446
Train Epoch:   1 [36800/48600 ( 76%)]	train_loss: 6.4967	val_loss: 6.2455	 acc:2.6546
Train Epoch:   1 [38400/48600 ( 79%)]	train_loss: 7.1699	val_loss: 6.2683	 acc:2.6923
Train Epoch:   1 [40000/48600 ( 82%)]	train_loss: 6.9097	val_loss: 6.7606	 acc:2.7246
Train Epoch:   1 [41600/48600 ( 86%)]	train_loss: 6.3366	val_loss: 6.3285	 acc:2.6775
Train Epoch:   1 [43200/48600 ( 89%)]	train_loss: 6.2499	val_loss: 6.0655	 acc:2.6317
Train Epoch:   1 [44800/48600 ( 92%)]	train_loss: 7.1336	val_loss: 5.7625	 acc:2.5691
Train Epoch:   1 [46400/48600 ( 96%)]	train_loss: 5.9713	val_loss: 6.0712	 acc:2.5194
Train Epoch:   1 [48000/48600 ( 99%)]	train_loss: 5.8089	val_loss: 5.9169	 acc:2.4688
Train Epoch:   2 [    0/48600 (  0%)]	train_loss: 6.2228	val_loss: 6.3985	 acc:0.0000
Train Epoch:   2 [ 1600/48600 (  3%)]	train_loss: 6.3038	val_loss: 5.3646	 acc:1.1310
Train Epoch:   2 [ 3200/48600 (  7%)]	train_loss: 6.0695	val_loss: 6.5455	 acc:1.1890
Train Epoch:   2 [ 4800/48600 ( 10%)]	train_loss: 6.3791	val_loss: 6.8738	 acc:1.0451
Train Epoch:   2 [ 6400/48600 ( 13%)]	train_loss: 7.1383	val_loss: 5.7615	 acc:0.9877
Train Epoch:   2 [ 8000/48600 ( 16%)]	train_loss: 6.0784	val_loss: 5.4069	 acc:0.9777
Train Epoch:   2 [ 9600/48600 ( 20%)]	train_loss: 5.9274	val_loss: 6.2934	 acc:0.9401
Train Epoch:   2 [11200/48600 ( 23%)]	train_loss: 6.2468	val_loss: 6.4788	 acc:0.8865
Train Epoch:   2 [12800/48600 ( 26%)]	train_loss: 5.4620	val_loss: 5.8104	 acc:0.8385
Train Epoch:   2 [14400/48600 ( 30%)]	train_loss: 5.8782	val_loss: 5.9951	 acc:0.7735
Train Epoch:   2 [16000/48600 ( 33%)]	train_loss: 5.6917	val_loss: 6.0670	 acc:0.7649
Train Epoch:   2 [17600/48600 ( 36%)]	train_loss: 6.7636	val_loss: 6.5156	 acc:0.7353
Train Epoch:   2 [19200/48600 ( 40%)]	train_loss: 6.0429	val_loss: 5.4953	 acc:0.7624
Train Epoch:   2 [20800/48600 ( 43%)]	train_loss: 5.4111	val_loss: 5.5173	 acc:0.7615
Train Epoch:   2 [22400/48600 ( 46%)]	train_loss: 5.7747	val_loss: 5.2676	 acc:0.7295
Train Epoch:   2 [24000/48600 ( 49%)]	train_loss: 5.5873	val_loss: 5.3546	 acc:0.6977
Train Epoch:   2 [25600/48600 ( 53%)]	train_loss: 5.6621	val_loss: 5.2749	 acc:0.6815
Train Epoch:   2 [27200/48600 ( 56%)]	train_loss: 5.6667	val_loss: 5.7213	 acc:0.6708
Train Epoch:   2 [28800/48600 ( 59%)]	train_loss: 5.8191	val_loss: 5.1638	 acc:0.6440
Train Epoch:   2 [30400/48600 ( 63%)]	train_loss: 5.7820	val_loss: 6.2523	 acc:0.6266
Train Epoch:   2 [32000/48600 ( 66%)]	train_loss: 5.4359	val_loss: 5.2851	 acc:0.6047
Train Epoch:   2 [33600/48600 ( 69%)]	train_loss: 5.3940	val_loss: 5.4324	 acc:0.5879
Train Epoch:   2 [35200/48600 ( 72%)]	train_loss: 5.1442	val_loss: 4.8492	 acc:0.5782
Train Epoch:   2 [36800/48600 ( 76%)]	train_loss: 4.7314	val_loss: 5.0989	 acc:0.5613
Train Epoch:   2 [38400/48600 ( 79%)]	train_loss: 4.7219	val_loss: 4.7842	 acc:0.5509
Train Epoch:   2 [40000/48600 ( 82%)]	train_loss: 4.6455	val_loss: 5.1125	 acc:0.5314
Train Epoch:   2 [41600/48600 ( 86%)]	train_loss: 4.6511	val_loss: 5.7798	 acc:0.5134
Train Epoch:   2 [43200/48600 ( 89%)]	train_loss: 5.1393	val_loss: 4.8760	 acc:0.5083
Train Epoch:   2 [44800/48600 ( 92%)]	train_loss: 6.5540	val_loss: 5.3094	 acc:0.5058
Train Epoch:   2 [46400/48600 ( 96%)]	train_loss: 4.7982	val_loss: 6.1943	 acc:0.5077
Train Epoch:   2 [48000/48600 ( 99%)]	train_loss: 4.1264	val_loss: 5.1458	 acc:0.4950
Train Epoch:   3 [    0/48600 (  0%)]	train_loss: 4.8764	val_loss: 5.4665	 acc:0.0000
Train Epoch:   3 [ 1600/48600 (  3%)]	train_loss: 4.5176	val_loss: 6.2836	 acc:0.1786
Train Epoch:   3 [ 3200/48600 (  7%)]	train_loss: 10.4286	val_loss: 4.8917	 acc:0.2134
Train Epoch:   3 [ 4800/48600 ( 10%)]	train_loss: 5.2087	val_loss: 5.5892	 acc:0.1844
Train Epoch:   3 [ 6400/48600 ( 13%)]	train_loss: 4.4848	val_loss: 3.3409	 acc:0.2469
Train Epoch:   3 [ 8000/48600 ( 16%)]	train_loss: 4.4374	val_loss: 4.4239	 acc:0.2351
Train Epoch:   3 [ 9600/48600 ( 20%)]	train_loss: 5.6288	val_loss: 3.5758	 acc:0.2273
Train Epoch:   3 [11200/48600 ( 23%)]	train_loss: 3.2552	val_loss: 5.1751	 acc:0.2216
Train Epoch:   3 [12800/48600 ( 26%)]	train_loss: 4.5074	val_loss: 3.0077	 acc:0.1941
Train Epoch:   3 [14400/48600 ( 30%)]	train_loss: 3.7271	val_loss: 2.9183	 acc:0.2072
Train Epoch:   3 [16000/48600 ( 33%)]	train_loss: 3.5051	val_loss: 3.6776	 acc:0.2177
Train Epoch:   3 [17600/48600 ( 36%)]	train_loss: 4.3313	val_loss: 5.3244	 acc:0.2262
Train Epoch:   3 [19200/48600 ( 40%)]	train_loss: 3.5998	val_loss: 4.9520	 acc:0.2127
Train Epoch:   3 [20800/48600 ( 43%)]	train_loss: 4.8491	val_loss: 3.7798	 acc:0.2203
Train Epoch:   3 [22400/48600 ( 46%)]	train_loss: 4.6884	val_loss: 3.9837	 acc:0.2046
Train Epoch:   3 [24000/48600 ( 49%)]	train_loss: 3.8375	val_loss: 4.6442	 acc:0.1993
Train Epoch:   3 [25600/48600 ( 53%)]	train_loss: 5.0202	val_loss: 4.2715	 acc:0.2103
Train Epoch:   3 [27200/48600 ( 56%)]	train_loss: 4.1756	val_loss: 3.3878	 acc:0.2126
Train Epoch:   3 [28800/48600 ( 59%)]	train_loss: 2.9999	val_loss: 4.4034	 acc:0.2181
Train Epoch:   3 [30400/48600 ( 63%)]	train_loss: 3.9675	val_loss: 3.5241	 acc:0.2297
Train Epoch:   3 [32000/48600 ( 66%)]	train_loss: 4.3625	val_loss: 4.5549	 acc:0.2213
Train Epoch:   3 [33600/48600 ( 69%)]	train_loss: 3.7151	val_loss: 2.5271	 acc:0.2108
Train Epoch:   3 [35200/48600 ( 72%)]	train_loss: 2.9919	val_loss: 2.8000	 acc:0.2041
Train Epoch:   3 [36800/48600 ( 76%)]	train_loss: 3.0007	val_loss: 4.2227	 acc:0.2034
Train Epoch:   3 [38400/48600 ( 79%)]	train_loss: 4.5245	val_loss: 5.1717	 acc:0.2027
Train Epoch:   3 [40000/48600 ( 82%)]	train_loss: 4.7978	val_loss: 2.6621	 acc:0.1971
Train Epoch:   3 [41600/48600 ( 86%)]	train_loss: 2.5702	val_loss: 6.0276	 acc:0.1919
Train Epoch:   3 [43200/48600 ( 89%)]	train_loss: 6.6297	val_loss: 2.6658	 acc:0.1941
Train Epoch:   3 [44800/48600 ( 92%)]	train_loss: 6.1077	val_loss: 5.1710	 acc:0.1939
Train Epoch:   3 [46400/48600 ( 96%)]	train_loss: 2.8425	val_loss: 3.3412	 acc:0.1915
Train Epoch:   3 [48000/48600 ( 99%)]	train_loss: 5.5447	val_loss: 3.2437	 acc:0.1872
Train Epoch:   4 [    0/48600 (  0%)]	train_loss: 4.1451	val_loss: 2.6004	 acc:0.0000
Train Epoch:   4 [ 1600/48600 (  3%)]	train_loss: 3.4725	val_loss: 4.0200	 acc:0.1786
Train Epoch:   4 [ 3200/48600 (  7%)]	train_loss: 2.9762	val_loss: 4.0769	 acc:0.1829
Train Epoch:   4 [ 4800/48600 ( 10%)]	train_loss: 3.0323	val_loss: 4.2024	 acc:0.1844
Train Epoch:   4 [ 6400/48600 ( 13%)]	train_loss: 2.6579	val_loss: 3.0648	 acc:0.2006
Train Epoch:   4 [ 8000/48600 ( 16%)]	train_loss: 2.5428	val_loss: 4.2719	 acc:0.2228
Train Epoch:   4 [ 9600/48600 ( 20%)]	train_loss: 2.8139	val_loss: 2.3869	 acc:0.2169
Train Epoch:   4 [11200/48600 ( 23%)]	train_loss: 2.8816	val_loss: 3.6125	 acc:0.2039
Train Epoch:   4 [12800/48600 ( 26%)]	train_loss: 3.4528	val_loss: 4.4378	 acc:0.1941
Train Epoch:   4 [14400/48600 ( 30%)]	train_loss: 2.8270	val_loss: 2.6190	 acc:0.1865
Train Epoch:   4 [16000/48600 ( 33%)]	train_loss: 3.2068	val_loss: 2.4297	 acc:0.1803
Train Epoch:   4 [17600/48600 ( 36%)]	train_loss: 4.2910	val_loss: 1.9477	 acc:0.2036
Train Epoch:   4 [19200/48600 ( 40%)]	train_loss: 2.8218	val_loss: 2.9456	 acc:0.2230
Train Epoch:   4 [20800/48600 ( 43%)]	train_loss: 2.1940	val_loss: 2.7161	 acc:0.2203
Train Epoch:   4 [22400/48600 ( 46%)]	train_loss: 6.1431	val_loss: 2.2810	 acc:0.2135
Train Epoch:   4 [24000/48600 ( 49%)]	train_loss: 3.4211	val_loss: 1.5608	 acc:0.2076
Train Epoch:   4 [25600/48600 ( 53%)]	train_loss: 2.5813	val_loss: 1.6128	 acc:0.1947
Train Epoch:   4 [27200/48600 ( 56%)]	train_loss: 4.0675	val_loss: 2.5155	 acc:0.1906
Train Epoch:   4 [28800/48600 ( 59%)]	train_loss: 3.6483	val_loss: 1.7550	 acc:0.1870
Train Epoch:   4 [30400/48600 ( 63%)]	train_loss: 3.9263	val_loss: 2.0115	 acc:0.1870
Train Epoch:   4 [32000/48600 ( 66%)]	train_loss: 2.7900	val_loss: 2.4709	 acc:0.1808
Train Epoch:   4 [33600/48600 ( 69%)]	train_loss: 2.4542	val_loss: 3.4334	 acc:0.1752
Train Epoch:   4 [35200/48600 ( 72%)]	train_loss: 3.8415	val_loss: 2.0339	 acc:0.1701
Train Epoch:   4 [36800/48600 ( 76%)]	train_loss: 2.6147	val_loss: 3.0280	 acc:0.1708
Train Epoch:   4 [38400/48600 ( 79%)]	train_loss: 2.9265	val_loss: 3.4645	 acc:0.1767
Train Epoch:   4 [40000/48600 ( 82%)]	train_loss: 4.2819	val_loss: 3.5770	 acc:0.1697
Train Epoch:   4 [41600/48600 ( 86%)]	train_loss: 2.9299	val_loss: 3.5644	 acc:0.1655
Train Epoch:   4 [43200/48600 ( 89%)]	train_loss: 3.7723	val_loss: 3.9711	 acc:0.1594
Train Epoch:   4 [44800/48600 ( 92%)]	train_loss: 2.3379	val_loss: 1.9857	 acc:0.1582
Train Epoch:   4 [46400/48600 ( 96%)]	train_loss: 1.9125	val_loss: 2.5104	 acc:0.1549
Train Epoch:   4 [48000/48600 ( 99%)]	train_loss: 2.7565	val_loss: 1.8923	 acc:0.1498
Train Epoch:   5 [    0/48600 (  0%)]	train_loss: 3.1658	val_loss: 3.4583	 acc:0.0000
Train Epoch:   5 [ 1600/48600 (  3%)]	train_loss: 1.7627	val_loss: 1.9335	 acc:0.0000
Train Epoch:   5 [ 3200/48600 (  7%)]	train_loss: 3.0281	val_loss: 2.2129	 acc:0.0000
Train Epoch:   5 [ 4800/48600 ( 10%)]	train_loss: 3.0675	val_loss: 2.8893	 acc:0.0410
Train Epoch:   5 [ 6400/48600 ( 13%)]	train_loss: 3.3126	val_loss: 1.0205	 acc:0.0463
Train Epoch:   5 [ 8000/48600 ( 16%)]	train_loss: 5.3068	val_loss: 1.5672	 acc:0.1114
Train Epoch:   5 [ 9600/48600 ( 20%)]	train_loss: 2.2021	val_loss: 3.3186	 acc:0.1446
Train Epoch:   5 [11200/48600 ( 23%)]	train_loss: 0.9840	val_loss: 1.4443	 acc:0.1330
Train Epoch:   5 [12800/48600 ( 26%)]	train_loss: 3.4536	val_loss: 1.9790	 acc:0.1320
Train Epoch:   5 [14400/48600 ( 30%)]	train_loss: 1.9458	val_loss: 1.9623	 acc:0.1243
Train Epoch:   5 [16000/48600 ( 33%)]	train_loss: 3.2405	val_loss: 1.6737	 acc:0.1182
Train Epoch:   5 [17600/48600 ( 36%)]	train_loss: 1.9205	val_loss: 1.2705	 acc:0.1075
Train Epoch:   5 [19200/48600 ( 40%)]	train_loss: 1.6301	val_loss: 4.0377	 acc:0.0985
Train Epoch:   5 [20800/48600 ( 43%)]	train_loss: 1.8229	val_loss: 1.1486	 acc:0.1006
Train Epoch:   5 [22400/48600 ( 46%)]	train_loss: 2.2251	val_loss: 2.9438	 acc:0.0934
Train Epoch:   5 [24000/48600 ( 49%)]	train_loss: 3.0371	val_loss: 2.4870	 acc:0.0872
Train Epoch:   5 [25600/48600 ( 53%)]	train_loss: 1.6742	val_loss: 1.4461	 acc:0.0935
Train Epoch:   5 [27200/48600 ( 56%)]	train_loss: 4.4680	val_loss: 2.3280	 acc:0.0916
Train Epoch:   5 [28800/48600 ( 59%)]	train_loss: 4.5051	val_loss: 0.8891	 acc:0.0900
Train Epoch:   5 [30400/48600 ( 63%)]	train_loss: 1.9852	val_loss: 3.2037	 acc:0.0951
Train Epoch:   5 [32000/48600 ( 66%)]	train_loss: 1.2374	val_loss: 3.0852	 acc:0.0904
Train Epoch:   5 [33600/48600 ( 69%)]	train_loss: 1.3662	val_loss: 1.4447	 acc:0.0891
Train Epoch:   5 [35200/48600 ( 72%)]	train_loss: 2.8708	val_loss: 4.2809	 acc:0.0879
Train Epoch:   5 [36800/48600 ( 76%)]	train_loss: 2.5946	val_loss: 2.4300	 acc:0.0895
Train Epoch:   5 [38400/48600 ( 79%)]	train_loss: 2.4010	val_loss: 1.4934	 acc:0.0858
Train Epoch:   5 [40000/48600 ( 82%)]	train_loss: 2.0134	val_loss: 1.8889	 acc:0.0923
Train Epoch:   5 [41600/48600 ( 86%)]	train_loss: 1.7741	val_loss: 3.0926	 acc:0.0912
Train Epoch:   5 [43200/48600 ( 89%)]	train_loss: 2.5331	val_loss: 1.5891	 acc:0.0878
Train Epoch:   5 [44800/48600 ( 92%)]	train_loss: 2.1189	val_loss: 1.8439	 acc:0.0847
Train Epoch:   5 [46400/48600 ( 96%)]	train_loss: 1.8148	val_loss: 2.7419	 acc:0.0861
Train Epoch:   5 [48000/48600 ( 99%)]	train_loss: 3.9189	val_loss: 1.6547	 acc:0.0894
Train Epoch:   6 [    0/48600 (  0%)]	train_loss: 2.1578	val_loss: 3.6621	 acc:0.0000
Train Epoch:   6 [ 1600/48600 (  3%)]	train_loss: 0.9891	val_loss: 2.1597	 acc:0.1190
Train Epoch:   6 [ 3200/48600 (  7%)]	train_loss: 1.9248	val_loss: 1.6877	 acc:0.1220
Train Epoch:   6 [ 4800/48600 ( 10%)]	train_loss: 4.8497	val_loss: 1.7575	 acc:0.1230
Train Epoch:   6 [ 6400/48600 ( 13%)]	train_loss: 1.5550	val_loss: 2.3763	 acc:0.0926
Train Epoch:   6 [ 8000/48600 ( 16%)]	train_loss: 2.9609	val_loss: 2.3509	 acc:0.0866
Train Epoch:   6 [ 9600/48600 ( 20%)]	train_loss: 2.7966	val_loss: 2.4106	 acc:0.0723
Train Epoch:   6 [11200/48600 ( 23%)]	train_loss: 2.6849	val_loss: 1.3554	 acc:0.0709
Train Epoch:   6 [12800/48600 ( 26%)]	train_loss: 2.4549	val_loss: 1.6572	 acc:0.0699
Train Epoch:   6 [14400/48600 ( 30%)]	train_loss: 1.7165	val_loss: 2.6332	 acc:0.0622
Train Epoch:   6 [16000/48600 ( 33%)]	train_loss: 2.1179	val_loss: 3.0956	 acc:0.0622
Train Epoch:   6 [17600/48600 ( 36%)]	train_loss: 2.9120	val_loss: 2.2677	 acc:0.0792
Train Epoch:   6 [19200/48600 ( 40%)]	train_loss: 1.4499	val_loss: 1.7480	 acc:0.0726
Train Epoch:   6 [20800/48600 ( 43%)]	train_loss: 2.7923	val_loss: 1.0949	 acc:0.0670
Train Epoch:   6 [22400/48600 ( 46%)]	train_loss: 1.8943	val_loss: 2.4960	 acc:0.0623
Train Epoch:   6 [24000/48600 ( 49%)]	train_loss: 3.1129	val_loss: 2.2752	 acc:0.0664
Train Epoch:   6 [25600/48600 ( 53%)]	train_loss: 2.2632	val_loss: 1.1593	 acc:0.0662
Train Epoch:   6 [27200/48600 ( 56%)]	train_loss: 3.5387	val_loss: 1.1749	 acc:0.0660
Train Epoch:   6 [28800/48600 ( 59%)]	train_loss: 2.7328	val_loss: 2.7489	 acc:0.0658
Train Epoch:   6 [30400/48600 ( 63%)]	train_loss: 4.1653	val_loss: 1.8294	 acc:0.0656
Train Epoch:   6 [32000/48600 ( 66%)]	train_loss: 1.1152	val_loss: 1.5345	 acc:0.0686
Train Epoch:   6 [33600/48600 ( 69%)]	train_loss: 3.2282	val_loss: 1.1130	 acc:0.0683
Train Epoch:   6 [35200/48600 ( 72%)]	train_loss: 2.9923	val_loss: 1.3599	 acc:0.0680
Train Epoch:   6 [36800/48600 ( 76%)]	train_loss: 2.0414	val_loss: 2.7477	 acc:0.0651
Train Epoch:   6 [38400/48600 ( 79%)]	train_loss: 1.7603	val_loss: 1.3875	 acc:0.0676
Train Epoch:   6 [40000/48600 ( 82%)]	train_loss: 4.2912	val_loss: 2.0376	 acc:0.0649
Train Epoch:   6 [41600/48600 ( 86%)]	train_loss: 3.7480	val_loss: 1.9521	 acc:0.0696
Train Epoch:   6 [43200/48600 ( 89%)]	train_loss: 2.5395	val_loss: 2.3025	 acc:0.0670
Train Epoch:   6 [44800/48600 ( 92%)]	train_loss: 2.3830	val_loss: 3.2211	 acc:0.0646
Train Epoch:   6 [46400/48600 ( 96%)]	train_loss: 2.0125	val_loss: 2.4834	 acc:0.0624
Train Epoch:   6 [48000/48600 ( 99%)]	train_loss: 1.6565	val_loss: 2.0749	 acc:0.0666
Train Epoch:   7 [    0/48600 (  0%)]	train_loss: 4.3678	val_loss: 2.5146	 acc:0.0000
Train Epoch:   7 [ 1600/48600 (  3%)]	train_loss: 3.4234	val_loss: 2.7618	 acc:0.1190
Train Epoch:   7 [ 3200/48600 (  7%)]	train_loss: 2.0455	val_loss: 1.2993	 acc:0.0610
Train Epoch:   7 [ 4800/48600 ( 10%)]	train_loss: 2.1082	val_loss: 0.8334	 acc:0.1434
Train Epoch:   7 [ 6400/48600 ( 13%)]	train_loss: 1.8545	val_loss: 1.2720	 acc:0.1852
Train Epoch:   7 [ 8000/48600 ( 16%)]	train_loss: 3.3667	val_loss: 1.6813	 acc:0.1856
Train Epoch:   7 [ 9600/48600 ( 20%)]	train_loss: 2.0930	val_loss: 3.2544	 acc:0.2169
Train Epoch:   7 [11200/48600 ( 23%)]	train_loss: 5.3971	val_loss: 0.6087	 acc:0.2128
Train Epoch:   7 [12800/48600 ( 26%)]	train_loss: 1.9346	val_loss: 1.6443	 acc:0.2562
Train Epoch:   7 [14400/48600 ( 30%)]	train_loss: 3.0240	val_loss: 2.7684	 acc:0.2486
Train Epoch:   7 [16000/48600 ( 33%)]	train_loss: 1.4758	val_loss: 3.2663	 acc:0.2736
Train Epoch:   7 [17600/48600 ( 36%)]	train_loss: 2.5216	val_loss: 2.1520	 acc:0.2771
Train Epoch:   7 [19200/48600 ( 40%)]	train_loss: 3.1282	val_loss: 1.4846	 acc:0.2541
Train Epoch:   7 [20800/48600 ( 43%)]	train_loss: 1.7439	val_loss: 3.0154	 acc:0.2490
Train Epoch:   7 [22400/48600 ( 46%)]	train_loss: 1.5755	val_loss: 0.8257	 acc:0.2536
Train Epoch:   7 [24000/48600 ( 49%)]	train_loss: 0.5203	val_loss: 1.8853	 acc:0.2450
Train Epoch:   7 [25600/48600 ( 53%)]	train_loss: 1.1313	val_loss: 1.4520	 acc:0.2375
Train Epoch:   7 [27200/48600 ( 56%)]	train_loss: 1.5652	val_loss: 1.0145	 acc:0.2419
Train Epoch:   7 [28800/48600 ( 59%)]	train_loss: 0.9993	val_loss: 0.3366	 acc:0.2597
Train Epoch:   7 [30400/48600 ( 63%)]	train_loss: 10.0994	val_loss: 0.3272	 acc:0.2526
Train Epoch:   7 [32000/48600 ( 66%)]	train_loss: 1.5385	val_loss: 1.8713	 acc:0.2494
Train Epoch:   7 [33600/48600 ( 69%)]	train_loss: 3.9916	val_loss: 0.4924	 acc:0.2435
Train Epoch:   7 [35200/48600 ( 72%)]	train_loss: 1.9014	val_loss: 4.9956	 acc:0.2353
Train Epoch:   7 [36800/48600 ( 76%)]	train_loss: 0.8910	val_loss: 2.5894	 acc:0.2278
Train Epoch:   7 [38400/48600 ( 79%)]	train_loss: 2.0577	val_loss: 0.7675	 acc:0.2261
Train Epoch:   7 [40000/48600 ( 82%)]	train_loss: 2.4401	val_loss: 2.3511	 acc:0.2171
Train Epoch:   7 [41600/48600 ( 86%)]	train_loss: 0.8720	val_loss: 0.7571	 acc:0.2111
Train Epoch:   7 [43200/48600 ( 89%)]	train_loss: 1.8235	val_loss: 1.4891	 acc:0.2033
Train Epoch:   7 [44800/48600 ( 92%)]	train_loss: 0.7328	val_loss: 0.8030	 acc:0.2005
Train Epoch:   7 [46400/48600 ( 96%)]	train_loss: 0.4907	val_loss: 0.5466	 acc:0.1958
Train Epoch:   7 [48000/48600 ( 99%)]	train_loss: 0.8253	val_loss: 0.9916	 acc:0.1913
Train Epoch:   8 [    0/48600 (  0%)]	train_loss: 2.1975	val_loss: 3.1143	 acc:0.0000
Train Epoch:   8 [ 1600/48600 (  3%)]	train_loss: 1.2722	val_loss: 2.1082	 acc:0.3571
Train Epoch:   8 [ 3200/48600 (  7%)]	train_loss: 2.1132	val_loss: 0.9495	 acc:0.2439
Train Epoch:   8 [ 4800/48600 ( 10%)]	train_loss: 3.8457	val_loss: 1.1225	 acc:0.2254
Train Epoch:   8 [ 6400/48600 ( 13%)]	train_loss: 1.0284	val_loss: 1.9752	 acc:0.2006
Train Epoch:   8 [ 8000/48600 ( 16%)]	train_loss: 1.6263	val_loss: 1.8020	 acc:0.1609
Train Epoch:   8 [ 9600/48600 ( 20%)]	train_loss: 4.3424	val_loss: 0.6326	 acc:0.1756
Train Epoch:   8 [11200/48600 ( 23%)]	train_loss: 0.7334	val_loss: 1.1563	 acc:0.1684
Train Epoch:   8 [12800/48600 ( 26%)]	train_loss: 1.8218	val_loss: 0.4827	 acc:0.1863
Train Epoch:   8 [14400/48600 ( 30%)]	train_loss: 1.6978	val_loss: 1.0881	 acc:0.1796
Train Epoch:   8 [16000/48600 ( 33%)]	train_loss: 4.1991	val_loss: 2.0204	 acc:0.1866
Train Epoch:   8 [17600/48600 ( 36%)]	train_loss: 1.6649	val_loss: 3.1394	 acc:0.1867
Train Epoch:   8 [19200/48600 ( 40%)]	train_loss: 0.7311	val_loss: 0.9547	 acc:0.1763
Train Epoch:   8 [20800/48600 ( 43%)]	train_loss: 1.0196	val_loss: 2.2460	 acc:0.1820
Train Epoch:   8 [22400/48600 ( 46%)]	train_loss: 1.2451	val_loss: 4.5765	 acc:0.1735
Train Epoch:   8 [24000/48600 ( 49%)]	train_loss: 0.3058	val_loss: 0.2852	 acc:0.1786
Train Epoch:   8 [25600/48600 ( 53%)]	train_loss: 1.5444	val_loss: 0.3506	 acc:0.1830
Train Epoch:   8 [27200/48600 ( 56%)]	train_loss: 0.2598	val_loss: 0.6633	 acc:0.1833
Train Epoch:   8 [28800/48600 ( 59%)]	train_loss: 0.8047	val_loss: 0.6396	 acc:0.1801
Train Epoch:   8 [30400/48600 ( 63%)]	train_loss: 3.1570	val_loss: 5.5352	 acc:0.1739
Train Epoch:   8 [32000/48600 ( 66%)]	train_loss: 1.7883	val_loss: 1.5653	 acc:0.1777
Train Epoch:   8 [33600/48600 ( 69%)]	train_loss: 3.0879	val_loss: 2.5457	 acc:0.1692
Train Epoch:   8 [35200/48600 ( 72%)]	train_loss: 1.5755	val_loss: 1.1518	 acc:0.1701
Train Epoch:   8 [36800/48600 ( 76%)]	train_loss: 1.3925	val_loss: 1.7460	 acc:0.1627
Train Epoch:   8 [38400/48600 ( 79%)]	train_loss: 1.4959	val_loss: 0.5023	 acc:0.1559
Train Epoch:   8 [40000/48600 ( 82%)]	train_loss: 2.5972	val_loss: 2.2564	 acc:0.1497
Train Epoch:   8 [41600/48600 ( 86%)]	train_loss: 1.0541	val_loss: 1.4504	 acc:0.1464
Train Epoch:   8 [43200/48600 ( 89%)]	train_loss: 2.7753	val_loss: 2.5657	 acc:0.1456
Train Epoch:   8 [44800/48600 ( 92%)]	train_loss: 0.5221	val_loss: 0.8378	 acc:0.1471
Train Epoch:   8 [46400/48600 ( 96%)]	train_loss: 1.8296	val_loss: 0.1730	 acc:0.1463
Train Epoch:   8 [48000/48600 ( 99%)]	train_loss: 2.7760	val_loss: 0.2322	 acc:0.1435
Train Epoch:   9 [    0/48600 (  0%)]	train_loss: 1.7369	val_loss: 0.5453	 acc:0.0000
Train Epoch:   9 [ 1600/48600 (  3%)]	train_loss: 1.8183	val_loss: 3.5716	 acc:0.0000
Train Epoch:   9 [ 3200/48600 (  7%)]	train_loss: 0.5386	val_loss: 0.6056	 acc:0.0610
Train Epoch:   9 [ 4800/48600 ( 10%)]	train_loss: 1.9499	val_loss: 1.5724	 acc:0.0615
Train Epoch:   9 [ 6400/48600 ( 13%)]	train_loss: 0.1948	val_loss: 0.1295	 acc:0.0463
Train Epoch:   9 [ 8000/48600 ( 16%)]	train_loss: 0.5698	val_loss: 1.3827	 acc:0.0619
Train Epoch:   9 [ 9600/48600 ( 20%)]	train_loss: 6.7942	val_loss: 1.7945	 acc:0.0723
Train Epoch:   9 [11200/48600 ( 23%)]	train_loss: 2.0678	val_loss: 0.2870	 acc:0.0975
Train Epoch:   9 [12800/48600 ( 26%)]	train_loss: 0.3828	val_loss: 1.1246	 acc:0.1009
Train Epoch:   9 [14400/48600 ( 30%)]	train_loss: 0.4578	val_loss: 5.6668	 acc:0.1036
Train Epoch:   9 [16000/48600 ( 33%)]	train_loss: 0.1977	val_loss: 0.8671	 acc:0.0933
Train Epoch:   9 [17600/48600 ( 36%)]	train_loss: 0.9688	val_loss: 0.1571	 acc:0.0905
Train Epoch:   9 [19200/48600 ( 40%)]	train_loss: 0.2019	val_loss: 0.9592	 acc:0.0882
Train Epoch:   9 [20800/48600 ( 43%)]	train_loss: 0.9002	val_loss: 1.0291	 acc:0.0862
Train Epoch:   9 [22400/48600 ( 46%)]	train_loss: 0.2466	val_loss: 2.1738	 acc:0.0845
Train Epoch:   9 [24000/48600 ( 49%)]	train_loss: 0.2533	val_loss: 0.3966	 acc:0.0872
Train Epoch:   9 [25600/48600 ( 53%)]	train_loss: 1.2943	val_loss: 0.2307	 acc:0.0974
Train Epoch:   9 [27200/48600 ( 56%)]	train_loss: 0.2004	val_loss: 0.5857	 acc:0.0990
Train Epoch:   9 [28800/48600 ( 59%)]	train_loss: 0.5910	val_loss: 1.4511	 acc:0.0970
Train Epoch:   9 [30400/48600 ( 63%)]	train_loss: 1.5112	val_loss: 1.1903	 acc:0.0919
Train Epoch:   9 [32000/48600 ( 66%)]	train_loss: 0.2350	val_loss: 1.2722	 acc:0.0935
Train Epoch:   9 [33600/48600 ( 69%)]	train_loss: 1.0798	val_loss: 0.7553	 acc:0.0920
Train Epoch:   9 [35200/48600 ( 72%)]	train_loss: 1.0279	val_loss: 1.0756	 acc:0.0907
Train Epoch:   9 [36800/48600 ( 76%)]	train_loss: 1.0904	val_loss: 0.4587	 acc:0.0976
Train Epoch:   9 [38400/48600 ( 79%)]	train_loss: 1.7454	val_loss: 1.8685	 acc:0.0988
Train Epoch:   9 [40000/48600 ( 82%)]	train_loss: 0.2800	val_loss: 2.7765	 acc:0.0948
Train Epoch:   9 [41600/48600 ( 86%)]	train_loss: 0.6667	val_loss: 0.8545	 acc:0.1008
Train Epoch:   9 [43200/48600 ( 89%)]	train_loss: 0.5946	val_loss: 0.6464	 acc:0.1086
Train Epoch:   9 [44800/48600 ( 92%)]	train_loss: 1.2888	val_loss: 0.3420	 acc:0.1047
Train Epoch:   9 [46400/48600 ( 96%)]	train_loss: 1.0532	val_loss: 0.6097	 acc:0.1076
Train Epoch:   9 [48000/48600 ( 99%)]	train_loss: 0.6210	val_loss: 0.7133	 acc:0.1061
Train Epoch:  10 [    0/48600 (  0%)]	train_loss: 0.6377	val_loss: 0.2274	 acc:0.0000
Train Epoch:  10 [ 1600/48600 (  3%)]	train_loss: 3.9319	val_loss: 1.6273	 acc:0.0595
Train Epoch:  10 [ 3200/48600 (  7%)]	train_loss: 1.4825	val_loss: 0.9609	 acc:0.0915
Train Epoch:  10 [ 4800/48600 ( 10%)]	train_loss: 1.2792	val_loss: 2.4387	 acc:0.1434
Train Epoch:  10 [ 6400/48600 ( 13%)]	train_loss: 5.9921	val_loss: 2.7745	 acc:0.1235
Train Epoch:  10 [ 8000/48600 ( 16%)]	train_loss: 0.4326	val_loss: 0.3611	 acc:0.1361
Train Epoch:  10 [ 9600/48600 ( 20%)]	train_loss: 0.9879	val_loss: 0.6829	 acc:0.1240
Train Epoch:  10 [11200/48600 ( 23%)]	train_loss: 0.3936	val_loss: 0.6048	 acc:0.1330
Train Epoch:  10 [12800/48600 ( 26%)]	train_loss: 2.9969	val_loss: 4.3568	 acc:0.1320
Train Epoch:  10 [14400/48600 ( 30%)]	train_loss: 0.1183	val_loss: 1.6866	 acc:0.1381
Train Epoch:  10 [16000/48600 ( 33%)]	train_loss: 0.2589	val_loss: 1.4382	 acc:0.1368
Train Epoch:  10 [17600/48600 ( 36%)]	train_loss: 0.6312	val_loss: 1.1788	 acc:0.1301
Train Epoch:  10 [19200/48600 ( 40%)]	train_loss: 0.7317	val_loss: 1.5340	 acc:0.1193
Train Epoch:  10 [20800/48600 ( 43%)]	train_loss: 1.4202	val_loss: 1.0849	 acc:0.1293
Train Epoch:  10 [22400/48600 ( 46%)]	train_loss: 0.7380	val_loss: 0.2882	 acc:0.1290
Train Epoch:  10 [24000/48600 ( 49%)]	train_loss: 2.0038	val_loss: 0.7929	 acc:0.1204
Train Epoch:  10 [25600/48600 ( 53%)]	train_loss: 0.6806	val_loss: 1.0988	 acc:0.1207
Train Epoch:  10 [27200/48600 ( 56%)]	train_loss: 1.6568	val_loss: 0.3962	 acc:0.1246
Train Epoch:  10 [28800/48600 ( 59%)]	train_loss: 0.5131	val_loss: 0.3527	 acc:0.1212
Train Epoch:  10 [30400/48600 ( 63%)]	train_loss: 0.5177	val_loss: 0.3591	 acc:0.1214
Train Epoch:  10 [32000/48600 ( 66%)]	train_loss: 0.4515	val_loss: 0.3276	 acc:0.1185
Train Epoch:  10 [33600/48600 ( 69%)]	train_loss: 1.0951	val_loss: 2.4004	 acc:0.1217
Train Epoch:  10 [35200/48600 ( 72%)]	train_loss: 0.9232	val_loss: 1.5728	 acc:0.1219
Train Epoch:  10 [36800/48600 ( 76%)]	train_loss: 0.2229	val_loss: 1.1192	 acc:0.1166
Train Epoch:  10 [38400/48600 ( 79%)]	train_loss: 2.5061	val_loss: 1.6676	 acc:0.1143
Train Epoch:  10 [40000/48600 ( 82%)]	train_loss: 0.5621	val_loss: 0.4986	 acc:0.1098
Train Epoch:  10 [41600/48600 ( 86%)]	train_loss: 0.2997	val_loss: 2.0493	 acc:0.1128
Train Epoch:  10 [43200/48600 ( 89%)]	train_loss: 1.0935	val_loss: 1.0866	 acc:0.1109
Train Epoch:  10 [44800/48600 ( 92%)]	train_loss: 0.5907	val_loss: 0.3966	 acc:0.1092
Train Epoch:  10 [46400/48600 ( 96%)]	train_loss: 0.2979	val_loss: 1.3323	 acc:0.1097
Train Epoch:  10 [48000/48600 ( 99%)]	train_loss: 0.8537	val_loss: 0.4341	 acc:0.1102

</code></pre>

ptrblck: <p>The accuracy calculation seems to be wrong as it has values <code>&gt;1</code> (so more than 100% of samples are correct, which is impossible).<br>
Check the shapes of <code>predicty</code> and <code>target</code> and make sure you are normalizing with the right value. E.g. if you are working on a segmentation use case you would need to divide by the number of pixels, not the batch size.</p>

S_M: <p>Hi ptrblck, thank you for your answer. I found some accuracy related to this loss <a href="https://github.com/tomp11/metric_learning/blob/master/src/t_SNE.py" rel="noopener nofollow ugc">here</a>. The proposed accuracy world be as:</p>
<pre><code class="lang-auto">    def angular_acc(data, target, model, embeded, pred_categories):

        target_array = target.cpu().numpy()
        master_features = [] 
        pred_ = []
        classes = ["0","1","2","3","4","5","6","7","8","9"]
        for i in classes:
            indexes = np.where(target_array==int(i))[0]
            if len(indexes)==0:
                continue
            master_img = data[np.random.choice(indexes)].to(device)
            master_img = torch.unsqueeze(master_img, dim=0)
            master_img = master_img.to(self.device)
            embedded_master_img = model(master_img)
            master_features.append(embedded_master_img)
        master_features = torch.cat(master_features) # (10, 128)
        
        output_unbind = torch.unbind(embeded)
        for embedded_img in output_unbind:
            distances = torch.sum((master_features - embedded_img)**2, dim=1) #(10)F.pairwise_distance(master_features, embedded_img, 2)#
            pred_category = classes[distances.argmin()]
            pred_categories.append(int(pred_category))
            pred_.append(int(pred_category))

        pred_category = torch.LongTensor(pred_).to(device)
        correct = (target == pred_category).sum()
        # print(correct)
        return correct, pred_categories
</code></pre>
<p>It can be used in a model training as:</p>
<pre><code class="lang-auto">pred_categories  = []
correct_total = 0
model.train()
for batch_idx, (data, target) in enumerate(train_set):
    self.optimizer.zero_grad()
    embedded= model_t(data)
    loss = angular_loss(embedded, target)
    correct, pred_categories_a = angular_acc(data, target, model, embedded, pred_categories)
    correct_total += correct
    acc = float(correct_total)*100 / len(pred_categories)
.
.
.
</code></pre>
<p>Unfortunately, the number of correct predictions in each batch (128) is low. What is the reason?</p>