示例#1
0
    def test(self, dbg=None):

        npTrain = numpy.load(self.ntmTrainFpath)
        l = len(npTrain)
        ndX = nd.empty((l, 150))
        ndY = nd.empty((l, 20))
        for i, row in enumerate(npTrain):
            npDescriptionVector = row[0]
            ndX[i] = nd.array(npDescriptionVector)
            npGeoVector = row[2]
            ndY[i] = nd.array(npGeoVector)

        dataset = mx.gluon.data.dataset.ArrayDataset(ndX, ndY)

        #http://mxnet.incubator.apache.org/versions/master/tutorials/gluon/datasets.html
        from multiprocessing import cpu_count
        CPU_COUNT = cpu_count()

        CPU_COUNT = 1

        data_loader = mx.gluon.data.DataLoader(
            dataset, batch_size=5)  #, num_workers=CPU_COUNT)

        for X_batch, y_batch in data_loader:
            print("X_batch has shape {}, and y_batch has shape {}".format(
                X_batch.shape, y_batch.shape))

        ndTrain0 = nd.array(npTrain[:, 0])

        mx.random.seed(42)  # Fix the seed for reproducibility
        X = mx.random.uniform(shape=(10, 3))
        y = mx.random.uniform(shape=(10, 1))
        dataset = mx.gluon.data.dataset.ArrayDataset(X, y)

        x = 1
示例#2
0
 def backward(self, dZ):
     ctx = context(dZ)
     X, Y = self.saved_tensors
     gidx, op = self.gidx, self.op
     lhs_target, rhs_target = self.lhs_target, self.rhs_target
     dX, dY = nd.empty((), ctx=ctx), nd.empty((), ctx=ctx)
     if op != 'copy_rhs':
         if lhs_target in ['u', 'v']:
             _gidx = gidx if self.lhs_target == 'v' else gidx.reverse()
             if op in ['add', 'sub', 'copy_lhs']:
                 dX = _gspmm(_gidx, 'copy_rhs', 'sum', None, dZ)[0]
             else:  # mul, div, dot
                 if rhs_target == lhs_target:
                     dX = _gspmm(_gidx, 'copy_rhs', 'sum', None,
                                 dZ)[0] * _muldiv(op, Y)
                 elif self.rhs_target == 'e':
                     dX = _gspmm(_gidx, 'copy_rhs', 'sum', None,
                                 dZ * _muldiv(op, Y))[0]
                 else:  # rhs_target = !lhs_target
                     dX = _gspmm(_gidx, 'mul', 'sum', _muldiv(op, Y), dZ)[0]
         else:  # lhs_target == 'e'
             if op in ['add', 'sub', 'copy_lhs']:
                 dX = dZ
             else:  # mul, div, dot
                 dX = _gsddmm(gidx, 'mul', dZ, _muldiv(op, Y), 'e',
                              rhs_target)
         dX = _reduce_grad(dX, X.shape)
     if op != 'copy_lhs':
         if self.rhs_target in ['u', 'v']:
             _gidx = gidx if rhs_target == 'v' else gidx.reverse()
             if op in ['add', 'sub', 'copy_rhs']:
                 dY = _gspmm(_gidx, 'copy_rhs', 'sum', None,
                             _addsub(op, dZ))[0]
             else:  # mul, div, dot
                 if lhs_target == rhs_target:
                     dY = _gspmm(_gidx, 'copy_rhs', 'sum', None, dZ)[0] * X
                 elif self.lhs_target == 'e':
                     dY = _gspmm(_gidx, 'copy_rhs', 'sum', None, dZ * X)[0]
                 else:  # rhs_target = !lhs_target
                     dY = _gspmm(_gidx, 'mul', 'sum', X, dZ)[0]
                 if op == 'div': dY = -dY / (Y**2)
         else:
             if op in ['add', 'sub', 'copy_rhs']:
                 dY = _addsub(op, dZ)
             else:  # mul, div, dot
                 dY = _gsddmm(gidx, 'mul', dZ, X, 'e', lhs_target)
                 if op == 'div': dY = -dY / (Y**2)
         dY = _reduce_grad(dY, Y.shape)
     self.saved_tensors = None
     return dX, dY
