Exemplo n.º 1
0
    def zero_state(self, batch_size, dtype):
        with tf.variable_scope('init', reuse=self.reuse):
            # 读取权重的初始化
            read_vector_list = [
                expand(tf.tanh(learned_init(self.memory_vector_dim)),
                       dim=0,
                       N=batch_size) for i in range(self.read_head_num)
            ]
            # 写入权重的初始化
            w_list = [
                expand(tf.nn.softmax(learned_init(self.memory_size)),
                       dim=0,
                       N=batch_size)
                for i in range(self.read_head_num + self.write_head_num)
            ]
            # RNN初始化
            controller_init_state = self.controller.zero_state(
                batch_size, dtype)
            # 存储单元初始
            M = expand(tf.get_variable(
                'init_M', [self.memory_size, self.memory_vector_dim],
                initializer=tf.constant_initializer(1e-6)),
                       dim=0,
                       N=batch_size)

            # 前面定义的:NTMControllerState = collections.namedtuple(
            #   'NTMControllerState', ('controller_state', 'read_vector_list',
            #   'w_list', 'M'))
            return NTMControllerState(controller_state=controller_init_state,
                                      read_vector_list=read_vector_list,
                                      w_list=w_list,
                                      M=M)
Exemplo n.º 2
0
    def train(self):
        self.costs = []
        gradients = []

        for i, (char, target) in enumerate(zip(self.text, self.text[1:])):
            self.x = expand(np.eye(self.x_size)[self.char_to_i[char]])
            self.target = expand(np.eye(self.x_size)[self.char_to_i[target]])

            self.y = self.forward_pass()
            gradients.append(self.backward_pass())

            if (i + 1) % self.batch_size == 0:
                dCdWxh, dCdWhh, dCdWhy, dCdBh, dCdBy = np.average(gradients,
                                                                  axis=0)
                self.W_xh -= self.learning_rate * dCdWxh
                self.W_hh -= self.learning_rate * dCdWhh
                self.W_hy -= self.learning_rate * dCdWhy
                self.B_h -= self.learning_rate * dCdBh.T
                self.B_y -= self.learning_rate * dCdBy.T

            self.costs.append(cost(self.target, self.y))
            self.diagnose(i)

        plt.plot(self.costs)
        plt.show(block=False)
        time.sleep(1000)
Exemplo n.º 3
0
    def zero_state(self, batch_size, dtype):
        with tf.compat.v1.variable_scope('init', reuse=self.reuse):
            read_vector_list = [expand(tf.tanh(learned_init(self.memory_vector_dim)), dim=0, N=batch_size)
                for i in range(self.read_head_num)]

            w_list = [expand(tf.nn.softmax(learned_init(self.memory_size)), dim=0, N=batch_size)
                for i in range(self.read_head_num + self.write_head_num)]

            controller_init_state = self.controller.zero_state(batch_size, dtype)

            if self.init_mode == 'learned':
                M = expand(tf.tanh(
                    tf.reshape(
                        learned_init(self.memory_size * self.memory_vector_dim),
                        [self.memory_size, self.memory_vector_dim])
                    ), dim=0, N=batch_size)
            elif self.init_mode == 'random':
                M = expand(
                    tf.tanh(tf.get_variable('init_M', [self.memory_size, self.memory_vector_dim],
                        initializer=tf.random_normal_initializer(mean=0.0, stddev=0.5))),
                    dim=0, N=batch_size)
            elif self.init_mode == 'constant':
                M = expand(
                    tf.get_variable('init_M', [self.memory_size, self.memory_vector_dim],
                        initializer=tf.constant_initializer(1e-6)),
                    dim=0, N=batch_size)

            return NTMControllerState(
                controller_state=controller_init_state,
                read_vector_list=read_vector_list,
                w_list=w_list,
                M=M)
Exemplo n.º 4
0
def system_generator(x0, z0, dt):
    t = 0
    x = x0
    z = z0

    while True:
        xx = delta_matrix(x)
        xx_norm = expand(norm(xx))

        zz = delta_matrix(z)
        zz_norm = expand(norm(zz))

        xz = delta_matrix(x, z)
        zx = -np.moveaxis(xz, 0, 1)

        xz_norm = norm(xz)
        zx_norm = xz_norm.T
        xz_norm = expand(xz_norm)
        zx_norm = expand(zx_norm)

        with np.errstate(divide='ignore', invalid='ignore'):
            vx = np.nansum(prey_social(xx_norm) / xx_norm * xx, 1) / N + \
                np.nansum(prey_predator(xz_norm) / xz_norm * xz, 1) / N2
            vz = np.nansum(predator_social(zz_norm) / zz_norm * zz, 1) / N2 + \
                np.nansum(predator_prey(zx_norm) / zx_norm * zx, 1) / N

        yield t, x, z, vx, vz

        x = x + vx * dt
        z = z + vz * dt
        t += dt
Exemplo n.º 5
0
def system_generator(x0,  dt):
    x = x0
    t = 0
    dB = 0
    while True:
        xx = delta_matrix(x)
        r_xx = norm(xx, keepdims=True)

        # Interaction among individuals
        with np.errstate(divide='ignore', invalid='ignore'):
            v = np.nansum(F(r_xx, a) / r_xx * xx, axis=1) / N

        # Environment influence
        if barr_type != NO_BARR:
            # Add env
            if gate_len != 0:
                # Point repulsion
                c = (0, 1.5)
                xc = x - c
                v += .5 / norm(xc, keepdims=True) ** 2 * xc
            else:
                # Gravity
                v += (0, - .1)

        # dx without barrier consideration
        # Plus random diffusion
        dx = v * dt + mu * dB

        # Now consider different barrier
        if barr_type != NO_BARR:
            x_next = x + dx

            # barrier with EPS (vague judgement, not so strict)
            pr = x[:, 1]
            nx = x_next[:, 1]
            invalid = (pr > BARR) & (nx < BARR + EPS)  # Upper to lower
            invalid |= (pr < BARR) & (nx > BARR - EPS)  # Lower to upper

            if barr_type == ABSORB:
                dx *= ~ expand(invalid)
            else:
                # Reflecting barrier
                if gate_len != 0:
                    # Exclude those via gate: if before or after are within gate
                    exclude = (np.abs(x_next[:, 0] - 0) < gate_len /
                               2) | (np.abs(x[:, 0] - 0) < gate_len / 2)
                    invalid &= ~ exclude

                bdd = np.hstack((np.tile(False, (N, 1)), expand(invalid)))
                dx = np.where(bdd, - dx, dx)

        v = dx / dt
        yield t, x, v
        t += dt
        # Do not use +=, as it modifies the mutable variable x (by `__iadd__` method)
        x = x + dx
        if mu != 0:
            dB = np.random.randn(N, 2)
Exemplo n.º 6
0
    def _build_model(self):
        if args.mann == 'none':

            def single_cell(num_units):
                return tf.contrib.rnn.BasicLSTMCell(num_units, forget_bias=1.0)

            cell = tf.contrib.rnn.OutputProjectionWrapper(
                tf.contrib.rnn.MultiRNNCell([
                    single_cell(args.num_units) for _ in range(args.num_layers)
                ]),
                args.num_bits_per_vector,
                activation=None)

            initial_state = tuple(
                tf.contrib.rnn.LSTMStateTuple(
                    c=expand(tf.tanh(learned_init(args.num_units)),
                             dim=0,
                             N=args.batch_size),
                    h=expand(tf.tanh(learned_init(args.num_units)),
                             dim=0,
                             N=args.batch_size))
                for _ in range(args.num_layers))

        elif args.mann == 'ntm':
            cell = NTMCell(args.num_layers,
                           args.num_units,
                           args.num_memory_locations,
                           args.memory_size,
                           args.num_read_heads,
                           args.num_write_heads,
                           addressing_mode='content_and_location',
                           shift_range=args.conv_shift_range,
                           reuse=False,
                           output_dim=args.num_bits_per_vector,
                           clip_value=args.clip_value,
                           init_mode=args.init_mode)

            initial_state = cell.zero_state(args.batch_size, tf.float32)

        output_sequence, _ = tf.nn.dynamic_rnn(cell=cell,
                                               inputs=self.inputs,
                                               time_major=False,
                                               initial_state=initial_state)

        if args.task == 'copy':
            self.output_logits = output_sequence[:, self.max_seq_len + 1:, :]
        elif args.task == 'associative_recall':
            self.output_logits = output_sequence[:,
                                                 3 * (self.max_seq_len + 1) +
                                                 2:, :]

        if args.task in ('copy', 'associative_recall'):
            self.outputs = tf.sigmoid(self.output_logits)
Exemplo n.º 7
0
 def _refresh(self, uid, params):
     s, api_url, params = prepare_request(params)
     url = api_url + self.instance_url(uid=uid)
     r = s.get(url, params=utils.expand(params))
     r.raise_for_status()
     self._refresh_from(r.json())
     return self
Exemplo n.º 8
0
    def loss_fun(self, inputs, targets):
        tanh_arg, softmax, h = {}, {}, {-1: self.h_prev}
        dwxh, dwhh, dwhy = np.zeros_like(self.wxh), np.zeros_like(
            self.whh), np.zeros_like(self.why)
        dbh, dby = np.zeros_like(self.bh), np.zeros_like(self.by)
        loss = 0

        for t in range(len(inputs)):
            tanh_arg[t] = inputs[t].dot(self.wxh) + h[t - 1].dot(
                self.whh) + self.bh
            h[t] = np.tanh(tanh_arg[t])
            y = h[t].dot(self.why) + self.by
            exp_y = np.exp(y)
            softmax[t] = exp_y / exp_y.sum()
            correct_scores = (softmax[t] * targets[t]).sum(1)

        for t in reversed(range(len(inputs))):
            df = softmax[t] - targets[t]
            dby += df
            dwhy += h[t].T.dot(df)

            dh = self.why.dot(df.T)
            dtanh_arg = tanh_prime(tanh_arg[t]) * dh.T
            dbh += dtanh_arg
            dwxh += expand(inputs[t]).dot(dtanh_arg)
            dwhh += h[t - 1].T.dot(dtanh_arg)

        self.h_prev = h[-1]
        return dwxh, dwhh, dwhy, dbh, dby
Exemplo n.º 9
0
    def __BFS(self, verbose=False):
        visited = []
        max_depth = 0
        max_queue_len = 0
        queue = [
            self.START_PUZZLE,
        ]
        while queue:
            v = queue.pop(0)
            if v.status not in visited:
                visited.append(v.status)
                if v.status == GOAL_STATE:
                    break
                queue.extend([i for i in expand(v) if i not in visited])
                max_queue_len = max(max_queue_len, len(queue))
                max_depth = max(max_depth, v.depth)

                if verbose:
                    print('depth:', v.depth)
                    print('visited list:', visited,
                          '(visited len: {})'.format(len(visited)))
                    print('queue:', queue, '(len: {})'.format(len(queue)))

                if len(visited) >= 100000:
                    print('search space exceeded.')
                    break

        goal_puzzle = v
        return self.START_PUZZLE, goal_puzzle, len(
            visited), max_queue_len, max_depth
Exemplo n.º 10
0
    def __DFS(self, verbose=False):
        visited = []
        max_depth = 0
        max_stack_len = 0
        stack = [
            self.START_PUZZLE,
        ]
        while stack:
            v = stack.pop()
            if v.status not in visited:
                visited.append(v.status)
                if v.status == GOAL_STATE:
                    break
                stack.extend([i for i in expand(v) if i not in visited])
                max_stack_len = max(max_stack_len, len(stack))
                max_depth = max(max_depth, v.depth)

                if verbose:
                    print('depth:', v.depth)
                    print('visited list:', visited,
                          '(visited len: {})'.format(len(visited)))
                    print('stack:', stack, '(len: {})'.format(len(stack)))

                if len(visited) >= 100000:
                    print('search space exceeded.')
                    break

        goal_puzzle = v
        return self.START_PUZZLE, goal_puzzle, len(
            visited), max_stack_len, max_depth
Exemplo n.º 11
0
 def _refresh(self, uid, params):
     s, api_url, params = prepare_request(params)
     url = api_url + self.instance_url(uid=uid)
     r = s.get(url, params=utils.expand(params))
     r.raise_for_status()
     self._refresh_from(r.json())
     return self
Exemplo n.º 12
0
def loss_fun(inputs, targets, h_prev):
    tanh_arg, softmax, h = {}, {}, {}
    dwxh, dwhh, dwhy = np.zeros_like(wxh), np.zeros_like(whh), np.zeros_like(
        why)
    dbh, dby = np.zeros_like(bh), np.zeros_like(by)
    loss = 0
    h[-1] = h_prev

    for t in range(len(inputs)):
        tanh_arg[t] = inputs[t].dot(wxh) + h[t - 1].dot(whh) + bh
        h[t] = np.tanh(tanh_arg[t])
        y = h[t].dot(why) + by
        exp_y = np.exp(y)
        softmax[t] = exp_y / exp_y.sum()
        correct_scores = (softmax[t] * targets[t]).sum(1)
        loss += np.sum(-np.log(correct_scores))

    for t in reversed(range(len(inputs))):
        df = softmax[t] - targets[t]
        dby += df.sum(0, keepdims=True)
        dwhy += h[t].T.dot(df)

        dh = why.dot(df.T)
        dtanh_arg = tanh_prime(tanh_arg[t]) * dh.T
        dbh += dtanh_arg
        dwxh += expand(inputs[t]).dot(dtanh_arg)
        dwhh += h[t - 1].T.dot(dtanh_arg)

    return loss, dwxh, dwhh, dwhy, dbh, dby, h[-1]
Exemplo n.º 13
0
def genereal_search(puzzle, args):
  if args.method == 1:
    # init node of uniform cost search using its heuristic
    start_node = UniformNode(0, puzzle, None, None)
  elif args.method == 2:
    # init node of A* with misplaces tile using its heuristic
    start_node = MisplacedNode(0, puzzle, None, None)
  elif args.method == 3:
    # init node of A* with Manhattan distance using its heuristic
    start_node = ManhattanNode(0, puzzle, None, None)
  else:
    raise ValueError("Not Implemented Method!")
  
  if (start_node.state == goal_state).all():  # no need to expand if input is the goal state
    print("Number of expanded nodes: ", 0)
    print("Max queue size: ", 0)
    return start_node
  
  queue = []
  max_queue = 0
  heapq.heappush(queue, start_node)
  explored_states = [start_node.state]
  while len(queue) > 0:
    max_queue = max(len(queue), max_queue)
    expand_node = heapq.heappop(queue)  # pop smallest cost node from queue to expand/explore
    end_node = expand(queue, expand_node, goal_state, explored_states)
    if end_node is not None:
      print("Number of expanded nodes: ", len(explored_states))
      print("Max queue size: ", max_queue)
      print("Depth:", end_node.depth)
      return end_node
  return None
Exemplo n.º 14
0
 def table(self):
     '''Generate a mysql like result table'''
     # more pythonic approach for assigning value to multi vars?
     header, body, sep = [''], [''], ['']
     for key, value in self.items():
         if isinstance(value, Dataset):
             value = value.summarize()
         value = str(value)
         width = real_len(max(len(key), len(value)))
         header.append(expand(key, width))
         body.append(expand(value, width))
         sep.append('-' * (width + TAB_WIDTH))
     for i in (header, body, sep):
         i.append('')
     header = '|'.join(header)
     body = '|'.join(body)
     sep = '+'.join(sep)
     return '\n'.join((sep, header, sep, body, sep))
Exemplo n.º 15
0
 def table(self):
     '''Generate a mysql like result table'''
     # more pythonic approach for assigning value to multi vars?
     header, body, sep = [''], [''], ['']
     for key, value in self.items():
         if isinstance(value, Dataset):
             value = value.summarize()
         value = str(value)
         width = real_len(max(len(key), len(value)))
         header.append(expand(key, width))
         body.append(expand(value, width))
         sep.append('-' * (width + TAB_WIDTH))
     for i in (header, body, sep):
         i.append('')
     header = '|'.join(header)
     body = '|'.join(body)
     sep = '+'.join(sep)
     return '\n'.join((sep, header, sep, body, sep))
Exemplo n.º 16
0
    def table(self):
        '''Generate a mysql like result table'''
        header, body, sep = [''], [], ['']

        #: find out the max length of word of each column
        max_len = {}
        for key in self.header:
            width = len(str(key))
            for line in self.body:
                if isinstance(line[key], Dataset):
                    value = line[key].summarize()
                else:
                    value = str(line[key])
                width = max(width, len(value))
            max_len[key] = real_len(width)
            #: build the header and sep in the same time
            header.append(expand(key, max_len[key]))
            sep.append('-' * (max_len[key] + TAB_WIDTH))
        for i in (header, sep):
            i.append('')
        header = '|'.join(header)
        sep = '+'.join(sep)

        #: build body
        for line in self.body:
            b = ['']
            for key, value in line.items():
                if isinstance(value, Dataset):
                    value = value.summarize()
                else:
                    value = str(value)
                width = max_len[key]
                b.append(expand(value, width))
            b.append('')
            body.append('|'.join(b))

        #: build table
        table = [sep, header, sep]
        for line in body:
            table.append(line)
            table.append(sep)
        return '\n'.join(table)
Exemplo n.º 17
0
    def table(self):
        '''Generate a mysql like result table'''
        header, body, sep = [''], [], ['']

        #: find out the max length of word of each column
        max_len = {}
        for key in self.header:
            width = len(str(key))
            for line in self.body:
                if isinstance(line[key], Dataset):
                    value = line[key].summarize()
                else:
                    value = str(line[key])
                width = max(width, len(value))
            max_len[key] = real_len(width)
            #: build the header and sep in the same time
            header.append(expand(key, max_len[key]))
            sep.append('-' * (max_len[key] + TAB_WIDTH))
        for i in (header, sep):
            i.append('')
        header = '|'.join(header)
        sep = '+'.join(sep)

        #: build body
        for line in self.body:
            b = ['']
            for key, value in line.items():
                if isinstance(value, Dataset):
                    value = value.summarize()
                else:
                    value = str(value)
                width = max_len[key]
                b.append(expand(value, width))
            b.append('')
            body.append('|'.join(b))

        #: build table
        table = [sep, header, sep]
        for line in body:
            table.append(line)
            table.append(sep)
        return '\n'.join(table)
Exemplo n.º 18
0
def barrier_dist(x):
    bdd = np.abs(x[:, 1] - BARR) <= 2 * EPS
    bdd = expand(bdd)

    # e.g. [[nan, nan], [x_h, barrier], ...]
    h = np.where(bdd, x, np.nan)[:, 0]
    # Drop nan
    dist = h[~ np.isnan(h)]

    plt.figure()
    sns.distplot(dist, hist=False, kde=True, bins=int(180 / 5), rug=True)
Exemplo n.º 19
0
    def __Astar(self, h_func, verbose=False):
        if h_func == 'h1':
            h_func = h1
        elif h_func == 'h2':
            h_func = h2

        open_, closed_ = [], []
        max_open_len = 0
        max_depth = 0

        start_puzzle = copy.deepcopy(self.START_PUZZLE)
        start_puzzle.cost = 0 + h_func(self.START_PUZZLE)
        open_.append(start_puzzle)

        while True:
            if not open_:
                print('There is no solution.')
                return None

            while True:
                v = open_.pop(0)  # lowest cost
                max_depth = max(max_depth, v.depth)

                if verbose:
                    print(v.status, v.cost)

                if closed_:
                    if v.status not in [i.status for i in closed_]:
                        break
                    else:
                        for i in closed_:
                            if v.status == i.status:
                                if v.cost <= i.cost:
                                    closed_.remove(i)
                                break
                else:
                    break

            if v.status == GOAL_STATE:
                goal_puzzle = v
                return start_puzzle, goal_puzzle, len(
                    closed_), max_open_len, max_depth

            closed_.append(v)
            children = [i for i in expand(v)]
            for i in children:
                i.cost = i.depth + h_func(i)
            open_.extend(children)
            max_open_len = max(max_open_len, len(open_))

            open_ = sorted(open_, key=lambda i: i.cost)
            closed_ = sorted(closed_, key=lambda i: i.cost)
Exemplo n.º 20
0
 def diagnose(self, i):
     if i % 100 == 0:
         print('Iteration: {}. Cost: {}'.format(
             i, np.average(self.costs[-10:])))
         x = np.random.randint(self.x_size)
         for j in range(200):
             print(self.i_to_char[x], end='')
             self.x = expand(np.eye(self.x_size)[x])
             y = self.forward_pass()
             y = normalize(y.reshape(self.x_size, ))
             x = np.argmax(y)
             #x = np.random.choice(np.arange(self.x_size), p=y)
         print('\n')
Exemplo n.º 21
0
def get_resnet_result(model, dataset, result_save_folder):
    if not os.path.exists(result_save_folder):
        os.makedirs(result_save_folder)
    model.eval()
    if CUDA:
        model.cuda()
    transform = utils.transform_factory(split='test', dataset=dataset)
    cleansing_video_dataset = VideoDataset_(
        root=os.path.join(args.dataset, 'test_dataset'),
        dataset=dataset,
        special_list=[],
        filter_type='not_in',
        feature_folder=feature_floder,
        ground_truth_folder='annotation_folder')
    for video_index in range(cleansing_video_dataset.__len__()):
        video_imgs, video_labels, video_name = cleansing_video_dataset.__getitem__(
            video_index, video_index + 1)

        video_dataset = VideoDataset(video_imgs, video_labels, transform)
        video_loader = data.DataLoader(dataset=video_dataset,
                                       shuffle=False,
                                       batch_size=1,
                                       drop_last=False)  # tackle one by one

        video_name = video_name[0]
        lines = [i for i in range(len(video_labels[0]) + 1)]
        lines[0] = 'Frame\tPhase\n'
        result_save_path = os.path.join(result_save_folder,
                                        video_name + '_pred.txt')
        with torch.no_grad():
            for iter, (imgs, labels, img_names) in enumerate(video_loader):
                if CUDA:
                    imgs, labels = imgs.cuda(), labels.cuda()
                bs, ncrops, c, h, w = imgs.size()
                feature, res = model(imgs.view(-1, c, h, w))

                avg_res = res.view(bs, ncrops, -1).mean(1)
                avg_res = F.softmax(avg_res, dim=1)

                predict = avg_res.max(1)[1].item()
                lines[iter + 1] = '{}\t{}\n'.format(iter, predict)

            print('testing video {} done!'.format(video_name, ))
            lines = utils.expand(
                lines, dataset=dataset,
                timeF=5)[0:config[dataset]['testset_len'][video_name]]
            with open(result_save_path, 'w') as f:
                for line in lines:
                    f.write(str(line))
Exemplo n.º 22
0
def put_on(img, el, condition, bg_color=1):
    # Center of the structural element
    x_c = el.size[0] // 2
    y_c = el.size[1] // 2

    # Additional emptiness to add to img along x and y
    dx_min = x_c
    dx_max = el.size[0] - x_c - 1
    dy_min = y_c
    dy_max = el.size[1] - y_c - 1


    element_pixels = Image2ll_binary(el)
    image_pixels = expand(Image2ll_binary(img),
        dx_min, dx_max, dy_min, dy_max, bg_color)
    new_image_pixels = [[
        1 for idx_y in range(img.size[1] + dy_min + dy_max)]
            for idx_x in range(img.size[0] + dx_min + dx_max)]

    black_count = 0
    for idx_x in range(dx_min, dx_min + img.size[0]):
        for idx_y in range(dy_min, dy_min + img.size[1]):
            if condition == 'any' and image_pixels[idx_x][idx_y] == 0:
                for dx in range(-dx_min, dx_max + 1):
                    for dy in range(-dy_min, dy_max + 1):
                        if element_pixels[x_c + dx][y_c + dy] == 0:
                            if new_image_pixels[idx_x + dx][idx_y + dy] != 0:
                                black_count += 1
                            new_image_pixels[idx_x + dx][idx_y + dy] = 0
            elif condition == 'all':
                if image_pixels[idx_x][idx_y] == 0:
                    all_match = True
                    for dx in range(-dx_min, dx_max + 1):
                        for dy in range(-dy_min, dy_max + 1):
                            if (image_pixels[idx_x + dx][idx_y + dy] != 0
                                and element_pixels[x_c + dx][y_c + dy] == 0):
                                    all_match = False
                                    break
                        if not all_match:
                            break
                    if all_match:
                        new_image_pixels[idx_x][idx_y] = 0
                        black_count += 1
    print('black:', black_count,
          'white:', img.size[0] * img.size[1] - black_count)
    return ll2Image_binary(cut(new_image_pixels, dx_min, dx_max, dy_min, dy_max))
Exemplo n.º 23
0
    def get_labels(self, images, bboxes):
        """ 
        Method for assigning labels to bounding boxes in images 
    
        Arguments:
        ---
        images: list of PIL images
        bboxes: list of bounding boxes for images
        (bbox config: [x1, y1, x2, y2])

        Returns:
        ---
        label_list: list of labels assigned to bounding boxes in images
        prob_list: confidence values for labels assigned 
        
        """

        label_list = []
        prob_list = []
        for index, img in enumerate(images):
            boxes = bboxes[index]
            data = []
            for i in range(len(boxes)):
                im = img.resize([224, 224], box=expand(boxes[i], img))
                data.append(self.transform(im))
            data = torch.stack(data).to(self.device)
            outputs = self.model(data)
            labels = [torch.max(op, 1)[1] for op in outputs]
            labels = {
                self.tasks[i]: label.cpu().numpy()
                for i, label in enumerate(labels)
            }
            probs = {}
            for idx, task in enumerate(self.tasks):
                task_probs = []
                for i in range(len(labels[task])):
                    p = outputs[idx][i][labels[task][i]].item()
                    task_probs.append(p)
                probs[task] = task_probs
            label_list.append(labels)
            prob_list.append(probs)

        return label_list, prob_list
Exemplo n.º 24
0
        for batch_i, batch in enumerate(chunker(list(profiles.keys()),
                                                FLAGS.batch_size)):
            size = min(len(batch), FLAGS.batch_size)
            
            # create needed binary vectors
            bin_profiles = {}
            masks = {}
            # only consider the movie that users have iteracted 
            for userid in batch:
                user_profile = np.array([0.] * len(all_movies))
                mask = [0] * (len(all_movies) * 5)
                for movie_id, rat in profiles[userid]:
                    user_profile[all_movies.index(movie_id)] = rat
                    for _i in range(5):
                        mask[5 * all_movies.index(movie_id) + _i] = 1
                example = expand(np.array([user_profile])).astype('float32')
                bin_profiles[userid] = example
                masks[userid] = mask

            profile_batch = [bin_profiles[el] for el in batch]
            masks_batch = [masks[id] for id in batch]
            
            # train_batch = np.array(profile_batch).reshape(size,
                                                        #   len(all_movies * 5))
            train_batch = np.array(profile_batch).reshape(size, len(all_movies) * 5)
            train_masks = np.array(masks_batch).reshape(size,
                                                        len(all_movies) * 5)
            # _  = sess.run([rbm.optimizer], feed_dict={rbm.input: train_batch, rbm.mask : masks_batch})
            _  = sess.run([rbm.optimizer], feed_dict={rbm.input: train_batch, rbm.mask : train_masks}) 
            sys.stdout.write('.')
            sys.stdout.flush()
Exemplo n.º 25
0
    def compute(self, context, *expressions, **kwargs):
        if not expressions:
            raise TypeError("groupby() takes at least 1 argument")

        # TODO: allow lists/tuples of arguments to group by the combinations
        # of keys
        for expr in expressions:
            if isinstance(expr, (bool, int, float)):
                raise TypeError("groupby() does not work with constant "
                                "arguments")
            if isinstance(expr, (tuple, list)):
                raise TypeError("groupby() takes expressions as arguments, "
                                "not a list of expressions")

        # On python 3, we could clean up this code (keyword only arguments).
        expr = kwargs.pop('expr', None)
        if expr is None:
            expr = Count()

#        by = kwargs.pop('by', None)
        filter_value = kwargs.pop('filter', None)
        percent = kwargs.pop('percent', False)
        possible_values = kwargs.pop('pvalues', None)
        totals = kwargs.pop('totals', True)

        expr_vars = [v.name for v in collect_variables(expr)]
        labels = [str(e) for e in expressions]
        columns = [expr_eval(e, context) for e in expressions]
        columns = [expand(c, context_length(context)) for c in columns]

        if filter_value is not None:
            filtered_columns = [col[filter_value] for col in columns]
            # FIXME: use the actual filter_expr instead of not_hashable
            filtered_context = context.subset(filter_value, expr_vars,
                                              not_hashable)
        else:
            filtered_columns = columns
            filtered_context = context

        if possible_values is None:
            possible_values = [np.unique(col) for col in filtered_columns]

        # We pre-filtered columns instead of passing the filter to partition_nd
        # because it is a bit faster this way. The indices are still correct,
        # because we use them on a filtered_context.
        groups = partition_nd(filtered_columns, True, possible_values)
        if not groups:
            return LabeledArray([], labels, possible_values)

        # evaluate the expression on each group
        # we use not_hashable to avoid storing the subset in the cache
        contexts = [filtered_context.subset(indices, expr_vars, not_hashable)
                    for indices in groups]
        data = [expr_eval(expr, c) for c in contexts]

        # TODO: use group_indices_nd directly to avoid using np.unique
        # this is twice as fast (unique is very slow) but breaks because
        # the rest of the code assumes all combinations are present
#        if self.filter is not None:
#            filter_value = expr_eval(self.filter, context)
#        else:
#            filter_value = True
#
#        d = group_indices_nd(columns, filter_value)
#        pvalues = sorted(d.keys())
#        ndim = len(columns)
#        possible_values = [[pv[i] for pv in pvalues]
#                           for i in range(ndim)]
#        groups = [d[k] for k in pvalues]

        # groups is a (flat) list of list.
        # the first variable is the outer-most "loop",
        # the last one the inner most.

        # add total for each row
        len_pvalues = [len(vals) for vals in possible_values]

        if percent:
            totals = True

        if totals:
            width = len_pvalues[-1]
            height = prod(len_pvalues[:-1])
            rows_indices = [np.concatenate([groups[y * width + x]
                                            for x in range(width)])
                            for y in range(height)]
            cols_indices = [np.concatenate([groups[y * width + x]
                                            for y in range(height)])
                            for x in range(width)]
            cols_indices.append(np.concatenate(cols_indices))

            # evaluate the expression on each "combined" group (ie compute totals)
            row_ctxs = [filtered_context.subset(indices, expr_vars, not_hashable)
                        for indices in rows_indices]
            row_totals = [expr_eval(expr, ctx) for ctx in row_ctxs]
            col_ctxs = [filtered_context.subset(indices, expr_vars, not_hashable)
                        for indices in cols_indices]
            col_totals = [expr_eval(expr, ctx) for ctx in col_ctxs]
        else:
            row_totals = None
            col_totals = None

        if percent:
            # convert to np.float64 to get +-inf if total_value is int(0)
            # instead of Python's built-in behaviour of raising an exception.
            # This can happen at least when using the default expr (count())
            # and the filter yields empty groups
            total_value = np.float64(col_totals[-1])
            data = [100.0 * value / total_value for value in data]
            row_totals = [100.0 * value / total_value for value in row_totals]
            col_totals = [100.0 * value / total_value for value in col_totals]

#        if self.by or self.percent:
#            if self.percent:
#                total_value = data[-1]
#                divisors = [total_value for _ in data]
#            else:
#                num_by = len(self.by)
#                inc = prod(len_pvalues[-num_by:])
#                num_groups = len(groups)
#                num_categories = prod(len_pvalues[:-num_by])
#
#                categories_groups_idx = [range(cat_idx, num_groups, inc)
#                                         for cat_idx in range(num_categories)]
#
#                divisors = ...
#
#            data = [100.0 * value / divisor
#                    for value, divisor in izip(data, divisors)]

        # convert to a 1d array. We don't simply use data = np.array(data),
        # because if data is a list of ndarray (for example if we use
        # groupby(a, expr=id), *and* all the ndarrays have the same length,
        # the result is a 2d array instead of an array of ndarrays like we
        # need (at this point).
        arr = np.empty(len(data), dtype=type(data[0]))
        arr[:] = data
        data = arr

        # and reshape it
        data = data.reshape(len_pvalues)
        return LabeledArray(data, labels, possible_values,
                            row_totals, col_totals)
Exemplo n.º 26
0
def boundary_condition(bias):
    eps = .01
    return (bias <= expand(np.min(bias, axis=-1))) & (bias < eps)
Exemplo n.º 27
0
def boundary_condition(bias):
    eps = .01
    return (bias <= expand(np.min(bias, axis=-1))) & (bias < eps)


fig, ax = plt.subplots()
ax.autoscale_view()
camera = Camera(fig)
for t in progressbar(range(int(T / dt))):
    plt.plot(*(corner.T), color='r')
    plt.scatter(*(x.T), color='black', s=5)
    plt.scatter(*z, color='blue', s=5)
    camera.snap()

    m = delta_matrix(x)
    m_norm = expand(norm(m, 2))
    with np.errstate(divide='ignore', invalid='ignore'):
        social = (1 / m_norm - a) * m
        social = np.nan_to_num(social)

    # axis 1 is the dummy varible
    social = social.sum(axis=1) / N

    zx = x - np.tile(z, (N, 1))

    vx = social + b * zx / expand(norm(zx, 2))
    vz = c / N * (zx / expand(norm(zx, p))).sum(axis=0)

    # Boundary condition
    eps = 0.01
    # If at door: |x[0]| <= 1 and |x[1]| < eps
Exemplo n.º 28
0
def run(name, dataset, config, all_users, all_movies, tests, initial_v, sep):
    config_name = config['name']
    number_hidden = config['number_hidden']
    epochs = config['epochs']
    ks = config['ks']
    momentums = config['momentums']
    l_w = config['l_w']
    l_v = config['l_v']
    l_h = config['l_h']
    decay = config['decay']
    batch_size = config['batch_size']

    config_result = config.copy()
    config_result['results'] = []

    vis = T.matrix()
    vmasks = T.matrix()

    rbm = CFRBM(len(all_movies) * 5, number_hidden)

    profiles = defaultdict(list)

    with open(dataset, 'rt') as data:
        for i, line in enumerate(data):
            uid, mid, rat, timstamp = line.strip().split(sep)
            profiles[uid].append((mid, float(rat)))

    print("Users and ratings loaded")

    for j in range(epochs):
        def get_index(col):
            if j/(epochs/len(col)) < len(col):
                return j/(epochs/len(col))
            else:
                return -1

        index = get_index(ks)
        mindex = get_index(momentums)
        icurrent_l_w = get_index(l_w)
        icurrent_l_v = get_index(l_v)
        icurrent_l_h = get_index(l_h)

        k = ks[index]
        momentum = momentums[mindex]
        current_l_w = l_w[icurrent_l_w]
        current_l_v = l_v[icurrent_l_v]
        current_l_h = l_h[icurrent_l_h]

        train = rbm.cdk_fun(vis,
                            vmasks,
                            k=k,
                            w_lr=current_l_w,
                            v_lr=current_l_v,
                            h_lr=current_l_h,
                            decay=decay,
                            momentum=momentum)
        predict = rbm.predict(vis)

        for batch_i, batch in enumerate(chunker(profiles.keys(),
                                                batch_size)):
            size = min(len(batch), batch_size)

            # create needed binary vectors
            bin_profiles = {}
            masks = {}
            for userid in batch:
                user_profile = [0.] * len(all_movies)
                mask = [0] * (len(all_movies) * 5)

                for movie_id, rat in profiles[userid]:
                    user_profile[all_movies.index(movie_id)] = rat
                    for _i in range(5):
                        mask[5 * all_movies.index(movie_id) + _i] = 1

                example = expand(np.array([user_profile])).astype('float32')
                bin_profiles[userid] = example
                masks[userid] = mask

            profile_batch = [bin_profiles[id] for id in batch]
            masks_batch = [masks[id] for id in batch]
            train_batch = np.array(profile_batch).reshape(size,
                                                          len(all_movies) * 5)
            train_masks = np.array(masks_batch).reshape(size,
                                                        len(all_movies) * 5)
            train_masks = train_masks.astype('float32')
            train(train_batch, train_masks)
            sys.stdout.write('.')
            sys.stdout.flush()

        ratings = []
        predictions = []

        for batch in chunker(tests.keys(), batch_size):
            size = min(len(batch), batch_size)

            # create needed binary vectors
            bin_profiles = {}
            masks = {}
            for userid in batch:
                user_profile = [0.] * len(all_movies)
                mask = [0] * (len(all_movies) * 5)

                for movie_id, rat in profiles[userid]:
                    user_profile[all_movies.index(movie_id)] = rat
                    for _i in range(5):
                        mask[5 * all_movies.index(movie_id) + _i] = 1

                example = expand(np.array([user_profile])).astype('float32')
                bin_profiles[userid] = example
                masks[userid] = mask

            positions = {profile_id: pos for pos, profile_id
                         in enumerate(batch)}
            profile_batch = [bin_profiles[el] for el in batch]
            test_batch = np.array(profile_batch).reshape(size,
                                                         len(all_movies) * 5)
            user_preds = revert_expected_value(predict(test_batch))
            for profile_id in batch:
                test_movies = tests[profile_id]
                try:
                    for movie, rating in test_movies:
                        current_profile = user_preds[positions[profile_id]]
                        predicted = current_profile[all_movies.index(movie)]
                        rating = float(rating)
                        ratings.append(rating)
                        predictions.append(predicted)
                except Exception:
                    pass

        vabs = np.vectorize(abs)
        distances = np.array(ratings) - np.array(predictions)

        mae = vabs(distances).mean()
        rmse = sqrt((distances ** 2).mean())

        iteration_result = {
            'iteration': j,
            'k': k,
            'momentum': momentum,
            'mae': mae,
            'rmse': rmse,
            'lrate': current_l_w
        }

        config_result['results'].append(iteration_result)

        print(iteration_str.format(j, k, current_l_w, momentum, mae, rmse))

        with open('experiments/{}_{}.json'.format(config_name, name), 'wt') as res_output:
            res_output.write(json.dumps(config_result, indent=4))

        W,V,H = rbm.get_weights()
        print H
Exemplo n.º 29
0
kernel_size = int(8*std+1) #SIFT
localWorkSizeX= 16 #for shared local memory
localWorkSizeY= 16
mode = "mirror"   #borders handling : mirror, reflect, nearest, wrap


if ((kernel_size % 2) == 0):
	print("Error: the filter size is not odd !")
	exit(1)

	
'''
***** Pre-allocating *****
'''
l2=lena().astype("float32")
l = utils.expand(l2, 4*std, mode=mode)
#l=numpy.copy(l[0:32,0:32]) #memory layout is not contiguous...
[imwidth, imheight] = l.shape
output = numpy.zeros_like(l)
x=numpy.arange(kernel_size,dtype="float64")
shift = kernel_size//2.0
v = (x-shift)/std
g1 = numpy.exp(-v*v/2.0)
g=numpy.outer(g1,g1).astype("float32")
g/=g.sum()
#plt.imshow(g)
#plt.show()

patterns = [('iwidth',imwidth),('iheight',imheight),('fsize',kernel_size),('hsize',kernel_size//2),('tsize',(kernel_size//2)*2),('hisize',(kernel_size//2)*imwidth)]
opts = "-D IMAGE_W=iwidth -D IMAGE_H=iheight -D FILTER_SIZE=fsize -D HALF_FILTER_SIZE=hsize -D TWICE_HALF_FILTER_SIZE=tsize -D HALF_FILTER_SIZE_IMAGE_W=hisize"
for (p,r) in patterns:
Exemplo n.º 30
0
 def list(cls, **params):
     s, api_url, params = prepare_request(params)
     url = api_url + cls._class_url()
     r = s.get(url, params=utils.expand(params))
     r.raise_for_status()
     return convert_to_sense_object(None, r.json())
Exemplo n.º 31
0
def run(name, dataset, config, all_users, all_movies, tests, initial_v, sep):
    config_name = config['name']
    number_hidden = config['number_hidden']
    epochs = config['epochs']
    ks = config['ks']
    momentums = config['momentums']
    l_w = config['l_w']
    l_v = config['l_v']
    l_h = config['l_h']
    decay = config['decay']
    batch_size = config['batch_size']

    config_result = config.copy()
    config_result['results'] = []

    vis = T.matrix()
    vmasks = T.matrix()

    rbm = CFRBM(len(all_movies) * 5, number_hidden)

    profiles = defaultdict(list)

    with open(dataset, 'rt') as data:
        for i, line in enumerate(data):
            uid, mid, rat, timstamp = line.strip().split(sep)
            profiles[uid].append((mid, float(rat)))

    print("Users and ratings loaded")

    for j in range(epochs):
        def get_index(col):
            if j/(epochs/len(col)) < len(col):
                return j/(epochs/len(col))
            else:
                return -1

        index = get_index(ks)
        mindex = get_index(momentums)
        icurrent_l_w = get_index(l_w)
        icurrent_l_v = get_index(l_v)
        icurrent_l_h = get_index(l_h)

        k = ks[index]
        momentum = momentums[mindex]
        current_l_w = l_w[icurrent_l_w]
        current_l_v = l_v[icurrent_l_v]
        current_l_h = l_h[icurrent_l_h]

        train = rbm.cdk_fun(vis,
                            vmasks,
                            k=k,
                            w_lr=current_l_w,
                            v_lr=current_l_v,
                            h_lr=current_l_h,
                            decay=decay,
                            momentum=momentum)
        predict = rbm.predict(vis)

        for batch_i, batch in enumerate(chunker(profiles.keys(),
                                                batch_size)):
            size = min(len(batch), batch_size)

            # create needed binary vectors
            bin_profiles = {}
            masks = {}
            for userid in batch:
                user_profile = [0.] * len(all_movies)
                mask = [0] * (len(all_movies) * 5)

                for movie_id, rat in profiles[userid]:
                    user_profile[all_movies.index(movie_id)] = rat
                    for _i in range(5):
                        mask[5 * all_movies.index(movie_id) + _i] = 1

                example = expand(np.array([user_profile])).astype('float32')
                bin_profiles[userid] = example
                masks[userid] = mask

            profile_batch = [bin_profiles[id] for id in batch]
            masks_batch = [masks[id] for id in batch]
            train_batch = np.array(profile_batch).reshape(size,
                                                          len(all_movies) * 5)
            train_masks = np.array(masks_batch).reshape(size,
                                                        len(all_movies) * 5)
            train_masks = train_masks.astype('float32')
            train(train_batch, train_masks)
            sys.stdout.write('.')
            sys.stdout.flush()

        ratings = []
        predictions = []

        for batch in chunker(tests.keys(), batch_size):
            size = min(len(batch), batch_size)

            # create needed binary vectors
            bin_profiles = {}
            masks = {}
            for userid in batch:
                user_profile = [0.] * len(all_movies)
                mask = [0] * (len(all_movies) * 5)

                for movie_id, rat in profiles[userid]:
                    user_profile[all_movies.index(movie_id)] = rat
                    for _i in range(5):
                        mask[5 * all_movies.index(movie_id) + _i] = 1

                example = expand(np.array([user_profile])).astype('float32')
                bin_profiles[userid] = example
                masks[userid] = mask

            positions = {profile_id: pos for pos, profile_id
                         in enumerate(batch)}
            profile_batch = [bin_profiles[el] for el in batch]
            test_batch = np.array(profile_batch).reshape(size,
                                                         len(all_movies) * 5)
            user_preds = revert_expected_value(predict(test_batch))
            for profile_id in batch:
                test_movies = tests[profile_id]
                try:
                    for movie, rating in test_movies:
                        current_profile = user_preds[positions[profile_id]]
                        predicted = current_profile[all_movies.index(movie)]
                        rating = float(rating)
                        ratings.append(rating)
                        predictions.append(predicted)
                except Exception:
                    pass

        vabs = np.vectorize(abs)
        distances = np.array(ratings) - np.array(predictions)

        mae = vabs(distances).mean()
        rmse = sqrt((distances ** 2).mean())

        iteration_result = {
            'iteration': j,
            'k': k,
            'momentum': momentum,
            'mae': mae,
            'rmse': rmse,
            'lrate': current_l_w
        }

        config_result['results'].append(iteration_result)

        print(iteration_str.format(j, k, current_l_w, momentum, mae, rmse))

        with open('{}_{}.json'.format(config_name, name), 'wt') as res_output:
            res_output.write(json.dumps(config_result, indent=4))
Exemplo n.º 32
0
def run(name, dataset, user_info, config, all_users, all_movies, all_occupations, all_sex, all_ages, tests, initial_v, sep):
    config_name = config['name']
    number_hidden = config['number_hidden']
    epochs = config['epochs']
    ks = config['ks']
    momentums = config['momentums']
    l_w = config['l_w']
    l_v = config['l_v']
    l_h = config['l_h']
    decay = config['decay']
    batch_size = config['batch_size']

    config_result = config.copy()
    config_result['results'] = []

    vis_x = T.matrix()
    vis_o = T.matrix()
    vis_s = T.matrix()
    vis_a = T.matrix()
    vmasks_x = T.matrix()
    vmasks_o = T.matrix()
    vmasks_s = T.matrix()
    vmasks_a = T.matrix()

    rbm = CFRBM(len(all_movies) * 5, len(all_occupations), 1, len(all_ages), number_hidden)

    profiles = defaultdict(list)

    with open(dataset, 'rt') as data:
        for i, line in enumerate(data):
            uid, mid, rat, timstamp = line.strip().split(sep)
            profiles[uid].append((mid, float(rat)))

    print("Users and ratings loaded")

    user_occ = defaultdict(list)
    user_sex = defaultdict(list)
    user_age = defaultdict(list)

    r = csv.reader(open(user_info, 'rb'), delimiter='|')
    for row in r:
        user_age[row[0]] = [int(x) for x in row[1:7]]
        user_sex[row[0]] = [int(row[7])]
        user_occ[row[0]] = [int(x) for x in row[8:]]

    print("User info loaded")

    for j in range(epochs):
        def get_index(col):
            if j/(epochs/len(col)) < len(col):
                return j/(epochs/len(col))
            else:
                return -1

        index = get_index(ks)
        mindex = get_index(momentums)
        icurrent_l_w = get_index(l_w)
        icurrent_l_v = get_index(l_v)
        icurrent_l_h = get_index(l_h)

        k = ks[index]
        momentum = momentums[mindex]
        current_l_w = l_w[icurrent_l_w]
        current_l_v = l_v[icurrent_l_v]
        current_l_h = l_h[icurrent_l_h]

        train = rbm.cdk_fun(vis_x,
                            vis_o,
                            vis_s,
                            vis_a,
                            vmasks_x,
                            vmasks_o,
                            vmasks_s,
                            vmasks_a,
                            k=k,
                            w_lr=current_l_w,
                            v_lr=current_l_v,
                            h_lr=current_l_h,
                            decay=decay,
                            momentum=momentum)
        predict = rbm.predict(vis_x, vis_o, vis_s, vis_a)

        start_time = time.time()

        for batch_i, batch in enumerate(chunker(profiles.keys(),
                                                batch_size)):
            size = min(len(batch), batch_size)

            # create needed binary vectors
            bin_profiles = {}
            occ_profiles = {}
            sex_profiles = {}
            age_profiles = {}
            masks_x = {}
            masks_o = {}
            masks_s = {}
            masks_a = {}
            for userid in batch:
                user_profile = [0.] * len(all_movies)
                occ_profile = [0.] * len(all_occupations)
                sex_profile = [0.] * 1
                age_profile = [0.] * len(all_ages)
                mask_x = [0] * (len(all_movies) * 5)
                mask_o = [1] * (len(all_occupations))
                mask_s = [1] * (1)
                mask_a = [1] * (len(all_ages))

                for movie_id, rat in profiles[userid]:
                    user_profile[all_movies.index(movie_id)] = rat
                    for _i in range(5):
                        mask_x[5 * all_movies.index(movie_id) + _i] = 1

                mask_o = [1] * len(all_occupations)
                mask_s = [1] * 1
                mask_a = [1] * len(all_ages)

                example_x = expand(np.array([user_profile])).astype('float32')
                example_o = expand(np.array([occ_profile]), k=1).astype('float32')
                example_s = expand(np.array([sex_profile]), k=1).astype('float32')
                example_a = expand(np.array([age_profile]), k=1).astype('float32')
                bin_profiles[userid] = example_x
                occ_profiles[userid] = example_o
                sex_profiles[userid] = example_s
                age_profiles[userid] = example_a
                masks_x[userid] = mask_x
                masks_o[userid] = mask_o
                masks_s[userid] = mask_s
                masks_a[userid] = mask_a

            profile_batch = [bin_profiles[id] for id in batch]
            occ_batch = [occ_profiles[id] for id in batch]
            sex_batch = [sex_profiles[id] for id in batch]
            age_batch = [age_profiles[id] for id in batch]
            masks_x_batch = [masks_x[id] for id in batch]
            masks_o_batch = [masks_o[id] for id in batch]
            masks_s_batch = [masks_s[id] for id in batch]
            masks_a_batch = [masks_a[id] for id in batch]
            train_batch_x = np.array(profile_batch).reshape(size,
                                                          len(all_movies) * 5)
            train_batch_o = np.array(occ_batch).reshape(size,
                                                         len(all_occupations))
            train_batch_s = np.array(sex_batch).reshape(size,
                                                         1)
            train_batch_a = np.array(age_batch).reshape(size,
                                                         len(all_ages))
            train_masks_x = np.array(masks_x_batch).reshape(size,
                                                        len(all_movies) * 5)
            train_masks_o = np.array(masks_o_batch).reshape(size,
                                                        len(all_occupations))
            train_masks_s = np.array(masks_s_batch).reshape(size,
                                                        1)
            train_masks_a = np.array(masks_a_batch).reshape(size,
                                                        len(all_ages))
            train_masks_x = train_masks_x.astype('float32')
            train_masks_o = train_masks_o.astype('float32')
            train_masks_s = train_masks_s.astype('float32')
            train_masks_a = train_masks_a.astype('float32')
            train(train_batch_x, train_batch_o, train_batch_s, train_batch_a, train_masks_x, train_masks_o, train_masks_s, train_masks_a)
            sys.stdout.write('.')
            sys.stdout.flush()

        end_time = time.time()

        train_time = end_time - start_time

        ratings = []
        predictions = []

        start_time = time.time()

        for batch in chunker(tests.keys(), batch_size):
            size = min(len(batch), batch_size)

            # create needed binary vectors
            bin_profiles = {}
            occ_profiles = {}
            sex_profiles = {}
            age_profiles = {}
            masks_x = {}
            masks_o = {}
            masks_s = {}
            masks_a = {}
            for userid in batch:
                user_profile = [0.] * len(all_movies)
                occ_profile = [0.] * len(all_occupations)
                sex_profile = [0.] * 1
                age_profile = [0.] * len(all_ages)
                mask_x = [0] * (len(all_movies) * 5)
                mask_o = [1] * (len(all_occupations))
                mask_s = [1] * (1)
                mask_a = [1] * (len(all_ages))

                for movie_id, rat in profiles[userid]:
                    user_profile[all_movies.index(movie_id)] = rat
                    for _i in range(5):
                        mask_x[5 * all_movies.index(movie_id) + _i] = 1

                mask_o = [1] * len(all_occupations)
                mask_s = [1] * 1
                mask_a = [1] * len(all_ages)

                example_x = expand(np.array([user_profile])).astype('float32')
                example_o = expand(np.array([occ_profile]), k=1).astype('float32')
                example_s = expand(np.array([sex_profile]), k=1).astype('float32')
                example_a = expand(np.array([age_profile]), k=1).astype('float32')
                bin_profiles[userid] = example_x
                occ_profiles[userid] = example_o
                sex_profiles[userid] = example_s
                age_profiles[userid] = example_a
                masks_x[userid] = mask_x
                masks_o[userid] = mask_o
                masks_s[userid] = mask_s
                masks_a[userid] = mask_a

            positions = {profile_id: pos for pos, profile_id
                         in enumerate(batch)}
            profile_batch = [bin_profiles[el] for el in batch]
            occ_batch = [occ_profiles[el] for el in batch]
            sex_batch = [sex_profiles[el] for el in batch]
            age_batch = [age_profiles[el] for el in batch]
            test_batch_x = np.array(profile_batch).reshape(size,
                                                         len(all_movies) * 5)
            test_batch_o = np.array(occ_batch).reshape(size,
                                                        len(all_occupations))
            test_batch_s = np.array(sex_batch).reshape(size,
                                                        1)
            test_batch_a = np.array(age_batch).reshape(size,
                                                        len(all_ages))
            user_preds = revert_expected_value(predict(test_batch_x, test_batch_o, test_batch_s, test_batch_a))
            for profile_id in batch:
                test_movies = tests[profile_id]
                try:
                    for movie, rating in test_movies:
                        current_profile = user_preds[positions[profile_id]]
                        predicted = current_profile[all_movies.index(movie)]
                        rating = float(rating)
                        ratings.append(rating)
                        predictions.append(predicted)
                except Exception:
                    pass

        end_time = time.time()

        test_time = end_time - start_time

        true_rat = np.array(ratings, dtype=np.uint8)
        pred_rat = np.array(predictions, dtype=np.uint8)

        #print true_rat < 3, true_rat
        prec_rec = precision_recall_fscore_support(true_rat < 3,pred_rat < 3, average='binary')
        print prec_rec

        vabs = np.vectorize(abs)
        distances = np.array(ratings) - np.array(predictions)

        mae = vabs(distances).mean()
        rmse = sqrt((distances ** 2).mean())

        iteration_result = {
            'iteration': j,
            'k': k,
            'momentum': momentum,
            'mae': mae,
            'rmse': rmse,
            'lrate': current_l_w,
            'train_time': train_time,
            'test_time': test_time,
            'prec_rec': prec_rec
        }

        config_result['results'].append(iteration_result)

        print(iteration_str.format(j, k, current_l_w, momentum, mae, rmse))

        with open('experiments/{}_{}.json'.format(config_name, name), 'wt') as res_output:
            res_output.write(json.dumps(config_result, indent=4))
Exemplo n.º 33
0
compare_with_2d_conv = 0
show_bench_results = 0

'''
if ((kernel_size % 2) == 0):
	print("Error: the filter size is not odd !")
	exit(1)
'''
	
'''
***** Pre-allocating *****
'''
l2=lena().astype("float32")
#l2=l2[:,:-1]
l2 = l2#[350:500,100:250]
if (expand == 1): l = utils.expand(l2, 4*std, mode=mode)
else: l=numpy.ascontiguousarray(l2,dtype="float32")
[imheight, imwidth] = l.shape
IMAGE_W = numpy.int32(imwidth)
IMAGE_H = numpy.int32(imheight)
FILTER_SIZE = numpy.int32(kernel_size)

output = numpy.zeros_like(l)
x=numpy.arange(kernel_size,dtype="float64")
shift = (kernel_size-1.0)/2.0
v = (x-shift)/std
g1 = numpy.exp(-v*v/2.0).astype("float32")
g1/=g1.sum()
print g1

Exemplo n.º 34
0

elif mode == "reflect":
    # 4 corners
        output[s0 + k0:, s1 + k1:] = input_img[-1:-k0 - 1:-1, -1:-k1 - 1:-1]
        output[:k0, :k1] = input_img[k0 - 1::-1, k1 - 1::-1]
        output[:k0, s1 + k1:] = input_img[k0 - 1::-1, s1 - 1: s1 - k1 - 1:-1]
        output[s0 + k0:, :k1] = input_img[s0 - 1: s0 - k0 - 1:-1, k1 - 1::-1]
    # 4 sides
        output[k0:k0 + s0, :k1] = input_img[:s0, k1 - 1::-1]
        output[:k0, k1:k1 + s1] = input_img[k0 - 1::-1, :s1]
        output[-k0:, k1:s1 + k1] = input_img[:s0 - k0 - 1:-1, :]
        output[k0:s0 + k0, -k1:] = input_img[:, :s1 - k1 - 1:-1]
'''

output_mirror= utils.expand(input_img, sigma, mode="mirror")
output_reflect=utils.expand(input_img, sigma, mode="reflect")
output_nearest=utils.expand(input_img, sigma, mode="nearest")
output_wrap=utils.expand(input_img, sigma, mode="wrap")

#conv_ref = scipy.ndimage.gaussian_filter(l2,std,mode="mirror")



fig = plt.figure()
sp1 = fig.add_subplot(2,3,1,title="Input image")
sp2 = fig.add_subplot(2,3,2,title="Mirror")
sp3 = fig.add_subplot(2,3,3,title="Reflect")
sp4 = fig.add_subplot(2,3,4,title="Nearest")
sp5 = fig.add_subplot(2,3,5,title="Wrap")
#sp6 = fig.add_subplot(2,3,6,title="Error for: mirror")
Exemplo n.º 35
0
def run(name, dataset, config, all_users, all_movies, tests, initial_v, sep):
    config_name = config["name"]
    number_hidden = config["number_hidden"]
    epochs = config["epochs"]
    ks = config["ks"]
    momentums = config["momentums"]
    l_w = config["l_w"]
    l_v = config["l_v"]
    l_h = config["l_h"]
    decay = config["decay"]

    config_result = config.copy()
    config_result["results"] = []

    vis = T.matrix()
    vmasks = T.matrix()

    rbm = CFRBM(len(all_users) * 5, number_hidden)

    profiles = defaultdict(list)

    with open(dataset, "rt") as data:
        for i, line in enumerate(data):
            uid, mid, rat, timstamp = line.strip().split(sep)
            profiles[mid].append((uid, float(rat)))

    print("Users and ratings loaded")

    for j in range(epochs):

        def get_index(col):
            if j / (epochs / len(col)) < len(col):
                return j / (epochs / len(col))
            else:
                return -1

        index = get_index(ks)
        mindex = get_index(momentums)
        icurrent_l_w = get_index(l_w)
        icurrent_l_v = get_index(l_v)
        icurrent_l_h = get_index(l_h)

        k = ks[index]
        momentum = momentums[mindex]
        current_l_w = l_w[icurrent_l_w]
        current_l_v = l_v[icurrent_l_v]
        current_l_h = l_h[icurrent_l_h]

        train = rbm.cdk_fun(
            vis, vmasks, k=k, w_lr=current_l_w, v_lr=current_l_v, h_lr=current_l_h, decay=decay, momentum=momentum
        )
        predict = rbm.predict(vis)

        batch_size = 10
        for batch_i, batch in enumerate(utils.chunker(profiles.keys(), batch_size)):
            size = min(len(batch), batch_size)

            # create needed binary vectors
            bin_profiles = {}
            masks = {}
            for movieid in batch:
                movie_profile = [0.0] * len(all_users)
                mask = [0] * (len(all_users) * 5)

                for user_id, rat in profiles[movieid]:
                    movie_profile[all_users.index(user_id)] = rat
                    for _i in range(5):
                        mask[5 * all_users.index(user_id) + _i] = 1

                example = expand(np.array([movie_profile])).astype("float32")
                bin_profiles[movieid] = example
                masks[movieid] = mask

            movies_batch = [bin_profiles[id] for id in batch]
            masks_batch = [masks[id] for id in batch]
            train_batch = np.array(movies_batch).reshape(size, len(all_users) * 5)
            train_masks = np.array(masks_batch).reshape(size, len(all_users) * 5)
            train_masks = train_masks.astype("float32")
            train(train_batch, train_masks)
            sys.stdout.write(".")
            sys.stdout.flush()

        batch_size = 10
        ratings = []
        predictions = []

        for batch in utils.chunker(tests.keys(), batch_size):
            size = min(len(batch), batch_size)

            # create needed binary vectors
            bin_profiles = {}
            masks = {}
            for movieid in batch:
                movie_profile = [0.0] * len(all_users)
                mask = [0] * (len(all_users) * 5)

                for userid, rat in profiles[movieid]:
                    movie_profile[all_users.index(userid)] = rat
                    for _i in range(5):
                        mask[5 * all_users.index(userid) + _i] = 1

                example = expand(np.array([movie_profile])).astype("float32")
                bin_profiles[movieid] = example
                masks[movieid] = mask

            positions = {movie_id: pos for pos, movie_id in enumerate(batch)}
            movies_batch = [bin_profiles[el] for el in batch]
            test_batch = np.array(movies_batch).reshape(size, len(all_users) * 5)
            movie_predictions = revert_expected_value(predict(test_batch))
            for movie_id in batch:
                test_users = tests[movie_id]
                try:
                    for user, rating in test_users:
                        current_movie = movie_predictions[positions[movie_id]]
                        predicted = current_movie[all_users.index(user)]
                        rating = float(rating)
                        ratings.append(rating)
                        predictions.append(predicted)
                except Exception:
                    pass

        vabs = np.vectorize(abs)
        distances = np.array(ratings) - np.array(predictions)

        mae = vabs(distances).mean()
        rmse = sqrt((distances ** 2).mean())

        iteration_result = {
            "iteration": j,
            "k": k,
            "momentum": momentum,
            "mae": mae,
            "rmse": rmse,
            "lrate": current_l_w,
        }

        config_result["results"].append(iteration_result)

        print(iteration_str.format(j, k, current_l_w, momentum, mae, rmse))

        with open("{}_{}.json".format(config_name, name), "wt") as res_output:
            res_output.write(json.dumps(config_result, indent=4))
Exemplo n.º 36
0
    def train(self, tasks, model_name, criterion, attention, device, n_epoch=30, lr=0.1, verbose=True):
        """
        trains the given model
        
        args:
            task: task to train the model on
            criterion: loss for train
            attention: whether to use attention mechanism
            model_name: name of the model to train
            n_epoch: number of epochs for training
            lr: learning rate for the optimization
            device: whether to use CPU or GPU
            verbose: if set to True prints additional info
        
        returns: the obtained metrics, the final predictions and the wrong ones
        """
        
        total_loss_train = []
        total_loss_val = []
        total_accuracy_val = []
        total_accuracy_train = []
        total_accuracy_val_pix = []
        total_accuracy_train_pix = []
        total_predictions = []
        total_wrong_predictions = []
        
        criterion = criterion()
        for task in tasks:
            
            sh1_big = 0
            sh2_big = 0
            for i in range(len(task['train'])):
                sh1 = task['train'][i]['input'].shape[0]
                sh2 = task['train'][i]['input'].shape[1]
                if sh1 > sh1_big:
                    sh1_big = sh1
                if sh2 > sh2_big:
                    sh2_big = sh2     
            for i in range(len(task['test'])):
                sh1 = task['test'][i]['input'].shape[0]
                sh2 = task['test'][i]['input'].shape[1]
                if sh1 > sh1_big:
                    sh1_big = sh1
                if sh2 > sh2_big:
                    sh2_big = sh2  
                    
            net = model_name(task['train'], sh1_big, sh2_big, attention).to(device)
            optimizer = Adam(net.parameters(), lr = lr)
        
            loss_train = []
            loss_val = []
            accuracy_val = []
            accuracy_train = []
            accuracy_val_pix = []
            accuracy_train_pix = []

            for epoch in tqdm(range(n_epoch)):

                net.train()
                loss_iter = 0
                for sample in task['train']:
                    img = FloatTensor(expand(sample['input'])).to(device)
                    labels = LongTensor(sample['output']).unsqueeze(dim=0).to(device)
                    optimizer.zero_grad()    
                    outputs = net(img)
                    loss = criterion(outputs, labels)
                    loss.backward()
                    optimizer.step()  

                net.eval()
                with torch.no_grad():

                    correct_val = 0
                    correct_val_pix = 0
                    total_val = 0
                    loss_iter_val = 0
                    predictions = []
                    wrong_pred = []
                    n_pixels_val = 0
                    for sample in task['test']:

                        img = FloatTensor(expand(sample['input'])).to(device)
                        labels = LongTensor(sample['output']).unsqueeze(dim=0).to(device)
                        outputs = net(img)
                        _, pred = torch.max(outputs.data, 1)
                        predictions.append((img, pred))  
                        n_pixels_val += pred.shape[1]*pred.shape[2]

                        total_val += labels.size(0)
                        flag =  (torch.all(torch.eq(pred, labels))).sum().item() 
                        correct_val += flag
                        if flag == 0:
                            wrong_pred.append((img, pred))
                        correct_val_pix += (torch.eq(pred, labels)).sum().item()            
                        loss = criterion(outputs, labels)
                        loss_iter_val += loss.item()

                correct_train = 0
                correct_train_pix = 0 
                total_train = 0
                loss_iter_train = 0
                n_pixels_train = 0
                for sample in task['train']:

                    img = FloatTensor(expand(sample['input'])).to(device)
                    labels = LongTensor(sample['output']).unsqueeze(dim=0).to(device)
                    outputs = net(img)
                    _, pred = torch.max(outputs.data, 1)
                    n_pixels_train += pred.shape[1]*pred.shape[2]

                    total_train += labels.size(0)
                    correct_train += (torch.all(torch.eq(pred, labels))).sum().item()
                    correct_train_pix += (torch.eq(pred, labels)).sum().item()
                    loss = criterion(outputs, labels)
                    loss_iter_train += loss.item()

                loss_train.append(loss_iter_train/len(task['train']))
                loss_val.append(loss_iter_val/len(task['test']))

                val_accuracy = 100 * correct_val / total_val
                val_accuracy_pix = 100 * correct_val_pix/(n_pixels_val)
                accuracy_val.append(val_accuracy)
                accuracy_val_pix.append(val_accuracy_pix)

                train_accuracy = 100 * correct_train / total_train
                train_accuracy_pix = 100 * correct_train_pix/(n_pixels_train)
                accuracy_train.append(train_accuracy)
                accuracy_train_pix.append(train_accuracy_pix)

                if verbose:
                    print('\nEpoch: ['+str(epoch+1)+'/'+str(n_epoch)+']')
                    print('Train loss is: {}'.format(loss_train[-1]))
                    print('Validation loss is: {}'.format(loss_val[-1]))
                    print('Train accuracy is: {} %'.format(accuracy_train[-1]))
                    print('Train accuracy for pixels is: {} %'.format(accuracy_train_pix[-1]))
                    print('Validation accuracy is: {} %'.format(accuracy_val[-1]))
                    print('Validation accuracy for pixels is: {} %'.format(accuracy_val_pix[-1]))
                    
        total_loss_train += loss_train
        total_loss_val += loss_val
        total_accuracy_train += accuracy_train
        total_accuracy_train_pix += accuracy_train_pix
        total_accuracy_val += accuracy_val
        total_accuracy_val_pix += accuracy_val_pix
        total_predictions += predictions
        total_wrong_predictions += wrong_pred

        metrics = {'loss_train': total_loss_train, 'loss_val': total_loss_val, 'accuracy_train':total_accuracy_train, 
                   'accuracy_train_pix': total_accuracy_train_pix, 'accuracy_val':total_accuracy_val, 
                   'accuracy_val_pix': total_accuracy_val_pix}
        final_pred = total_predictions
        
        return metrics, final_pred, total_wrong_predictions
Exemplo n.º 37
0
class RNN:
    h_size = 100
    learning_rate = 1e-1
    batch_size = 32
    regul = .5
    initial_h = expand(np.zeros(h_size))
    filename = 'input8.txt'

    def __init__(self):
        self.h = self.initial_h
        self.text = open(self.filename, 'r').read()[:-1]
        chars = set(self.text)
        self.x_size = len(chars)

        self.i_to_char = {i: char for i, char in enumerate(chars)}
        self.char_to_i = {char: i for i, char in enumerate(chars)}

        self.W_xh = np.random.randn(self.h_size, self.x_size) * .01
        self.W_hh = np.random.randn(self.h_size, self.h_size) * .01
        self.W_hy = np.random.randn(self.x_size, self.h_size) * .01
        self.B_h = np.zeros((self.h_size, 1))
        self.B_y = np.zeros((self.x_size, 1))

    def forward_pass(self):
        self.prev_h = self.h  #np.copy(self.h)
        self.h = np.tanh(
            np.dot(self.W_xh, self.x) + np.dot(self.W_hh, self.h) + self.B_h)
        return np.tanh(np.dot(self.W_hy, self.h) + self.B_y)

    def backward_pass(self):
        dCdY = self.y - self.target
        dYdZ = np.diag(
            tanh_prime(np.dot(self.W_hy, self.h)).reshape(self.x_size, ))
        dZdWhy = np.repeat(self.h.T, self.x_size, axis=0)
        dYdWhy = np.dot(dYdZ, dZdWhy)
        dCdWhy = dCdY * dYdWhy
        dCdBy = np.dot(dCdY.T, dYdZ)

        dZdHt = self.W_hy
        dYdHt = np.dot(dYdZ, dZdHt)
        dHtdZ2 = tanh_prime(
            np.dot(self.W_xh, self.x) + np.dot(self.W_hh, self.h))
        dHtdZ2 = np.diag(dHtdZ2.reshape(self.h_size, ))
        dZ2dWxh = np.repeat(self.x.T, self.h_size, axis=0)
        dZ2dWhh = np.repeat(self.prev_h.T, self.h_size, axis=0)
        dYdZ2 = np.dot(dYdHt, dHtdZ2)
        dYdWxh = np.dot(dYdZ2, dZ2dWxh)
        dYdWhh = np.dot(dYdZ2, dZ2dWhh)
        dCdWxh = np.repeat(np.dot(dCdY.T, dYdWxh), self.h_size, axis=0)
        dCdWhh = np.repeat(np.dot(dCdY.T, dYdWhh), self.h_size, axis=0)
        dCdBh = np.dot(dCdY.T, dYdZ2)

        return [dCdWxh, dCdWhh, dCdWhy, dCdBh, dCdBy]

    def diagnose(self, i):
        if i % 100 == 0:
            print('Iteration: {}. Cost: {}'.format(
                i, np.average(self.costs[-10:])))
            x = np.random.randint(self.x_size)
            for j in range(200):
                print(self.i_to_char[x], end='')
                self.x = expand(np.eye(self.x_size)[x])
                y = self.forward_pass()
                y = normalize(y.reshape(self.x_size, ))
                x = np.argmax(y)
                #x = np.random.choice(np.arange(self.x_size), p=y)
            print('\n')

    def train(self):
        self.costs = []
        gradients = []

        for i, (char, target) in enumerate(zip(self.text, self.text[1:])):
            self.x = expand(np.eye(self.x_size)[self.char_to_i[char]])
            self.target = expand(np.eye(self.x_size)[self.char_to_i[target]])

            self.y = self.forward_pass()
            gradients.append(self.backward_pass())

            if (i + 1) % self.batch_size == 0:
                dCdWxh, dCdWhh, dCdWhy, dCdBh, dCdBy = np.average(gradients,
                                                                  axis=0)
                self.W_xh -= self.learning_rate * dCdWxh
                self.W_hh -= self.learning_rate * dCdWhh
                self.W_hy -= self.learning_rate * dCdWhy
                self.B_h -= self.learning_rate * dCdBh.T
                self.B_y -= self.learning_rate * dCdBy.T

            self.costs.append(cost(self.target, self.y))
            self.diagnose(i)

        plt.plot(self.costs)
        plt.show(block=False)
        time.sleep(1000)
Exemplo n.º 38
0
    def train(self, tasks, model_name, criterion, n_epoch=30, lr=0.1, device = "cpu", verbose=True,  inner_lr = 0.1, inner_iter = 10, meta_size = 50):
        """
        trains the given model
        
        args:
            task: task to train the model on
            criterion: loss for train
            model_name: name of the model to train
            n_epoch: number of epochs for training
            lr: learning rate for the optimization
            device: whether to use CPU or GPU
            verbose: if set to True prints additional info
            inner_lr: if using a meta-learning algorithm, the learning rate of the inner loop
            inner_iter: if using a meta-learning algorithm, the iterations of the inner loop
            meta_size: if using a meta-learning algorithm, how big to set the meta samples size
        
        returns: the obtained metrics, the final predictions and the wrong ones
        """
        
        total_loss_train = []
        total_loss_val = []
        total_accuracy_val = []
        total_accuracy_train = []
        total_accuracy_val_pix = []
        total_accuracy_train_pix = []
        total_predictions = []
        total_wrong_predictions = []
        
        criterion = criterion()
        
        sh1_big = 0
        sh2_big = 0
        for task in tasks:
            for i in range(len(task['train'])):
                sh1 = task['train'][i]['input'].shape[0]
                sh2 = task['train'][i]['input'].shape[1]
                if sh1 > sh1_big:
                    sh1_big = sh1
                if sh2 > sh2_big:
                    sh2_big = sh2     
            for i in range(len(task['test'])):
                sh1 = task['test'][i]['input'].shape[0]
                sh2 = task['test'][i]['input'].shape[1]
                if sh1 > sh1_big:
                    sh1_big = sh1
                if sh2 > sh2_big:
                    sh2_big = sh2  
        net = model_name(device, sh1_big, sh2_big).to(device)
        optimizer = Adam(net.parameters(), lr = lr)
        
        for epoch in tqdm(range(n_epoch)):

            losses = []
            for task in tasks:
            
                  loss_train = []
                  loss_val = []
                  accuracy_val = []
                  accuracy_train = []
                  accuracy_val_pix = []
                  accuracy_train_pix = []

                  inputs = []
                  outputs = []
                  for sample in task["train"]:
                      x = Tensor(sample['input'])
                      x = pad_crop(x, sh1_big, sh2_big, x.shape[0], x.shape[1], goal = "pad")
                      inputs.append(FloatTensor(expand(x).float()).to(device))
                      y = Tensor(sample['output'])
                      y = pad_crop(y, sh1_big, sh2_big, y.shape[0], y.shape[1], goal = "pad")
                      outputs.append(LongTensor(y.long()).unsqueeze(0).to(device))

                  inputs_train = inputs[:meta_size]
                  inputs_val = inputs[meta_size:]
                  outputs_train = outputs[:meta_size]
                  outputs_val = outputs[meta_size:]


                  fast_weights = OrderedDict(net.named_parameters())

                  for _ in range(inner_iter):
                      grads = []
                      loss = 0
                      for x,y in zip(inputs_train, outputs_train):
                          logits = net._forward(x.to(device), fast_weights)
                          loss += criterion(logits.to(device), y.to(device))
                      loss /= len(inputs_train)
                      gradients = torch.autograd.grad(loss, fast_weights.values(), create_graph=True)
                      fast_weights = OrderedDict((name, param - inner_lr * grad)
                                for ((name, param), grad) in zip(fast_weights.items(), gradients))

                  loss = 0    
                  for x,y in zip(inputs_val, outputs_val):
                      logits = net._forward(x.to(device), fast_weights)
                      loss += criterion(logits.to(device), y.to(device))

                  loss /= len(inputs_val)
                  loss.backward(retain_graph=True)
                  losses.append(loss.float())
                  gradients = torch.autograd.grad(loss, fast_weights.values(), create_graph=True)

            net.train()
            optimizer.zero_grad()
            meta_loss = torch.stack(losses).mean()
            meta_loss.backward()
            optimizer.step()

            net.eval()
            with torch.no_grad():

                correct_val = 0
                correct_val_pix = 0
                total_val = 0
                loss_iter_val = 0
                predictions = []
                wrong_pred = []
                n_pixels_val = 0
                for sample in task['test']:

                    img = FloatTensor(expand(sample['input'])).to(device)
                    y = LongTensor(sample['output'])
                    labels = pad_crop(y, sh1_big, sh2_big, y.shape[0], y.shape[1], "pad").unsqueeze(0).to(device)
                    outputs = net(img)
                    _, pred = torch.max(outputs.data, 1)
                    predictions.append((img, pred))  
                    n_pixels_val += pred.shape[1]*pred.shape[2]

                    total_val += labels.size(0)
                    flag =  (torch.all(torch.eq(pred, labels))).sum().item() 
                    correct_val += flag
                    if flag == 0:
                        wrong_pred.append((img, pred))
                    correct_val_pix += (torch.eq(pred, labels)).sum().item()            
                    loss = criterion(outputs, labels)
                    loss_iter_val += loss.item()

            correct_train = 0
            correct_train_pix = 0 
            total_train = 0
            loss_iter_train = 0
            n_pixels_train = 0
            for sample in task['train']:

                img = FloatTensor(expand(sample['input'])).to(device)
                y = LongTensor(sample['output'])
                labels = pad_crop(y, sh1_big, sh2_big, y.shape[0], y.shape[1], "pad").unsqueeze(0).to(device)
                outputs = net(img)
                _, pred = torch.max(outputs.data, 1)
                n_pixels_train += pred.shape[1]*pred.shape[2]

                total_train += labels.size(0)
                correct_train += (torch.all(torch.eq(pred, labels))).sum().item()
                correct_train_pix += (torch.eq(pred, labels)).sum().item()
                loss = criterion(outputs, labels)
                loss_iter_train += loss.item()

            loss_train.append(loss_iter_train/len(task['train']))
            loss_val.append(loss_iter_val/len(task['test']))

            val_accuracy = 100 * correct_val / total_val
            val_accuracy_pix = 100 * correct_val_pix/(n_pixels_val)
            accuracy_val.append(val_accuracy)
            accuracy_val_pix.append(val_accuracy_pix)

            train_accuracy = 100 * correct_train / total_train
            train_accuracy_pix = 100 * correct_train_pix/(n_pixels_train)
            accuracy_train.append(train_accuracy)
            accuracy_train_pix.append(train_accuracy_pix)

            if verbose:
                print('\nEpoch: ['+str(epoch+1)+'/'+str(n_epoch)+']')
                print('Train loss is: {}'.format(loss_train[-1]))
                print('Validation loss is: {}'.format(loss_val[-1]))
                print('Train accuracy is: {} %'.format(accuracy_train[-1]))
                print('Train accuracy for pixels is: {} %'.format(accuracy_train_pix[-1]))
                print('Validation accuracy is: {} %'.format(accuracy_val[-1]))
                print('Validation accuracy for pixels is: {} %'.format(accuracy_val_pix[-1]))
            
            total_loss_train.append(loss_train)
            total_loss_val.append(loss_val)
            total_accuracy_train.append(accuracy_train)
            total_accuracy_train_pix.append(accuracy_train_pix)
            total_accuracy_val.append(accuracy_val)
            total_accuracy_val_pix.append(accuracy_val_pix)
            total_predictions.append(total_predictions)
            total_wrong_predictions.append(wrong_pred)
                
        metrics = {'loss_train': total_loss_train, 'loss_val': total_loss_val, 'accuracy_train':total_accuracy_train, 
                   'accuracy_train_pix': total_accuracy_train_pix, 'accuracy_val':total_accuracy_val, 
                   'accuracy_val_pix': total_accuracy_val_pix}
        final_pred = total_predictions
        
        return metrics, final_pred, total_wrong_predictions
 def odd_expansion(n):
     i = len(expand(n)) - 1
     return 0 < i and i % 2 == 1
Exemplo n.º 40
0
	def scan(self):

		#login items files
		loginItems = []

		#dbg msg
		utils.logMessage(utils.MODE_INFO, 'running scan')

		#init results dictionary
		results = self.initResults(LOGIN_ITEM_NAME, LOGIN_ITEM_DESCRIPTION)

		#process
		# ->open file and read each line
		for userLoginItems in utils.expand(LOGIN_ITEM_FILE):

			#wrap
			try:

				#dbg msg
				utils.logMessage(utils.MODE_INFO, 'scanning %s' % userLoginItems)

				#load plist and check
				plistData = utils.loadPlist(userLoginItems)

				#extract sessions items
				sesssionItems = plistData['SessionItems']

				#extract custom list items
				customListItems = sesssionItems['CustomListItems']

				#iterate over all login items
				for customListItem in customListItems:

					#wrap it
					try:

						#extact alias data
						aliasData = list((customListItem['Alias']).bytes())

						#parse alias data
						loginItem = self.parseAliasData(aliasData)

						#save extracted login item
						if loginItem:

							#save
							results['items'].append(file.File(loginItem))

					#ignore exceptions
					except Exception, e:

						#skip
						continue

			#ignore exceptions
			except:

				#skip
				continue

		return results
Exemplo n.º 41
0
    def _build_model(self):
        if args.mann == 'none':

            def single_cell(num_units):
                return tf.contrib.rnn.BasicLSTMCell(num_units, forget_bias=1.0)

            cell = tf.contrib.rnn.OutputProjectionWrapper(
                tf.contrib.rnn.MultiRNNCell([
                    single_cell(args.num_units) for _ in range(args.num_layers)
                ]),
                args.num_bits_per_vector,
                activation=None)

            initial_state = tuple(
                tf.contrib.rnn.LSTMStateTuple(
                    c=expand(tf.tanh(learned_init(args.num_units)),
                             dim=0,
                             N=args.batch_size),
                    h=expand(tf.tanh(learned_init(args.num_units)),
                             dim=0,
                             N=args.batch_size))
                for _ in range(args.num_layers))

        elif args.mann == 'ntm':
            cell = NTMCell(args.num_layers,
                           args.num_units,
                           args.num_memory_locations,
                           args.memory_size,
                           args.num_read_heads,
                           args.num_write_heads,
                           addressing_mode='content_and_location',
                           shift_range=args.conv_shift_range,
                           reuse=False,
                           output_dim=args.num_bits_per_vector,
                           clip_value=args.clip_value,
                           init_mode=args.init_mode)

            initial_state = cell.zero_state(args.batch_size, tf.float32)
        elif args.mann == 'dnc':
            access_config = {
                'memory_size': args.num_memory_locations,
                'word_size': args.memory_size,
                'num_reads': args.num_read_heads,
                'num_writes': args.num_write_heads,
            }
            controller_config = {
                'hidden_size': args.num_units,
            }

            cell = DNC(access_config, controller_config,
                       args.num_bits_per_vector, args.clip_value)
            initial_state = cell.initial_state(args.batch_size)

        output_sequence, _ = tf.nn.dynamic_rnn(cell=cell,
                                               inputs=self.inputs,
                                               time_major=False,
                                               initial_state=initial_state)

        if args.task == 'copy' or args.task == 'repeat_copy':
            self.output_logits = output_sequence[:, self.max_seq_len + 1:, :]
        elif args.task == 'associative_recall':
            self.output_logits = output_sequence[:,
                                                 3 * (self.max_seq_len + 1) +
                                                 2:, :]
        elif args.task in ('traversal', 'shortest_path'):
            self.output_logits = output_sequence[:, -self.max_seq_len:, :]

        if args.task in ('copy', 'repeat_copy', 'associative_recall'):
            self.outputs = tf.sigmoid(self.output_logits)

        if args.task in ('traversal', 'shortest_path'):
            output_logits_split = tf.split(self.output_logits, 9, axis=2)
            self.outputs = tf.concat(
                [tf.nn.softmax(logits) for logits in output_logits_split],
                axis=2)
Exemplo n.º 42
0
 def expand(self, w):
     return expand(w, { 'name': self.name, 'number': str(self.hits.current) })