Run batch prediciton on a TF graph.

    Args:
      element: list of strings, representing one batch input to the TF graph.
184952
def process(self, element):
    
    import collections
    import apache_beam as beam

    num_in_batch = 0
    try:
      assert self._session is not None

      feed_dict = collections.defaultdict(list)
      for line in element:

        # Remove trailing newline.
        if line.endswith('\n'):
          line = line[:-1]

        feed_dict[self._input_alias_map.values()[0]].append(line)
        num_in_batch += 1

      # batch_result is list of numpy arrays with batch_size many rows.
      batch_result = self._session.run(fetches=self._tensor_names,
                                       feed_dict=feed_dict)

      # ex batch_result for batch_size > 1:
      # (array([value1, value2, ..., value_batch_size]),
      #  array([[a1, b1, c1]], ..., [a_batch_size, b_batch_size, c_batch_size]]),
      #  ...)
      # ex batch_result for batch_size == 1:
      # (value,
      #  array([a1, b1, c1]),
      #  ...)

      # Convert the results into a dict and unbatch the results.
      if num_in_batch > 1:
        for result in zip(*batch_result):
          predictions = {}
          for name, value in zip(self._aliases, result):
            predictions[name] = (value.tolist() if getattr(value, 'tolist', None) else value)
          yield predictions
      else:
        predictions = {}
        for i in range(len(self._aliases)):
          value = batch_result[i]
          value = (value.tolist() if getattr(value, 'tolist', None)
                   else value)
          predictions[self._aliases[i]] = value
        yield predictions

    except Exception as e:  # pylint: disable=broad-except
      yield beam.pvalue.TaggedOutput('errors', (str(e), element))
170549
def window_iterator(data, width):
    
    start = 0
    while start < len(data):
        yield data[start:start+width]
        start += width
715465
def _ensure_element(tup, elem):
    
    try:
        return tup, tup.index(elem)
    except ValueError:
        return tuple(chain(tup, (elem,))), len(tup)
513666
def append(self, element):
        
        if is_valid_type(element, Element):
            self._class_collection_map[element.__class__].setdefault(element.id, element)
1234098
def batch_predict(dataset, model_dir, output_csv=None, output_bq_table=None, cloud=None):
  
  with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    job = batch_predict_async(dataset, model_dir, output_csv, output_bq_table, cloud)
    job.wait()
    print(job.state)
1374912
def pool(data, batch_size, key, batch_size_fn=lambda new, count, sofar: count,
         random_shuffler=None, shuffle=False, sort_within_batch=False):
    
    if random_shuffler is None:
        random_shuffler = random.shuffle
    for p in batch(data, batch_size * 100, batch_size_fn):
        p_batch = batch(sorted(p, key=key), batch_size, batch_size_fn) \
            if sort_within_batch \
            else batch(p, batch_size, batch_size_fn)
        if shuffle:
            for b in random_shuffler(list(p_batch)):
                yield b
        else:
            for b in list(p_batch):
                yield b
756669
def pairwise(iterable):
    
    iterator = iter(iterable)
    try:
        first = next(iterator)
    except StopIteration:
        return
    for element in iterator:
        yield first, element
        first = element
140045
def process_element(self, element, key, **params):
        
        self.p = param.ParamOverrides(self, params)
        return self._apply(element, key)
123084
def run_mnist():
  """"""
  mnist_classifier = tf.estimator.Estimator(
      model_fn=model_fn,
      model_dir=FLAGS.model_dir)

  # Set up training and evaluation input functions.
  def train_input_fn():
    """Prepare data for training."""

    # When choosing shuffle buffer sizes, larger sizes result in better
    # randomness, while smaller sizes use less memory. MNIST is a small
    # enough dataset that we can easily shuffle the full epoch.
    ds = dataset.train(FLAGS.data_dir)
    ds_batched = ds.cache().shuffle(buffer_size=50000).batch(FLAGS.batch_size)

    # Iterate through the dataset a set number (`epochs_between_evals`) of times
    # during each training session.
    ds = ds_batched.repeat(FLAGS.epochs_between_evals)
    return ds

  def eval_input_fn():
    return dataset.test(FLAGS.data_dir).batch(
        FLAGS.batch_size).make_one_shot_iterator().get_next()

  # Train and evaluate model.
  for _ in range(FLAGS.train_epochs // FLAGS.epochs_between_evals):
    mnist_classifier.train(input_fn=train_input_fn, hooks=None)
    eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn)
    print("\nEvaluation results:\n\t%s\n" % eval_results)
