def tensor(self, shape, variant=None): key = construct_minmax_tensor_key(shape) # If a tensor with these properties exists already, then load it. if os.path.exists(key): return self.loader.load(key) else: # Otherwise, we must create a random tensor with the desired properties, # dump it to the output file, then return it. crds = self.generate_crds(shape) values = dict() for c in crds: ind_list = numpy.random.rand(2) * shape[-1] ind_list = ind_list.astype('int') start = numpy.min(ind_list) stop = numpy.max(ind_list) for i in range(start, stop): temp = tuple(c[1:] + [i]) values[temp] = int(20 * numpy.random.rand() - 10) dok = sparse.DOK(shape, values) ind = tuple([e - 1 for e in shape]) write_shape = False if ind in values.keys() else True TnsFileDumper().dump_dict_to_file(shape, dok.data, key, write_shape) result = dok.asformat('coo') return result
def random(self, shape, sparsity, variant=None): key = construct_random_tensor_key(shape, sparsity, variant) # If a tensor with these properties exists already, then load it. if os.path.exists(key): return self.loader.load(key) else: # Otherwise, we must create a random tensor with the desired properties, # dump it to the output file, then return it. result = sparse.random(shape, density=sparsity) dok = sparse.DOK(result) TnsFileDumper().dump_dict_to_file(shape, dok.data, key) return result
def sparse_window(self, num, variant=3): path = TENSOR_PATH key = "image" + str(num) + "-" + str(variant) + ".tns" key = os.path.join(path, "image", "tensors", key) shape = self.shape[num] if os.path.exists(key): return self.loader.load(key) else: result_np = self.dense_window(num) result = sparse.COO.from_numpy(result_np) dok = sparse.DOK(result) write_shape = result_np.flat[-1] == 0 TnsFileDumper().dump_dict_to_file(shape, dok.data, key, write_shape) return result
def sparse_image(self, num, pt, variant=None, path='no'): key = construct_image_tensor_key(num, pt, variant) # If an image with these properties exists already, then load it. if os.path.exists(key): result = self.loader.load(key) self.shape[num] = result.shape return result else: # Otherwise, we must create load the image and preprocess it with the desired properties. # dump it to the output file, then return it. bin_img = self.dense_image(num, pt, variant, path) result = sparse.COO.from_numpy(bin_img) dok = sparse.DOK(result) write_shape = bin_img.flat[-1] == 0 TnsFileDumper().dump_dict_to_file(self.shape[num], dok.data, key, write_shape) return result
def dump(self, tensor, path): self.dumper.dump_dict_to_file(tensor.shape, sparse.DOK(tensor).data, path)