def test_Softmax_CE(input, target):
    f = Softmax_CE()
    output = f.forward(input, target)
    grad_output = f.backward()

    softmax = Softmax()
    CE = CrossEntropy()
    pred = softmax.forward(input)
    div_output = CE.forward(pred, target)
    ce_grad = CE.backward()
    div_grad = softmax.backward(ce_grad)
    return output, grad_output, div_output, div_grad
示例#2
0
device_id = int(os.getenv('DEVICE_ID'))

context.set_context(mode=context.GRAPH_MODE,
                    device_target="Ascend",
                    save_graphs=False)
context.set_context(enable_task_sink=True, device_id=device_id)
context.set_context(enable_loop_sink=True)
context.set_context(enable_mem_reuse=True)

if __name__ == '__main__':

    net = resnet50(class_num=config.class_num)
    if not config.use_label_smooth:
        config.label_smooth_factor = 0.0
    loss = CrossEntropy(smooth_factor=config.label_smooth_factor,
                        num_classes=config.class_num)

    if args_opt.do_eval:
        dataset = create_dataset(dataset_path=args_opt.dataset_path,
                                 do_train=False,
                                 batch_size=config.batch_size)
        step_size = dataset.get_dataset_size()

        if args_opt.checkpoint_path:
            param_dict = load_checkpoint(args_opt.checkpoint_path)
            load_param_into_net(net, param_dict)
        net.set_train(False)

        model = Model(net, loss_fn=loss, metrics={'acc'})
        res = model.eval(dataset)
        print("result:", res, "ckpt=", args_opt.checkpoint_path)