827806
def startElement(self, name, attrs):
        """"""

        self._contextStack.append(self._context)
        self._contentList = []

        if name in self._statusDict:
            self._itemTag, itemType = self._statusDict[name]
            self._progress.startItem(itemType)
        elif name == self._itemTag:
            self._error = False
            self._progress.newItem()

        try:
            if self._context == 'root':
                if name == 'xtvd':
                    self._context = 'xtvd'
                    self._startXTVDNode(name, attrs)
            elif self._context == 'xtvd':
                self._context = name
            elif self._context == 'stations':
                self._startStationsNode(name, attrs)
            elif self._context == 'lineups':
                self._startLineupsNode(name, attrs)
            elif self._context == 'schedules':
                self._startSchedulesNode(name, attrs)
            elif self._context == 'programs':
                self._startProgramsNode(name, attrs)
            elif self._context == 'productionCrew':
                self._startProductionCrewNode(name, attrs)
            elif self._context == 'genres':
                self._startGenresNode(name, attrs)
        except Exception, e:
            self._error = True
            self._progress.printMsg(str(e), error=True)
728983
def load_elements(self, filename):
        
        input_data = load_b26_file(filename)
        if isinstance(input_data, dict) and self.elements_type in input_data:
            return input_data[self.elements_type]
        else:
            return {}
905102
public int addElementAtPosition(int prevNode, int nextNode, int element,
			boolean bUnique, boolean bCallCompare, int treap) {
		int treap_ = treap;
		if (treap_ == -1) {
			if (m_defaultTreap == nullNode())
				m_defaultTreap = createTreap(-1);
			treap_ = m_defaultTreap;
		}

		// dbg_check_(m_root);
		if (getRoot_(treap_) == nullNode()) {
			assert (nextNode == nullNode() && prevNode == nullNode());
			int root = newNode_(element);
			setRoot_(root, treap_);
			addToList_(-1, root, treap_);
			return root;
		}

		int cmpNext;
		int cmpPrev;
		if (bCallCompare) {
			cmpNext = nextNode != nullNode() ? m_comparator.compare(this,
					element, nextNode) : -1;
			assert (cmpNext <= 0);
			cmpPrev = prevNode != nullNode() ? m_comparator.compare(this,
					element, prevNode) : 1;
			// cmpPrev can be negative in plane sweep when intersection is
			// detected.
		} else {
			cmpNext = -1;
			cmpPrev = 1;
		}

		if (bUnique && (cmpNext == 0 || cmpPrev == 0)) {
			m_comparator.onAddUniqueElementFailedImpl_(element);
			int cur = cmpNext == 0 ? nextNode : prevNode;
			setDuplicateElement_(cur, treap_);
			return -1;// return negative value.
		}

		int cur;
		int cmp;
		boolean bNext;
		if (nextNode != nullNode() && prevNode != nullNode()) {
			// randomize the the cost to insert a node.
			bNext = m_random > NumberUtils.nextRand(m_random) >> 1;
		} else
			bNext = nextNode != nullNode();

		if (bNext) {
			cmp = cmpNext;
			cur = nextNode;
		} else {
			cmp = cmpPrev;
			cur = prevNode;
		}

		int newNode = -1;
		int before = -1;
		boolean b_first = true;
		for (;;) {
			if (cmp < 0) {
				int left = getLeft(cur);
				if (left != nullNode())
					cur = left;
				else {
					before = cur;
					newNode = newNode_(element);
					setLeft_(cur, newNode);
					setParent_(newNode, cur);
					break;
				}
			} else {
				int right = getRight(cur);
				if (right != nullNode())
					cur = right;
				else {
					before = getNext(cur);
					newNode = newNode_(element);
					setRight_(cur, newNode);
					setParent_(newNode, cur);
					break;
				}
			}

			if (b_first) {
				cmp *= -1;
				b_first = false;
			}
		}

		bubbleUp_(newNode);
		if (getParent(newNode) == nullNode())
			setRoot_(newNode, treap_);

		addToList_(before, newNode, treap_);
		// dbg_check_(m_root);
		return newNode;
	}
