Browse Source

!27038 Modify Tensor API Note And Some Error Information.

Merge pull request !27038 from liuyang/tensor_ms
tags/v1.6.0
i-robot Gitee 4 years ago
parent
commit
1ed8956684
22 changed files with 70 additions and 74 deletions
  1. +14
    -20
      mindspore/common/tensor.py
  2. +1
    -1
      mindspore/context.py
  3. +1
    -1
      mindspore/nn/cell.py
  4. +2
    -2
      mindspore/nn/metrics/__init__.py
  5. +3
    -3
      mindspore/nn/metrics/accuracy.py
  6. +1
    -1
      mindspore/nn/metrics/bleu_score.py
  7. +1
    -1
      mindspore/nn/metrics/confusion_matrix.py
  8. +1
    -1
      mindspore/nn/metrics/cosine_similarity.py
  9. +4
    -4
      mindspore/nn/metrics/fbeta.py
  10. +1
    -1
      mindspore/nn/metrics/hausdorff_distance.py
  11. +1
    -1
      mindspore/nn/metrics/mean_surface_distance.py
  12. +2
    -1
      mindspore/nn/metrics/metric.py
  13. +1
    -1
      mindspore/nn/metrics/occlusion_sensitivity.py
  14. +3
    -3
      mindspore/nn/metrics/precision.py
  15. +3
    -3
      mindspore/nn/metrics/recall.py
  16. +1
    -1
      mindspore/nn/metrics/roc.py
  17. +1
    -1
      mindspore/nn/metrics/root_mean_square_surface_distance.py
  18. +9
    -9
      mindspore/nn/optim/optimizer.py
  19. +1
    -1
      mindspore/nn/wrap/cell_wrapper.py
  20. +1
    -1
      mindspore/nn/wrap/loss_scale.py
  21. +3
    -3
      mindspore/train/model.py
  22. +15
    -14
      mindspore/train/serialization.py

+ 14
- 20
mindspore/common/tensor.py View File