示例#3
0
 def backward(self, dZ):
     ctx = context(dZ)
     X, Y, argX, argY = self.saved_tensors
     gidx, op, reduce_op = self.gidx, self.op, self.reduce_op
     dX, dY = nd.empty((), ctx=ctx), nd.empty((), ctx=ctx)
     if op != 'copy_rhs' and X.grad is not None:
         g_rev = gidx.reverse()
         if reduce_op == 'sum':
             if op in ['mul', 'div']:
                 dX = _gspmm(g_rev, 'mul', 'sum', dZ, _muldiv(op, Y))[0]
             elif op in ['add', 'sub']:
                 dX = _gspmm(g_rev, 'copy_lhs', 'sum', dZ, Y)[0]
             elif op == 'copy_lhs':
                 dX = _gspmm(g_rev, 'copy_lhs', 'sum', dZ, None)[0]
         else:
             if op in ['mul', 'div']:
                 dX = _scatter_nd(
                     argX,
                     _muldiv(
                         op,
                         _gather_nd(
                             argY,
                             Y.broadcast_to(
                                 (Y.shape[0], *dZ.shape[1:])))) * dZ,
                     X.shape[0])
             elif op in ['add', 'sub', 'copy_lhs']:
                 dX = _scatter_nd(argX, dZ, X.shape[0])
         dX = _reduce_grad(dX, X.shape)
     if op != 'copy_lhs' and Y.grad is not None:
         if reduce_op == 'sum':
             if op in ['mul', 'div']:
                 dY = _gsddmm(gidx, 'mul', X, dZ)
                 if op == 'div': dY = -dY / (Y**2)
             elif op in ['add', 'sub', 'copy_rhs']:
                 dY = _gsddmm(gidx, 'copy_rhs', X, _addsub(op, dZ))
         else:
             if op in ['mul', 'div']:
                 dY = _scatter_nd(
                     argY,
                     _gather_nd(argX,
                                X.broadcast_to(
                                    (X.shape[0], *dZ.shape[1:]))) * dZ,
                     Y.shape[0])
                 if op == 'div': dY = -dY / (Y**2)
             elif op in ['add', 'sub', 'copy_rhs']:
                 dY = _scatter_nd(argY, _addsub(op, dZ), Y.shape[0])
         dY = _reduce_grad(dY, Y.shape)
     self.saved_tensors = None
     return dX, dY
    def store_samples(self, data, y, query_network, store_prob, context):
        if not (self.memory_replacement_strategy == "no_replacement" and self.max_stored_samples != -1 and self.key_memory.shape[0] >= self.max_stored_samples):
            num_pus = len(data)
            sub_batch_sizes = [data[i][0][0].shape[0] for i in range(num_pus)]
            num_inputs = len(data[0][0])
            num_outputs = len(y)
            mx_context = context[0]

            if len(self.key_memory) == 0:
                self.key_memory = nd.empty(0, ctx=mx.cpu())
                self.value_memory = []
                self.label_memory = []#nd.empty((num_outputs, 0), ctx=mx.cpu())

            ind = [nd.sample_multinomial(store_prob, sub_batch_sizes[i]).as_in_context(mx_context) for i in range(num_pus)]

            max_inds = [nd.max(ind[i]) for i in range(num_pus)]
            if any(max_inds):
                to_store_values = []
                for i in range(num_inputs):
                    tmp_values = []
                    for j in range(0, num_pus):
                        if max_inds[j]:
                            if isinstance(tmp_values, list):
                                tmp_values = nd.contrib.boolean_mask(data[j][0][i].as_in_context(mx_context), ind[j])
                            else:
                                tmp_values = nd.concat(tmp_values, nd.contrib.boolean_mask(data[j][0][i].as_in_context(mx_context), ind[j]), dim=0)
                    to_store_values.append(tmp_values)

                to_store_labels = []
                for i in range(num_outputs):
                    tmp_labels = []
                    for j in range(0, num_pus):
                        if max_inds[j]:
                            if isinstance(tmp_labels, list):
                                tmp_labels = nd.contrib.boolean_mask(y[i][j].as_in_context(mx_context), ind[j])
                            else:
                                tmp_labels = nd.concat(tmp_labels, nd.contrib.boolean_mask(y[i][j].as_in_context(mx_context), ind[j]), dim=0)
                    to_store_labels.append(tmp_labels)

                to_store_keys = query_network(*to_store_values[0:self.query_net_num_inputs])

                if self.key_memory.shape[0] == 0:
                    self.key_memory = to_store_keys.as_in_context(mx.cpu())
                    for i in range(num_inputs):
                        self.value_memory.append(to_store_values[i].as_in_context(mx.cpu()))
                    for i in range(num_outputs):
                        self.label_memory.append(to_store_labels[i].as_in_context(mx.cpu()))
                elif self.memory_replacement_strategy == "replace_oldest" and self.max_stored_samples != -1 and self.key_memory.shape[0] >= self.max_stored_samples:
                    num_to_store = to_store_keys.shape[0]
                    self.key_memory = nd.concat(self.key_memory[num_to_store:], to_store_keys.as_in_context(mx.cpu()), dim=0)
                    for i in range(num_inputs):
                        self.value_memory[i] = nd.concat(self.value_memory[i][num_to_store:], to_store_values[i].as_in_context(mx.cpu()), dim=0)
                    for i in range(num_outputs):
                        self.label_memory[i] = nd.concat(self.label_memory[i][num_to_store:], to_store_labels[i].as_in_context(mx.cpu()), dim=0)
                else:
                    self.key_memory = nd.concat(self.key_memory, to_store_keys.as_in_context(mx.cpu()), dim=0)
                    for i in range(num_inputs):
                        self.value_memory[i] = nd.concat(self.value_memory[i], to_store_values[i].as_in_context(mx.cpu()), dim=0)
                    for i in range(num_outputs):
                        self.label_memory[i] = nd.concat(self.label_memory[i], to_store_labels[i].as_in_context(mx.cpu()), dim=0)
示例#5
0
    def tokenize(self, path, ctx=None):
        """Tokenizes a text file."""
        assert os.path.exists(path)
        # Add words to the dictionary
        with open(path, 'r', encoding='utf-8') as f:
            tokens = 0
            for line in f:
                words = line.split() + ['<eos>']
                tokens += len(words)
                for word in words:
                    self.dictionary.add_word(word)

        # Tokenize file content
        sents = []
        with open(path, 'r', encoding='utf-8') as f:
            for line in f:
                if not line:
                    continue
                words = line.split() + ['<eos>']
                sent = nd.empty(len(words), ctx, dtype=np.int64)
                for i, word in enumerate(words):
                    sent[i] = self.dictionary.word2idx[word]
                sents.append(sent)

        return sents