1188916
def _compile_batch_fluents(self,
            fluents: List[Tuple[str, TensorFluent]],
            batch_size: int) -> Sequence[tf.Tensor]:
        '''
        '''
        batch_fluents = []
        with self.graph.as_default():
            for name, fluent in fluents:
                name_scope = utils.identifier(name)
                with tf.name_scope(name_scope):
                    t = tf.stack([fluent.tensor] * batch_size)
                batch_fluents.append(t)
        return tuple(batch_fluents)
1035823
def _example_generator(filename):
  
  for idx, line in enumerate(tf.gfile.Open(filename, "rb")):
    if idx == 0: continue  # skip header
    line = text_encoder.to_unicode_utf8(line.strip())
    split_line = line.split("\t")
    # Works for both splits even though dev has some extra human labels.
    yield {
        "premise": split_line[8],
        "hypothesis": split_line[9],
        "label": split_line[-1]
    }
518611
def generate(self, x, **kwargs):
    
    assert self.sess is not None, \
        'Cannot use `generate` when no `sess` was provided'
    self.parse_params(**kwargs)

    if self.y_target is None:
      self.y_target, nb_classes = self.get_or_guess_labels(x, kwargs)
      self.targeted_attack = False
    else:
      _, nb_classes = self.get_or_guess_labels(x, kwargs)
      self.targeted_attack = True

    attack = LBFGS_impl(
        self.sess, x, self.model.get_logits(x),
        self.y_target, self.targeted_attack,
        self.binary_search_steps, self.max_iterations, self.initial_const,
        self.clip_min, self.clip_max, nb_classes, self.batch_size)

    def lbfgs_wrap(x_val, y_val):
      """
      Wrapper creating TensorFlow interface for use with py_func
      """
      return np.array(attack.attack(x_val, y_val), dtype=self.np_dtype)

    wrap = tf.py_func(lbfgs_wrap, [x, self.y_target], self.tf_dtype)
    wrap.set_shape(x.get_shape())

    return wrap
675009
public void previsit(Collection<? extends IRenderingElement> elements) {
        Deque<IRenderingElement> queue = new ArrayDeque<>(2 * elements.size());
        queue.addAll(elements);

        FreqMap<Color> strokeFreq = new FreqMap<>();
        FreqMap<Color> fillFreq = new FreqMap<>();
        FreqMap<Double> strokeWidthFreq = new FreqMap<>();

        while (!queue.isEmpty()) {
            IRenderingElement element = queue.poll();
            // wrappers first
            if (element instanceof Bounds) {
                queue.add(((Bounds) element).root());
            } else if (element instanceof MarkedElement) {
                queue.add(((MarkedElement) element).element());
            } else if (element instanceof ElementGroup) {
                for (IRenderingElement child : (ElementGroup) element)
                    queue.add(child);
            } else if (element instanceof LineElement) {
                strokeFreq.add(((LineElement) element).color);
                strokeWidthFreq.add(scaled(((LineElement) element).width));
            } else if (element instanceof GeneralPath) {
                if (((GeneralPath) element).fill)
                    fillFreq.add(((GeneralPath) element).color);
            } else {
                // ignored
            }
        }

        if (!defaultsWritten) {
            defaultFill = fillFreq.getMostFrequent();
            defaultStroke = strokeFreq.getMostFrequent();
            Double strokeWidth = strokeWidthFreq.getMostFrequent();
            if (strokeWidth != null)
                defaultStrokeWidth = toStr(strokeWidth);
        }
    }
662426
function(_element){
                var _id = _element.id;
                if (!!_dataset[_id]) return;
                var _map = {};
                _u._$forEach(
                    _element.attributes,
                    function(_node){
                        var _key  = _node.nodeName;
                        if (_key.indexOf(_tag)!=0) return;
                        _key = _key.replace(_tag,'')
                                   .replace(_reg,function($1,$2){
                                        return $2.toUpperCase();
                                   });
                        _map[_key] = _node.nodeValue||'';
                    }
                );
                _dataset[_id] = _map;
            }