@@ -80,7 +80,7 @@ class Tensor(Tensor_):
>>> print(t2.shape)
()
>>> print(t2.dtype)
Float64
Float32
>>>
>>> # initialize a tensor with a tuple
>>> t3 = Tensor((1, 2))
@@ -125,8 +125,8 @@ class Tensor(Tensor_):
if isinstance(input_data, np.ndarray) and input_data.dtype not in valid_dtypes and \
input_data.dtype.kind != 'U': # Support dtype np.str_
raise TypeError(f"For Tensor, the input_data is a numpy array, "
f"but it's data type: {input_data.dtype} is not in supported list:\
{list(i.__name__ for i in valid_dtypes)}.")
f"but it's data type: {input_data.dtype} is not in supported list: "
f"{list(i.__name__ for i in valid_dtypes)}.")
if isinstance(input_data, (tuple, list)):
if np.array(input_data).dtype not in valid_dtypes:
raise TypeError(f"For Tensor, the input_data is {input_data} that contain unsupported element.")
@@ -454,7 +454,7 @@ class Tensor(Tensor_):
>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[1,2,3],[4,5,6]], dtype=np.float32))
>>> print(itemset((0,1), 4))
>>> print(x.itemset((0,1), 4))
[[1. 4. 3.]
[4. 5. 6.]]
>>> print(x)
@@ -477,7 +477,7 @@ class Tensor(Tensor_):
>>> import numpy as np
>>> x = Tensor(np.array([1, 2], dtype=np.float32))
>>> y = x.asnumpy()
>>> x[0] = 100
>>> y[0] = 11
>>> print(x)
[11. 2.]
>>> print(y)
@@ -888,15 +888,12 @@ class Tensor(Tensor_):
Examples:
>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.ones((1,2,2,1), dtype=np.float32))
>>> x = Tensor(np.ones((1,2,2), dtype=np.float32))
>>> print(x)
[[[[1.]
[1.]]

[[1.]
[1.]]]]
[[[1. 1.]
[1. 1.]]]
>>> print(x.shape)
(1, 2, 2, 1)
(1, 2, 2)
>>> y = x.squeeze()
>>> print(y)
[[1. 1.]
@@ -905,13 +902,10 @@ class Tensor(Tensor_):
(2, 2)
>>> y = x.squeeze(axis=0)
>>> print(y)
[[[1.]
[1.]]

[[1.]
[1.]]]
[[1. 1.]
[1. 1.]]
>>> print(y.shape)
(2, 2, 1)
(2, 2)
"""
self._init_check()
if axis is None:
@@ -1911,8 +1905,8 @@ class Tensor(Tensor_):
if not isinstance(ddof, int):
raise TypeError("For 'Tensor.var', the type of the argument 'ddof' must be int, but got "
"{}.".format(type(ddof)))
if not isinstance(keepdims, int):
raise TypeError("For 'Tensor.var', the type of the argument 'keepdims' must be int, but "
if not isinstance(keepdims, bool):
raise TypeError("For 'Tensor.var', the type of the argument 'keepdims' must be bool, but "
"got {}.".format(type(keepdims)))

if axis is None:


+ 1
- 1
mindspore/context.py View File

@@ -96,7 +96,7 @@ class _ThreadLocalInfo(threading.local):
def reserve_class_name_in_scope(self, reserve_class_name_in_scope):
"""Set whether to save the network class name in the scope."""
if not isinstance(reserve_class_name_in_scope, bool):
raise ValueError("For '_ThreadLocalInfo', the type of the property 'reserve_class_name_in_scope' must "
raise ValueError("For 'context.set_context', the type of the property 'reserve_class_name_in_scope' must "
"be bool, but got {}.".format(type(reserve_class_name_in_scope)))
self._reserve_class_name_in_scope = reserve_class_name_in_scope



+ 1
- 1
mindspore/nn/cell.py View File

@@ -215,7 +215,7 @@ class Cell(Cell_):
@cell_init_args.setter
def cell_init_args(self, value):
if not isinstance(value, str):
raise TypeError(f"For 'Cell', the property 'cell_init_args' must be bool type, "
raise TypeError(f"For 'Cell', the property 'cell_init_args' must be string type, "
f"but got type {type(value)}.")
self._cell_init_args = value



+ 2
- 2
mindspore/nn/metrics/__init__.py View File

@@ -120,8 +120,8 @@ def get_metric_fn(name, *args, **kwargs):
>>> metric = nn.get_metric_fn('precision', eval_type='classification')
"""
if name not in __factory__:
raise KeyError(f"Unsupported metric {name}, please refer to official website for more details about "
f"supported metrics.")
raise KeyError(f"For 'get_metric_fn', unsupported metric {name}, please refer to official website "
f"for more details about supported metrics.")
return __factory__[name](*args, **kwargs)




+ 3
- 3
mindspore/nn/metrics/accuracy.py View File

@@ -90,9 +90,9 @@ class Accuracy(EvaluationBase):
if self._class_num == 0:
self._class_num = y_pred.shape[1]
elif y_pred.shape[1] != self._class_num:
raise ValueError("Class number not match, last input predicted data contain {} classes, but current "
"predicted data contain {} classes, please check your predicted value(inputs[0])."
.format(self._class_num, y_pred.shape[1]))
raise ValueError("For 'Accuracy.update', class number not match, last input predicted data contain {} "
"classes, but current predicted data contain {} classes, please check your predicted "
"value(inputs[0]).".format(self._class_num, y_pred.shape[1]))

if self._type == 'classification':
indices = y_pred.argmax(axis=1)


+ 1
- 1
mindspore/nn/metrics/bleu_score.py View File

@@ -146,7 +146,7 @@ class BleuScore(Metric):

"""
if self._is_update is False:
raise RuntimeError('Please call the update method before calling eval method.')
raise RuntimeError("Please call the 'update' method before calling 'eval' method.")
if min(self._numerator) == 0.0:
return np.array(0.0)



+ 1
- 1
mindspore/nn/metrics/confusion_matrix.py View File

@@ -128,7 +128,7 @@ class ConfusionMatrix(Metric):
"""

if not self._is_update:
raise RuntimeError('Please call the update method before calling eval method.')
raise RuntimeError("Please call the 'update' method before calling 'eval' method.")

confusion_matrix = self.confusion_matrix.astype(float)



+ 1
- 1
mindspore/nn/metrics/cosine_similarity.py View File

@@ -92,7 +92,7 @@ class CosineSimilarity(Metric):
RuntimeError: If the update method is not called first, an error will be reported.
"""
if not self._is_update:
raise RuntimeError('Please call the update method before calling eval method.')
raise RuntimeError("Please call the 'update' method before calling 'eval' method.")

if self.zero_diagonal:
np.fill_diagonal(self.sqr_mtx_res, 0)


+ 4
- 4
mindspore/nn/metrics/fbeta.py View File

@@ -49,7 +49,7 @@ class Fbeta(Metric):
super(Fbeta, self).__init__()
self.eps = sys.float_info.min
if not beta > 0:
raise ValueError("For Fbeta, the argument 'beta' must be greater than 0, but got {}.".format(beta))
raise ValueError("For 'Fbeta', the argument 'beta' must be greater than 0, but got {}.".format(beta))
self.beta = beta
self.clear()

@@ -83,13 +83,13 @@ class Fbeta(Metric):
if self._class_num == 0:
self._class_num = y_pred.shape[1]
elif y_pred.shape[1] != self._class_num:
raise ValueError("Class number not match, last input predicted data contain {} classes, "
"but current predicted data contain {} classes, please check your "
raise ValueError("For 'Fbeta.update', class number not match, last input predicted data contain {} "
"classes, but current predicted data contain {} classes, please check your "
"predicted value(inputs[0]).".format(self._class_num, y_pred.shape[1]))
class_num = self._class_num

if y.max() + 1 > class_num:
raise ValueError("For 'Fbeta', predicted value(inputs[0]) and true value(inputs[1]) "
raise ValueError("For 'Fbeta.update', predicted value(inputs[0]) and true value(inputs[1]) "
"should contain same classes, but got predicted value contains {} classes"
" and true value contains {} classes.".format(class_num, y.max() + 1))
y = np.eye(class_num)[y.reshape(-1)]


+ 1
- 1
mindspore/nn/metrics/hausdorff_distance.py View File

@@ -314,7 +314,7 @@ class HausdorffDistance(Metric):
RuntimeError: If the update method is not called first, an error will be reported.
"""
if self._is_update is False:
raise RuntimeError('Please call the update method before calling eval method.')
raise RuntimeError("Please call the 'update' method before calling 'eval' method.")

hd = self._calculate_percent_hausdorff_distance(self.y_pred_edges, self.y_edges)
if self.directed:


+ 1
- 1
mindspore/nn/metrics/mean_surface_distance.py View File

@@ -166,7 +166,7 @@ class MeanSurfaceDistance(Metric):
RuntimeError: If the update method is not called first, an error will be reported.
"""
if self._is_update is False:
raise RuntimeError('Please call the update method before calling eval method.')
raise RuntimeError("Please call the 'update' method before calling 'eval' method.")

mean_surface_distance = self._get_surface_distance(self._y_pred_edges, self._y_edges)



+ 2
- 1
mindspore/nn/metrics/metric.py View File

@@ -94,7 +94,8 @@ class Metric(metaclass=ABCMeta):
elif isinstance(data, np.ndarray):
pass
else:
raise TypeError(f'The Input data type must be tensor, list or numpy.ndarray, but got {type(data)}.')
raise TypeError(f"For 'Metric' and its derived classes, the input data type must be tensor, list or "
f"numpy.ndarray, but got {type(data)}.")
return data

def _check_onehot_data(self, data):


+ 1
- 1
mindspore/nn/metrics/occlusion_sensitivity.py View File

@@ -205,7 +205,7 @@ class OcclusionSensitivity(Metric):

"""
if not self._is_update:
raise RuntimeError('Please call the update method before calling eval method.')
raise RuntimeError("Please call the 'update' method before calling 'eval' method.")

sensitivity = self._baseline - np.squeeze(self._sensitivity_im)



+ 3
- 3
mindspore/nn/metrics/precision.py View File

@@ -102,9 +102,9 @@ class Precision(EvaluationBase):
if self._class_num == 0:
self._class_num = y_pred.shape[1]
elif y_pred.shape[1] != self._class_num:
raise ValueError("Class number not match, last input predicted data contain {} classes, but current "
"predicted data contain {} classes, please check your predicted value(inputs[0])"
.format(self._class_num, y_pred.shape[1]))
raise ValueError("For 'Precision.update', class number not match, last input predicted data contain {} "
"classes, but current predicted data contain {} classes, please check your predicted "
"value(inputs[0])".format(self._class_num, y_pred.shape[1]))

class_num = self._class_num
if self._type == "classification":


+ 3
- 3
mindspore/nn/metrics/recall.py View File

@@ -103,9 +103,9 @@ class Recall(EvaluationBase):
if self._class_num == 0:
self._class_num = y_pred.shape[1]
elif y_pred.shape[1] != self._class_num:
raise ValueError("Class number not match, last input predicted data contain {} classes, but current "
"predicted data contain {} classes, please check your predicted value(inputs[0])."
.format(self._class_num, y_pred.shape[1]))
raise ValueError("For 'Recall.update', class number not match, last input predicted data contain {} "
"classes, but current predicted data contain {} classes, please check your predicted "
"value(inputs[0]).".format(self._class_num, y_pred.shape[1]))

class_num = self._class_num
if self._type == "classification":


+ 1
- 1
mindspore/nn/metrics/roc.py View File

@@ -206,7 +206,7 @@ class ROC(Metric):

"""
if self._is_update is False:
raise RuntimeError('Please call the update method before calling eval method.')
raise RuntimeError("Please call the 'update' method before calling 'eval' method.")

y_pred = np.squeeze(np.vstack(self.y_pred))
y = np.squeeze(np.vstack(self.y))


+ 1
- 1
mindspore/nn/metrics/root_mean_square_surface_distance.py View File

@@ -165,7 +165,7 @@ class RootMeanSquareDistance(Metric):

"""
if self._is_update is False:
raise RuntimeError('Please call the update method before calling eval method.')
raise RuntimeError("Please call the 'update' method before calling 'eval' method.")

residual_mean_square_distance = self._get_surface_distance(self._y_pred_edges, self._y_edges)



+ 9
- 9
mindspore/nn/optim/optimizer.py View File

@@ -395,8 +395,8 @@ class Optimizer(Cell):
return Tensor(np.array(list(learning_rate)).astype(np.float32))
if isinstance(learning_rate, Tensor):
if learning_rate.ndim > 1:
raise ValueError(f"For 'Optimizer', the dim of Tensor type 'learning_rate' should be a 0 or 1, "
f"but got {learning_rate.ndim}.")
raise ValueError(f"For 'Optimizer', if 'learning_rate' is Tensor type, then the dimension of it should "
f"be 0 or 1, but got {learning_rate.ndim}.")
if learning_rate.ndim == 1 and learning_rate.size < 2:
logger.warning("If use `Tensor` type dynamic learning rate, please make sure that the number "
"of elements in the tensor is greater than 1.")
@@ -523,8 +523,8 @@ class Optimizer(Cell):
for param in group_param['params']:
validator.check_value_type("parameter", param, [Parameter], self.cls_name)
if param.name in params_store:
raise RuntimeError(f"The {param.name} parameter already exists, it does not support "
f"repeated setting. Please check whether the optimizer parameter "
raise RuntimeError(f"For 'Optimizer', the {param.name} parameter already exists, it does not "
f"support repeated setting. Please check whether the optimizer parameter "
f"has been set multiple times.")

params_store.append(param.name)
@@ -627,8 +627,8 @@ class Optimizer(Cell):
for p in param_list:
validator.check_value_type("parameter", p, [Parameter], self.cls_name)
if id(p) not in ids:
raise ValueError(f"The parameter {p.name} is not in optimizer, please check whether the "
f"argument 'param' is correct.")
raise ValueError(f"For 'get_lr_parameter', the parameter {p.name} is not in optimizer, please check "
f"whether the argument 'param' is correct.")
if self.is_group_lr:
index = ids.index(id(p))
lr.append(get_lr_value(self.learning_rate[index]))
@@ -797,7 +797,7 @@ class _ConvertToCell(LearningRateSchedule):
def __init__(self, learning_rate):
super(_ConvertToCell, self).__init__()
if not isinstance(learning_rate, Parameter):
raise TypeError("For '_ConvertToCell', the argument 'learning_rate' must be Parameter, "
raise TypeError("For 'Optimizer', the argument 'learning_rate' must be Parameter, "
"but got {}.".format(type(learning_rate)))
self.learning_rate = learning_rate

@@ -811,10 +811,10 @@ class _IteratorLearningRate(LearningRateSchedule):
super(_IteratorLearningRate, self).__init__()
if isinstance(learning_rate, Tensor):
if learning_rate.ndim != 1:
raise ValueError(f"For '_IteratorLearningRate', the dimension of the argument 'learning_rate' should "
raise ValueError(f"For 'Optimizer', the dimension of the argument 'learning_rate' should "
f"be 1, but got {learning_rate.ndim}.")
else:
raise TypeError("For '_IteratorLearningRate', the argument 'learning_rate' should be Tensor, "
raise TypeError("For 'Optimizer', the argument 'learning_rate' should be Tensor, "
"but got {}.".format(type(learning_rate)))

self.learning_rate = Parameter(learning_rate, name)


+ 1
- 1
mindspore/nn/wrap/cell_wrapper.py View File

@@ -260,7 +260,7 @@ class ForwardValueAndGrad(Cell):
f"the type of 'get_by_list' should be bool, but got '{type(get_by_list)}'")
if get_by_list and not isinstance(weights, ParameterTuple):
raise TypeError(f"For 'ForwardValueAndGrad', "
f"when 'get_by_list' is set to True, the argument 'network' should be "
f"when 'get_by_list' is set to True, the argument 'weights' should be "
f"ParameterTuple type, but got '{type(weights)}'")
self.network = network
if isinstance(network, Cell):


+ 1
- 1
mindspore/nn/wrap/loss_scale.py View File

@@ -360,7 +360,7 @@ class TrainOneStepWithLossScaleCell(TrainOneStepCell):
self.scale_sense.set_data(sens)
else:
raise TypeError("For 'TrainOneStepWithLossScaleCell', "
"the input type must be Tensor, but got {}".format(type(sens)))
"the type of 'sens' must be Tensor, but got {}".format(type(sens)))

def start_overflow_check(self, pre_cond, compute_input):
"""


+ 3
- 3
mindspore/train/model.py View File

@@ -65,7 +65,7 @@ def _check_bpckpt_file_name_if_same_exist(directory, prefix):
suffix_num = 0
pre_len = len(prefix)
for filename in files:
if filename[-16:] != "breakpoint.ckpt":
if filename[-16:] != "_breakpoint.ckpt":
continue
# find same prefix file
if filename.find(prefix) == 0 and not filename[pre_len].isalpha():
@@ -78,7 +78,7 @@ def _check_bpckpt_file_name_if_same_exist(directory, prefix):
if num.isdigit():
suffix_num = max(suffix_num, int(num)+1)
if suffix_num != 0:
prefix = prefix + "" + str(suffix_num)
prefix = prefix + "_" + str(suffix_num)
return prefix


@@ -100,7 +100,7 @@ def _save_final_ckpt(func):
try:
func(self, *args, **kwargs)
except BaseException as e:
prefix = self._check_bpckpt_file_name_if_same_exist(obj._directory, obj._exception_prefix)
prefix = _check_bpckpt_file_name_if_same_exist(obj._directory, obj._exception_prefix)
cur_ckpoint_file = prefix + "-" + str(self._current_epoch_num) + "_" \
+ str(self._current_step_num) + "_breakpoint.ckpt"
cur_file = os.path.join(obj._directory, cur_ckpoint_file)


+ 15
- 14
mindspore/train/serialization.py View File

@@ -202,7 +202,7 @@ def _exec_save(ckpt_file_name, data_list, enc_key=None, enc_mode="AES-GCM"):
os.chmod(ckpt_file_name, stat.S_IRUSR)

except BaseException as e:
logger.critical("Failed to save the checkpoint file %s. May don't have the permission to write files, "
logger.critical("Failed to save the checkpoint file %s. Maybe don't have the permission to write files, "
"or the disk space is insufficient and so on.", ckpt_file_name)
raise e

@@ -438,7 +438,7 @@ def load_checkpoint(ckpt_file_name, net=None, strict_load=False, filter_prefix=N
else:
pb_content = _decrypt(ckpt_file_name, dec_key, len(dec_key), dec_mode)
if pb_content is None:
raise ValueError('Failed to decrypt the checkpoint file.')
raise ValueError("For 'load_checkpoint', Failed to decrypt the checkpoint file.")
checkpoint_list.ParseFromString(pb_content)
except BaseException as e:
if _is_cipher_file(ckpt_file_name):
@@ -447,8 +447,8 @@ def load_checkpoint(ckpt_file_name, net=None, strict_load=False, filter_prefix=N
else:
logger.critical("Failed to read the checkpoint file '%s' , may not have permission to read it, please "
"check the correct of the file.", ckpt_file_name)
raise ValueError(e.__str__() + "\nFailed to read the checkpoint file {}, may not have permission to "
"read it.".format(ckpt_file_name))
raise ValueError(e.__str__() + "\nFor 'load_checkpoint', failed to read the checkpoint file {}, may not have "
"permission to read it.".format(ckpt_file_name))

parameter_dict = {}
try:
@@ -1413,11 +1413,12 @@ def merge_sliced_parameter(sliced_parameters, strategy=None):
if parameter.name != parameter_name \
or len(parameter.data.shape) != parameter_shape_length \
or parameter.data.shape[1:] != parameter_shape[1:]:
raise ValueError(f"Please make sure that the elements in 'slice_parameters' have the same name, "
f"dimension length and shape except 0 axis. The name, dimension length, shape except "
f"0 axis should be {parameter_name}, {parameter_shape_length}, {parameter_shape[1:]}, "
f"but got name: {parameter.name}, dimension length: {len(parameter.data.shape)}, "
f"shape except 0 axis: {parameter.data.shape[1:]} at index {index}.")
raise ValueError(f"For 'merge_sliced_parameter', please make sure that the elements in 'slice_parameters'"
f" have the same name, dimension length and shape except 0 axis. The name, dimension "
f"length, shape except 0 axis should be {parameter_name}, {parameter_shape_length}, "
f"{parameter_shape[1:]}, but got name: {parameter.name}, dimension length: "
f"{len(parameter.data.shape)}, shape except 0 axis: {parameter.data.shape[1:]} "
f"at index {index}.")

if parameter.data.shape != parameter_shape:
is_even = False
@@ -1432,8 +1433,8 @@ def merge_sliced_parameter(sliced_parameters, strategy=None):

else:
if parameter_name not in strategy.keys():
raise KeyError(f"The parameter name {parameter_name} should be a key in the 'strategy'. Please check "
f"'sliced_parameter' and 'strategy'.")
raise KeyError(f"For 'merge_sliced_parameter', the parameter name {parameter_name} should be a key in "
f"the 'strategy'. Please check 'sliced_parameter' and 'strategy'.")
merged_tensor = _merge_param_with_strategy(sliced_data, parameter_name, strategy, is_even)
merged_parameter = Parameter(merged_tensor, parameter_name, requires_grad, layerwise_parallel)

@@ -1553,9 +1554,9 @@ def load_distributed_checkpoint(network, checkpoint_filenames, predict_strategy=
except BaseException as e:
logger.critical("Failed to load opt shard slice in load distributed checkpoint for {}. Data shape is {}"
" and group is {}".format(param.name, split_param.data.shape, opt_shard_group))
raise RuntimeError(e.__str__() + f"\nFailed to load opt shard slice in load distributed "
f"checkpoint for {param.name}. Data shape is {split_param.data.shape} "
f"and group is {opt_shard_group}.")
raise RuntimeError(e.__str__() + f"\nFor 'load_distributed_checkpoint', failed to load opt shard slice"
f" in load distributed checkpoint for {param.name}. Data shape is "
f"{split_param.data.shape} and group is {opt_shard_group}.")
split_param = Parameter(Tensor(data_slice), param.name,
split_param.requires_grad, split_param.layerwise_parallel)
param_dict[param.name] = split_param


Loading…
Cancel
Save