示例#6
0
    def tokenize(self, path, ctx=None):
        """ Tokenizes a text file into a list of indexes of tokens."""
        assert os.path.exists(path)
        # Add words to the dictionary
        with open(path, 'r', encoding='utf-8') as f:
            tokens = 0
            for line in f:
                words = line.split() + ['<eos>']
                tokens += len(words)
                for word in words:
                    self.dictionary.add_word(word)

        # Tokenize file content
        with open(path, 'r', encoding='utf-8') as f:
            ids = nd.empty(self.debug if self.debug else tokens,
                           ctx,
                           dtype=np.int64)
            token = 0
            for line in f:
                words = line.split() + ['<eos>']
                for word in words:
                    ids[token] = self.dictionary.word2idx[word]
                    token += 1
                    if self.debug and token >= self.debug:
                        return ids

        return ids
def clip_grad(grads: Union[Generator[NDArray, NDArray, NDArray], List[NDArray],
                           Tuple[NDArray]],
              clip_method: GradientClippingMethod,
              clip_val: float,
              inplace=True) -> List[NDArray]:
    """
    Clip gradient values inplace
    :param grads: gradients to be clipped
    :param clip_method: clipping method
    :param clip_val: clipping value. Interpreted differently depending on clipping method.
    :param inplace: modify grads if True, otherwise create NDArrays
    :return: clipped gradients
    """
    output = list(grads) if inplace else list(nd.empty(g.shape) for g in grads)
    if clip_method == GradientClippingMethod.ClipByGlobalNorm:
        norm_unclipped_grads = global_norm(grads)
        scale = clip_val / (norm_unclipped_grads.asscalar() + 1e-8
                            )  # todo: use branching operators?
        if scale < 1.0:
            for g, o in zip(grads, output):
                nd.broadcast_mul(g, nd.array([scale]), out=o)
    elif clip_method == GradientClippingMethod.ClipByValue:
        for g, o in zip(grads, output):
            g.clip(-clip_val, clip_val, out=o)
    elif clip_method == GradientClippingMethod.ClipByNorm:
        for g, o in zip(grads, output):
            nd.broadcast_mul(g,
                             nd.minimum(1.0, clip_val / (g.norm() + 1e-8)),
                             out=o)
    else:
        raise KeyError('Unsupported gradient clipping method')
    return output
示例#8
0
def data_preprocess():
    '''
    A is the adjacency matrix of the road graph
    X is the feature matrix
    '''
    with open('remainID5min.json', 'r') as f:
        data = json.loads(f.read().strip())

    indices = sorted([int(i[:i.find(',')]) for i in data.keys()])
    indices_dict = {i: index for index, i in enumerate(indices)}

    new_data = {
        indices_dict[int(key[:key.find(',')])]: value
        for key, value in data.items()
    }

    with open('thre3.5twoPointIndex.txt', 'r') as f:
        t = (list(map(int, i.strip().split(','))) for i in f.readlines())

    A = nd.zeros(shape=(len(indices), len(indices)), ctx=ctx)
    for x, y in t:
        A[x, y] = 1.0

    X = nd.empty(shape=(len(indices), 3, 16992), ctx=ctx)
    for i in range(len(indices)):
        data = new_data[i]
        X[i, 0, :] = nd.array(data['flow'], ctx=ctx)
        X[i, 1, :] = nd.array(data['occupy'], ctx=ctx)
        X[i, 2, :] = nd.array(data['speed'], ctx=ctx)
    return A, X
示例#9
0
    def step(self, s, num_steps_so_far):
        # s is channel-last, NHWC
        s = np.transpose(s, (0, 3, 1, 2))
        s = nd.array(s, ctx=self.args.ctx)

        value, logits = self.net(s)
        # action = nd.argmax(logits, axis=1)
        # action = self.net.sample(logits)
        # logpac = self.net.log_prob(logits, action)

        # Epsilon greedy exploration
        eps = np.maximum(1. - num_steps_so_far / self.args.annealing_end,
                         self.args.epsilon_min)
        action = nd.empty(shape=s.shape[0], ctx=self.args.ctx)
        logits_np = logits.asnumpy()
        for i in range(s.shape[0]):
            sample = np.random.random()
            if sample < eps:
                ac = random.randint(0, self.action_dim - 1)
            else:
                ac = int(np.argmax(logits[i]))
            action[i] = ac

        # Pick the probability of the chosen action
        logpac = nd.pick(logits, action, 1)

        # Reshaping the output
        logpac = logpac.asnumpy().reshape((-1))
        action = action.asnumpy().astype(np.int32).reshape((-1))
        value = value.asnumpy().reshape((-1))

        return value, action, logpac
示例#10
0
 def _init_training(self):
     self.frame_counter = 0.  # Counts the number of steps so far
     self.annealing_count = 0.  # Counts the number of annealing steps
     self.epis_count = 0.  # Counts the number episodes so far
     self.replay_memory = Replay_Buffer(
         self.opt.replay_buffer_size)  # Initialize the replay buffer
     self.tot_clipped_reward = []
     self.tot_reward = []
     self.frame_count_record = []
     self.moving_average_clipped = 0.
     self.moving_average = 0.
     self.batch_state = nd.empty((self.opt.batch_size, self.opt.frame_len,
                                  self.opt.image_size, self.opt.image_size),
                                 self.opt.ctx)
     self.batch_state_next = nd.empty(
         (self.opt.batch_size, self.opt.frame_len, self.opt.image_size,
          self.opt.image_size), self.opt.ctx)
     self.batches = np.arange(self.opt.batch_size)
