예제 #1
0
                              args.preprocess_path, args.glove_path)

    embedding_table = np.loadtxt(
        os.path.join(args.preprocess_path, "weight.txt")).astype(np.float32)
    # DynamicRNN in this network on Ascend platform only support the condition that the shape of input_size
    # and hiddle_size is multiples of 16, this problem will be solved later.
    if args.device_target == 'Ascend':
        pad_num = int(np.ceil(cfg.embed_size / 16) * 16 - cfg.embed_size)
        if pad_num > 0:
            embedding_table = np.pad(embedding_table, [(0, 0), (0, pad_num)],
                                     'constant')
        cfg.embed_size = int(np.ceil(cfg.embed_size / 16) * 16)
    network = SentimentNet(vocab_size=embedding_table.shape[0],
                           embed_size=cfg.embed_size,
                           num_hiddens=cfg.num_hiddens,
                           num_layers=cfg.num_layers,
                           bidirectional=cfg.bidirectional,
                           num_classes=cfg.num_classes,
                           weight=Tensor(embedding_table),
                           batch_size=cfg.batch_size)
    # pre_trained
    if args.pre_trained:
        load_param_into_net(network, load_checkpoint(args.pre_trained))

    ds_train = lstm_create_dataset(args.preprocess_path, cfg.batch_size, 1)

    loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
    if cfg.dynamic_lr:
        lr = Tensor(
            get_lr(global_step=cfg.global_step,
                   lr_init=cfg.lr_init,
                   lr_end=cfg.lr_end,
예제 #2
0
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='MindSpore LSTM Exporter')
    parser.add_argument('--preprocess_path',
                        type=str,
                        default='./preprocess',
                        help='path where the pre-process data is stored.')
    parser.add_argument('--ckpt_file',
                        type=str,
                        required=True,
                        help='lstm ckpt file.')
    args = parser.parse_args()

    embedding_table = np.loadtxt(
        os.path.join(args.preprocess_path, "weight.txt")).astype(np.float32)
    network = SentimentNet(vocab_size=embedding_table.shape[0],
                           embed_size=cfg.embed_size,
                           num_hiddens=cfg.num_hiddens,
                           num_layers=cfg.num_layers,
                           bidirectional=cfg.bidirectional,
                           num_classes=cfg.num_classes,
                           weight=Tensor(embedding_table),
                           batch_size=cfg.batch_size)

    param_dict = load_checkpoint(args.ckpt_file)
    load_param_into_net(network, param_dict)

    input_arr = Tensor(
        np.random.uniform(0.0, 1e5, size=[64, 500]).astype(np.int32))
    export(network, input_arr, file_name="lstm", file_format="MINDIR")