The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.
Example 1
def test(self, test_data, batch_size): # ???????? questions_ok, documents_ok, context_mask, candidates_ok, y_true = self.preprocess_input_sequences(test_data) logging.info("Test on {} samples, {} per batch.".format(len(questions_ok), batch_size)) # ?? batch_num = len(questions_ok) // batch_size batch_idx = np.arange(batch_num) correct_num, total_num = 0, 0 for i in range(batch_num): start = batch_idx[i % batch_num] * batch_size stop = (batch_idx[i % batch_num] + 1) * batch_size _slice = np.index_exp[start:stop] data = {self.q_input: questions_ok[_slice], self.d_input: documents_ok[_slice], self.context_mask_bt: context_mask[_slice], self.candidates_bi: candidates_ok[_slice], self.y_true: y_true[_slice]} correct, = self.sess.run([self.correct_prediction], feed_dict=data) correct_num, total_num = correct_num + correct, total_num + batch_size test_acc = correct_num / total_num logging.info("Test accuracy is : {:.5f}".format(test_acc))
Example 2
def get_next_batch(self, mode, idx): """ return next batch of data samples """ batch_size = self.args.batch_size if mode == "train": dataset = self.train_data sample_num = self.train_sample_num elif mode == "valid": dataset = self.valid_data sample_num = self.valid_sample_num else: dataset = self.test_data sample_num = self.test_sample_num if mode == "train": start = self.train_idx[idx] * batch_size stop = (self.train_idx[idx] + 1) * batch_size else: start = idx * batch_size stop = (idx + 1) * batch_size if start < sample_num and (idx + 1) * batch_size < sample_num else -1 samples = batch_size if stop != -1 else len(dataset[0]) - start _slice = np.index_exp[start:stop] return self.next_batch_feed_dict_by_dataset(dataset, _slice, samples)
Example 3
def trajectory_set_item(self, idx, value): """ :param self: mdtraj.Trajectory :param idx: possible slices over frames, :param value: :return: """ import mdtraj assert isinstance(self, mdtraj.Trajectory), type(self) if not isinstance(value, mdtraj.Trajectory): raise TypeError("value to assign is of incorrect type(%s). Should be mdtraj.Trajectory" % type(value)) idx = np.index_exp[idx] frames, atoms = None, None if isinstance(idx, (list, tuple)): if len(idx) == 1: frames, atoms = idx[0], slice(None, None, None) if len(idx) == 2: frames, atoms = idx[0], idx[1] if len(idx) >= 3 or len(idx) == 0: raise IndexError("invalid slice by %s" % idx) self.xyz[frames, atoms] = value.xyz self._time[frames] = value.time self.unitcell_lengths[frames] = value.unitcell_lengths self.unitcell_angles[frames] = value.unitcell_angles
Example 4
def _handle_slice(self, idx): idx = np.index_exp[idx] itrajs, frames, dims = None, None, None if isinstance(idx, (list, tuple)): if len(idx) == 1: itrajs, frames, dims = idx[0], slice(None, None, None), slice(None, None, None) if len(idx) == 2: itrajs, frames, dims = idx[0], idx[1], slice(None, None, None) if len(idx) == 3: itrajs, frames, dims = idx[0], idx[1], idx[2] if len(idx) > 3 or len(idx) == 0: raise IndexError("invalid slice by %s" % idx) return self._get_itraj_random_accessible(itrajs, frames, dims)
Example 5
def _handle_slice(self, idx): idx = np.index_exp[idx] frames, dims = None, None if isinstance(idx, (tuple, list)): if len(idx) == 1: frames, dims = idx[0], slice(None, None, None) if len(idx) == 2: frames, dims = idx[0], idx[1] if len(idx) > 2: raise IndexError("Slice was more than two-dimensional, not supported.") cumsum = np.cumsum(self._source.trajectory_lengths()) if not isinstance(frames, (list, np.ndarray)): frames = self._get_indices(frames, cumsum[-1]) dims = self._get_indices(dims, self._source.ndim) nframes = len(frames) ndims = len(dims) data = np.empty((nframes, ndims), dtype=self._source.output_type()) from chainsaw.clustering import UniformTimeClustering for i, x in enumerate(frames): traj, idx = UniformTimeClustering._idx_to_traj_idx(x, cumsum) data[i, :] = self._source.data[traj][idx, dims] return data
Example 6
def _handle_slice(self, idx): idx = np.index_exp[idx] itrajs, frames, dims = None, None, None if isinstance(idx, (list, tuple)): if len(idx) == 1: itrajs, frames, dims = idx[0], slice(None, None, None), slice(None, None, None) if len(idx) == 2: itrajs, frames, dims = idx[0], idx[1], slice(None, None, None) if len(idx) == 3: itrajs, frames, dims = idx[0], idx[1], idx[2] if len(idx) > 3 or len(idx) == 0: raise IndexError("invalid slice by %s" % idx) return self._get_itraj_random_accessible(itrajs, frames, dims)
Example 7
def _handle_slice(self, idx): idx = np.index_exp[idx] frames, dims = None, None if isinstance(idx, (tuple, list)): if len(idx) == 1: frames, dims = idx[0], slice(None, None, None) if len(idx) == 2: frames, dims = idx[0], idx[1] if len(idx) > 2: raise IndexError("Slice was more than two-dimensional, not supported.") cumsum = np.cumsum(self._source.trajectory_lengths()) frames = self._get_indices(frames, cumsum[-1]) dims = self._get_indices(dims, self._source.ndim) nframes = len(frames) ndims = len(dims) frames_order = frames.argsort().argsort() frames_sorted = np.sort(frames) from chainsaw.clustering import UniformTimeClustering ra_stride = np.array([UniformTimeClustering._idx_to_traj_idx(x, cumsum) for x in frames_sorted]) data = np.empty((nframes, ndims), dtype=self._source.output_type()) offset = 0 for X in self._source.iterator(stride=ra_stride, lag=0, chunk=0, return_trajindex=False): L = len(X) data[offset:offset + L, :] = X[:, dims] offset += L return data[frames_order]
Example 8
def grey_blit(src, dst, blend_mode=MJBLEND_NORMAL): """ This is for grey + alpha images """ # http://stackoverflow.com/a/3375291/190597 # http://stackoverflow.com/a/9166671/190597 # blending with alpha http://stackoverflow.com/questions/1613600/direct3d-rendering-2d-images-with-multiply-blending-mode-and-alpha # blending modes from: http://www.linuxtopia.org/online_books/graphics_tools/gimp_advanced_guide/gimp_guide_node55.html dt = dst.dtype src = src.astype(n.single) dst = dst.astype(n.single) out = n.empty(src.shape, dtype = 'float') alpha = n.index_exp[:, :, 1] rgb = n.index_exp[:, :, 0] src_a = src[alpha]/255.0 dst_a = dst[alpha]/255.0 out[alpha] = src_a+dst_a*(1-src_a) old_setting = n.seterr(invalid = 'ignore') src_pre = src[rgb]*src_a dst_pre = dst[rgb]*dst_a # blend: blendfuncs = { MJBLEND_NORMAL: lambda s, d, sa_: s + d*sa_, MJBLEND_ADD: lambda s, d, sa_: n.minimum(255, s + d), MJBLEND_SUB: lambda s, d, sa_: n.maximum(0, s - d), MJBLEND_MULT: lambda s, d, sa_: s*d*sa_ / 255.0, MJBLEND_MULTINV: lambda s, d, sa_: (255.0 - s)*d*sa_ / 255.0, MJBLEND_SCREEN: lambda s, d, sa_: 255 - (1.0/255.0)*(255.0 - s)*(255.0 - d*sa_), MJBLEND_DIVIDE: lambda s, d, sa_: n.minimum(255, d*sa_*256.0 / (s + 1.0)), MJBLEND_MIN: lambda s, d, sa_: n.minimum(d*sa_, s), MJBLEND_MAX: lambda s, d, sa_: n.maximum(d*sa_, s), } out[rgb] = blendfuncs[blend_mode](src_pre, dst_pre, (1-src_a)) out[rgb] /= out[alpha] n.seterr(**old_setting) out[alpha] *= 255 n.clip(out,0,255) # astype('uint8') maps np.nan (and np.inf) to 0 out = out.astype(dt) return out
Example 9
def __init__(self, inputA=None, indexExp=None): if indexExp is None: raise ValueError("Must provide index Expression as numpy.index_exp!") self.indexExp = indexExp super().__init__(inputA)
Example 10
def test_ellipsis(self): numpy_n = numpy.arange(24, dtype=self.dtype).reshape((2, 3, 4)) n = self.shared(numpy_n) test_cases = [ (0, Subtensor, self.sub, numpy.index_exp[...]), (1, Subtensor, self.sub, numpy.index_exp[..., 1]), (1, Subtensor, self.sub, numpy.index_exp[1, ...]), (1, Subtensor, self.sub, numpy.index_exp[..., 1, 2, 3]), (1, Subtensor, self.sub, numpy.index_exp[1, ..., 2, 3]), (1, Subtensor, self.sub, numpy.index_exp[1, 2, 3, ...]), (3, DimShuffle, self.dimshuffle, numpy.index_exp[..., [0, 2, 3]]), (1, DimShuffle, self.dimshuffle, numpy.index_exp[numpy.newaxis, ...]), (1, AdvancedSubtensor, self.adv_sub, numpy.index_exp[..., numpy.newaxis, [1, 2]])] for length, op_type, op_type_opt, slice_ in test_cases: numpy_tval = numpy_n[slice_] t = n[slice_] self.assertTrue(isinstance(t.owner.op, op_type)) tval = self.eval_output_and_check(t, op_type=op_type_opt, length=length) assert_equal(tval.shape, numpy_tval.shape) assert_array_equal(tval, numpy_tval)
Example 11
def grey_blit(src, dst, blend_mode=MJBLEND_NORMAL): """ This is for grey + alpha images """ # http://stackoverflow.com/a/3375291/190597 # http://stackoverflow.com/a/9166671/190597 # blending with alpha http://stackoverflow.com/questions/1613600/direct3d-rendering-2d-images-with-multiply-blending-mode-and-alpha # blending modes from: http://www.linuxtopia.org/online_books/graphics_tools/gimp_advanced_guide/gimp_guide_node55.html dt = dst.dtype src = src.astype(n.single) dst = dst.astype(n.single) out = n.empty(src.shape, dtype='float') alpha = n.index_exp[:, :, 1] rgb = n.index_exp[:, :, 0] src_a = src[alpha] / 255.0 dst_a = dst[alpha] / 255.0 out[alpha] = src_a + dst_a * (1 - src_a) old_setting = n.seterr(invalid='ignore') src_pre = src[rgb] * src_a dst_pre = dst[rgb] * dst_a # blend: blendfuncs = { MJBLEND_NORMAL: lambda s, d, sa_: s + d * sa_, MJBLEND_ADD: lambda s, d, sa_: n.minimum(255, s + d), MJBLEND_SUB: lambda s, d, sa_: n.maximum(0, s - d), MJBLEND_MULT: lambda s, d, sa_: s * d * sa_ / 255.0, MJBLEND_MULTINV: lambda s, d, sa_: (255.0 - s) * d * sa_ / 255.0, MJBLEND_SCREEN: lambda s, d, sa_: 255 - (1.0 / 255.0) * (255.0 - s) * (255.0 - d * sa_), MJBLEND_DIVIDE: lambda s, d, sa_: n.minimum(255, d * sa_ * 256.0 / (s + 1.0)), MJBLEND_MIN: lambda s, d, sa_: n.minimum(d * sa_, s), MJBLEND_MAX: lambda s, d, sa_: n.maximum(d * sa_, s), } out[rgb] = blendfuncs[blend_mode](src_pre, dst_pre, (1 - src_a)) out[rgb] /= out[alpha] n.seterr(**old_setting) out[alpha] *= 255 n.clip(out, 0, 255) # astype('uint8') maps np.nan (and np.inf) to 0 out = out.astype(dt) return out
Example 12
def grey_blit(src, dst, blend_mode=MJBLEND_NORMAL): """ This is for grey + alpha images """ # http://stackoverflow.com/a/3375291/190597 # http://stackoverflow.com/a/9166671/190597 # blending with alpha http://stackoverflow.com/questions/1613600/direct3d-rendering-2d-images-with-multiply-blending-mode-and-alpha # blending modes from: http://www.linuxtopia.org/online_books/graphics_tools/gimp_advanced_guide/gimp_guide_node55.html dt = dst.dtype src = src.astype(n.single) dst = dst.astype(n.single) out = n.empty(src.shape, dtype='float') alpha = n.index_exp[:, :, 1] rgb = n.index_exp[:, :, 0] src_a = src[alpha] / 255.0 dst_a = dst[alpha] / 255.0 out[alpha] = src_a + dst_a * (1 - src_a) old_setting = n.seterr(invalid='ignore') src_pre = src[rgb] * src_a dst_pre = dst[rgb] * dst_a # blend: blendfuncs = { MJBLEND_NORMAL: lambda s, d, sa_: s + d * sa_, MJBLEND_ADD: lambda s, d, sa_: n.minimum(255, s + d), MJBLEND_SUB: lambda s, d, sa_: n.maximum(0, s - d), MJBLEND_MULT: lambda s, d, sa_: s * d * sa_ / 255.0, MJBLEND_MULTINV: lambda s, d, sa_: (255.0 - s) * d * sa_ / 255.0, MJBLEND_SCREEN: lambda s, d, sa_: 255 - (1.0 / 255.0) * (255.0 - s) * (255.0 - d * sa_), MJBLEND_DIVIDE: lambda s, d, sa_: n.minimum(255, d * sa_ * 256.0 / (s + 1.0)), MJBLEND_MIN: lambda s, d, sa_: n.minimum(d * sa_, s), MJBLEND_MAX: lambda s, d, sa_: n.maximum(d * sa_, s), } out[rgb] = blendfuncs[blend_mode](src_pre, dst_pre, (1 - src_a)) out[rgb] /= out[alpha] n.seterr(**old_setting) out[alpha] *= 255 n.clip(out, 0, 255) # astype('uint8') maps np.nan (and np.inf) to 0 out = out.astype(dt) return out
Example 13
def addInitialRNNLayer(mainGraph, inputOperation=None, activation=TanhActivation, nHidden=100): """Add a RNN layer to input data Parameters ---------- mainGraph : ga.Graph computation graph to which append the dense layer inputOperation : ga.Operation operation feeding the data to the layer, must have a shape of (nExamples, seriesLength, nFeatures) activation : ga.SingleInputOperation [class] activatin operation for hidden units nHidden : int number of hidden units Returns ------- list(ga.Operation) List of activation operations from the RNN layer """ nExamples, seriesLength, nFeatures = inputOperation.shape h0 = generateZeroVariable(shape=(nExamples, nHidden), transpose=False) W = generateRandomVariable(shape=(nFeatures, nHidden), transpose=False, nInputs=nFeatures * seriesLength ** 3) U = generateRandomVariable(shape=(nHidden, nHidden), transpose=False, nInputs=nHidden * seriesLength ** 3) B = generateRandomVariable(shape=(1, nHidden), transpose=False, nInputs=nHidden * seriesLength ** 3) h0op = mainGraph.addOperation(h0) Wop = mainGraph.addOperation(W, doGradient=True) Uop = mainGraph.addOperation(U, doGradient=True) Bop = mainGraph.addOperation(B, doGradient=True) hactivations = [h0op] # ------ append activation gates for indexRNN in range(seriesLength): xSliceop = mainGraph.addOperation(SliceOperation(inputOperation, np.index_exp[:, indexRNN, :])) newHActiv = createRNNgate(mainGraph, xSliceop, hactivations[-1], Wop, Uop, Bop, activation) hactivations.append(newHActiv) return hactivations