Python numpy.hstack() Examples
The following are 30
code examples of numpy.hstack().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: tcpr.py From libTLDA with MIT License | 7 votes |
def add_intercept(self, X): """Add 1's to data as last features.""" # Data shape N, D = X.shape # Check if there's not already an intercept column if np.any(np.sum(X, axis=0) == N): # Report print('Intercept is not the last feature. Swapping..') # Find which column contains the intercept intercept_index = np.argwhere(np.sum(X, axis=0) == N) # Swap intercept to last X = X[:, np.setdiff1d(np.arange(D), intercept_index)] # Add intercept as last column X = np.hstack((X, np.ones((N, 1)))) # Append column of 1's to data, and increment dimensionality return X, D+1
Example #2
Source File: datasets.py From discomll with Apache License 2.0 | 6 votes |
def regression_data(): f = open(path + "regression_data1.txt") data = np.loadtxt(f, delimiter=",") x1 = np.insert(data[:, 0].reshape(len(data), 1), 0, np.ones(len(data)), axis=1) y1 = data[:, 1] f = open(path + "regression_data2.txt") data = np.loadtxt(f, delimiter=",") x2 = np.insert(data[:, 0].reshape(len(data), 1), 0, np.ones(len(data)), axis=1) y2 = data[:, 1] x1 = np.vstack((x1, x2)) y1 = np.hstack((y1, y2)) f = open(path + "regression_data_test1.txt") data = np.loadtxt(f, delimiter=",") x1_test = np.insert(data[:, 0].reshape(len(data), 1), 0, np.ones(len(data)), axis=1) y1_test = data[:, 1] f = open(path + "regression_data_test2.txt") data = np.loadtxt(f, delimiter=",") x2_test = np.insert(data[:, 0].reshape(len(data), 1), 0, np.ones(len(data)), axis=1) y2_test = data[:, 1] x1_test = np.vstack((x1_test, x2_test)) y1_test = np.hstack((y1_test, y2_test)) return x1, y1, x1_test, y1_test
Example #3
Source File: matcher_test.py From DOTA_models with Apache License 2.0 | 6 votes |
def test_all_columns_accounted_for(self): # Note: deliberately setting to small number so not always # all possibilities appear (matched, unmatched, ignored) num_matches = 10 match_results = tf.random_uniform( [num_matches], minval=-2, maxval=5, dtype=tf.int32) match = matcher.Match(match_results) matched_column_indices = match.matched_column_indices() unmatched_column_indices = match.unmatched_column_indices() ignored_column_indices = match.ignored_column_indices() with self.test_session() as sess: matched, unmatched, ignored = sess.run([ matched_column_indices, unmatched_column_indices, ignored_column_indices ]) all_indices = np.hstack((matched, unmatched, ignored)) all_indices_sorted = np.sort(all_indices) self.assertAllEqual(all_indices_sorted, np.arange(num_matches, dtype=np.int32))
Example #4
Source File: np_box_list_ops.py From DOTA_models with Apache License 2.0 | 6 votes |
def scale(boxlist, y_scale, x_scale): """Scale box coordinates in x and y dimensions. Args: boxlist: BoxList holding N boxes y_scale: float x_scale: float Returns: boxlist: BoxList holding N boxes """ y_min, x_min, y_max, x_max = np.array_split(boxlist.get(), 4, axis=1) y_min = y_scale * y_min y_max = y_scale * y_max x_min = x_scale * x_min x_max = x_scale * x_max scaled_boxlist = np_box_list.BoxList(np.hstack([y_min, x_min, y_max, x_max])) fields = boxlist.get_extra_fields() for field in fields: extra_field_data = boxlist.get_field(field) scaled_boxlist.add_field(field, extra_field_data) return scaled_boxlist
Example #5
Source File: datasets.py From discomll with Apache License 2.0 | 6 votes |
def ex3(replication=2): f = open(path + "ex3.txt") train_data = np.loadtxt(f, delimiter=",") f = open(path + "ex3_test.txt") test_data = np.loadtxt(f, delimiter=",") x_train = np.insert(train_data[:, (0, 1)], 0, np.ones(len(train_data)), axis=1) y_train = train_data[:, 2] x_test = np.insert(test_data[:, (0, 1)], 0, np.ones(len(test_data)), axis=1) y_test = test_data[:, 2] for i in range(replication - 1): x_train = np.vstack((x_train, np.insert(train_data[:, (0, 1)], 0, np.ones(len(train_data)), axis=1))) y_train = np.hstack((y_train, train_data[:, 2])) x_test = np.vstack((x_test, np.insert(test_data[:, (0, 1)], 0, np.ones(len(test_data)), axis=1))) y_test = np.hstack((y_test, test_data[:, 2])) return x_train, y_train, x_test, y_test
Example #6
Source File: datasets.py From discomll with Apache License 2.0 | 6 votes |
def iris(replication=2): f = open(path + "iris.txt") data = np.loadtxt(f, delimiter=",", dtype=np.string0) x_train = np.array(data[:, range(0, 4)], dtype=np.float) y_train = data[:, 4] for j in range(replication - 1): x_train = np.vstack([x_train, data[:, range(0, 4)]]) y_train = np.hstack([y_train, data[:, 4]]) x_train = np.array(x_train, dtype=np.float) f = open(path + "iris_test.txt") data = np.loadtxt(f, delimiter=",", dtype=np.string0) x_test = np.array(data[:, range(0, 4)], dtype=np.float) y_test = data[:, 4] for j in range(replication - 1): x_test = np.vstack([x_test, data[:, range(0, 4)]]) y_test = np.hstack([y_test, data[:, 4]]) x_test = np.array(x_test, dtype=np.float) return x_train, y_train, x_test, y_test
Example #7
Source File: test_util.py From libTLDA with MIT License | 6 votes |
def test_one_hot(): """Check if one_hot returns correct label matrices.""" # Generate label vector y = np.hstack((np.ones((10,))*0, np.ones((10,))*1, np.ones((10,))*2)) # Map to matrix Y, labels = one_hot(y) # Check for only 0's and 1's assert len(np.setdiff1d(np.unique(Y), [0, 1])) == 0 # Check for correct labels assert np.all(labels == np.unique(y)) # Check correct shape of matrix assert Y.shape[0] == y.shape[0] assert Y.shape[1] == len(labels)
Example #8
Source File: datasets.py From discomll with Apache License 2.0 | 6 votes |
def breastcancer_disc(replication=2): f = open(path + "breast_cancer_wisconsin_disc.txt") data = np.loadtxt(f, delimiter=",") x_train = data[:, range(1, 10)] y_train = data[:, 10] for j in range(replication - 1): x_train = np.vstack([x_train, data[:, range(1, 10)]]) y_train = np.hstack([y_train, data[:, 10]]) f = open(path + "breast_cancer_wisconsin_disc_test.txt") data = np.loadtxt(f, delimiter=",") x_test = data[:, range(1, 10)] y_test = data[:, 10] for j in range(replication - 1): x_test = np.vstack([x_test, data[:, range(1, 10)]]) y_test = np.hstack([y_test, data[:, 10]]) return x_train, y_train, x_test, y_test
Example #9
Source File: datasets.py From discomll with Apache License 2.0 | 6 votes |
def breastcancer_cont(replication=2): f = open(path + "breast_cancer_wisconsin_cont.txt", "r") data = np.loadtxt(f, delimiter=",", dtype=np.string0) x_train = np.array(data[:, range(0, 9)]) y_train = np.array(data[:, 9]) for j in range(replication - 1): x_train = np.vstack([x_train, data[:, range(0, 9)]]) y_train = np.hstack([y_train, data[:, 9]]) x_train = np.array(x_train, dtype=np.float) f = open(path + "breast_cancer_wisconsin_cont_test.txt") data = np.loadtxt(f, delimiter=",", dtype=np.string0) x_test = np.array(data[:, range(0, 9)]) y_test = np.array(data[:, 9]) for j in range(replication - 1): x_test = np.vstack([x_test, data[:, range(0, 9)]]) y_test = np.hstack([y_test, data[:, 9]]) x_test = np.array(x_test, dtype=np.float) return x_train, y_train, x_test, y_test
Example #10
Source File: test_rnn.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_lstm_forget_bias(): forget_bias = 2.0 stack = mx.rnn.SequentialRNNCell() stack.add(mx.rnn.LSTMCell(100, forget_bias=forget_bias, prefix='l0_')) stack.add(mx.rnn.LSTMCell(100, forget_bias=forget_bias, prefix='l1_')) dshape = (32, 1, 200) data = mx.sym.Variable('data') sym, _ = stack.unroll(1, data, merge_outputs=True) mod = mx.mod.Module(sym, label_names=None, context=mx.cpu(0)) mod.bind(data_shapes=[('data', dshape)], label_shapes=None) mod.init_params() bias_argument = next(x for x in sym.list_arguments() if x.endswith('i2h_bias')) expected_bias = np.hstack([np.zeros((100,)), forget_bias * np.ones(100, ), np.zeros((2 * 100,))]) assert_allclose(mod.get_params()[0][bias_argument].asnumpy(), expected_bias)
Example #11
Source File: nn.py From Att-ChemdNER with Apache License 2.0 | 6 votes |
def build(self): #{{{ import numpy as np; self.W = shared((self.input_dim, 4 * self.output_dim), name='{}_W'.format(self.name)) self.U = shared((self.output_dim, 4 * self.output_dim), name='{}_U'.format(self.name)) self.b = K.variable(np.hstack((np.zeros(self.output_dim), K.get_value(self.forget_bias_init( (self.output_dim,))), np.zeros(self.output_dim), np.zeros(self.output_dim))), name='{}_b'.format(self.name)) #self.c_0 = shared((self.output_dim,), name='{}_c_0'.format(self.name) ) #self.h_0 = shared((self.output_dim,), name='{}_h_0'.format(self.name) ) self.c_0=np.zeros(self.output_dim).astype(theano.config.floatX); self.h_0=np.zeros(self.output_dim).astype(theano.config.floatX); self.params=[self.W,self.U, self.b, # self.c_0,self.h_0 ]; #}}}
Example #12
Source File: test.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License | 6 votes |
def _get_rois_blob(im_rois, im_scale_factors): """Converts RoIs into network inputs. Arguments: im_rois (ndarray): R x 4 matrix of RoIs in original image coordinates im_scale_factors (list): scale factors as returned by _get_image_blob Returns: blob (ndarray): R x 5 matrix of RoIs in the image pyramid """ rois_blob_real = [] for i in range(len(im_scale_factors)): rois, levels = _project_im_rois(im_rois, np.array([im_scale_factors[i]])) rois_blob = np.hstack((levels, rois)) rois_blob_real.append(rois_blob.astype(np.float32, copy=False)) return rois_blob_real
Example #13
Source File: test_train.py From Collaborative-Learning-for-Weakly-Supervised-Object-Detection with MIT License | 6 votes |
def _get_rois_blob(im_rois, im_scale_factors): """Converts RoIs into network inputs. Arguments: im_rois (ndarray): R x 4 matrix of RoIs in original image coordinates im_scale_factors (list): scale factors as returned by _get_image_blob Returns: blob (ndarray): R x 5 matrix of RoIs in the image pyramid """ rois_blob_real = [] for i in range(len(im_scale_factors)): rois, levels = _project_im_rois(im_rois, np.array([im_scale_factors[i]])) rois_blob = np.hstack((levels, rois)) rois_blob_real.append(rois_blob.astype(np.float32, copy=False)) return rois_blob_real
Example #14
Source File: test_gluon_rnn.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def test_lstm_forget_bias(): forget_bias = 2.0 stack = gluon.rnn.SequentialRNNCell() stack.add(gluon.rnn.LSTMCell(100, i2h_bias_initializer=mx.init.LSTMBias(forget_bias), prefix='l0_')) stack.add(gluon.rnn.LSTMCell(100, i2h_bias_initializer=mx.init.LSTMBias(forget_bias), prefix='l1_')) dshape = (32, 1, 200) data = mx.sym.Variable('data') sym, _ = stack.unroll(1, data, merge_outputs=True) mod = mx.mod.Module(sym, label_names=None, context=mx.cpu(0)) mod.bind(data_shapes=[('data', dshape)], label_shapes=None) mod.init_params() bias_argument = next(x for x in sym.list_arguments() if x.endswith('i2h_bias')) expected_bias = np.hstack([np.zeros((100,)), forget_bias * np.ones(100, ), np.zeros((2 * 100,))]) assert_allclose(mod.get_params()[0][bias_argument].asnumpy(), expected_bias)
Example #15
Source File: sort_io.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def make_data_iter_plan(self): "make a random data iteration plan" # truncate each bucket into multiple of batch-size bucket_n_batches = [] for i in range(len(self.data)): bucket_n_batches.append(len(self.data[i]) / self.batch_size) self.data[i] = self.data[i][:int(bucket_n_batches[i]*self.batch_size)] bucket_plan = np.hstack([np.zeros(n, int)+i for i, n in enumerate(bucket_n_batches)]) np.random.shuffle(bucket_plan) bucket_idx_all = [np.random.permutation(len(x)) for x in self.data] self.bucket_plan = bucket_plan self.bucket_idx_all = bucket_idx_all self.bucket_curr_idx = [0 for x in self.data] self.data_buffer = [] self.label_buffer = [] for i_bucket in range(len(self.data)): data = np.zeros((self.batch_size, self.buckets[i_bucket])) label = np.zeros((self.batch_size, self.buckets[i_bucket])) self.data_buffer.append(data) self.label_buffer.append(label)
Example #16
Source File: tools_fri_doa_plane.py From FRIDA with MIT License | 6 votes |
def output_shrink(K, L): """ shrink the convolution output to half the size. used when both the annihilating filter and the uniform samples of sinusoids satisfy Hermitian symmetric. :param K: the annihilating filter size: K + 1 :param L: length of the (complex-valued) b vector :return: """ out_len = L - K if out_len % 2 == 0: half_out_len = np.int(out_len / 2.) mtx_r = np.hstack((np.eye(half_out_len), np.zeros((half_out_len, half_out_len)))) mtx_i = mtx_r else: half_out_len = np.int((out_len + 1) / 2.) mtx_r = np.hstack((np.eye(half_out_len), np.zeros((half_out_len, half_out_len - 1)))) mtx_i = np.hstack((np.eye(half_out_len - 1), np.zeros((half_out_len - 1, half_out_len)))) return linalg.block_diag(mtx_r, mtx_i)
Example #17
Source File: NeuralNetwork.py From fuku-ml with MIT License | 6 votes |
def backward_process(self, x, y, W, neuron_output): backward_output = [] layer_num = len(neuron_output) score = np.dot(np.hstack((1, neuron_output[layer_num - 2])), W[layer_num - 1]) error_gradient = np.array([-2 * (y - neuron_output[layer_num - 1][0]) * self.tanh_prime(score)]) # error_gradient = np.array([np.sum(-2 * (y - score) * np.hstack((1, neuron_output[layer_num-2])))]) backward_output.insert(0, error_gradient) # Hidden layer for i in range(layer_num - 2, -1, -1): if i == 0: score = np.dot(x, W[i]) else: score = np.dot(np.hstack((1, neuron_output[i - 1])), W[i]) error_gradient = np.dot(error_gradient, W[i + 1][1:].transpose()) * self.tanh_prime(score) backward_output.insert(0, error_gradient) return backward_output
Example #18
Source File: element.py From StructEngPy with MIT License | 6 votes |
def _N(self,s,r): """ Lagrange's interpolate function params: s,r:natural position of evalue point.2-array. returns: 2x(2x4) shape function matrix. """ la1=(1-s)/2 la2=(1+s)/2 lb1=(1-r)/2 lb2=(1+r)/2 N1=la1*lb1 N2=la1*lb2 N3=la2*lb1 N4=la2*lb2 N=np.hstack(N1*np.eye(2),N2*np.eye(2),N3*np.eye(2),N4*np.eye(2)) return N
Example #19
Source File: dataset.py From Deep_VoiceChanger with MIT License | 6 votes |
def wave2input_image(wave, window, pos=0, pad=0): wave_image = np.hstack([wave[pos+i*sride:pos+(i+pad*2)*sride+dif].reshape(height+pad*2, sride) for i in range(256//sride)])[:,:254] wave_image *= window spectrum_image = np.fft.fft(wave_image, axis=1) input_image = np.abs(spectrum_image[:,:128].reshape(1, height+pad*2, 128), dtype=np.float32) np.clip(input_image, 1000, None, out=input_image) np.log(input_image, out=input_image) input_image += bias input_image /= scale if np.max(input_image) > 0.95: print('input image max bigger than 0.95', np.max(input_image)) if np.min(input_image) < 0.05: print('input image min smaller than 0.05', np.min(input_image)) return input_image
Example #20
Source File: core.py From neuropythy with GNU Affero General Public License v3.0 | 5 votes |
def repmat(x, r, c): ''' repmat(x, r, c) is equivalent to numpy.matlib.repmat(x, r, c) except that it works correctly for sparse matrices. ''' if sps.issparse(x): row = sps.hstack([x for _ in range(c)]) return sps.vstack([row for _ in range(r)], format=x.format) else: return np.matlib.repmat(x, r, c)
Example #21
Source File: core.py From neuropythy with GNU Affero General Public License v3.0 | 5 votes |
def close_curves(*crvs, **kw): ''' close_curves(crv1, crv2...) yields a single curve that merges all of the given list of curves together. The curves must be given in order, such that the i'th curve should be connected to to the (i+1)'th curve circularly to form a perimeter. The following optional parameters may be given: * grid may specify the number of grid-points to use in the initial search for a start-point (default: 16). * order may specify the order of the resulting curve; by default (None) uses the lowest order of all curves. * smoothing (None) the amount to smooth the points. * even_out (True) whether to even out the distances along the curve. * meta_data (None) an optional map of meta-data to give the spline representation. ''' for k in six.iterkeys(kw): if k not in close_curves.default_options: raise ValueError('Unrecognized option: %s' % k) kw = {k:(kw[k] if k in kw else v) for (k,v) in six.iteritems(close_curves.default_options)} (grid, order) = (kw['grid'], kw['order']) crvs = [(crv if is_curve_spline(crv) else to_curve_spline(crv)).even_out() for crv in crvs] # find all intersections: isects = [curve_intersection(u,v, grid=grid) for (u,v) in zip(crvs, np.roll(crvs,-1))] # subsample curves crds = np.hstack([crv.subcurve(s1[1], s0[0]).coordinates[:,:-1] for (crv,s0,s1) in zip(crvs, isects, np.roll(isects,1,0))]) kw['order'] = np.min([crv.order for crv in crvs]) if order is None else order kw = {k:v for (k,v) in six.iteritems(kw) if v is not None and k in ('order','smoothing','even_out','meta_data')} return curve_spline(crds, periodic=True, **kw)
Example #22
Source File: core.py From neuropythy with GNU Affero General Public License v3.0 | 5 votes |
def colors_to_cmap(colors): colors = np.asarray(colors) if len(colors.shape) == 1: return colors_to_cmap([colors])[0] if colors.shape[1] == 3: colors = np.hstack((colors, np.ones((len(colors),1)))) steps = (0.5 + np.asarray(range(len(colors)-1), dtype=np.float))/(len(colors) - 1) return matplotlib.colors.LinearSegmentedColormap( 'auto_cmap', {clrname: ([(0, col[0], col[0])] + [(step, c0, c1) for (step,c0,c1) in zip(steps, col[:-1], col[1:])] + [(1, col[-1], col[-1])]) for (clridx,clrname) in enumerate(['red', 'green', 'blue', 'alpha']) for col in [colors[:,clridx]]}, N=(len(colors)))
Example #23
Source File: TensorFlowInterface.py From IntroToDeepLearning with MIT License | 5 votes |
def plotOutput(layer,feed_dict,fieldShape=None,channel=None,figOffset=1,cmap=None): # Output summary W = layer.output wp = W.eval(feed_dict=feed_dict); if len(np.shape(wp)) < 4: # Fully connected layer, has no shape temp = np.zeros(np.product(fieldShape)); temp[0:np.shape(wp.ravel())[0]] = wp.ravel() fields = np.reshape(temp,[1]+fieldShape) else: # Convolutional layer already has shape wp = np.rollaxis(wp,3,0) features, channels, iy,ix = np.shape(wp) if channel is not None: fields = wp[:,channel,:,:] else: fields = np.reshape(wp,[features*channels,iy,ix]) perRow = int(math.floor(math.sqrt(fields.shape[0]))) perColumn = int(math.ceil(fields.shape[0]/float(perRow))) fields2 = np.vstack([fields,np.zeros([perRow*perColumn-fields.shape[0]] + list(fields.shape[1:]))]) tiled = [] for i in range(0,perColumn*perRow,perColumn): tiled.append(np.hstack(fields2[i:i+perColumn])) tiled = np.vstack(tiled) if figOffset is not None: mpl.figure(figOffset); mpl.clf(); mpl.imshow(tiled,cmap=cmap); mpl.title('%s Output' % layer.name); mpl.colorbar();
Example #24
Source File: core.py From neuropythy with GNU Affero General Public License v3.0 | 5 votes |
def hstack(tup): ''' hstack(x) is equivalent to numpy.hstack(x) or scipy.sparse.hstack(x) except that it works correctly with both sparse and dense arrays (if any inputs are dense, it converts all inputs to dense arrays). ''' if all([sps.issparse(u) for u in tup]): return sps.hstack(tup, format=tup[0].format) else: return np.hstack([u.toarray() if sps.issparse(u) else u for u in tup])
Example #25
Source File: TensorFlowInterface.py From IntroToDeepLearning with MIT License | 5 votes |
def plotFields(layer,fieldShape=None,channel=None,maxFields=25,figName='ReceptiveFields',cmap=None,padding=0.01): # Receptive Fields Summary W = layer.W wp = W.eval().transpose(); if len(np.shape(wp)) < 4: # Fully connected layer, has no shape fields = np.reshape(wp,list(wp.shape[0:-1])+fieldShape) else: # Convolutional layer already has shape features, channels, iy, ix = np.shape(wp) if channel is not None: fields = wp[:,channel,:,:] else: fields = np.reshape(wp,[features*channels,iy,ix]) fieldsN = min(fields.shape[0],maxFields) perRow = int(math.floor(math.sqrt(fieldsN))) perColumn = int(math.ceil(fieldsN/float(perRow))) fig = mpl.figure(figName); mpl.clf() # Using image grid from mpl_toolkits.axes_grid1 import ImageGrid grid = ImageGrid(fig,111,nrows_ncols=(perRow,perColumn),axes_pad=padding,cbar_mode='single') for i in range(0,fieldsN): im = grid[i].imshow(fields[i],cmap=cmap); grid.cbar_axes[0].colorbar(im) mpl.title('%s Receptive Fields' % layer.name) # old way # fields2 = np.vstack([fields,np.zeros([perRow*perColumn-fields.shape[0]] + list(fields.shape[1:]))]) # tiled = [] # for i in range(0,perColumn*perRow,perColumn): # tiled.append(np.hstack(fields2[i:i+perColumn])) # # tiled = np.vstack(tiled) # mpl.figure(figOffset); mpl.clf(); mpl.imshow(tiled,cmap=cmap); mpl.title('%s Receptive Fields' % layer.name); mpl.colorbar(); mpl.figure(figName+' Total'); mpl.clf(); mpl.imshow(np.sum(np.abs(fields),0),cmap=cmap); mpl.title('%s Total Absolute Input Dependency' % layer.name); mpl.colorbar()
Example #26
Source File: hcp.py From neuropythy with GNU Affero General Public License v3.0 | 5 votes |
def _siblings_to_pairs(rs): subject_list = [u for v in six.itervalues(rs) for uuu in [[six.iterkeys(v)], six.itervalues(v)] for uu in uuu for u in ([uu] if pimms.is_int(uu) else uu)] subject_list = np.unique(subject_list) # setup twin numbers so that we can export anonymized twin data (i.e., # files containing twin data but not the subject IDs) twin_pairs = {tw: pimms.imm_array(list(sorted(dat))) for tw in ['MZ','DZ'] for dat in [set([tuple(sorted([k,v])) for (k,v) in six.iteritems(rs[tw])])]} # also get a list of all siblings so we can track who is/isn't related siblings = {} for s1 in subject_list: q = [] for sibs in six.itervalues(rs): if s1 not in sibs: continue ss = sibs[s1] if pimms.is_int(ss): ss = [ss] for s2 in ss: q.append(s2) if len(q) > 0: siblings[s1] = q # Make up a list of all possible unrelated pairs unrelated_pairs = [] for sid in subject_list: # find a random subject to pair them with urs = np.setdiff1d(subject_list, [sid] + siblings.get(sid,[])) unrelated_pairs.append([urs, np.full(len(urs), sid)]) unrelated_pairs = np.unique(np.sort(np.hstack(unrelated_pairs), axis=0), axis=1).T unrelated_pairs.setflags(write=False) # Having made those unrelated pairs, we can add them to the twin pairs twin_pairs['UR'] = unrelated_pairs # finally, let's figure out the non-twin siblings: sibs = [(k,v) for (k,vv) in six.iteritems(rs['']) for v in vv] twin_pairs['SB'] = np.unique(np.sort(sibs, axis=1), axis=0) twin_pairs['SB'].setflags(write=False) return pyr.pmap({'monozygotic_twins': twin_pairs['MZ'], 'dizygotic_twins': twin_pairs['DZ'], 'nontwin_siblings': twin_pairs['SB'], 'unrelated_pairs': twin_pairs['UR']})
Example #27
Source File: NeuralNetwork.py From fuku-ml with MIT License | 5 votes |
def score_function(self, x, W): y_predict = x[1:] for i in range(0, len(W), 1): y_predict = np.tanh(np.dot(np.hstack((1, y_predict)), W[i])) score = y_predict[0] return score
Example #28
Source File: NeuralNetwork.py From fuku-ml with MIT License | 5 votes |
def forward_process(self, x, y, W): forward_output = [] pre_x = x for i in range(len(W)): pre_x = np.tanh(np.dot(pre_x, W[i])) forward_output.append(pre_x) pre_x = np.hstack((1, pre_x)) return forward_output
Example #29
Source File: saliency_visualization.py From VSE-C with MIT License | 5 votes |
def plot_saliency(raw_img, image_var, img_embedding_var, caption_var): dis = (caption_var.squeeze() * img_embedding_var.squeeze()).sum() dis.backward(retain_graph=True) grad = image_var.grad.data.cpu().squeeze().numpy().transpose((1, 2, 0)) grad = normalize_grad(grad, stat=True) grad = imresize((grad * 255).astype('uint8'), (raw_img.height, raw_img.width)) / 255 grad = normalize_grad(grad.mean(axis=-1, keepdims=True).repeat(3, axis=-1)) grad = np.float_power(grad, args.grad_power) np_img = np.array(raw_img) masked_img = np_img * grad final = np.hstack([np_img, masked_img.astype('uint8'), (grad * 255).astype('uint8')]) return Image.fromarray(final.astype('uint8'))
Example #30
Source File: TensorFlowInterface.py From IntroToDeepLearning with MIT License | 5 votes |
def plotOutput(layer,feed_dict,fieldShape=None,channel=None,figOffset=1,cmap=None): # Output summary try: W = layer.output except: W = layer wp = W.eval(feed_dict=feed_dict); if len(np.shape(wp)) < 4: # Fully connected layer, has no shape temp = np.zeros(np.product(fieldShape)); temp[0:np.shape(wp.ravel())[0]] = wp.ravel() fields = np.reshape(temp,[1]+fieldShape) else: # Convolutional layer already has shape wp = np.rollaxis(wp,3,0) features, channels, iy,ix = np.shape(wp) if channel is not None: fields = wp[:,channel,:,:] else: fields = np.reshape(wp,[features*channels,iy,ix]) perRow = int(math.floor(math.sqrt(fields.shape[0]))) perColumn = int(math.ceil(fields.shape[0]/float(perRow))) fields2 = np.vstack([fields,np.zeros([perRow*perColumn-fields.shape[0]] + list(fields.shape[1:]))]) tiled = [] for i in range(0,perColumn*perRow,perColumn): tiled.append(np.hstack(fields2[i:i+perColumn])) tiled = np.vstack(tiled) if figOffset is not None: mpl.figure(figOffset); mpl.clf(); mpl.imshow(tiled,cmap=cmap); mpl.title('%s Output' % layer.name); mpl.colorbar();