示例#11
0
def predict(batch_file):
    batch_data = nd.empty((batch_size, 3, 224, 224), ctx)
    for i in range(0, batch_size):
        fn = batch_file[i]
        img = mx.image.imread(fn)
        img = transform(img)
        batch_data[i][:] = img
        # img = nd.array(img, ctx)
    model.forward(Batch([batch_data]), is_train=False)
    outputs = model.get_outputs()[0].asnumpy()
    return outputs
示例#12
0
文件: fuse.py 项目: gyhandy/PoseEst
def default_mp_batchify_fn(data):
    """Collate data into batch. Use shared memory for stacking."""
    if isinstance(data[0], nd.NDArray):
        out = nd.empty((len(data), ) + data[0].shape,
                       dtype=data[0].dtype,
                       ctx=context.Context('cpu_shared', 0))
        return nd.stack(*data, out=out)
    elif isinstance(data[0], tuple):
        data = zip(*data)
        return [default_mp_batchify_fn(i) for i in data]
    else:
        data = np.asarray(data)
示例#13
0
文件: rbm.py 项目: yixuan/cdtau
    def __init__(self, m, n, w0=None, b0=None, c0=None, ctx=mx.cpu()):
        self.ctx = ctx

        # Dimensions
        self.m = m
        self.n = n

        # Parameters
        if w0 is not None and b0 is not None and c0 is not None:
            if w0.shape != (m, n):
                raise ValueError("w0 must be an [m x n] array")
            if b0.shape != (m, ):
                raise ValueError("b0 must be an [m] array")
            if c0.shape != (n, ):
                raise ValueError("c0 must be an [n] array")
            self.w = w0.as_in_context(self.ctx)
            self.b = b0.as_in_context(self.ctx)
            self.c = c0.as_in_context(self.ctx)
        else:
            self.w = nd.random.normal(scale=0.1, shape=(m, n), ctx=self.ctx)
            self.b = nd.random.normal(scale=0.1, shape=m, ctx=self.ctx)
            self.c = nd.random.normal(scale=0.1, shape=n, ctx=self.ctx)

        # Gradients
        self.dw1 = nd.empty(shape=self.w.shape, ctx=self.ctx)
        self.db1 = nd.empty(shape=self.b.shape, ctx=self.ctx)
        self.dc1 = nd.empty(shape=self.c.shape, ctx=self.ctx)

        self.dw2 = nd.empty(shape=self.w.shape, ctx=self.ctx)
        self.db2 = nd.empty(shape=self.b.shape, ctx=self.ctx)
        self.dc2 = nd.empty(shape=self.c.shape, ctx=self.ctx)
示例#14
0
def RunGAN(episode_states, discriminator, discriminator_trainer, generator, generator_trainer, should_print = False):
    sampler = mx.gluon.data.SequentialSampler(len(episode_states))
    batch_sampler = mx.gluon.data.BatchSampler(sampler, opt.batch_size_GAN)
    batch_state_GAN = nd.empty((opt.batch_size_GAN,opt.frame_len,opt.image_size,opt.image_size), opt.ctx)
    loss_disc = 0.
    for batch_indices in batch_sampler:
        for i in range(len(batch_indices)):
            index = batch_indices[i]
            batch_state_GAN[i] = nd.array(episode_states[index],opt.ctx)
        loss_disc = StepGAN(batch_state_GAN, discriminator, discriminator_trainer, generator, generator_trainer, False)

    results = 'Episode loss %f'%(loss_disc)
    if should_print:
        print(results)
    def __init__(self, layer_type, input_size, output_size, learning_rate,
                 activation, batch_size):
        self.layer_type = layer_type
        self.input_size = input_size
        self.output_size = output_size
        self.learning_rate = learning_rate
        self.batch_size = batch_size

        self.act = self.act_func(activation)
        self.d_act = self.d_act_func(activation)

        self.output = None

        self.W = None
        self.b = None
        self.z = None

        self.forward = None
        self.backward = None

        if self.layer_type == "fc":
            self.W = (np.random.rand(self.input_size, self.output_size) *
                      1) - 0.5
            self.b = (np.random.rand(self.output_size) * 1) - 0.5
            # self.delta_W = np.zeros((self.input_size, self.output_size))
            # self.delta_b = np.zeros(self.output_size)

            self.forward = self.dense_fw
            self.backward = self.dense_bw

            self.W = nd.array(self.W, ctx=ctx)
            self.b = nd.array(self.b, ctx=ctx)
            self.delta_W = nd.empty(self.W.shape, ctx=ctx)
            self.delta_b = nd.empty(self.b.shape, ctx=ctx)
            self.z = nd.empty((self.batch_size, self.output_size), ctx=ctx)
            self.d_act_z = nd.empty(self.z.shape, ctx=ctx)
示例#16
0
    def __getitem__(self, item):
        """get the x and y
        x is the [merged[0:3],trimap[3] ] ,
        y is the [bg[0:3],fg[3:6],mask[6],alpha[7] ]"""
        name = self.names[item]
        fcount,bcount = [int(x) for x in name.split('.')[0].split('_')]
        im_name = self.fg_files[fcount]
        bg_name = self.bg_files[bcount]

        merged,alpha,fg,bg,trimap = self.process(im_name,bg_name)
        x = nd.empty((self.size,self.size,4),dtype=np.float32)
        y = nd.empty((self.size,self.size,7),dtype=np.float32)

        if self.transform:
            merged = self.transform(nd.array(merged))


        x[:,:,0:3] = nd.array(merged)
        x[:,:,-1] = nd.array(trimap)

        y[:,:,0:3] = nd.array(bg)
        y[:,:,3:6] = nd.array(fg)
        y[:,:,-1] = nd.array(alpha)
        return x,y
