| 69 | return var |
| 70 | |
| 71 | def to_tensor(self, ndarray, requires_grad=False, is_deep_copy=True): |
| 72 | if ndarray is None: |
| 73 | return ndarray |
| 74 | |
| 75 | # this part is meaningless if tensor is in gpu |
| 76 | if is_deep_copy: |
| 77 | ndarray = copy.deepcopy(ndarray) |
| 78 | |
| 79 | if isinstance(ndarray, list) and len(ndarray) > 0: |
| 80 | var_ = [] |
| 81 | for v in ndarray: |
| 82 | temp = torch.from_numpy(v) |
| 83 | temp = temp.to(self.device) |
| 84 | temp.requires_grad = requires_grad |
| 85 | var_.append(temp) |
| 86 | return var_ |
| 87 | if isinstance(ndarray, dict) and len(ndarray) > 0: |
| 88 | var_ = {} |
| 89 | for k, v in ndarray.iteritems(): |
| 90 | temp = torch.from_numpy(v) |
| 91 | temp = temp.to(self.device) |
| 92 | temp.requires_grad = requires_grad |
| 93 | var_[k] = temp |
| 94 | |
| 95 | return var_ |
| 96 | |
| 97 | #TODO:bug? |
| 98 | # ndarray = torch.from_numpy(ndarray).type(dtype) |
| 99 | ndarray = torch.from_numpy(ndarray) |
| 100 | ndarray = ndarray.to(self.device) |
| 101 | ndarray.requires_grad = requires_grad |
| 102 | |
| 103 | return ndarray |
| 104 | |
| 105 | def to_int_tensor(self, ndarray, requires_grad=False, is_deep_copy=True): |
| 106 | if ndarray is None: |