def accuracy(p, l): import minpy.numpy as np if len(l.shape) == 1: return 1 - np.count_nonzero(p - l).val / float(p.shape[0]) else: inputs, labels = p, l return np.mean(np.sum((inputs - labels)**2, axis=1))
def discount_rewards(self, rs): drs = np.zeros_like(rs).asnumpy() s = 0 for t in reversed(range(0, len(rs))): # Reset the running sum at a game boundary. if rs[t] != 0: s = 0 s = s * self.gamma + rs[t] drs[t] = s drs -= np.mean(drs) drs /= np.std(drs) return drs
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 stochastic_gradient_loss(model, X, Y, sigma): mean = np.mean(X) noise_X = np.random.normal(mean, sigma, X.shape) def _loss_function(*args): predictions = model.forward(X, 'train') noisy_predictions = model.forward(noise_X, 'train') return model.loss(predictions, Y) + model.loss(noisy_predictions, Y) gl = _gradient_loss(_loss_function, range(len(model.params))) parameters = list(model.params.values()) return gl(*parameters)
def discount_rewards(self, rs): drs = np.zeros_like(rs).asnumpy() s = 0 for t in reversed(xrange(0, len(rs))): # Reset the running sum at a game boundary. if rs[t] != 0: s = 0 s = s * self.gamma + rs[t] drs[t] = s drs -= np.mean(drs) drs /= np.std(drs) return drs
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 preprocess_data(train, test, validation=None): """ Applies zero-centering and normalization of input data for better model performance. :param train: A numpy matrix containing the data the network is trained on. :param test: A numpy matrix containing the data used to test the network. :param validation: An optional numpy matrix containing the data used to apply hyperparameter validation on the network. :return: The preprocessed matrices. """ # Zero-centering (subtracting the mean) mean = np.mean(train, axis=0) # Using statistic of training set train -= mean if validation is not None: validation -= mean test -= mean # Normalization of data dimension to be of equal scale (division by standard deviation) std = np.std(train, axis=0) train /= std if validation is not None: validation /= std test /= std return train, validation, test, mean, std
std = {key: [] for key in model.params} L_2 = {key: [] for key in model.params} minimum = {key: [] for key in model.params} maximum = {key: [] for key in model.params} for i in range(iterations): X_batch = data[0][batch_index * batch_size:(batch_index + 1) * batch_size] Y_batch = data[1][batch_index * batch_size:(batch_index + 1) * batch_size] batch_index = (batch_index + 1) % batches gradients, loss = gradient_loss(model, X_batch, Y_batch) loss = loss.asnumpy()[0] loss_history.append(loss) for key, value in zip(model.params.keys(), gradients): mean[key].append(np.mean(value).asnumpy()) std[key].append(np.std(value).asnumpy()) L_2[key].append(np.mean(value**2).asnumpy()) minimum[key].append(np.min(value).asnumpy()) maximum[key].append(np.max(value).asnumpy()) updater.update(gradients) if (i + 1) % rescaling_interval == 0: rescale(mlp, data[2], model.params) # validation data print 'rescaled' if (i + 1) % interval == 0: print 'iteration %d loss %f' % (i + 1, loss) pickle.dump((loss_history, mean, std, L_2, minimum, maximum),
mat1part = mat[mat1Beg:mat1End, :] mat2part = mat2[:, mat2Beg:mat2End] partResult = mnp.dot(mat1part, mat2part) multiResult[mat1Beg:mat1End, mat2Beg:mat2End] = partResult print(type(partResult)) # tempResult.append(partResult) print(partResult) print(partResult.shape) # multiResult.append(tempResult) # multiResult = mnp.block(multiResult) print(multiResult) print(multiResult.shape) print(mnp.sum(multiResult)) print(mnp.mean(multiResult)) # multiResult[mat1Beg:mat1End, mat2Beg:mat2End] = partResult # # if j == 9: # mat1Beg = (i+1)*Dindex1 # mat1End = 64466 # print('mat1:{}:{}'.format(mat1Beg, mat1End)) # mat2Beg = j*Dindex2 # mat2End = (j+1)*Dindex2 # print('mat2:{}:{}'.format(mat2Beg, mat2End)) # mat1part = mat[mat1Beg:mat1End, :] # mat2part = mat2[:, mat2Beg:mat2End] # partResult = mnp.dot(mat1part, mat2part) # multiResult[mat1Beg:mat1End, mat2Beg:mat2End] = partResult #
xyz_train = (img_name_train.strip('.jpg')).split('_') x_train = float(xyz_train[0]) y_train = float(xyz_train[1]) z_train = float(xyz_train[2]) labels[flag, 0] = x_train labels[flag, 1] = y_train labels[flag, 2] = z_train labels_x[flag] = x_train labels_y[flag] = y_train labels_z[flag] = z_train flag = flag + 1 img_name_train = image_train_f.readline() img_name_train = img_name_train.strip('\n') f_mean = np.mean(features, axis=0) image_train_f.close() image_train_list.close() featuress = np.empty([flag, moments_num], dtype=float) labelss = np.empty([flag, angle_num], dtype=float) labelss_x = np.empty([flag, 1], dtype=float) labelss_y = np.empty([flag, 1], dtype=float) labelss_z = np.empty([flag, 1], dtype=float) for i in range(flag): featuress[i] = features[i] labelss[i] = labels[i] labelss_x[i] = labels_x[i] labelss_y[i] = labels_y[i]
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 frac_err(W_vect, X, T): return np.mean(np.argmax(T, axis=1) != np.argmax(pred_fun(W_vect, X), axis=1))
def barrier(S0, K, B, tau, r, q, v, M, N): S = pricepaths(S0, tau, r, q, v, M, N) l = np.min(S, 0) > B payoffs = l * np.maximum(S[-1, :] - K, 0) return math.exp(-r * tau) * np.mean(payoffs)
def main(): import argparse, random random.seed(0) _arg_parser = argparse.ArgumentParser() _arg_parser.add_argument('--vocab', default='s_lstm_deparser_data/vocab.txt', type=str, action='store', help='') _arg_parser.add_argument( '--train', default='s_lstm_deparser_data/small-train.unk.txt', type=str, action='store', help='') _arg_parser.add_argument('--dev', default='s_lstm_deparser_data/small-dev.unk.txt', type=str, action='store', help='') _arg_parser.add_argument('--emb_size', default=64, type=int, action='store', help='') _arg_parser.add_argument('--rnn_size', default=64, type=int, action='store', help='') _args = _arg_parser.parse_args() # load training and dev data vocab_words = Vocab.from_file(_args.vocab) train = list(read_oracle(_args.train, vocab_words, vocab_acts)) dev = list(read_oracle(_args.dev, vocab_words, vocab_acts)) tp = TransitionParser(vocab_words, _args.emb_size, _args.rnn_size, NUM_ACTIONS) solver = MySolver(model=tp, update_rule='adam') solver.init() validation_losses = [] import logging logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG) for epoch in range(5): random.shuffle(train) words = 0 total_loss = 0.0 for i, (s, a) in enumerate(train): instances_processed = float(i) # periodically report validation loss e = instances_processed / len(train) if instances_processed % 1000 == 0: dev_words = 0 dev_loss = 0.0 for (ds, da) in dev: dev_words += len(ds) dev_loss += np.mean(tp.parse(ds, da)) # print 'sent:', ds # print 'oracle:', da # exit() print('[validation] epoch {}: per-word loss: {}'.format( e, dev_loss / dev_words)) validation_losses.append(dev_loss) # report training loss if instances_processed % 100 == 0 and words > 0: print('epoch {}: per-word loss: {}'.format( e, total_loss / words)) words = 0 total_loss = 0.0 # here we do training total_loss += np.mean( solver.train_on_batch(s, a) ) # returns None for 1-word sentencs (it's clear how to parse them) words += len(s)
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 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 infer_std(inputs, weight_shape): import minpy.numpy as np E_X_2 = np.mean(inputs**2) return 1 / (E_X_2 * float(weight_shape[0]))**0.5
def garchSim(ret2, p): h = np.zeros(ret2.shape[0], dtype='float32') h[0] = np.mean(ret2) for i in range(1, ret2.shape[0]): h[i] = p[0] + p[1] * ret2[i - 1] + p[2] * h[i - 1] return h
from facility import * from solver_primitives import * HIDDEN_LAYERS = 4 shapes = (1024, ) * HIDDEN_LAYERS + (10, ) storage = {} activation = builder.ReLU mlp = builder.Sequential() for i, shape in enumerate(shapes[:-1]): mlp.append(builder.Affine(shape)) mlp.append(builder.Export('affine%d' % i, storage)) mlp.append(activation()) mlp.append(builder.Affine(shapes[-1])) mlp.append(builder.Export('affine%d' % (len(shapes) - 1), storage)) model = builder.Model(mlp, 'softmax', (3072, )) initialize(model) X = np.random.normal(0, 1, (64, 3072)) output = model.forward(X, 'train') print 'origin' for key, value in storage.items(): print key, np.std(value) rescale(mlp, X, model.params) rescaled_output = model.forward(X, 'train') print 'rescaled' for key, value in storage.items(): print key, np.std(value) print np.mean(np.abs(output - rescaled_output))