mxnet 配置gpu

关乎symbol和module的一些基本属性

# 查看json每一个op的属性:kernel size、padding、stride等
sym.attr_dict() # 返回一个字典,根据key获取对应op的属性
# 查看网络的输出name
sym.list_outputs()
# 查看网络所有的输入节点name
sym.list_arguments()
# 查看网络所有内部节点
sym.get_internals()
# 获取网络的参数节点name
mod.get_params()[0]
# 获取网络的中间结果 fc7 output
all_layers = sym.get_internals()
sym = all_layers['fc7_output']
mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None) # 然后做一次inference就能获取fc7 output


————————————————
版权声明:本文为CSDN博主「JustForYouW」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wwwhp/article/details/84556909

 

单gpu: 

gpu_id=0
ctx = mx.gpu(gpu_id)
nets = []

sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
#arg_params, aux_params = ch_dev(arg_params, aux_params, ctx)
all_layers = sym.get_internals()
sym = all_layers['fc1_output']
model = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
#model.bind(data_shapes=[('data', (args.batch_size, 3, image_size[0], image_size[1]))], label_shapes=[('softmax_label', (args.batch_size,))])
model.bind(data_shapes=[('data', (args.batch_size, 3, image_size[0],
                                  image_size[1]))])
model.set_params(arg_params, aux_params)
nets.append(model)
#使用单块显卡时的代码:

            devices = mx.gpu(0)
            data = mx.nd.array(batch_data).as_in_context(devices)
            label = mx.nd.array(batch_label).as_in_context(devices)
            # 更新生成器G
            with autograd.record():
                 output = G(data)
                 errG_idt = idt_loss(output, label)
                 errG_idt.backward()
                 metric_G.update([label, ], [output, ])
            G_trainer.step(batch_size)

多GPU:

区别有很多,这是一点点不一样
1、mxnet 的batch ,设置的是单卡的,多卡是 batch*多卡,多一张卡,速度可以提升一倍
2、pytorch 是 batch/多卡数量,分配给每个gpu,设置的batch是总的batch



使用多块显卡时的代码:

            devices = [mx.gpu(0), mx.gpu(1), mx.gpu(2)]
            gpu_Xs = gutils.split_and_load(batch_data, devices)
            gpu_ys = gutils.split_and_load(batch_label, devices)
            ls = []
            with autograd.record():
                for gpu_X, gpu_y in zip(gpu_Xs, gpu_ys):
                    output = G(gpu_X)
                    errG_idt = idt_loss(output, gpu_y)
                    ls.append(errG_idt)
                    # update metrics
                    metric_G.update([gpu_y, ], [output, ])
                for l in ls:
                    l.backward()
            G_trainer.step(batch_size)


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值