def stack(
    data,
    multi_processing: bool,
    dtype: DType,
    single_process_ctx: Optional[mx.Context] = None,
):
    """Stack a list of data.
        Used when creating a single batch from list of dicts
        depending on whether multiprocessing is turned on, the batches will be
        constructed using different memory allocation techniques"""
    if isinstance(data[0], mx.nd.NDArray):
        if multi_processing:
            out = nd.empty(
                (len(data), ) + data[0].shape,
                dtype=data[0].dtype,
                ctx=context.Context("cpu_shared", 0),
            )
            return mx.nd.stack(*data, out=out)
        else:
            return mx.nd.stack(*data)
    elif isinstance(data[0], np.ndarray):
        data = np.asarray(data)
        if data.dtype.kind == "f":
            data = data.astype(dtype)
        if multi_processing:
            # Workaround due to MXNet not being able to handle NDArrays with 0 in shape properly:
            if 0 in data.shape:
                return data
            return mx.nd.array(data,
                               dtype=data.dtype,
                               ctx=context.Context("cpu_shared", 0))
        else:
            return mx.nd.array(data, dtype=data.dtype, ctx=single_process_ctx)
    elif isinstance(data[0], list):
        return list(
            stack(t, multi_processing, dtype, single_process_ctx)
            for t in zip(*data))
    elif isinstance(data[0], tuple):
        return tuple(
            stack(t, multi_processing, dtype, single_process_ctx)
            for t in zip(*data))
    elif isinstance(data[0], (pd.Timestamp, str, int, pathlib.PosixPath)):
        return data
    else:
        raise TypeError(
            f"Invalid type of data: {type(data[0])} for argument loss_function."
        )
示例#18
0
def default_mp_pad_batchify_fn(data):
    """Use shared memory for collating data into batch, labels are padded to same shape"""
    if isinstance(data[0], nd.NDArray):
        out = nd.empty((len(data),) + data[0].shape, dtype=data[0].dtype,
                       ctx=context.Context('cpu_shared', 0))
        return nd.stack(*data, out=out)
    elif isinstance(data[0], tuple):
        data = zip(*data)
        return [default_mp_pad_batchify_fn(i) for i in data]
    else:
        data = np.asarray(data)
        batch_size = len(data)
        pad = max([l.shape[0] for l in data] + [1,])
        buf = np.full((batch_size, pad, data[0].shape[-1]), -1, dtype=data[0].dtype)
        for i, l in enumerate(data):
            buf[i][:l.shape[0], :] = l
        return nd.array(buf, dtype=data[0].dtype, ctx=context.Context('cpu_shared', 0))
示例#19
0
def default_mp_pad_batchify_fn(data):
    """Use shared memory for collating data into batch, labels are padded to same shape"""
    if isinstance(data[0], nd.NDArray):
        out = nd.empty((len(data),) + data[0].shape, dtype=data[0].dtype,
                       ctx=context.Context('cpu_shared', 0))
        return nd.stack(*data, out=out)
    elif isinstance(data[0], tuple):
        data = zip(*data)
        return [default_mp_pad_batchify_fn(i) for i in data]
    else:
        data = np.asarray(data)
        batch_size = len(data)
        pad = max([l.shape[0] for l in data] + [1,])
        buf = np.full((batch_size, pad, data[0].shape[-1]), -1, dtype=data[0].dtype)
        for i, l in enumerate(data):
            buf[i][:l.shape[0], :] = l
        return nd.array(buf, dtype=data[0].dtype, ctx=context.Context('cpu_shared', 0))
 def batchify_fn(self, data):
     """Collate data into batch. Split into supervised and unsupervised data accordingly if necessary"""
     if isinstance(data[0], nd.NDArray):
         out = nd.empty((len(data), ) + data[0].shape,
                        dtype=data[0].dtype,
                        ctx=context.Context('cpu_shared', 0))
         return nd.stack(*data, out=out)
     elif isinstance(data[0], tuple):
         if data.__len__() > self._batch_size:
             data_sup = zip(*[_data for _data in data if len(_data) == 3])
             data_unsup = zip(*[_data for _data in data if len(_data) == 2])
             return ([self.batchify_fn(i) for i in data_sup],
                     [self.batchify_fn(i) for i in data_unsup])
         else:
             data = zip(*data)
             return [self.batchify_fn(i) for i in data]
     else:
         if isinstance(data[0][0], np.ndarray):
             data = np.asarray(data).astype('float32')
             return nd.array(data,
                             dtype=data.dtype,
                             ctx=context.Context('cpu_shared', 0))
         return data  # augmentation sequences
示例#21
0
 def check_ndarray_empty():
     a = nd.empty(LARGE_X)
     assert a.shape == (LARGE_X,)
示例#22
0
        hidden = hidden.detach()
    return hidden


l2loss = gluon.loss.L2Loss(batch_axis=0)

frame_counter = 0.
annealing_count = 0.
epis_count = 0.
replay_memory = ReplayBuffer(opt.replay_buffer_size)
tot_clipped_reward = np.zeros(opt.num_episode)
tot_reward = np.zeros(opt.num_episode)
moving_average_clipped = 0.
moving_average = 0.

