# 3.9. Fit spin energy

In this section, we will take \$deepmd_source_dir/examples/NiO/se_e2_a/input.json as an example of the input file.

## 3.9.1. Spin

The construction of the fitting net is give by section spin

    "spin" : {
"use_spin":         [true, false],
"virtual_len":      [0.4],
"spin_norm":        [1.2737],
},

• use_spin determines whether to turn on the magnetism of the atoms.The index of this option matches option type_map <model/type_map>.

• virtual_len specifies the distance between virtual atom and the belonging real atom.

• spin_norm gives the magnitude of the magnetic moment for each magnatic atom.

## 3.9.2. Spin Loss

The spin loss function $$L$$ for training energy is given by

$L = p_e L_e + p_{fr} L_{fr} + p_{fm} L_{fm} + p_v L_v$

where $$L_e$$, $$L_{fr}$$, $$L_{fm}$$ and $$L_v$$ denote the loss in energy, atomic force, magnatic force and virial, respectively. $$p_e$$, $$p_{fr}$$, $$p_{fm}$$ and $$p_v$$ give the prefactors of the energy, atomic force, magnatic force and virial losses.

The prefectors may not be a constant, rather it changes linearly with the learning rate. Taking the atomic force prefactor for example, at training step $$t$$, it is given by

$p_{fr}(t) = p_{fr}^0 \frac{ \alpha(t) }{ \alpha(0) } + p_{fr}^\infty ( 1 - \frac{ \alpha(t) }{ \alpha(0) })$

where $$\alpha(t)$$ denotes the learning rate at step $$t$$. $$p_{fr}^0$$ and $$p_{fr}^\infty$$ specifies the $$p_f$$ at the start of the training and at the limit of $$t \to \infty$$ (set by start_pref_fr and limit_pref_f, respectively), i.e.

pref_fr(t) = start_pref_fr * ( lr(t) / start_lr ) + limit_pref_fr * ( 1 - lr(t) / start_lr )


The loss section in the input.json is

    "loss" :{
"type":		        "ener_spin",
"start_pref_e":	    0.02,
"limit_pref_e":	    1,
"start_pref_fr":	1000,
"limit_pref_fr":	1.0,
"start_pref_fm":	10000,
"limit_pref_fm":	10.0,
"start_pref_v":	    0,
"limit_pref_v":	    0,
},


The options start_pref_e, limit_pref_e, start_pref_fr, limit_pref_fm, start_pref_v and limit_pref_v determine the start and limit prefactors of energy, atomic force, magnatic force and virial, respectively.

If one does not want to train with virial, then he/she may set the virial prefactors start_pref_v and limit_pref_v to 0.