gscalar = get_scalar_csv(settings["valid_feat_file"]) escalar = get_scalar_csv(settings["valid_engy_file"]) dtype, device = locate(settings['dtype']), settings['device'] M2, M3 = settings['M2'], settings['M3'] num_feat, num_engy = M2 + M3 ** 3, 1 mlp = [num_feat, *settings['hidden_layers'], num_engy] Rc = settings['Router'] # initialize weights and biases if they are not provided weights = list(np2torch(*weights)) biases = list(np2torch(*biases)) for (w, b) in zip(weights, biases): w.requires_grad, b.requires_grad = True, True # convert gscalar and escale to torch tensors gscalar, escalar = list(np2torch(*gscalar, dtype=dtype)), list(np2torch(*escalar, dtype=dtype)) start = time.time() g, g_dldl, g_dpdl, idxNb = get_dG_dR(Rcart, lattice, basis='cosine', M2=M2, M3=M3, Rinner=0, Router=Rc) g, g_dldl, g_dpdl = np2torch(g, g_dldl, g_dpdl, dtype=dtype) feat_scaled = gscalar[0] * g + gscalar[1] nn_out2, d_nn_out2 = d_mlnn(feat_scaled, weights, biases, activation="sigmoid") energies = nn_out2 forces = get_forces(g_dldl, g_dpdl, d_nn_out2, idxNb, gscalar[0])
for iepoch in range(settings['epoch']): for step, (Rcart, lattice, atom_types, F, Ei) in enumerate(pwmat_mmt): g, g_dldl, g_dpdl, idxNb = ct_ft.get_dG_dR(Rcart, lattice, basis='cosine', M2=M2, M3=M3, Rinner=0, Router=Rc) g, g_dldl, g_dpdl = ct_nn.np2torch(g, g_dldl, g_dpdl, dtype=dtype) feat_scaled = feat_a * g + feat_b nn_out2, d_nn_out2 = ct_nn.d_mlnn(feat_scaled, weights, biases, activation="sigmoid") forces = ct_nn.get_forces(g_dldl, g_dpdl, d_nn_out2, idxNb, feat_a) z = (nn_out2 - engy_b) / engy_a dz = forces / engy_a y, dy = ct_nn.np2torch(Ei[:, None], F, dtype=dtype) F_mse = torch.mean((dz - dy)**2) E_mse = torch.mean((z - y)**2) loss = E_mse + F_mse ct_nn.mlnn_optimize(loss, optimizer) optimizer.step() print(step, *ct_nn.torch2np(torch.sqrt(E_mse), torch.sqrt(F_mse)), time.time() - start)