batch_state = nd.empty((opt.batch_size, opt.seq_len, opt.num_inputs), opt.ctx)
batch_state_next = nd.empty((opt.batch_size, opt.seq_len, opt.num_inputs),
                            opt.ctx)

for i in range(opt.num_episode):
    cum_clipped_reward = 0
    cum_reward = 0
    state = env.reset()
    t = 0.
    done = False
    target_dqn_hidden = target_dqn.begin_state(func=mx.nd.zeros,
                                               batch_size=opt.batch_size,
                                               ctx=opt.ctx)
    dqn_hidden = dqn.begin_state(func=mx.nd.zeros,
                                 batch_size=opt.batch_size,
                                 ctx=opt.ctx)
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 29 09:57:51 2018

@author: Taylor
"""

# import mxnet
from mxnet import nd



# this is just going to grab random rubbish from memory...
x = nd.empty(shape=(2, 3))
print(x)
print(type(x))


# this gives zeros
x = nd.zeros(shape = (3, 2))
print(x)
print(type(x))


# ones
x = nd.ones(shape = (2, 3))
print(x)


# ?nd.random_normal
# loc = mean
示例#24
0
    def _generator(self):
        """
        Yields a length `T` trajectory

        (Straight from the openAI code, essentially)
        """
        # Initial setup
        ac = self._env.action_space.sample(
        )  # not used, just so we have the datatype
        self.new = True  # marks if we're on first timestep of an episode
        self.ob = self._convert_state(self._env.reset())
        T = self._timesteps

        cur_ep_ret = 0  # return in current episode
        cur_ep_len = 0  # len of current episode
        ep_rets = []  # returns of completed episodes in this segment
        ep_lens = []  # lengths of ...

        # Initialize history arrays
        #obs = np.array([None for _ in range(T)])
        obs = nd.empty((T, ) + self._env.observation_space.shape)
        rews = np.zeros(T, 'float32')
        vpreds = np.zeros(T, 'float32')
        news = np.zeros(T, 'int32')
        acs = np.array([ac for _ in range(T)])
        prevacs = acs.copy()

        t = 0
        while True:
            ob = self.ob  # Use `self.` since `_evaluate` may have reset the env
            new = self.new
            prevac = ac
            ac, vpred = self._act(ob)
            # NOTE(openAI) Slight weirdness here because we need value function at time T
            # before returning segment [0, T-1] so we get the correct terminal value
            if t > 0 and t % T == 0:
                seg = {
                    "ob": obs,
                    "rew": rews,
                    "vpred": vpreds,
                    "new": news,
                    "ac": acs,
                    "nextvpred": vpred * (1 - new),
                    "ep_rets": np.array(copy.deepcopy(ep_rets)),
                    "ep_lens": np.array(copy.deepcopy(ep_lens))
                }
                self._add_vtarg_and_adv(seg, self._gamma, self._lambda)
                yield seg
                # NOTE: Do a deepcopy if the values formerly in these arrays are used later.
                ep_rets = []
                ep_lens = []
            i = t % T

            obs[i] = ob[0]
            vpreds[i] = vpred
            news[i] = new
            acs[i] = ac
            prevacs[i] = prevac

            ob, rew, new, _ = self._env.step(ac)
            ob = self._convert_state(ob)
            rews[i] = rew

            cur_ep_ret += rew
            cur_ep_len += 1
            if new:
                ep_rets.append(cur_ep_ret)
                ep_lens.append(cur_ep_len)
                cur_ep_ret = 0
                cur_ep_len = 0
                ob = self._convert_state(self._env.reset())
            self.new = new
            self.ob = ob
            t += 1
示例#25
0
        x = self.conv(concated)

        return x


net = Net()
# Build network #
#################

#########################
# Weight initialization #
net.initialize(mx.init.Xavier(), ctx=ctx, force_reinit=True)

state = net.encoder.begin_state(batch_size=batch_size, ctx=ctx)
x = nd.empty(shape=(batch_size, channel, depth, size, size), ctx=ctx)

out = net(x, state)

# Weight initialization #
#########################


###################
# Data processing #
def sliding_window(array, depth, target):
    length = len(array) - depth - target - 1
    data, label = [], []

    trans = Resize(size)
    for i in range(length):
示例#26
0
def test_ndarray_empty():
    a = nd.empty((LARGE_X, SMALL_Y))
    assert a.shape == (LARGE_X, SMALL_Y)
示例#27
0
def test_ndarray_empty():
    a = nd.empty((LARGE_X, SMALL_Y))
    assert a.shape == (LARGE_X, SMALL_Y)
示例#28
0
def test_ndarray_empty():
    a = nd.empty(LARGE_X)
    assert a.shape == (LARGE_X, )
示例#29
0
文件: bas.py 项目: yixuan/cdtau
    to_str_func = np.vectorize(lambda x: np.binary_repr(x).zfill(m))
    strs = to_str_func(arr)
    ret = np.zeros(list(arr.shape) + [m], dtype=np.int8)
    for bit_ix in range(0, m):
        fetch_bit_func = np.vectorize(lambda x: x[bit_ix] == '1')
        ret[..., bit_ix] = fetch_bit_func(strs).astype("int8")
    return ret


# The bars-and-stripes data studied by Schulz et al. (2010)
mx.random.seed(123)
np.random.seed(123)
d = 4
m = d * d
N = 2 ** d
dat = nd.empty(shape=(2 * N, m))
bits = vec_bin_array(np.arange(N), d).astype(np.float)
for i in range(N):
    mat = bits[i, :].reshape(1, -1).T.dot(np.ones(shape=(1, d)))
    dat[2 * i, :] = mat.flatten()
    dat[2 * i + 1, :] = mat.T.flatten()

ctx = mx.cpu()
dat = nd.array(dat, ctx=ctx)
N = dat.shape[0]
m = d * d
n = m

# Train RBM using CD-k
mx.random.seed(123)
np.random.seed(123)
示例#30
0
def train_net(args, ctx, pretrained, epoch, prefix, begin_epoch, end_epoch, lr,
              lr_step):
    mx.random.seed(3)
    np.random.seed(3)
    if not os.path.exists(config.output_path):
        os.mkdir(config.output_path)
    logger, final_output_path = create_logger(config.output_path, args.cfg,
                                              config.dataset.image_set)
    prefix = os.path.join(final_output_path, prefix)

    # load symbol
    shutil.copy2(os.path.join(curr_path, 'symbols', config.symbol + '.py'),
                 final_output_path)
    sym_instance = eval(config.symbol + '.' + config.symbol)()
    sym = sym_instance.get_symbol(config, is_train=True)

    feat_pyramid_level = np.log2(config.network.RPN_FEAT_STRIDE).astype(int)
    feat_sym = [
        sym.get_internals()['rpn_cls_score_p' + str(x) + '_output']
        for x in feat_pyramid_level
    ]

    # setup multi-gpu
    batch_size = len(ctx)
    input_batch_size = config.TRAIN.BATCH_IMAGES * batch_size

    # print config
    #pprint.pprint(config)
    #logger.info('training config:{}\n'.format(pprint.pformat(config)))

    # load dataset and prepare imdb for training
    image_sets = [iset for iset in config.dataset.image_set.split('+')]
    roidbs = [
        load_gt_roidb(config.dataset.dataset,
                      image_set,
                      config.dataset.root_path,
                      config.dataset.dataset_path,
                      flip=config.TRAIN.FLIP) for image_set in image_sets
    ]
    roidb = merge_roidb(roidbs)
    roidb = filter_roidb(roidb, config)

    # load training data

    train_data = PyramidAnchorIterator(
        feat_sym,
        roidb,
        config,
        batch_size=input_batch_size,
        shuffle=config.TRAIN.SHUFFLE,
        ctx=ctx,
        feat_strides=config.network.RPN_FEAT_STRIDE,
        anchor_scales=config.network.ANCHOR_SCALES,
        anchor_ratios=config.network.ANCHOR_RATIOS,
        aspect_grouping=config.TRAIN.ASPECT_GROUPING,
        allowed_border=np.inf)

    # infer max shape
    max_data_shape = [('data', (config.TRAIN.BATCH_IMAGES,
                                3 * config.CROP_NUM * config.CROP_NUM,
                                max([v[0] for v in config.SCALES]),
                                max([v[1] for v in config.SCALES])))]
    max_data_shape, max_label_shape = train_data.infer_shape(max_data_shape)
    max_data_shape.append(('gt_boxes', (config.TRAIN.BATCH_IMAGES, 100,
                                        6)))  #change gt_boxes to 1,100,6
    print 'providing maximum shape', max_data_shape, max_label_shape

    data_shape_dict = dict(train_data.provide_data_single +
                           train_data.provide_label_single)
    pprint.pprint(data_shape_dict)
    sym_instance.infer_shape(data_shape_dict)

    # load and initialize params
    if config.TRAIN.RESUME:
        print('continue training from ', begin_epoch)
        arg_params, aux_params = load_param(prefix, begin_epoch, convert=True)
    else:
        arg_params, aux_params = load_param(pretrained, epoch, convert=True)
        single_shape = arg_params['conv1_weight'].shape
        temp_conv1_weight = nd.empty(
            (single_shape[0], single_shape[1] * config.CROP_NUM *
             config.CROP_NUM, single_shape[2], single_shape[3]),
            dtype=arg_params['conv1_weight'].dtype)
        for i in range(config.CROP_NUM * config.CROP_NUM):
            temp_conv1_weight[:, i * single_shape[1]:(i + 1) *
                              single_shape[1], :, :] = arg_params[
                                  'conv1_weight'][:, :, :, :]
        del arg_params['conv1_weight']
        arg_params['conv1_weight'] = temp_conv1_weight
        sym_instance.init_weight(config, arg_params, aux_params)

    # check parameter shapes
    sym_instance.check_parameter_shapes(arg_params, aux_params,
                                        data_shape_dict)

    # create solver
    fixed_param_prefix = config.network.FIXED_PARAMS
    data_names = [k[0] for k in train_data.provide_data_single]
    label_names = [k[0] for k in train_data.provide_label_single]

    mod = MutableModule(
        sym,
        data_names=data_names,
        label_names=label_names,
        logger=logger,
        context=ctx,
        max_data_shapes=[max_data_shape for _ in range(batch_size)],
        max_label_shapes=[max_label_shape for _ in range(batch_size)],
        fixed_param_prefix=fixed_param_prefix)

    if config.TRAIN.RESUME:
        mod._preload_opt_states = '%s-%04d.states' % (prefix, begin_epoch)

    # decide training params
    # metric
    rpn_eval_metric = metric.RPNAccMetric()
    rpn_cls_metric = metric.RPNLogLossMetric()
    rpn_bbox_metric = metric.RPNL1LossMetric()
    rpn_fg_metric = metric.RPNFGFraction(config)
    eval_metric = metric.RCNNAccMetric(config)
    eval_fg_metric = metric.RCNNFGAccuracy(config)
    cls_metric = metric.RCNNLogLossMetric(config)
    bbox_metric = metric.RCNNL1LossMetric(config)
    #fl_metric = metric.FocalLoss()
    eval_metrics = mx.metric.CompositeEvalMetric()
    # rpn_eval_metric, rpn_cls_metric, rpn_bbox_metric, eval_metric, cls_metric, bbox_metric
    for child_metric in [
            rpn_eval_metric, rpn_cls_metric, rpn_bbox_metric, rpn_fg_metric,
            eval_fg_metric, eval_metric, cls_metric, bbox_metric
    ]:
        eval_metrics.add(child_metric)
    # callback
    batch_end_callback = callback.Speedometer(train_data.batch_size,
                                              frequent=args.frequent)
    means = np.tile(np.array(config.TRAIN.BBOX_MEANS),
                    2 if config.CLASS_AGNOSTIC else config.dataset.NUM_CLASSES)
    stds = np.tile(np.array(config.TRAIN.BBOX_STDS),
                   2 if config.CLASS_AGNOSTIC else config.dataset.NUM_CLASSES)
    epoch_end_callback = [
        mx.callback.module_checkpoint(mod,
                                      prefix,
                                      period=1,
                                      save_optimizer_states=True),
        callback.do_checkpoint(prefix, means, stds)
    ]
    # decide learning rate
    base_lr = lr
    lr_factor = config.TRAIN.lr_factor
    lr_epoch = [float(epoch) for epoch in lr_step.split(',')]
    lr_epoch_diff = [
        epoch - begin_epoch for epoch in lr_epoch if epoch > begin_epoch
    ]
    lr = base_lr * (lr_factor**(len(lr_epoch) - len(lr_epoch_diff)))
    lr_iters = [
        int(epoch * len(roidb) / batch_size) for epoch in lr_epoch_diff
    ]
    print('lr', lr, 'lr_epoch_diff', lr_epoch_diff, 'lr_iters', lr_iters)
    lr_scheduler = WarmupMultiFactorScheduler(lr_iters, lr_factor,
                                              config.TRAIN.warmup,
                                              config.TRAIN.warmup_lr,
                                              config.TRAIN.warmup_step)
    # optimizer
    optimizer_params = {
        'momentum': config.TRAIN.momentum,
        'wd': config.TRAIN.wd,
        'learning_rate': lr,
        'lr_scheduler': lr_scheduler,
        'clip_gradient': None
    }
    #
    if not isinstance(train_data, PrefetchingIter):
        train_data = PrefetchingIter(train_data)

    # train
    mod.fit(train_data,
            eval_metric=eval_metrics,
            epoch_end_callback=epoch_end_callback,
            batch_end_callback=batch_end_callback,
            kvstore=config.default.kvstore,
            optimizer='sgd',
            optimizer_params=optimizer_params,
            arg_params=arg_params,
            aux_params=aux_params,
            begin_epoch=begin_epoch,
            num_epoch=end_epoch)
示例#31
0
def stack(
    data,
    multi_processing: bool,
    dtype: DType,
    single_process_ctx: Optional[mx.Context] = None,
    variable_length: bool = False,
):
    """
    Stack a list of data. Used when creating a single batch from list of dicts
    depending on whether multiprocessing is turned on, the batches will be
    constructed using different memory allocation techniques. If `variable_length`
    is specified, the data will be 'padded' with zeros along the first axis.

    Parameters
    ----------
    data: List
        Lists of array-like, stacked into data batches and loaded to appropriate
        memory (according to whether `multi_processing` is specified).
    multi_processing: bool
        If True, data will be loaded to mxnet ndarrays on shared CPU memory.
    dtype: DType
    single_process_ctx: Optional[mx.Context]
    variable_length: bool
        If True, the function will check if the list of data are "stackable",
        i.e., they have matching axes. If not, it will assume that the first
        dimension of each array is heterogeneous (i.e., ragged) and will pad
        this axis before stacking.
    """
    if variable_length and not _is_stackable(data):
        data = _pad_arrays(data, axis=0)

    if isinstance(data[0], mx.nd.NDArray):
        if multi_processing:
            out = nd.empty(
                (len(data), ) + data[0].shape,
                dtype=data[0].dtype,
                ctx=context.Context("cpu_shared", 0),
            )
            return mx.nd.stack(*data, out=out)
        else:
            return mx.nd.stack(*data)
    elif isinstance(data[0], np.ndarray):
        data = np.asarray(data)
        if data.dtype.kind == "f":
            data = data.astype(dtype)
        if multi_processing:
            # Workaround due to MXNet not being able to handle NDArrays with 0 in shape properly:
            if 0 in data.shape:
                return data
            return mx.nd.array(data,
                               dtype=data.dtype,
                               ctx=context.Context("cpu_shared", 0))
        else:
            return mx.nd.array(data, dtype=data.dtype, ctx=single_process_ctx)
    elif isinstance(data[0], list):
        return list(
            stack(t, multi_processing, dtype, single_process_ctx)
            for t in zip(*data))
    elif isinstance(data[0], tuple):
        return tuple(
            stack(t, multi_processing, dtype, single_process_ctx)
            for t in zip(*data))

    return data
import mxnet as mx
from mxnet import nd
mx.random.seed(1)

x = nd.empty((3, 4))
print(x)

x = nd.zeros((3, 4))
print(x)

y = nd.ones((3, 4))
print(x)

print(nd.dot(x, y.T))