Skip to content
Snippets Groups Projects
Commit 75652270 authored by wangzeyangyi's avatar wangzeyangyi
Browse files

update deprecated API

pylint
parent bdf2d8bc
No related branches found
No related tags found
No related merge requests found
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -16,11 +16,10 @@
##############test textcnn example on movie review#################
python eval.py
"""
import mindspore as ms
import mindspore.nn as nn
from mindspore.nn.metrics import Accuracy
from mindspore import context
from mindspore.train.model import Model
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from model_utils.moxing_adapter import moxing_wrapper
from model_utils.device_adapter import get_device_id
......@@ -38,9 +37,9 @@ def eval_net():
elif config.dataset == 'SST2':
instance = SST2(root_dir=config.data_path, maxlen=config.word_len, split=0.9)
device_target = config.device_target
context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
ms.set_context(mode=ms.GRAPH_MODE, device_target=config.device_target)
if device_target == "Ascend":
context.set_context(device_id=get_device_id())
ms.set_context(device_id=get_device_id())
dataset = instance.create_test_dataset(batch_size=config.batch_size)
loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
net = TextCNN(vocab_len=instance.get_dict_len(), word_len=config.word_len,
......@@ -48,10 +47,10 @@ def eval_net():
opt = nn.Adam(filter(lambda x: x.requires_grad, net.get_parameters()), learning_rate=0.001,
weight_decay=float(config.weight_decay))
param_dict = load_checkpoint(config.checkpoint_file_path)
param_dict = ms.load_checkpoint(config.checkpoint_file_path)
print("load checkpoint from [{}].".format(config.checkpoint_file_path))
load_param_into_net(net, param_dict)
ms.load_param_into_net(net, param_dict)
net.set_train(False)
model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc': Accuracy()})
......
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -18,16 +18,17 @@ python export.py
"""
import os
import numpy as np
from mindspore import Tensor, load_checkpoint, load_param_into_net, export, context
import mindspore as ms
from mindspore import Tensor
from model_utils.config import config
from model_utils.moxing_adapter import moxing_wrapper
from src.textcnn import TextCNN
from src.dataset import MovieReview, SST2, Subjectivity
context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
ms.set_context(mode=ms.GRAPH_MODE, device_target=config.device_target)
if config.device_target == "Ascend":
context.set_context(device_id=config.device_id)
ms.set_context(device_id=config.device_id)
def modelarts_pre_process():
'''modelarts pre process function.'''
......@@ -48,11 +49,11 @@ def run_export():
net = TextCNN(vocab_len=instance.get_dict_len(), word_len=config.word_len,
num_classes=config.num_classes, vec_length=config.vec_length)
param_dict = load_checkpoint(config.checkpoint_file_path)
load_param_into_net(net, param_dict)
param_dict = ms.load_checkpoint(config.checkpoint_file_path)
ms.load_param_into_net(net, param_dict)
input_arr = Tensor(np.ones([config.batch_size, config.word_len], np.int32))
export(net, input_arr, file_name=config.file_name, file_format=config.file_format)
ms.export(net, input_arr, file_name=config.file_name, file_format=config.file_format)
if __name__ == '__main__':
run_export()
# Copyright 2021 Huawei Technologies Co., Ltd
# Copyright 2021-2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -17,7 +17,7 @@
import os
import functools
from mindspore import context
import mindspore as ms
from model_utils.config import config
_global_sync_count = 0
......@@ -92,7 +92,7 @@ def moxing_wrapper(pre_process=None, post_process=None):
sync_data(config.train_url, config.output_path)
print("Workspace downloaded: ", os.listdir(config.output_path))
context.set_context(save_graphs_path=os.path.join(config.output_path, str(get_rank_id())))
ms.set_context(save_graphs_path=os.path.join(config.output_path, str(get_rank_id())))
config.device_num = get_device_num()
config.device_id = get_device_id()
if not os.path.exists(config.output_path):
......
"""
# Copyright 2021 Huawei Technologies Co., Ltd
# Copyright 2021-2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -24,12 +24,11 @@ from model_utils.config import config
from src.textcnn import TextCNN
from src.textcnn import SoftmaxCrossEntropyExpand
from src.dataset import MovieReview, SST2, Subjectivity
import mindspore as ms
import mindspore.nn as nn
from mindspore.nn.metrics import Accuracy
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
from mindspore.train.model import Model
from mindspore import Tensor, load_checkpoint, load_param_into_net, export, context
import numpy as np
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)),
'../'))
......@@ -76,14 +75,14 @@ def run_export():
ckpt_models = list(
sorted(glob.glob(os.path.join(load_checkpoint_path, '*.ckpt')),
key=os.path.getctime))[-1]
param_dict = load_checkpoint(ckpt_models)
load_param_into_net(net, param_dict)
param_dict = ms.load_checkpoint(ckpt_models)
ms.load_param_into_net(net, param_dict)
input_arr = Tensor(np.ones([config.batch_size, config.word_len], np.int32))
export(net,
input_arr,
file_name=config.file_name,
file_format=config.file_format)
input_arr = ms.numpy.ones([config.batch_size, config.word_len], ms.int32)
ms.export(net,
input_arr,
file_name=config.file_name,
file_format=config.file_format)
@moxing_wrapper(pre_process=modelarts_pre_process)
......@@ -93,9 +92,9 @@ def train_net():
the function is adapted to Huawei Clouds Modelarts platform
"""
# set context
context.set_context(mode=context.GRAPH_MODE,
device_target=config.device_target)
context.set_context(device_id=get_device_id())
ms.set_context(mode=ms.GRAPH_MODE,
device_target=config.device_target)
ms.set_context(device_id=get_device_id())
if config.dataset == 'MR':
instance = MovieReview(root_dir=config.data_path,
maxlen=config.word_len,
......@@ -141,8 +140,8 @@ def train_net():
vec_length=config.vec_length)
# Continue training if set pre_trained to be True
if config.pre_trained:
param_dict = load_checkpoint(config.checkpoint_path)
load_param_into_net(net, param_dict)
param_dict = ms.load_checkpoint(config.checkpoint_path)
ms.load_param_into_net(net, param_dict)
opt = nn.Adam(filter(lambda x: x.requires_grad, net.get_parameters()),
learning_rate=learning_rate,
......
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -23,8 +23,7 @@ from pathlib import Path
import numpy as np
import pandas as pd
import mindspore.dataset as ds
from mindspore import context
import mindspore as ms
class Generator():
......@@ -129,7 +128,7 @@ class MovieReview(DataProcessor):
self.maxlen = float("-inf")
self.Pos = []
self.Neg = []
if context.get_context("device_target") == "CPU":
if ms.get_context("device_target") == "CPU":
encoding = "Latin1"
else:
encoding = None
......
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -14,11 +14,10 @@
# ============================================================================
"""TextCNN"""
import mindspore.ops as ops
import mindspore.nn as nn
import mindspore.ops.operations as P
from mindspore import Tensor
from mindspore.nn.cell import Cell
import mindspore.ops.functional as F
import mindspore
......@@ -54,21 +53,21 @@ class SoftmaxCrossEntropyExpand(Cell):
"""
def __init__(self, sparse=False):
super(SoftmaxCrossEntropyExpand, self).__init__()
self.exp = P.Exp()
self.reduce_sum = P.ReduceSum(keep_dims=True)
self.onehot = P.OneHot()
self.exp = ops.Exp()
self.reduce_sum = ops.ReduceSum(keep_dims=True)
self.onehot = ops.OneHot()
self.on_value = Tensor(1.0, mindspore.float32)
self.off_value = Tensor(0.0, mindspore.float32)
self.div = P.Div()
self.log = P.Log()
self.sum_cross_entropy = P.ReduceSum(keep_dims=False)
self.mul = P.Mul()
self.mul2 = P.Mul()
self.cast = P.Cast()
self.reduce_mean = P.ReduceMean(keep_dims=False)
self.div = ops.Div()
self.log = ops.Log()
self.sum_cross_entropy = ops.ReduceSum(keep_dims=False)
self.mul = ops.Mul()
self.mul2 = ops.Mul()
self.cast = ops.Cast()
self.reduce_mean = ops.ReduceMean(keep_dims=False)
self.sparse = sparse
self.reduce_max = P.ReduceMax(keep_dims=True)
self.sub = P.Sub()
self.reduce_max = ops.ReduceMax(keep_dims=True)
self.sub = ops.Sub()
def construct(self, logit, label):
"""
......@@ -79,11 +78,11 @@ class SoftmaxCrossEntropyExpand(Cell):
exp_sum = self.reduce_sum(exp, -1)
softmax_result = self.div(exp, exp_sum)
if self.sparse:
label = self.onehot(label, F.shape(logit)[1], self.on_value, self.off_value)
label = self.onehot(label, ops.shape(logit)[1], self.on_value, self.off_value)
softmax_result_log = self.log(softmax_result)
loss = self.sum_cross_entropy((self.mul(softmax_result_log, label)), -1)
loss = self.mul2(F.scalar_to_array(-1.0), loss)
loss = self.mul2(ops.scalar_to_array(-1.0), loss)
loss = self.reduce_mean(loss, -1)
return loss
......@@ -103,19 +102,19 @@ class TextCNN(nn.Cell):
self.word_len = word_len
self.num_classes = num_classes
self.unsqueeze = P.ExpandDims()
self.unsqueeze = ops.ExpandDims()
self.embedding = nn.Embedding(vocab_len, self.vec_length, embedding_table=embedding_table)
self.slice = P.Slice()
self.slice = ops.Slice()
self.layer1 = self.make_layer(kernel_height=3)
self.layer2 = self.make_layer(kernel_height=4)
self.layer3 = self.make_layer(kernel_height=5)
self.concat = P.Concat(1)
self.concat = ops.Concat(1)
self.fc = nn.Dense(96*3, self.num_classes)
self.drop = nn.Dropout(keep_prob=0.5)
self.reducemax = P.ReduceMax(keep_dims=False)
self.reducemax = ops.ReduceMax(keep_dims=False)
def make_layer(self, kernel_height):
return nn.SequentialCell(
......
# Copyright 2020 Huawei Technologies Co., Ltd
# Copyright 2020-2022 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
......@@ -19,12 +19,11 @@ python train.py
import os
import math
import mindspore as ms
import mindspore.nn as nn
from mindspore.nn.metrics import Accuracy
from mindspore import context
from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor
from mindspore.train.model import Model
from mindspore.train.serialization import load_checkpoint, load_param_into_net
from mindspore.common import set_seed
from model_utils.moxing_adapter import moxing_wrapper
......@@ -44,14 +43,14 @@ def modelarts_pre_process():
def train_net():
'''train net'''
# set context
context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target)
context.set_context(device_id=get_device_id())
ms.set_context(mode=ms.GRAPH_MODE, device_target=config.device_target)
ms.set_context(device_id=get_device_id())
if config.dataset == 'MR':
instance = MovieReview(root_dir=config.data_path, maxlen=config.word_len, split=0.9)
elif config.dataset == 'SUBJ':
instance = Subjectivity(root_dir=config.data_path, maxlen=config.word_len, split=0.9)
if config.device_target == "GPU":
context.set_context(enable_graph_kernel=True)
ms.set_context(enable_graph_kernel=True)
elif config.dataset == 'SST2':
instance = SST2(root_dir=config.data_path, maxlen=config.word_len, split=0.9)
......@@ -71,8 +70,8 @@ def train_net():
num_classes=config.num_classes, vec_length=config.vec_length)
# Continue training if set pre_trained to be True
if config.pre_trained:
param_dict = load_checkpoint(config.checkpoint_path)
load_param_into_net(net, param_dict)
param_dict = ms.load_checkpoint(config.checkpoint_path)
ms.load_param_into_net(net, param_dict)
opt = nn.Adam(filter(lambda x: x.requires_grad, net.get_parameters()), \
learning_rate=learning_rate, weight_decay=float(config.weight_decay))
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment