def test_customop(): x = np.array([[0.24854138, 1.94385293, 2.33848549, 2.75407309, 1.66905118], [0.26498274, 1.60618255, 1.25387436, 2.9215846, 1.26427169], [0.87108803, 1.45227827, 2.17339809, 0.50049702, 0.5883466], [0.66406034, 0.48855862, 1.53960508, 0.66568797, 2.23948055], [2.72220612, 1.82959485, 1.51552618, 1.54757016, 1.64023012], [1.69430802, 2.21234513, 0.44159807, 1.94465274, 0.11623679], [0.71774937, 1.99183721, 2.93154152, 0.23254174, 1.63623933], [1.54450952, 2.32885258, 1.64220968, 1.66349828, 2.50975782], [0.99172053, 2.60171951, 1.14377575, 0.28264201, 2.50368237], [1.99669231, 2.16996937, 1.77290071, 1.34783694, 2.42391734]]) label = np.array([4, 0, 0, 1, 0, 4, 0, 2, 1, 3]) grad_func = grad(softmax) # Check forward assert rel_error(softmax(x, label), 2.16612911224) < 1e-12 expected_out = np.array([[0.00323393, 0.01761957, 0.02614461, 0.0396159, -0.08661401], [-0.09591437, 0.01562194, 0.01098321, 0.05821122, 0.011098], [-0.08735776, 0.02260642, 0.04649542, 0.00872727, 0.00952864], [0.00992692, -0.09167096, 0.02382641, 0.00994309, 0.04797453], [-0.05756653, 0.01738011, 0.01269563, 0.01310903, 0.01438177], [0.02244518, 0.03767938, 0.00641325, 0.02883012, -0.09536792], [-0.09406418, 0.02122317, 0.05431486, 0.00365391, 0.01487223], [0.01242947, 0.02723256, -0.08629487, 0.01400002, 0.03263282], [0.00820025, -0.05897573, 0.00954693, 0.00403532, 0.03719322], [0.01982428, 0.02357495, 0.01584915, -0.08963896, 0.03039058]]) # Check backward assert rel_error(grad_func(x, label), expected_out) < 1e-7 print('All passed!')
def llh_time(): ret, x0, val_llh = garch_data() ret = np.array(ret) x0 = np.array(x0) t = benchmark(lambda: garchLLH(ret, x0), args.n, val_llh) return t
def test_creation(): a = np.array([1, 2, 3]) print(a) b = np.array([[1, 2, 3], [2, 3, 4]]) print(b) a = np.zeros((2, 3)) print(a) b = np.ones((2, 3)) print(b) c = np.full((2, 3), 7) print(c) d = np.empty((2, 3)) print(d)
def test_autoblocklist_policy(): p = AutoBlacklistPolicy(gen_rule=True, append_rule=True) minpy.set_global_policy(p) a = np.array([100, 100]) print(a) b = np.array([50, 50]) c = np.array([0, 0]) np.add(a, b, c) print(c) np.add(a, b, out=c) np.add(a, b, out=c) print(c)
def _select_polygon_points(self, num): x = self.np_random.randint(self.screen_width / 2 - 6, self.screen_width / 2 + 20, 1) y = self.np_random.randint(self.screen_width / 2 - 3, self.screen_width / 2 + 5, 1) start = (x[0], y[0]) base = 180 / config['rotate_degree'] base_angle = math.pi / base angle_options = (np.arange(base - 2) + 1) * base_angle angle = 0 rotate_degree = self.np_random.choice(angle_options, 3) size = np.array(np.random.choice(range(3, 5), 3)) * 5 #size = [3,3,4] points = [] points.append(start) for i in range(num - 1): angle += rotate_degree[i] next_x = int(start[0] + size[i] * math.cos(angle)) next_y = int(start[1] + size[i] * math.sin(angle)) next_point = (next_x, next_y) points.append(next_point) start = next_point dist = points[0][0] - points[-1][0] if dist < 0: next_y = math.tan(angle_options[0]) * dist else: next_y = math.tan(-angle_options[0]) * dist next_point = (points[0][0], int(points[-1][1] + next_y)) points.append(next_point) return points
def train(net_shapes, net_params, optimizer, utility, pool): # pass seed instead whole noise matrix to parallel will save your time noise_seed = np.random.randint( 0, 2**32 - 1, size=N_KID, dtype=np.uint32) #.repeat(2) # mirrored sampling noise_seed = np.concatenate([noise_seed, noise_seed ]).reshape([2, N_KID]).T.reshape([N_KID * 2]) # serially train with GPU rewards = [] for k_id in range(N_KID * 2): reward = get_reward(net_shapes, net_params, env, CONFIG['ep_max_step'], CONFIG['continuous_a'], [noise_seed[k_id], k_id]) rewards = np.array(rewards) kids_rank = np.argsort(rewards)[::-1] # rank kid id by reward cumulative_update = np.zeros_like(net_params) # initialize update values for ui, k_id in enumerate(kids_rank): np.random.seed(noise_seed[k_id]) # reconstruct noise using seed cumulative_update += utility[ui] * sign(k_id) * np.random.randn( net_params.size) gradients = optimizer.get_gradients(cumulative_update / (2 * N_KID * SIGMA)) return net_params + gradients, rewards
def count_not_target_points(self, idxs): answer = [] for idx in idxs: partition = self.graphics.pixels[idx[0]:idx[1], idx[2]:idx[3], config['target_channel']] answer.append( np.prod(partition.shape) - np.count_nonzero(partition == FULL)) return np.count_nonzero(np.array(answer) < 5)
def cross_entropy_loss_gradient(s, y): """ Calculates the gradient of the hinge loss function by the scores. The gradient formula is { ds_j / dL = e^s_j / sum e^j, ds_y_i / dL = e^s_y_i / sum e^j - 1 }. :param s: The score parameter of the loss function. :param y: The ground truth label parameter of the loss function. :return: The gradient as a matrix of the same shape as `s`. """ dscores = probs(s) dscores[np.array(range(y.shape[0])), y] -= 1 dscores /= y.shape[0] return dscores
def attack_mnist(model, alpha=0.2, beta=0.001, isTarget=False, num_attacks=10): imgs = np.array( nnp.load('data/mnist_data.npy')[60000:].transpose( (0, 2, 3, 1)).astype(np.float32) / 255.) labs = nnp.load('data/mnist_labels.npy')[60000:] nb_labs = nnp.max(labs) print( "\n\n Running {} attack on {} random MNIST test images for alpha= {} beta= {}\n\n" .format("targetted" if isTarget else "untargetted", num_attacks, alpha, beta)) total_distortion = [] samples = [] for i in range(nb_labs + 1): samples.append( np.random.permutation(np.arange(len(labs))[labs == i])[0]) # samples = [6312, 6891, 4243, 8377, 7962, 6635, 4970, 7809, 5867, 9559, 3579, 8269, 2282, 4618, 2290, 1554, 4105, 9862, 2408, 5082, 1619, 1209, 5410, 7736, 9172, 1650, 5181, 3351, 9053, 7816, 7254, 8542, 4268, 1021, 8990, 231, 1529, 6535, 19, 8087, 5459, 3997, 5329, 1032, 3131, 9299, 3910, 2335, 8897, 7340, 1495, 5244,8323, 8017, 1787, 4939, 9032, 4770, 2045, 8970, 5452, 8853, 3330, 9883, 8966, 9628, 4713, 7291, 9770, 6307, 5195, 9432, 3967, 4757, 3013, 3103, 3060, 541, 4261, 7808, 1132, 1472, 2134, 634, 1315, 8858, 6411, 8595, 4516, 8550, 3859, 3526] #true_labels = [3, 1, 6, 6, 9, 2, 7, 5, 5, 3, 3, 4, 5, 6, 7, 9, 1, 6, 3, 4, 0, 6, 5, 9, 7, 0, 3, 1, 6, 6, 9, 6, 4, 7, 6, 3, 4, 3, 4, 3, 0, 7, 3, 5, 3, 9, 3, 1, 9, 1, 3, 0, 2, 9, 9, 2, 2, 3, 3, 3, 0, 5, 2, 5, 2, 7, 2, 2, 5, 7, 4, 9, 9, 0, 0, 7, 9, 4, 5, 5, 2, 3, 5, 9, 3, 0, 9, 0, 1, 2, 9, 9] for idx in samples: #idx = random.randint(100, len(test_dataset)-1) image, label = imgs[idx], labs[idx] print("\n\n\n\n======== Image %d =========" % idx) print("Original label: ", label) lab = predict(model, image) print("Predicted label: ", lab) if lab != label: print( 'CHANGE IMAGES#{}: prediction of original image is not the same with true label' .format(i)) continue #target = None if not isTarget else random.choice(list(range(label)) + list(range(label+1, 10))) advs = [image] for i in range(nb_labs): target = (label + i) % (nb_labs + 1) adv = attack_targeted(model, imgs[labs == target], image, label, target, alpha=alpha, beta=beta, iterations=1000) print(i, "Predicted label for adversarial example: ", predict(model, adversarial)) advs.append(np.clip(adv, 0, 1)) total_distortion.append( np.linalg.norm(adv.reshape(-1) - image.reshape(-1))) np.save('advs/opt_attacks_{}_show.npy'.format(idx), advs) print("Average distortion on random {} images is {}".format( len(total_distortion), np.mean(total_distortion)))
def calc_prospective_reward(self, angle=None): if angle == None: angle = self.angle prospect_width = config['prospect_width'] else: prospect_width = config['target_line_width'] / 2 fps = self.calc_prepend_points( (self.tx + math.cos(angle) * config['target_line_width'] / 2, self.ty + math.sin(angle) * config['target_line_width'] / 2), angle, prospect_width) prospect = config['prospect_length'] npoint = [] for pr in prospect: npoint.append((math.cos(angle) * pr + self.tx, math.sin(angle) * pr + self.ty)) sps = [] for nps in npoint: sps.append(self.calc_prepend_points(nps, angle, prospect_width)) #TODO does not work properly for fractional degrees prospect_reward = [] for index, point in enumerate(sps): xs = [fps[0][0], fps[1][0], point[0][0], point[1][0]] ys = [fps[0][1], fps[1][1], point[0][1], point[1][1]] minx = max(min(xs), 0) maxx = min(max(xs), self.screen_width) miny = max(min(ys), 0) maxy = min(max(ys), self.screen_height) rectangle_view = np.array(self.graphics.pixels[minx:maxx, miny:maxy, :]) target_points = np.logical_and( rectangle_view[:, :, config['target_channel']] == FULL, rectangle_view[:, :, config['draw_channel']] == EMPTY) total_points = 2 * prospect_width * prospect[index] prospect_reward.append( np.count_nonzero(target_points) / total_points) rewards = prospect_reward * np.array(config['prospect_reward']) rewards *= ((1 + self.coverage - self.basic_coverage) * self.turn_effect)**3 return sum(rewards)
def initial_point_reward(self, loc): line_width = int(config['target_line_width'] / 2) distance = config['forward_distance'] long_width = line_width + distance left_idx = [-line_width, long_width, -line_width, line_width] right_idx = [-long_width, line_width, -line_width, line_width] down_idx = [-line_width, line_width, -line_width, long_width] up_idx = [-line_width, line_width, -long_width, line_width] base = np.array([loc[0], loc[0], loc[1], loc[1]]) idxs = np.array([left_idx, right_idx, down_idx, up_idx]) for i in range(len(idxs)): idxs[i] = np.minimum( np.maximum(idxs[i] + base, 0), [self.width, self.width, self.height, self.height]) target_direction = self.count_not_target_points(idxs) if target_direction == 0: return -300 elif target_direction == 1: return 300 elif target_direction == 2: return 0 elif target_direction > 2: return -200
def calculate_cross_entropy_loss(s, y, w, lambda_): """ Calculates the loss of a score matrix depending on the ground truth labels. This method uses cross entropy loss (from Softmax). :param s: The score matrix of form (N x K), where N is the number of observations and K is the number of classes. :param y: The ground truth label vector of length N. :param w: The weight matrix of the output layer of form (H x K), where H is the size of the previous layer. :param lambda_: The regularization loss hyperparameter. :return: The cross-entropy loss, where 0 indicates a perfect match between s and y and +Inf indicates a perfect mismatch. """ probabilities = probs(s) log_probabilities = -np.log(probabilities[np.array(range(y.shape[0])), y]) data_loss = np.sum(log_probabilities) / y.shape[0] return data_loss + regularization_loss(w, lambda_)
def rescale(container, inputs, parameters): """ recover original distribution at the final layer of every container. """ # returns outputs, factor list factors = [] all_factors = [] input_shape = inputs.shape[1:] # find final affine layer ending = None for index in range(len(container._modules) - 1, -1, -1): value = container._modules[index] if isinstance(value, Affine) or isinstance(value, Convolution): ending = index break # iterate through module for module_index, module in enumerate(container._modules): shapes = module.parameter_shape(input_shape) input_shape = module.output_shape(input_shape) if isinstance(module, Affine) or isinstance(module, Convolution): for key, value in shapes.items(): if 'weight' in key: E_X_2 = np.mean(inputs**2) if isinstance(module, Affine): n = value[0] else: C, W, H = value[1:] n = C * W * H std_from = np.std(parameters[key]) std_to = 1 / (E_X_2 * n)**0.5 rescaling_factor = std_to / std_from if module_index == ending: parameters[key] /= np.prod(np.array(factors)) else: factors.append(rescaling_factor) parameters[key] *= rescaling_factor ''' factors.append(rescaling_factor) parameters[key] *= rescaling_factor ''' inputs = module.forward(inputs, parameters) return inputs, factors
def main(): global LOOP, PREFIX, PATH, TARGET, TRIANGLE_NUM # 声明全局变量 img = Image.open(TARGET).resize((256, 256)).convert('RGBA') size = (256, 256) Canvas.target_pixels = [np.array(x) for x in list(img.split())] # 生成一系列的图片作为父本,选择其中最好的一个进行遗传 parentList = [] for i in range(20): print('正在生成第%d个初代个体' % (i)) parentList.append(Canvas()) parentList[i].add_triangles(TRIANGLE_NUM) parentList[i].calc_match_rate() parent = sorted(parentList, key=lambda x: x.match_rate)[0] del parentList gc.collect() # 进入遗传算法的循环 i = 0 while i < 30000: childList = [] # 每一代从父代中变异出10个个体 for j in range(10): childList.append(Canvas()) childList[j].mutate_from_parent(parent) childList[j].calc_match_rate() child = sorted(childList, key=lambda x: x.match_rate)[0] # 选择其中适应度最好的一个个体 del childList gc.collect() parent.calc_match_rate() if i % LOOP == 0: print('%10d parent rate %11d \t child1 rate %11d' % (i, parent.match_rate, child.match_rate)) parent = parent if parent.match_rate < child.match_rate else child # 如果子代比父代更适应环境,那么子代成为新的父代 # 否则保持原样 child = None if i % LOOP == 0: # 每隔LOOP代保存一次图片 parent.draw_it(i) #print(parent.match_rate) #print ('%10d parent rate %11d \t child1 rate %11d' % (i, parent.match_rate, child.match_rate)) i += 1
def test_startup_perf(): start = time.time() np.array([100, 100]).asnumpy() end = time.time() print('First call:', end - start) start = time.time() np.array([100, 100]).asnumpy() end = time.time() print('Second call:', end - start) start = time.time() np.array([100, 100]).asnumpy() end = time.time() print('Third call:', end - start)
def calc_match_rate(self): if self.match_rate > 0: return self.match_rate self.match_rate = 0 self.img = Image.new('RGBA', self.size) draw = ImageDraw.Draw(self.img) draw.polygon([(0, 0), (0, 255), (255, 255), (255, 0)], fill=(255, 255, 255, 255)) for triangle in self.triangles: self.img = Image.alpha_composite(self.img, triangle.img_t or triangle.draw_it(self.size)) # 与下方代码功能相同,此版本便于理解但效率低 # pixels = [self.img.getpixel((x, y)) for x in range(0, self.size[0], 2) for y in range(0, self.size[1], 2)] # for i in range(0, min(len(pixels), len(self.target_pixels))): # delta_red = pixels[i][0] - self.target_pixels[i][0] # delta_green = pixels[i][1] - self.target_pixels[i][1] # delta_blue = pixels[i][2] - self.target_pixels[i][2] # self.match_rate += delta_red * delta_red + \ # delta_green * delta_green + \ # delta_blue * delta_blue arrs = [np.array(x) for x in list(self.img.split())] # 分解为RGBA四通道 for i in range(3): # 对RGB通道三个矩阵分别与目标图片相应通道作差取平方加和评估相似度 self.match_rate += np.sum(np.square(arrs[i]-self.target_pixels[i]))[0]
def predict(models, img, t=0): img = np.clip(img, 0, 1) * 255 img = extend_data(config['permutation'], np.array([img])) scores = np.hstack([m.predict(img) for m in models])[0] #print(scores.shape) nat_labels = np.zeros(scores.shape).astype(np.float32) nat_labels[scores >= 0.5] = 1. rep = rep_labels[:len(scores)].T tmp = np.repeat([nat_labels], rep.shape[0], axis=0) dists = np.sum(np.absolute(tmp - rep), axis=-1) min_dist = np.min(dists) pred_labels = np.arange(len(dists))[dists == min_dist] pred_scores = [ np.sum([ scores[k] if rep[j][k] == 1 else 1 - scores[k] for k in np.arange(len(scores)) ]) for j in pred_labels ] pred_label = pred_labels[np.argmax(pred_scores)] if min_dist <= 0: return pred_label else: return -1
def reset(self): self.repeated_forwards = 0 self.circular = 0 self.basic_coverage = 0 self.graphics.reset() self.start_candidates = [] self.rotate_right = 0 self.rotate_left = 0 self.recent_actions = [] self.recent_rewards = deque(maxlen=10) self.recent_rotate_number = 0 self.pen = True self.wrong_forward = False self.wrong_forward_numbers = 0 self.consec_repeat = 1 self.first_forward = True self.before_pen = False if config['draw'] else True self.remaining_steps = self.maximum_steps self.total_repeat = 1 self.before_draw = config['select_start_point'] if config['draw']: x = 10 y = 20 else: x = np.random.choice(range(3, 16)) * 5 y = np.random.choice(range(6, 13)) * 5 self.origin = [x, y] size = np.array(np.random.choice(range(3, 5), 3) * 5).astype(int) #size = [15, 15, 20] prob = [0.9] * 9 prob[1] = 0.2 target_num = np.random.choice(range(1, 10), size=1) origin = self.origin self.corner_locations = [ origin, [origin[0] + size[0], origin[1]], [origin[0], origin[1] + size[1]], [origin[0] + size[0], origin[1] + size[1]], [origin[0], origin[1] + size[1] + size[2]], [origin[0] + size[0], origin[1] + size[1] + size[2]] ] self.graphics.draw_number(origin, target_num[0], size, chan=config['target_channel']) #self.graphics.draw_number(self.coordinate, self.rotate_right, [SSL] * 3) #self.graphics.draw_number(self.coordinate2, self.rotate_left, [SSL] * 3) #self.graphics.draw_number(self.coordinate3, self.recent_rotate_number, [SSL] * 3) #random polygon # polygon_points = self._select_polygon_points(config["polygon_size"]) # self.graphics.draw_polygon(polygon_points) #random rectangle # point = self.np_random.randint(10, self.screen_width/2, 2) # self.graphics.rect(*point, size[0], size[1]) if config['draw'] == True: self.no_repeat_boundary = 0 if config['edge_detection'] or config['draw_random_start']: #if config['edge_detection']: #TODO make it random self.angle = np.random.choice( [0, math.pi / 2, -math.pi / 2, math.pi]) lines = self.graphics.draw_digit(origin, target_num, SSL, 0, simulate=True) line = np.random.choice(lines, 1) ps = (np.array( self.graphics.give_line_num_points(line, origin, size)) / 5).astype(int) self.tx = np.random.choice(range(ps[0][0], ps[1][0] + 1)) * 5 self.ty = np.random.choice(range(ps[0][1], ps[1][1] + 1)) * 5 self.ot = [self.tx, self.ty] else: self.angle = 0 options = self.calc_available_starting_points(target_num) start = np.array(self.corner_locations)[options] available_points = sorted( np.random.choice(range(len(start)), size=np.random.randint(2, len(start) + 1), replace=False)) self.start_point = start[available_points] #self.start_point = start self.tx, self.ty = self.start_point[np.random.randint( len(self.start_point))] if config['select_start_point']: self.fill_the_point(self.start_point) #self.no_repeat_boundary = max(size[0], size[1], size[2])*2/5 else: self.angle = 0 x_opt = [5, 10, 15] self.tx = np.random.choice(x_opt, p=[0.6, 0.2, 0.2]) #self.tx = x_opt[0] y_opt = [20, 25, 30] if self.tx == x_opt[0]: self.ty = np.random.choice(y_opt) else: self.ty = 20 #self.ty = y_opt[0] self.graphics.draw_number(self.coordinate, self.rotate_right, [SSL] * 3) self.graphics.draw_number(self.coordinate2, self.rotate_left, [SSL] * 3) self.graphics.draw_number(self.coordinate3, self.recent_rotate_number, [SSL] * 3) if config['select_start_point'] == False: self._draw_turtle() self._done = False self.initial_distance = self.calc_distance(self.tx, self.ty) self.total_diff = 0 self.maximum_moving_step = int( math.sqrt((self.initial_distance**2) / 2) / config['forward_distance'] * 1.3) * 2 + 3 # num = self.np_random.randint(3, 8) # self._draw_polygon(num) return np.uint8(self.graphics.pixels)
def dU(beta): return mp.dot(X.T, (mp.exp(mp.dot(X,beta))/(1+mp.exp(mp.dot(X,beta))) - y)) + beta/alpha D = X.shape[1] q = mp.zeros((D, 1), dtype=mp.float32) out = mp.zeros((n_iter, D), dtype=mp.float32) for i in range(n_iter): q = hmc(U, dU, epsilon, L, q) out[i,:] = mp.ravel(q) return out with cpu() if args.mode == 'cpu' else gpu(0): with open('params.json') as params_file: out = {} params = json.load(params_file) X_train, y_train, X_test, y_test = get_data() X_train = mp.array(X_train) y_train = mp.array(y_train) X_test = mp.array(X_test) y_test = mp.array(y_test) y_train = mp.expand_dims(y_train, 1) z = lr_hmc(y_train, X_train, params['epsilon'], params['n_leaps'], params['alpha'], 1) # Warm-up t = time.perf_counter() z = lr_hmc(y_train, X_train, params['epsilon'], params['n_leaps'], params['alpha'], params['n_iter']) t = time.perf_counter() - t out[f'minpy-{args.mode}'] = t coef_ = mp.mean(z[params['burn_in']:], 0) acc = mp.mean((sigmoid(mp.dot(X_test, coef_)) > 0.5) == y_test)[0] assert acc > 0.8 print(json.dumps(out))
def train(x, y, epochs, wh, bh, w_out, b_out, learning_rate, p, alpha, beta1, beta2, eps, lambda_, batch_size=0): """ Trains a neural network. The learnable parameters `wh`, `bh`, `w_out` and `b_out` are optimized as long as `epochs` indicates and then returned. :param x: The input data of form (N x D), where N is the number of observations an D is the dimensionality. :param y: The ground truth labels for each observation. :param epochs: The amount of runs a network should take. (Note: The number of iterations is 2 * N * epochs, where N is the number of inputs.) :param wh: The initialized weights of each hidden layer connection as array. Each weight is a matrix of (H_i-1 ... H_i), where H_i-1 is the size of the previous hidden layer (or the input layer) and H_i is the size of the corresponding hidden layer.. :param bh: The initialized biases of each hidden layer as array. Each bias is a vector of the same length of the corresponding hidden layer. :param w_out: The initialized weight of the output layer as matrix of form (H x K), where H is the size of the last hidden layer and K is the number of classes. :param b_out: The initialized bias of the output layer as vector of length K, where K is the number of classes. :param learning_rate: Indicates the step size of each learning epoch. High values lead to faster trainings, but also inhibit the risk of overstepping. Low values take longer to train, but will eventually reach the desired effect. :param p: The probability of each neuron to be dropped out. Set to 1 to disable dropout. :param alpha: The factor by which negative inputs are scaled in ReLU activations. Set to 0 to avoid leaky ReLU. :param beta1: Hyperparameter for the Adam parameter update. Recommended to be .9. :param beta2: Hyperparameter for the Adam parameter update. Recommended to be .999. :param eps: Hyperparameter for the Adam parameter update. Recommended to be 1e-8. :param batch_size: The number of input observations to use in each epoch. If this parameter is set to a positive value, it will enable Minibatch gradient descent, whereby only a random subset of the input observations is used to optimize parameters. This greatly improves performance and yields about the same accuracy as using all input observations. :return: A tuple containing the optimized learnable parameters `wh`, `bh`, `w_out` and `b_out`. """ m_wh = [.0 for i in range(len(wh))] v_wh = [.0 for i in range(len(wh))] m_bh = [.0 for i in range(len(bh))] v_bh = [.0 for i in range(len(bh))] m_w_out = .0 v_w_out = .0 m_b_out = .0 v_b_out = .0 losses = [] accuracies = [] for epoch in range(1, epochs + 1): if batch_size > 0: random_indices = np.random.randint(x.shape[0], size=batch_size) batch_x = x[random_indices, :] batch_y = np.array([y[int(i)] for i in random_indices]) else: batch_x = x batch_y = y # Feed-forward the network hidden_layers, outs, w_out = forward_pass(batch_x, hidden_sizes, wh, bh, w_out, b_out, alpha, p) # Calculate loss loss = calculate_cross_entropy_loss(outs, batch_y, w_out, lambda_) losses.append(loss) predicted_classes = np.argmax(outs, axis=1, out=None) correct_classes = np.where(predicted_classes == batch_y, x=predicted_classes, y=np.array([]))[0].shape[0] accuracy = correct_classes / predicted_classes.shape[0] accuracies.append(accuracy) print(epoch, loss, accuracy) # Backpropagation dwh, dbh, dw_out, db_out = backpropagation(batch_x, outs, batch_y, hidden_layers, wh, bh, w_out, b_out, alpha) # Update parameters using gradients of backpropagation for h in range(len(hidden_layers)): wh[h], m_wh[h], v_wh[h] = update_parameter(wh[h], dwh[h], epoch, learning_rate, m_wh[h], v_wh[h], beta1, beta2, eps) bh[h], m_bh[h], v_bh[h] = update_parameter(bh[h], dbh[h], epoch, learning_rate, m_bh[h], v_bh[h], beta1, beta2, eps) w_out, m_w_out, v_w_out = update_parameter(w_out, dw_out, epoch, learning_rate, m_w_out, v_w_out, beta1, beta2, eps) b_out, m_b_out, v_b_out = update_parameter(b_out, db_out, epoch, learning_rate, m_b_out, v_b_out, beta1, beta2, eps) with open('losses.txt', 'w') as file: for loss in losses: file.write(str(loss) + '\n') with open('accuracies.txt', 'w') as file: for accuracy in accuracies: file.write(str(accuracy) + '\n') return wh, bh, w_out, b_out
def test_ufunc(): x = np.array([-1.2, 1.2]) np.absolute(x) np.absolute(1.2 + 1j) x = np.linspace(start=-10, stop=10, num=101) np.add(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.add(x1, x2) np.arccos([1, -1]) x = np.linspace(-1, 1, num=100) np.arccosh([np.e, 10.0]) np.arccosh(1) np.arcsin(0) np.arcsinh(np.array([np.e, 10.0])) np.arctan([0, 1]) np.pi/4 x = np.linspace(-10, 10) x = np.array([-1, +1, +1, -1]) y = np.array([-1, -1, +1, +1]) np.arctan2(y, x) * 180 / np.pi np.arctan2([1., -1.], [0., 0.]) np.arctan2([0., 0., np.inf], [+0., -0., np.inf]) np.arctanh([0, -0.5]) np.bitwise_and(13, 17) np.bitwise_and(14, 13) # np.binary_repr(12) return str np.bitwise_and([14,3], 13) np.bitwise_and([11,7], [4,25]) np.bitwise_and(np.array([2,5,255]), np.array([3,14,16])) np.bitwise_and([True, True], [False, True]) np.bitwise_or(13, 16) # np.binary_repr(29) np.bitwise_or(32, 2) np.bitwise_or([33, 4], 1) np.bitwise_or([33, 4], [1, 2]) np.bitwise_or(np.array([2, 5, 255]), np.array([4, 4, 4])) # np.array([2, 5, 255]) | np.array([4, 4, 4]) np.bitwise_or(np.array([2, 5, 255, 2147483647], dtype=np.int32), np.array([4, 4, 4, 2147483647], dtype=np.int32)) np.bitwise_or([True, True], [False, True]) np.bitwise_xor(13, 17) # np.binary_repr(28) np.bitwise_xor(31, 5) np.bitwise_xor([31,3], 5) np.bitwise_xor([31,3], [5,6]) np.bitwise_xor([True, True], [False, True]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.ceil(a) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.trunc(a) np.cos(np.array([0, np.pi/2, np.pi])) np.cosh(0) x = np.linspace(-4, 4, 1000) rad = np.arange(12.)*np.pi/6 np.degrees(rad) out = np.zeros((rad.shape)) r = np.degrees(rad, out) # np.all(r == out) return bool np.rad2deg(np.pi/2) np.divide(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.divide(2, 4) np.divide(2, 4.) np.equal([0, 1, 3], np.arange(3)) np.equal(1, np.ones(1)) x = np.linspace(-2*np.pi, 2*np.pi, 100) np.exp2([2, 3]) np.expm1(1e-10) np.exp(1e-10) - 1 np.fabs(-1) np.fabs([-1.2, 1.2]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.floor(a) np.floor_divide(7,3) np.floor_divide([1., 2., 3., 4.], 2.5) np.fmod([-3, -2, -1, 1, 2, 3], 2) np.remainder([-3, -2, -1, 1, 2, 3], 2) np.fmod([5, 3], [2, 2.]) a = np.arange(-3, 3).reshape(3, 2) np.fmod(a, [2,2]) np.greater([4,2],[2,2]) a = np.array([4,2]) b = np.array([2,2]) a > b np.greater_equal([4, 2, 1], [2, 2, 2]) np.hypot(3*np.ones((3, 3)), 4*np.ones((3, 3))) np.hypot(3*np.ones((3, 3)), [4]) np.bitwise_not is np.invert np.invert(np.array([13], dtype=np.uint8)) # np.binary_repr(242, width=8) np.invert(np.array([13], dtype=np.uint16)) np.invert(np.array([13], dtype=np.int8)) # np.binary_repr(-14, width=8) np.invert(np.array([True, False])) # np.isfinite(1) # np.isfinite(0) # np.isfinite(np.nan) # np.isfinite(np.inf) # np.isfinite(np.NINF) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) np.isfinite(x, y) # np.isinf(np.inf) # np.isinf(np.nan) # np.isinf(np.NINF) # np.isinf([np.inf, -np.inf, 1.0, np.nan]) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) # np.isinf(x, y) # np.isnan(np.nan) # np.isnan(np.inf) # np.binary_repr(5) np.left_shift(5, 2) # np.binary_repr(20) np.left_shift(5, [1,2,3]) np.less([1, 2], [2, 2]) np.less_equal([4, 2, 1], [2, 2, 2]) x = np.array([0, 1, 2, 2**4]) xi = np.array([0+1.j, 1, 2+0.j, 4.j]) np.log2(xi) prob1 = np.log(1e-50) prob2 = np.log(2.5e-50) prob12 = np.logaddexp(prob1, prob2) prob12 np.exp(prob12) prob1 = np.log2(1e-50) prob2 = np.log2(2.5e-50) prob12 = np.logaddexp2(prob1, prob2) prob1, prob2, prob12 2**prob12 np.log1p(1e-99) np.log(1 + 1e-99) # np.logical_and(True, False) # np.logical_and([True, False], [False, False]) x = np.arange(5) # np.logical_and(x>1, x<4) # np.logical_not(3) # np.logical_not([True, False, 0, 1]) x = np.arange(5) # np.logical_not(x<3) # np.logical_or(True, False) # np.logical_or([True, False], [False, False]) x = np.arange(5) # np.logical_or(x < 1, x > 3) # np.logical_xor(True, False) # np.logical_xor([True, True, False, False], [True, False, True, False]) x = np.arange(5) # np.logical_xor(x < 1, x > 3) # np.logical_xor(0, np.eye(2)) np.maximum([2, 3, 4], [1, 5, 2]) # np.maximum([np.nan, 0, np.nan], [0, np.nan, np.nan]) # np.maximum(np.Inf, 1) np.minimum([2, 3, 4], [1, 5, 2]) # np.minimum([np.nan, 0, np.nan],[0, np.nan, np.nan]) # np.minimum(-np.Inf, 1) np.fmax([2, 3, 4], [1, 5, 2]) np.fmax(np.eye(2), [0.5, 2]) # np.fmax([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.fmin([2, 3, 4], [1, 5, 2]) np.fmin(np.eye(2), [0.5, 2]) # np.fmin([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.modf([0, 3.5]) np.modf(-0.5) np.multiply(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.multiply(x1, x2) np.negative([1.,-1.]) np.not_equal([1.,2.], [1., 3.]) np.not_equal([1, 2], [[1, 3],[1, 4]]) x1 = range(6) np.power(x1, 3) x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0] np.power(x1, x2) x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]]) np.power(x1, x2) deg = np.arange(12.) * 30. np.radians(deg) out = np.zeros((deg.shape)) ret = np.radians(deg, out) ret is out np.deg2rad(180) np.reciprocal(2.) np.reciprocal([1, 2., 3.33]) np.remainder([4, 7], [2, 3]) np.remainder(np.arange(7), 5) # np.binary_repr(10) np.right_shift(10, 1) # np.binary_repr(5) np.right_shift(10, [1,2,3]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.rint(a) np.sign([-5., 4.5]) np.sign(0) # np.sign(5-2j) # np.signbit(-1.2) np.signbit(np.array([1, -2.3, 2.1])) np.copysign(1.3, -1) np.copysign([-1, 0, 1], -1.1) np.copysign([-1, 0, 1], np.arange(3)-1) np.sin(np.pi/2.) np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180. ) x = np.linspace(-np.pi, np.pi, 201) np.sinh(0) # np.sinh(np.pi*1j/2) np.sqrt([1,4,9]) np.sqrt([4, -1, -3+4J]) np.cbrt([1,8,27]) np.square([-1j, 1]) np.subtract(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.subtract(x1, x2) np.tan(np.array([-pi,pi/2,pi])) np.tanh((0, np.pi*1j, np.pi*1j/2)) x = np.arange(5) np.true_divide(x, 4) x = np.arange(9) y1, y2 = np.frexp(x) y1 * 2**y2 np.ldexp(5, np.arange(4)) x = np.arange(6) np.ldexp(*np.frexp(x))
def string_to_one_hot(string, maxchar): """Converts an ASCII string to a one-of-k encoding.""" ascii = np.array([ord(c) for c in string]).T return np.array(ascii[:, None] == np.arange(maxchar)[None, :], dtype=int)
def output_shape(self, input_shape): return (int(np.prod(np.array(input_shape))),)
def logsoftmax(x, valid_idx): x[np.array(valid_idx)] += 1e6 x_max = np.max(x) return x - x_max - np.log(np.sum(np.exp(x - x_max)))
def test_ufunc(): x = np.array([-1.2, 1.2]) np.absolute(x) np.absolute(1.2 + 1j) x = np.linspace(start=-10, stop=10, num=101) np.add(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.add(x1, x2) np.arccos([1, -1]) x = np.linspace(-1, 1, num=100) np.arccosh([np.e, 10.0]) np.arccosh(1) np.arcsin(0) np.arcsinh(np.array([np.e, 10.0])) np.arctan([0, 1]) np.pi / 4 x = np.linspace(-10, 10) x = np.array([-1, +1, +1, -1]) y = np.array([-1, -1, +1, +1]) np.arctan2(y, x) * 180 / np.pi np.arctan2([1., -1.], [0., 0.]) np.arctan2([0., 0., np.inf], [+0., -0., np.inf]) np.arctanh([0, -0.5]) np.bitwise_and(13, 17) np.bitwise_and(14, 13) # np.binary_repr(12) return str np.bitwise_and([14, 3], 13) np.bitwise_and([11, 7], [4, 25]) np.bitwise_and(np.array([2, 5, 255]), np.array([3, 14, 16])) np.bitwise_and([True, True], [False, True]) np.bitwise_or(13, 16) # np.binary_repr(29) np.bitwise_or(32, 2) np.bitwise_or([33, 4], 1) np.bitwise_or([33, 4], [1, 2]) np.bitwise_or(np.array([2, 5, 255]), np.array([4, 4, 4])) # np.array([2, 5, 255]) | np.array([4, 4, 4]) np.bitwise_or(np.array([2, 5, 255, 2147483647], dtype=np.int32), np.array([4, 4, 4, 2147483647], dtype=np.int32)) np.bitwise_or([True, True], [False, True]) np.bitwise_xor(13, 17) # np.binary_repr(28) np.bitwise_xor(31, 5) np.bitwise_xor([31, 3], 5) np.bitwise_xor([31, 3], [5, 6]) np.bitwise_xor([True, True], [False, True]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.ceil(a) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.trunc(a) np.cos(np.array([0, np.pi / 2, np.pi])) np.cosh(0) x = np.linspace(-4, 4, 1000) rad = np.arange(12.) * np.pi / 6 np.degrees(rad) out = np.zeros((rad.shape)) r = np.degrees(rad, out) # np.all(r == out) return bool np.rad2deg(np.pi / 2) np.divide(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.divide(2, 4) np.divide(2, 4.) np.equal([0, 1, 3], np.arange(3)) np.equal(1, np.ones(1)) x = np.linspace(-2 * np.pi, 2 * np.pi, 100) np.exp2([2, 3]) np.expm1(1e-10) np.exp(1e-10) - 1 np.fabs(-1) np.fabs([-1.2, 1.2]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.floor(a) np.floor_divide(7, 3) np.floor_divide([1., 2., 3., 4.], 2.5) np.fmod([-3, -2, -1, 1, 2, 3], 2) np.remainder([-3, -2, -1, 1, 2, 3], 2) np.fmod([5, 3], [2, 2.]) a = np.arange(-3, 3).reshape(3, 2) np.fmod(a, [2, 2]) np.greater([4, 2], [2, 2]) a = np.array([4, 2]) b = np.array([2, 2]) a > b np.greater_equal([4, 2, 1], [2, 2, 2]) np.hypot(3 * np.ones((3, 3)), 4 * np.ones((3, 3))) np.hypot(3 * np.ones((3, 3)), [4]) np.bitwise_not is np.invert np.invert(np.array([13], dtype=np.uint8)) # np.binary_repr(242, width=8) np.invert(np.array([13], dtype=np.uint16)) np.invert(np.array([13], dtype=np.int8)) # np.binary_repr(-14, width=8) np.invert(np.array([True, False])) # np.isfinite(1) # np.isfinite(0) # np.isfinite(np.nan) # np.isfinite(np.inf) # np.isfinite(np.NINF) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) np.isfinite(x, y) # np.isinf(np.inf) # np.isinf(np.nan) # np.isinf(np.NINF) # np.isinf([np.inf, -np.inf, 1.0, np.nan]) x = np.array([-np.inf, 0., np.inf]) y = np.array([2, 2, 2]) # np.isinf(x, y) # np.isnan(np.nan) # np.isnan(np.inf) # np.binary_repr(5) np.left_shift(5, 2) # np.binary_repr(20) np.left_shift(5, [1, 2, 3]) np.less([1, 2], [2, 2]) np.less_equal([4, 2, 1], [2, 2, 2]) x = np.array([0, 1, 2, 2**4]) xi = np.array([0 + 1.j, 1, 2 + 0.j, 4.j]) np.log2(xi) prob1 = np.log(1e-50) prob2 = np.log(2.5e-50) prob12 = np.logaddexp(prob1, prob2) prob12 np.exp(prob12) prob1 = np.log2(1e-50) prob2 = np.log2(2.5e-50) prob12 = np.logaddexp2(prob1, prob2) prob1, prob2, prob12 2**prob12 np.log1p(1e-99) np.log(1 + 1e-99) # np.logical_and(True, False) # np.logical_and([True, False], [False, False]) x = np.arange(5) # np.logical_and(x>1, x<4) # np.logical_not(3) # np.logical_not([True, False, 0, 1]) x = np.arange(5) # np.logical_not(x<3) # np.logical_or(True, False) # np.logical_or([True, False], [False, False]) x = np.arange(5) # np.logical_or(x < 1, x > 3) # np.logical_xor(True, False) # np.logical_xor([True, True, False, False], [True, False, True, False]) x = np.arange(5) # np.logical_xor(x < 1, x > 3) # np.logical_xor(0, np.eye(2)) np.maximum([2, 3, 4], [1, 5, 2]) # np.maximum([np.nan, 0, np.nan], [0, np.nan, np.nan]) # np.maximum(np.Inf, 1) np.minimum([2, 3, 4], [1, 5, 2]) # np.minimum([np.nan, 0, np.nan],[0, np.nan, np.nan]) # np.minimum(-np.Inf, 1) np.fmax([2, 3, 4], [1, 5, 2]) np.fmax(np.eye(2), [0.5, 2]) # np.fmax([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.fmin([2, 3, 4], [1, 5, 2]) np.fmin(np.eye(2), [0.5, 2]) # np.fmin([np.nan, 0, np.nan],[0, np.nan, np.nan]) np.modf([0, 3.5]) np.modf(-0.5) np.multiply(2.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.multiply(x1, x2) np.negative([1., -1.]) np.not_equal([1., 2.], [1., 3.]) np.not_equal([1, 2], [[1, 3], [1, 4]]) x1 = range(6) np.power(x1, 3) x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0] np.power(x1, x2) x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]]) np.power(x1, x2) deg = np.arange(12.) * 30. np.radians(deg) out = np.zeros((deg.shape)) ret = np.radians(deg, out) ret is out np.deg2rad(180) np.reciprocal(2.) np.reciprocal([1, 2., 3.33]) np.remainder([4, 7], [2, 3]) np.remainder(np.arange(7), 5) # np.binary_repr(10) np.right_shift(10, 1) # np.binary_repr(5) np.right_shift(10, [1, 2, 3]) a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) np.rint(a) np.sign([-5., 4.5]) np.sign(0) # np.sign(5-2j) # np.signbit(-1.2) np.signbit(np.array([1, -2.3, 2.1])) np.copysign(1.3, -1) np.copysign([-1, 0, 1], -1.1) np.copysign([-1, 0, 1], np.arange(3) - 1) np.sin(np.pi / 2.) np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180.) x = np.linspace(-np.pi, np.pi, 201) np.sinh(0) # np.sinh(np.pi*1j/2) np.sqrt([1, 4, 9]) np.sqrt([4, -1, -3 + 4J]) np.cbrt([1, 8, 27]) np.square([-1j, 1]) np.subtract(1.0, 4.0) x1 = np.arange(9.0).reshape((3, 3)) x2 = np.arange(3.0) np.subtract(x1, x2) np.tan(np.array([-pi, pi / 2, pi])) np.tanh((0, np.pi * 1j, np.pi * 1j / 2)) x = np.arange(5) np.true_divide(x, 4) x = np.arange(9) y1, y2 = np.frexp(x) y1 * 2**y2 np.ldexp(5, np.arange(4)) x = np.arange(6) np.ldexp(*np.frexp(x))
def forward(self, inputs, *args): shape = (inputs.shape[0], int(np.prod(np.array(inputs.shape[1:])))) return np.reshape(inputs, shape)
def test_fromnumeric(): # Functions # 'alen', 'all', 'alltrue', 'amax', 'amin', 'any', 'argmax', # 'argmin', 'argpartition', 'argsort', 'around', 'choose', 'clip', # 'compress', 'cumprod', 'cumproduct', 'cumsum', 'diagonal', 'mean', # 'ndim', 'nonzero', 'partition', 'prod', 'product', 'ptp', 'put', # 'rank', 'ravel', 'repeat', 'reshape', 'resize', 'round_', # 'searchsorted', 'shape', 'size', 'sometrue', 'sort', 'squeeze', # 'std', 'sum', 'swapaxes', 'take', 'trace', 'transpose', 'var', a = [4, 3, 5, 7, 6, 8] indices = [0, 1, 4] np.take(a, indices) a = np.array(a) # a[indices] np.take(a, [[0, 1], [2, 3]]) a = np.zeros((10, 2)) b = a.T a = np.arange(6).reshape((3, 2)) np.reshape(a, (2, 3)) # C-like index ordering np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape np.reshape(a, (2, 3), order='F') # Fortran-like index ordering np.reshape(np.ravel(a, order='F'), (2, 3), order='F') a = np.array([[1, 2, 3], [4, 5, 6]]) np.reshape(a, 6) np.reshape(a, 6, order='F') np.reshape(a, (3, -1)) # the unspecified value is inferred to be 2 choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]] np.choose([2, 3, 1, 0], choices) np.choose([2, 4, 1, 0], choices, mode='clip') # 4 goes to 3 (4-1) np.choose([2, 4, 1, 0], choices, mode='wrap') # 4 goes to (4 mod 4) a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]] choices = [-10, 10] np.choose(a, choices) a = np.array([0, 1]).reshape((2, 1, 1)) c1 = np.array([1, 2, 3]).reshape((1, 3, 1)) c2 = np.array([-1, -2, -3, -4, -5]).reshape((1, 1, 5)) np.choose(a, (c1, c2)) # result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2 np.repeat(3, 4) x = np.array([[1, 2], [3, 4]]) np.repeat(x, 2) np.repeat(x, 3, axis=1) np.repeat(x, [1, 2], axis=0) a = np.arange(5) np.put(a, [0, 2], [-44, -55]) a = np.arange(5) np.put(a, 22, -5, mode='clip') x = np.array([[1, 2, 3]]) np.swapaxes(x, 0, 1) x = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) np.swapaxes(x, 0, 2) x = np.arange(4).reshape((2, 2)) np.transpose(x) x = np.ones((1, 2, 3)) np.transpose(x, (1, 0, 2)).shape a = np.array([3, 4, 2, 1]) np.partition(a, 3) np.partition(a, (1, 3)) x = np.array([3, 4, 2, 1]) x[np.argpartition(x, 3)] x[np.argpartition(x, (1, 3))] x = [3, 4, 2, 1] np.array(x)[np.argpartition(x, 3)] a = np.array([[1, 4], [3, 1]]) np.sort(a) # sort along the last axis np.sort(a, axis=None) # sort the flattened array np.sort(a, axis=0) # sort along the first axis dtype = [('name', 'S10'), ('height', float), ('age', int)] values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38), ('Galahad', 1.7, 38)] a = np.array(values, dtype=dtype) # create a structured array np.sort(a, order='height') # doctest: +SKIP np.sort(a, order=['age', 'height']) # doctest: +SKIP x = np.array([3, 1, 2]) np.argsort(x) x = np.array([[0, 3], [2, 2]]) np.argsort(x, axis=0) np.argsort(x, axis=1) x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')]) np.argsort(x, order=('x', 'y')) np.argsort(x, order=('y', 'x')) a = np.arange(6).reshape(2, 3) np.argmax(a) np.argmax(a, axis=0) np.argmax(a, axis=1) b = np.arange(6) b[1] = 5 np.argmax(b) # Only the first occurrence is returned. a = np.arange(6).reshape(2, 3) np.argmin(a) np.argmin(a, axis=0) np.argmin(a, axis=1) b = np.arange(6) b[4] = 0 np.argmin(b) # Only the first occurrence is returned. np.searchsorted([1, 2, 3, 4, 5], 3) np.searchsorted([1, 2, 3, 4, 5], 3, side='right') np.searchsorted([1, 2, 3, 4, 5], [-10, 10, 2, 3]) a = np.array([[0, 1], [2, 3]]) np.resize(a, (2, 3)) np.resize(a, (1, 4)) np.resize(a, (2, 4)) x = np.array([[[0], [1], [2]]]) x.shape np.squeeze(x).shape np.squeeze(x, axis=(2, )).shape a = np.arange(4).reshape(2, 2) a = np.arange(8).reshape(2, 2, 2) a a[:, :, 0] # main diagonal is [0 6] a[:, :, 1] # main diagonal is [1 7] np.trace(np.eye(3)) a = np.arange(8).reshape((2, 2, 2)) np.trace(a) a = np.arange(24).reshape((2, 2, 2, 3)) np.trace(a).shape x = np.array([[1, 2, 3], [4, 5, 6]]) np.ravel(x) x.reshape(-1) np.ravel(x, order='F') np.ravel(x.T) np.ravel(x.T, order='A') a = np.arange(3)[::-1] a # a = np.arange(12).reshape(2,3,2).swapaxes(1,2); a x = np.eye(3) np.nonzero(x) x[np.nonzero(x)] np.transpose(np.nonzero(x)) a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) a > 3 np.nonzero(a > 3) np.shape(np.eye(3)) np.shape([[1, 2]]) np.shape([0]) np.shape(0) a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) np.shape(a) a.shape a = np.array([[1, 2], [3, 4], [5, 6]]) np.compress([0, 1], a, axis=0) np.compress([False, True, True], a, axis=0) np.compress([False, True], a, axis=1) np.compress([False, True], a) a = np.arange(10) np.clip(a, 1, 8) np.clip(a, 3, 6, out=a) a = np.arange(10) np.clip(a, [3, 4, 1, 1, 1, 4, 4, 4, 4, 4], 8) np.sum([]) np.sum([0.5, 1.5]) np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32) np.sum([[0, 1], [0, 5]]) np.sum([[0, 1], [0, 5]], axis=0) np.sum([[0, 1], [0, 5]], axis=1) # np.ones(128, dtype=np.int8).sum(dtype=np.int8) # np.any([[True, False], [True, True]]) # np.any([[True, False], [False, False]], axis=0) # np.any([-1, 0, 5]) # np.any(np.nan) # np.all([[True,False],[True,True]]) # np.all([[True,False],[True,True]], axis=0) # np.all([-1, 4, 5]) # np.all([1.0, np.nan]) a = np.array([[1, 2, 3], [4, 5, 6]]) np.cumsum(a) np.cumsum(a, dtype=float) # specifies type of output value(s) np.cumsum(a, axis=0) # sum over rows for each of the 3 columns np.cumsum(a, axis=1) # sum over columns for each of the 2 rows x = np.arange(4).reshape((2, 2)) np.ptp(x, axis=0) np.ptp(x, axis=1) a = np.arange(4).reshape((2, 2)) np.amax(a) # Maximum of the flattened array np.amax(a, axis=0) # Maxima along the first axis np.amax(a, axis=1) # Maxima along the second axis b = np.arange(5, dtype=np.float) # b[2] = np.NaN np.amax(b) np.nanmax(b) a = np.arange(4).reshape((2, 2)) np.amin(a) # Minimum of the flattened array np.amin(a, axis=0) # Minima along the first axis np.amin(a, axis=1) # Minima along the second axis b = np.arange(5, dtype=np.float) # b[2] = np.NaN np.amin(b) np.nanmin(b) a = np.zeros((7, 4, 5)) a.shape[0] np.alen(a) x = np.array([536870910, 536870910, 536870910, 536870910]) np.prod(x) #random np.prod([]) np.prod([1., 2.]) np.prod([[1., 2.], [3., 4.]]) np.prod([[1., 2.], [3., 4.]], axis=1) x = np.array([1, 2, 3], dtype=np.uint8) # np.prod(x).dtype == np.uint x = np.array([1, 2, 3], dtype=np.int8) # np.prod(x).dtype == np.int a = np.array([1, 2, 3]) np.cumprod(a) # intermediate results 1, 1*2 a = np.array([[1, 2, 3], [4, 5, 6]]) np.cumprod(a, dtype=float) # specify type of output np.cumprod(a, axis=0) np.cumprod(a, axis=1) np.ndim([[1, 2, 3], [4, 5, 6]]) np.ndim(np.array([[1, 2, 3], [4, 5, 6]])) np.ndim(1) a = np.array([[1, 2, 3], [4, 5, 6]]) np.size(a) np.size(a, 1) np.size(a, 0) np.around([0.37, 1.64]) np.around([0.37, 1.64], decimals=1) np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value np.around([1, 2, 3, 11], decimals=1) # ndarray of ints is returned np.around([1, 2, 3, 11], decimals=-1) a = np.array([[1, 2], [3, 4]]) np.mean(a) np.mean(a, axis=0) np.mean(a, axis=1) a = np.zeros((2, 512 * 512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.mean(a) np.mean(a, dtype=np.float64) a = np.array([[1, 2], [3, 4]]) np.std(a) np.std(a, axis=0) np.std(a, axis=1) a = np.zeros((2, 512 * 512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.std(a) np.std(a, dtype=np.float64) a = np.array([[1, 2], [3, 4]]) np.var(a) np.var(a, axis=0) np.var(a, axis=1) a = np.zeros((2, 512 * 512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.var(a) np.var(a, dtype=np.float64)
def test_numeric(): # 'newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', # 'arange', 'array', 'zeros', 'count_nonzero', 'empty', 'broadcast', # 'dtype', 'fromstring', 'fromfile', 'frombuffer', 'int_asbuffer', # 'where', 'argwhere', 'copyto', 'concatenate', 'fastCopyAndTranspose', # 'lexsort', 'set_numeric_ops', 'can_cast', 'promote_types', # 'min_scalar_type', 'result_type', 'asarray', 'asanyarray', # 'ascontiguousarray', 'asfortranarray', 'isfortran', 'empty_like', # 'zeros_like', 'ones_like', 'correlate', 'convolve', 'inner', 'dot', # 'einsum', 'outer', 'vdot', 'alterdot', 'restoredot', 'roll', # 'rollaxis', 'moveaxis', 'cross', 'tensordot', 'array2string', # 'get_printoptions', 'set_printoptions', 'array_repr', 'array_str', # 'set_string_function', 'little_endian', 'require', 'fromiter', # 'array_equal', 'array_equiv', 'indices', 'fromfunction', 'isclose', 'load', # 'loads', 'isscalar', 'binary_repr', 'base_repr', 'ones', 'identity', # 'allclose', 'compare_chararrays', 'putmask', 'seterr', 'geterr', # 'setbufsize', 'getbufsize', 'seterrcall', 'geterrcall', 'errstate', # 'flatnonzero', 'Inf', 'inf', 'infty', 'Infinity', 'nan', 'NaN', 'False_', # 'True_', 'bitwise_not', 'full', 'full_like', 'matmul' x = np.arange(6) x = x.reshape((2, 3)) np.zeros_like(x) y = np.arange(3, dtype=np.float) np.zeros_like(y) np.ones(5) np.ones((5, ), dtype=np.int) np.ones((2, 1)) s = (2, 2) np.ones(s) x = np.arange(6) x = x.reshape((2, 3)) np.ones_like(x) y = np.arange(3, dtype=np.float) np.ones_like(y) np.full((2, 2), np.inf) x = np.arange(6, dtype=np.int) np.full_like(x, 1) np.full_like(x, 0.1) np.full_like(y, 0.1) np.count_nonzero(np.eye(4)) np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]]) np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=0) np.count_nonzero([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]], axis=1) a = [1, 2] np.asarray(a) a = np.array([1, 2]) np.asarray(a) is a a = np.array([1, 2], dtype=np.float32) np.asarray(a, dtype=np.float32) is a np.asarray(a, dtype=np.float64) is a np.asarray(a) is a np.asanyarray(a) is a a = [1, 2] np.asanyarray(a) np.asanyarray(a) is a x = np.arange(6).reshape(2, 3) np.ascontiguousarray(x, dtype=np.float32) x = np.arange(6).reshape(2, 3) y = np.asfortranarray(x) x = np.arange(6).reshape(2, 3) y = np.require(x, dtype=np.float32, requirements=['A', 'O', 'W', 'F']) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = np.array([[1, 2, 3], [4, 5, 6]], order='FORTRAN') np.isfortran(b) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = a.T np.isfortran(b) np.isfortran(np.array([1, 2], order='FORTRAN')) x = np.arange(6).reshape(2, 3) np.argwhere(x > 1) x = np.arange(-2, 3) np.flatnonzero(x) np.correlate([1, 2, 3], [0, 1, 0.5]) np.correlate([1, 2, 3], [0, 1, 0.5], "same") np.correlate([1, 2, 3], [0, 1, 0.5], "full") np.correlate([1 + 1j, 2, 3 - 1j], [0, 1, 0.5j], 'full') np.correlate([0, 1, 0.5j], [1 + 1j, 2, 3 - 1j], 'full') np.convolve([1, 2, 3], [0, 1, 0.5]) np.convolve([1, 2, 3], [0, 1, 0.5], 'same') np.convolve([1, 2, 3], [0, 1, 0.5], 'valid') rl = np.outer(np.ones((5, )), np.linspace(-2, 2, 5)) # im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,))) # grid = rl + im x = np.array(['a', 'b', 'c'], dtype=object) np.outer(x, [1, 2, 3]) a = np.arange(60.).reshape(3, 4, 5) b = np.arange(24.).reshape(4, 3, 2) c = np.tensordot(a, b, axes=([1, 0], [0, 1])) c.shape # A slower but equivalent way of computing the same... d = np.zeros((5, 2)) a = np.array(range(1, 9)) A = np.array(('a', 'b', 'c', 'd'), dtype=object) x = np.arange(10) np.roll(x, 2) x2 = np.reshape(x, (2, 5)) np.roll(x2, 1) np.roll(x2, 1, axis=0) np.roll(x2, 1, axis=1) a = np.ones((3, 4, 5, 6)) np.rollaxis(a, 3, 1).shape np.rollaxis(a, 2).shape np.rollaxis(a, 1, 4).shape x = np.zeros((3, 4, 5)) np.moveaxis(x, 0, -1).shape np.moveaxis(x, -1, 0).shape np.transpose(x).shape np.moveaxis(x, [0, 1], [-1, -2]).shape np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape x = [1, 2, 3] y = [4, 5, 6] np.cross(x, y) x = [1, 2] y = [4, 5, 6] np.cross(x, y) x = [1, 2, 0] y = [4, 5, 6] np.cross(x, y) x = [1, 2] y = [4, 5] np.cross(x, y) x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.array([[4, 5, 6], [1, 2, 3]]) np.cross(x, y) np.cross(x, y, axisc=0) x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) y = np.array([[7, 8, 9], [4, 5, 6], [1, 2, 3]]) np.cross(x, y) np.cross(x, y, axisa=0, axisb=0) # np.array_repr(np.array([1,2])) # np.array_repr(np.ma.array([0.])) # np.array_repr(np.array([], np.int32)) x = np.array([1e-6, 4e-7, 2, 3]) # np.array_repr(x, precision=6, suppress_small=True) # np.array_str(np.arange(3)) a = np.arange(10) x = np.arange(4) np.set_string_function(lambda x: 'random', repr=False) grid = np.indices((2, 3)) grid.shape grid[0] # row indices grid[1] # column indices x = np.arange(20).reshape(5, 4) row, col = np.indices((2, 3)) x[row, col] np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) np.isscalar(3.1) np.isscalar([3.1]) np.isscalar(False) # np.binary_repr(3) # np.binary_repr(-3) # np.binary_repr(3, width=4) # np.binary_repr(-3, width=3) # np.binary_repr(-3, width=5) # np.base_repr(5) # np.base_repr(6, 5) # np.base_repr(7, base=5, padding=3) # np.base_repr(10, base=16) # np.base_repr(32, base=16) np.identity(3) np.allclose([1e10, 1e-7], [1.00001e10, 1e-8]) np.allclose([1e10, 1e-8], [1.00001e10, 1e-9]) np.allclose([1e10, 1e-8], [1.0001e10, 1e-9]) # np.allclose([1.0, np.nan], [1.0, np.nan]) # np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.isclose([1e10, 1e-7], [1.00001e10, 1e-8]) np.isclose([1e10, 1e-8], [1.00001e10, 1e-9]) np.isclose([1e10, 1e-8], [1.0001e10, 1e-9]) # np.isclose([1.0, np.nan], [1.0, np.nan]) # np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.array_equal([1, 2], [1, 2]) np.array_equal(np.array([1, 2]), np.array([1, 2])) np.array_equal([1, 2], [1, 2, 3]) np.array_equal([1, 2], [1, 4]) np.array_equiv([1, 2], [1, 2]) np.array_equiv([1, 2], [1, 3]) np.array_equiv([1, 2], [[1, 2], [1, 2]]) np.array_equiv([1, 2], [[1, 2, 1, 2], [1, 2, 1, 2]]) np.array_equiv([1, 2], [[1, 2], [1, 3]])
def test_fromnumeric(): # Functions # 'alen', 'all', 'alltrue', 'amax', 'amin', 'any', 'argmax', # 'argmin', 'argpartition', 'argsort', 'around', 'choose', 'clip', # 'compress', 'cumprod', 'cumproduct', 'cumsum', 'diagonal', 'mean', # 'ndim', 'nonzero', 'partition', 'prod', 'product', 'ptp', 'put', # 'rank', 'ravel', 'repeat', 'reshape', 'resize', 'round_', # 'searchsorted', 'shape', 'size', 'sometrue', 'sort', 'squeeze', # 'std', 'sum', 'swapaxes', 'take', 'trace', 'transpose', 'var', a = [4, 3, 5, 7, 6, 8] indices = [0, 1, 4] np.take(a, indices) a = np.array(a) # a[indices] np.take(a, [[0, 1], [2, 3]]) a = np.zeros((10, 2)) b = a.T a = np.arange(6).reshape((3, 2)) np.reshape(a, (2, 3)) # C-like index ordering np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape np.reshape(a, (2, 3), order='F') # Fortran-like index ordering np.reshape(np.ravel(a, order='F'), (2, 3), order='F') a = np.array([[1,2,3], [4,5,6]]) np.reshape(a, 6) np.reshape(a, 6, order='F') np.reshape(a, (3,-1)) # the unspecified value is inferred to be 2 choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]] np.choose([2, 3, 1, 0], choices) np.choose([2, 4, 1, 0], choices, mode='clip') # 4 goes to 3 (4-1) np.choose([2, 4, 1, 0], choices, mode='wrap') # 4 goes to (4 mod 4) a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]] choices = [-10, 10] np.choose(a, choices) a = np.array([0, 1]).reshape((2,1,1)) c1 = np.array([1, 2, 3]).reshape((1,3,1)) c2 = np.array([-1, -2, -3, -4, -5]).reshape((1,1,5)) np.choose(a, (c1, c2)) # result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2 np.repeat(3, 4) x = np.array([[1,2],[3,4]]) np.repeat(x, 2) np.repeat(x, 3, axis=1) np.repeat(x, [1, 2], axis=0) a = np.arange(5) np.put(a, [0, 2], [-44, -55]) a = np.arange(5) np.put(a, 22, -5, mode='clip') x = np.array([[1,2,3]]) np.swapaxes(x,0,1) x = np.array([[[0,1],[2,3]],[[4,5],[6,7]]]) np.swapaxes(x,0,2) x = np.arange(4).reshape((2,2)) np.transpose(x) x = np.ones((1, 2, 3)) np.transpose(x, (1, 0, 2)).shape a = np.array([3, 4, 2, 1]) np.partition(a, 3) np.partition(a, (1, 3)) x = np.array([3, 4, 2, 1]) x[np.argpartition(x, 3)] x[np.argpartition(x, (1, 3))] x = [3, 4, 2, 1] np.array(x)[np.argpartition(x, 3)] a = np.array([[1,4],[3,1]]) np.sort(a) # sort along the last axis np.sort(a, axis=None) # sort the flattened array np.sort(a, axis=0) # sort along the first axis dtype = [('name', 'S10'), ('height', float), ('age', int)] values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38), ('Galahad', 1.7, 38)] a = np.array(values, dtype=dtype) # create a structured array np.sort(a, order='height') # doctest: +SKIP np.sort(a, order=['age', 'height']) # doctest: +SKIP x = np.array([3, 1, 2]) np.argsort(x) x = np.array([[0, 3], [2, 2]]) np.argsort(x, axis=0) np.argsort(x, axis=1) x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')]) np.argsort(x, order=('x','y')) np.argsort(x, order=('y','x')) a = np.arange(6).reshape(2,3) np.argmax(a) np.argmax(a, axis=0) np.argmax(a, axis=1) b = np.arange(6) b[1] = 5 np.argmax(b) # Only the first occurrence is returned. a = np.arange(6).reshape(2,3) np.argmin(a) np.argmin(a, axis=0) np.argmin(a, axis=1) b = np.arange(6) b[4] = 0 np.argmin(b) # Only the first occurrence is returned. np.searchsorted([1,2,3,4,5], 3) np.searchsorted([1,2,3,4,5], 3, side='right') np.searchsorted([1,2,3,4,5], [-10, 10, 2, 3]) a=np.array([[0,1],[2,3]]) np.resize(a,(2,3)) np.resize(a,(1,4)) np.resize(a,(2,4)) x = np.array([[[0], [1], [2]]]) x.shape np.squeeze(x).shape np.squeeze(x, axis=(2,)).shape a = np.arange(4).reshape(2,2) a = np.arange(8).reshape(2,2,2); a a[:,:,0] # main diagonal is [0 6] a[:,:,1] # main diagonal is [1 7] np.trace(np.eye(3)) a = np.arange(8).reshape((2,2,2)) np.trace(a) a = np.arange(24).reshape((2,2,2,3)) np.trace(a).shape x = np.array([[1, 2, 3], [4, 5, 6]]) np.ravel(x) x.reshape(-1) np.ravel(x, order='F') np.ravel(x.T) np.ravel(x.T, order='A') a = np.arange(3)[::-1]; a # a = np.arange(12).reshape(2,3,2).swapaxes(1,2); a x = np.eye(3) np.nonzero(x) x[np.nonzero(x)] np.transpose(np.nonzero(x)) a = np.array([[1,2,3],[4,5,6],[7,8,9]]) a > 3 np.nonzero(a > 3) np.shape(np.eye(3)) np.shape([[1, 2]]) np.shape([0]) np.shape(0) a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')]) np.shape(a) a.shape a = np.array([[1, 2], [3, 4], [5, 6]]) np.compress([0, 1], a, axis=0) np.compress([False, True, True], a, axis=0) np.compress([False, True], a, axis=1) np.compress([False, True], a) a = np.arange(10) np.clip(a, 1, 8) np.clip(a, 3, 6, out=a) a = np.arange(10) np.clip(a, [3,4,1,1,1,4,4,4,4,4], 8) np.sum([]) np.sum([0.5, 1.5]) np.sum([0.5, 0.7, 0.2, 1.5], dtype=np.int32) np.sum([[0, 1], [0, 5]]) np.sum([[0, 1], [0, 5]], axis=0) np.sum([[0, 1], [0, 5]], axis=1) # np.ones(128, dtype=np.int8).sum(dtype=np.int8) # np.any([[True, False], [True, True]]) # np.any([[True, False], [False, False]], axis=0) # np.any([-1, 0, 5]) # np.any(np.nan) # np.all([[True,False],[True,True]]) # np.all([[True,False],[True,True]], axis=0) # np.all([-1, 4, 5]) # np.all([1.0, np.nan]) a = np.array([[1,2,3], [4,5,6]]) np.cumsum(a) np.cumsum(a, dtype=float) # specifies type of output value(s) np.cumsum(a,axis=0) # sum over rows for each of the 3 columns np.cumsum(a,axis=1) # sum over columns for each of the 2 rows x = np.arange(4).reshape((2,2)) np.ptp(x, axis=0) np.ptp(x, axis=1) a = np.arange(4).reshape((2,2)) np.amax(a) # Maximum of the flattened array np.amax(a, axis=0) # Maxima along the first axis np.amax(a, axis=1) # Maxima along the second axis b = np.arange(5, dtype=np.float) # b[2] = np.NaN np.amax(b) np.nanmax(b) a = np.arange(4).reshape((2,2)) np.amin(a) # Minimum of the flattened array np.amin(a, axis=0) # Minima along the first axis np.amin(a, axis=1) # Minima along the second axis b = np.arange(5, dtype=np.float) # b[2] = np.NaN np.amin(b) np.nanmin(b) a = np.zeros((7,4,5)) a.shape[0] np.alen(a) x = np.array([536870910, 536870910, 536870910, 536870910]) np.prod(x) #random np.prod([]) np.prod([1.,2.]) np.prod([[1.,2.],[3.,4.]]) np.prod([[1.,2.],[3.,4.]], axis=1) x = np.array([1, 2, 3], dtype=np.uint8) # np.prod(x).dtype == np.uint x = np.array([1, 2, 3], dtype=np.int8) # np.prod(x).dtype == np.int a = np.array([1,2,3]) np.cumprod(a) # intermediate results 1, 1*2 a = np.array([[1, 2, 3], [4, 5, 6]]) np.cumprod(a, dtype=float) # specify type of output np.cumprod(a, axis=0) np.cumprod(a,axis=1) np.ndim([[1,2,3],[4,5,6]]) np.ndim(np.array([[1,2,3],[4,5,6]])) np.ndim(1) a = np.array([[1,2,3],[4,5,6]]) np.size(a) np.size(a,1) np.size(a,0) np.around([0.37, 1.64]) np.around([0.37, 1.64], decimals=1) np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value np.around([1,2,3,11], decimals=1) # ndarray of ints is returned np.around([1,2,3,11], decimals=-1) a = np.array([[1, 2], [3, 4]]) np.mean(a) np.mean(a, axis=0) np.mean(a, axis=1) a = np.zeros((2, 512*512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.mean(a) np.mean(a, dtype=np.float64) a = np.array([[1, 2], [3, 4]]) np.std(a) np.std(a, axis=0) np.std(a, axis=1) a = np.zeros((2, 512*512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.std(a) np.std(a, dtype=np.float64) a = np.array([[1, 2], [3, 4]]) np.var(a) np.var(a, axis=0) np.var(a, axis=1) a = np.zeros((2, 512*512), dtype=np.float32) a[0, :] = 1.0 a[1, :] = 0.1 np.var(a) np.var(a, dtype=np.float64)
def output_shape(self, input_shape): return (int(np.prod(np.array(input_shape))), )
import minpy.numpy as np import minpy.dispatch.policy as policy import time # mp.set_policy(policy.OnlyNumPyPolicy()) start = time.time() np.array([100, 100]).asnumpy() end = time.time() print 'First call:', end - start start = time.time() np.array([100, 100]).asnumpy() end = time.time() print 'Second call:', end - start start = time.time() np.array([100, 100]).asnumpy() end = time.time() print 'Third call:', end - start
def string_to_one_hot(string, maxchar): """Converts an ASCII string to a one-of-k encoding.""" ascii = np.array([ord(c) for c in string]).T return np.array(ascii[:,None] == np.arange(maxchar)[None, :], dtype=int)
def parse(self, tokens, oracle_actions=None): def _valid_actions(stack, buffer): valid_actions = [] if len(buffer) > 0: valid_actions += [SHIFT] if len(stack) >= 2: valid_actions += [REDUCE_L, REDUCE_R] return valid_actions if oracle_actions: oracle_actions = list(oracle_actions) buffer = StackRNN(self.buffRNN, self.params['empty_buffer_emb']) stack = StackRNN(self.stackRNN) # Put the parameters in the cg W_comp = self.params['pW_comp'] # syntactic composition b_comp = self.params['pb_comp'] W_s2h = self.params['pW_s2h'] # state to hidden b_s2h = self.params['pb_s2h'] W_act = self.params['pW_act'] # hidden to action b_act = self.params['pb_act'] emb = self.params['wemb'] # We will keep track of all the losses we accumulate during parsing. # If some decision is unambiguous because it's the only thing valid given # the parser state, we will not model it. We only model what is ambiguous. loss = 0. # push the tokens onto the buffer (tokens is in reverse order) for tok in tokens: # TODO: I remember numpy ndarray supports python built-in list indexing tok_embedding = emb[np.array([tok])] buffer.push(tok_embedding, (tok_embedding, self.vocab.i2w[tok])) while not (len(stack) == 1 and len(buffer) == 0): # compute probability of each of the actions and choose an action # either from the oracle or if there is no oracle, based on the model valid_actions = _valid_actions(stack, buffer) log_probs = None action = valid_actions[0] if len(valid_actions) > 1: p_t = np.transpose( np.concatenate([buffer.top(), stack.top()], axis=1)) h = np.tanh(np.dot(W_s2h, p_t) + b_s2h) logits = np.dot(W_act, h) + b_act log_probs = logsoftmax(logits, valid_actions) if oracle_actions is None: # Temporary work around by manually back-off to numpy https://github.com/dmlc/minpy/issues/15 action = numpy.argmax(map(lambda x: x[0], list(log_probs))) if oracle_actions is not None: action = oracle_actions.pop() if log_probs is not None: # append the action-specific loss # print action, log_probs[action], map(lambda x: x[0], list(log_probs)) loss += log_probs[action] # execute the action to update the parser state if action == SHIFT: tok_embedding, token = buffer.pop() stack.push(tok_embedding, (tok_embedding, token)) else: # one of the REDUCE actions right = stack.pop() # pop a stack state left = stack.pop() # pop another stack state # figure out which is the head and which is the modifier head, modifier = (left, right) if action == REDUCE_R else (right, left) # compute composed representation head_rep, head_tok = head mod_rep, mod_tok = modifier composed_rep = np.tanh( np.dot( W_comp, np.transpose( np.concatenate([head_rep, mod_rep], axis=1))) + b_comp) composed_rep = np.transpose(composed_rep) stack.push(composed_rep, (composed_rep, head_tok)) if oracle_actions is None: print('{0} --> {1}'.format(head_tok, mod_tok)) # the head of the tree that remains at the top of the stack is the root if oracle_actions is None: head = stack.pop()[1] print('ROOT --> {0}'.format(head)) return -loss
def test_numeric(): # 'newaxis', 'ndarray', 'flatiter', 'nditer', 'nested_iters', 'ufunc', # 'arange', 'array', 'zeros', 'count_nonzero', 'empty', 'broadcast', # 'dtype', 'fromstring', 'fromfile', 'frombuffer', 'int_asbuffer', # 'where', 'argwhere', 'copyto', 'concatenate', 'fastCopyAndTranspose', # 'lexsort', 'set_numeric_ops', 'can_cast', 'promote_types', # 'min_scalar_type', 'result_type', 'asarray', 'asanyarray', # 'ascontiguousarray', 'asfortranarray', 'isfortran', 'empty_like', # 'zeros_like', 'ones_like', 'correlate', 'convolve', 'inner', 'dot', # 'einsum', 'outer', 'vdot', 'alterdot', 'restoredot', 'roll', # 'rollaxis', 'moveaxis', 'cross', 'tensordot', 'array2string', # 'get_printoptions', 'set_printoptions', 'array_repr', 'array_str', # 'set_string_function', 'little_endian', 'require', 'fromiter', # 'array_equal', 'array_equiv', 'indices', 'fromfunction', 'isclose', 'load', # 'loads', 'isscalar', 'binary_repr', 'base_repr', 'ones', 'identity', # 'allclose', 'compare_chararrays', 'putmask', 'seterr', 'geterr', # 'setbufsize', 'getbufsize', 'seterrcall', 'geterrcall', 'errstate', # 'flatnonzero', 'Inf', 'inf', 'infty', 'Infinity', 'nan', 'NaN', 'False_', # 'True_', 'bitwise_not', 'full', 'full_like', 'matmul' x = np.arange(6) x = x.reshape((2, 3)) np.zeros_like(x) y = np.arange(3, dtype=np.float) np.zeros_like(y) np.ones(5) np.ones((5,), dtype=np.int) np.ones((2, 1)) s = (2,2) np.ones(s) x = np.arange(6) x = x.reshape((2, 3)) np.ones_like(x) y = np.arange(3, dtype=np.float) np.ones_like(y) np.full((2, 2), np.inf) x = np.arange(6, dtype=np.int) np.full_like(x, 1) np.full_like(x, 0.1) np.full_like(y, 0.1) np.count_nonzero(np.eye(4)) np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]]) np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=0) np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=1) a = [1, 2] np.asarray(a) a = np.array([1, 2]) np.asarray(a) is a a = np.array([1, 2], dtype=np.float32) np.asarray(a, dtype=np.float32) is a np.asarray(a, dtype=np.float64) is a np.asarray(a) is a np.asanyarray(a) is a a = [1, 2] np.asanyarray(a) np.asanyarray(a) is a x = np.arange(6).reshape(2,3) np.ascontiguousarray(x, dtype=np.float32) x = np.arange(6).reshape(2,3) y = np.asfortranarray(x) x = np.arange(6).reshape(2,3) y = np.require(x, dtype=np.float32, requirements=['A', 'O', 'W', 'F']) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = np.array([[1, 2, 3], [4, 5, 6]], order='FORTRAN') np.isfortran(b) a = np.array([[1, 2, 3], [4, 5, 6]], order='C') np.isfortran(a) b = a.T np.isfortran(b) np.isfortran(np.array([1, 2], order='FORTRAN')) x = np.arange(6).reshape(2,3) np.argwhere(x>1) x = np.arange(-2, 3) np.flatnonzero(x) np.correlate([1, 2, 3], [0, 1, 0.5]) np.correlate([1, 2, 3], [0, 1, 0.5], "same") np.correlate([1, 2, 3], [0, 1, 0.5], "full") np.correlate([1+1j, 2, 3-1j], [0, 1, 0.5j], 'full') np.correlate([0, 1, 0.5j], [1+1j, 2, 3-1j], 'full') np.convolve([1, 2, 3], [0, 1, 0.5]) np.convolve([1,2,3],[0,1,0.5], 'same') np.convolve([1,2,3],[0,1,0.5], 'valid') rl = np.outer(np.ones((5,)), np.linspace(-2, 2, 5)) # im = np.outer(1j*np.linspace(2, -2, 5), np.ones((5,))) # grid = rl + im x = np.array(['a', 'b', 'c'], dtype=object) np.outer(x, [1, 2, 3]) a = np.arange(60.).reshape(3,4,5) b = np.arange(24.).reshape(4,3,2) c = np.tensordot(a,b, axes=([1,0],[0,1])) c.shape # A slower but equivalent way of computing the same... d = np.zeros((5,2)) a = np.array(range(1, 9)) A = np.array(('a', 'b', 'c', 'd'), dtype=object) x = np.arange(10) np.roll(x, 2) x2 = np.reshape(x, (2,5)) np.roll(x2, 1) np.roll(x2, 1, axis=0) np.roll(x2, 1, axis=1) a = np.ones((3,4,5,6)) np.rollaxis(a, 3, 1).shape np.rollaxis(a, 2).shape np.rollaxis(a, 1, 4).shape x = np.zeros((3, 4, 5)) np.moveaxis(x, 0, -1).shape np.moveaxis(x, -1, 0).shape np.transpose(x).shape np.moveaxis(x, [0, 1], [-1, -2]).shape np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape x = [1, 2, 3] y = [4, 5, 6] np.cross(x, y) x = [1, 2] y = [4, 5, 6] np.cross(x, y) x = [1, 2, 0] y = [4, 5, 6] np.cross(x, y) x = [1,2] y = [4,5] np.cross(x, y) x = np.array([[1,2,3], [4,5,6]]) y = np.array([[4,5,6], [1,2,3]]) np.cross(x, y) np.cross(x, y, axisc=0) x = np.array([[1,2,3], [4,5,6], [7, 8, 9]]) y = np.array([[7, 8, 9], [4,5,6], [1,2,3]]) np.cross(x, y) np.cross(x, y, axisa=0, axisb=0) # np.array_repr(np.array([1,2])) # np.array_repr(np.ma.array([0.])) # np.array_repr(np.array([], np.int32)) x = np.array([1e-6, 4e-7, 2, 3]) # np.array_repr(x, precision=6, suppress_small=True) # np.array_str(np.arange(3)) a = np.arange(10) x = np.arange(4) np.set_string_function(lambda x:'random', repr=False) grid = np.indices((2, 3)) grid.shape grid[0] # row indices grid[1] # column indices x = np.arange(20).reshape(5, 4) row, col = np.indices((2, 3)) x[row, col] np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) np.isscalar(3.1) np.isscalar([3.1]) np.isscalar(False) # np.binary_repr(3) # np.binary_repr(-3) # np.binary_repr(3, width=4) # np.binary_repr(-3, width=3) # np.binary_repr(-3, width=5) # np.base_repr(5) # np.base_repr(6, 5) # np.base_repr(7, base=5, padding=3) # np.base_repr(10, base=16) # np.base_repr(32, base=16) np.identity(3) np.allclose([1e10,1e-7], [1.00001e10,1e-8]) np.allclose([1e10,1e-8], [1.00001e10,1e-9]) np.allclose([1e10,1e-8], [1.0001e10,1e-9]) # np.allclose([1.0, np.nan], [1.0, np.nan]) # np.allclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.isclose([1e10,1e-7], [1.00001e10,1e-8]) np.isclose([1e10,1e-8], [1.00001e10,1e-9]) np.isclose([1e10,1e-8], [1.0001e10,1e-9]) # np.isclose([1.0, np.nan], [1.0, np.nan]) # np.isclose([1.0, np.nan], [1.0, np.nan], equal_nan=True) np.array_equal([1, 2], [1, 2]) np.array_equal(np.array([1, 2]), np.array([1, 2])) np.array_equal([1, 2], [1, 2, 3]) np.array_equal([1, 2], [1, 4]) np.array_equiv([1, 2], [1, 2]) np.array_equiv([1, 2], [1, 3]) np.array_equiv([1, 2], [[1, 2], [1, 2]]) np.array_equiv([1, 2], [[1, 2, 1, 2], [1, 2, 1, 2]]) np.array_equiv([1, 2], [[1, 2], [1, 3]])
caffe.TanhLayer(), caffe.FullyConnectedLayer(10), caffe.SoftMaxLayer()] # Training parameters param_scale = 0.1 learning_rate = 1e-3 momentum = 0.9 batch_size = 256 num_epochs = 50 # Load and process MNIST data (borrowing from Kayak) print("Loading training data...") import imp, urllib add_color_channel = lambda x : x.reshape((x.shape[0], 1, x.shape[1], x.shape[2])) one_hot = lambda x, K : np.array(x[:,None] == np.arange(K)[None, :], dtype=int) source, _ = urllib.urlretrieve( 'https://raw.githubusercontent.com/HIPS/Kayak/master/examples/data.py') data = imp.load_source('data', source).mnist() train_images, train_labels, test_images, test_labels = data train_images = add_color_channel(train_images) / 255.0 test_images = add_color_channel(test_images) / 255.0 train_labels = one_hot(train_labels, 10) test_labels = one_hot(test_labels, 10) N_data = train_images.shape[0] # Make neural net functions N_weights, pred_fun, loss_fun, frac_err = make_nn_funs(input_shape, caffe_layer_specs, L2_reg) loss_grad = grad(loss_fun) # Initialize weights
def d_sigmoid(x): return sigmoid(x) * (1 - sigmoid(x)) def tanh(x): return np.tanh(x) def d_tanh(x): return 1 - tanh(x)**2 # 0. Seed the random value for repeatable np.random.seed(1234) # 1. Create a data set for the network to work on Data, Label = make_blobs(n_samples=1500, random_state=8, centers=2) Data = np.array(Data) Label = np.array(Label) # plt.scatter(Data[:,0],Data[:,1],c=Label) # plt.show() # 1.3 Split the training and test set data_x, data_y, label_x, label_y = model_selection.train_test_split( Data, Label, test_size=0.3333333) # 2. Declare hyper parameter number_of_epoch = 10 learning_rate = 100 w1 = np.random.randn(2, 100)