在 Pytorch 训练中,我使用复合损失函数,定义为:
。为了更新权重 alpha 和 beta,我需要计算三个值:
它们是网络中所有权重的损失项梯度的平均值。
有没有一种有效的方法可以在 pytorch 中编写它?
我的训练代码如下所示:
for epoc in range(1, nb_epochs+1):
#init
optimizer_fo.zero_grad()
#get the current loss
loss_total = mynet_fo.loss(tensor_xy_dirichlet,g_boundaries_d,tensor_xy_inside,tensor_f_inter,tensor_xy_neuman,g_boundaries_n)
#compute gradients
loss_total.backward(retain_graph=True)
#optimize
optimizer_fo.step()
Run Code Online (Sandbox Code Playgroud)
我的 .loss() 函数直接返回各项之和。我考虑过进行第二次前向传递并独立地向后调用每个损失项,但这会非常昂贵。
我正在尝试使用 Open MPI 学习并行计算。我在 MacBook Pro 上使用 Ubuntu 16 启动。
我已经安装了 OpenMP 并尝试运行一个hello_world
来测试它。
#include <mpi.h>
#include <stdio.h>
int main(int argc, char** argv) {
// Initialize the MPI environment
MPI_Init(NULL, NULL);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
// Print off a hello world message
printf("Hello world from processor %s, rank %d" …
Run Code Online (Sandbox Code Playgroud)