Helper: build tf.Example from (string -> int/float/str list) dictionary.
427622
def to_example(dictionary):
  """"""
  features = {}
  for (k, v) in six.iteritems(dictionary):
    if not v:
      raise ValueError("Empty generated field: %s" % str((k, v)))
    if isinstance(v[0], six.integer_types):
      features[k] = tf.train.Feature(int64_list=tf.train.Int64List(value=v))
    elif isinstance(v[0], float):
      features[k] = tf.train.Feature(float_list=tf.train.FloatList(value=v))
    elif isinstance(v[0], six.string_types):
      if not six.PY2:  # Convert in python 3.
        v = [bytes(x, "utf-8") for x in v]
      features[k] = tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
    elif isinstance(v[0], bytes):
      features[k] = tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
    else:
      raise ValueError("Value for %s is not a recognized type; v: %s type: %s" %
                       (k, str(v[0]), str(type(v[0]))))
  return tf.train.Example(features=tf.train.Features(feature=features))
1023267
def constant(cls,
            value: Value,
            dtype: tf.DType = tf.float32) -> 'TensorFluent':
        '''
        '''
        t = tf.constant(value, dtype=dtype)
        scope = [] # type: List
        batch = False
        return TensorFluent(t, scope, batch=batch)
447011
def _from_dict(cls, _dict):
        """"""
        args = {}
        if 'examples' in _dict:
            args['examples'] = [
                TrainingExample._from_dict(x) for x in (_dict.get('examples'))
            ]
        return cls(**args)
522142
def create_input(self, t_input=None, forget_xy_shape=True):
    """"""
    if t_input is None:
      t_input = tf.placeholder(tf.float32, self.image_shape)
    t_prep_input = t_input
    if len(t_prep_input.shape) == 3:
      t_prep_input = tf.expand_dims(t_prep_input, 0)
    if forget_xy_shape:
      t_prep_input = model_util.forget_xy(t_prep_input)
    if hasattr(self, "is_BGR") and self.is_BGR is True:
      t_prep_input = tf.reverse(t_prep_input, [-1])
    lo, hi = self.image_value_range
    t_prep_input = lo + t_prep_input * (hi - lo)
    return t_input, t_prep_input
318815
def fromTuple(self, locationTuple):
        
        for key, value in locationTuple:
            try:
                self[key] = float(value)
            except TypeError:
                self[key] = tuple([float(v) for v in value])
543492
def _parse_example(serialized_example):
  """"""
  data_fields = {
      "inputs": tf.VarLenFeature(tf.int64),
      "targets": tf.VarLenFeature(tf.int64)
  }
  parsed = tf.parse_single_example(serialized_example, data_fields)
  inputs = tf.sparse_tensor_to_dense(parsed["inputs"])
  targets = tf.sparse_tensor_to_dense(parsed["targets"])
  return inputs, targets
582374
def encode_example(self, example_data):
    """"""
    np_dtype = np.dtype(self._dtype.as_numpy_dtype)
    # Convert to numpy if possible
    if not isinstance(example_data, np.ndarray):
      example_data = np.array(example_data, dtype=np_dtype)
    # Ensure the shape and dtype match
    if example_data.dtype != np_dtype:
      raise ValueError('Dtype {} do not match {}'.format(
          example_data.dtype, np_dtype))
    utils.assert_shape_match(example_data.shape, self._shape)
    # For booleans, convert to integer (tf.train.Example does not support bool)
    if example_data.dtype == np.bool_:
      example_data = example_data.astype(int)
    return example_data
1003763
def validate_examples(example_file):
    

    def test_example(raw):
        example = tf.train.Example()
        example.ParseFromString(raw)

        pi = np.frombuffer(example.features.feature['pi'].bytes_list.value[0], np.float32)
        value = example.features.feature['outcome'].float_list.value[0]
        assert abs(pi.sum() - 1) < 1e-4, pi.sum()
        assert value in (-1, 1), value

    opts = tf.python_io.TFRecordOptions(tf.python_io.TFRecordCompressionType.ZLIB)
    for record in tqdm(tf.python_io.tf_record_iterator(example_file, opts)):
        test_example(record)
663945
def FloatProperty(name, default=0.0, readonly=False, docs=None):
    '''
    
    '''

    private_name = '_' + name

    def getf(self):
        if not hasattr(self, private_name):
            setattr(self, private_name, default)
        return getattr(self, private_name)

    if readonly:
        setf = None
    else:
        def setf(self, newValue):
            def epsilon_set(v):
                # epsilon_set: creates a float from v unless that
                #              float is less than epsilon, which will
                #              be considered effectively zero.
                fv = float(v)
                return 0.0 if nearly_zero(fv) else fv

            try:
                setattr(self, private_name, epsilon_set(newValue))
                return
            except TypeError:
                pass

            if isinstance(newValue, collections.Mapping):
                try:
                    setattr(self, private_name, epsilon_set(newValue[name]))
                except KeyError:
                    pass
                return

            if isinstance(newValue, collections.Iterable):
                try:
                    setattr(self, private_name, epsilon_set(newValue[0]))
                    return
                except (IndexError, TypeError):
                    pass

            try:
                mapping = vars(newValue)
                setattr(self, private_name, epsilon_set(mapping[name]))
                return
            except (TypeError, KeyError):
                pass

            if newValue is None:
                setattr(self, private_name, epsilon_set(default))
                return

            raise ValueError(newValue)

    return property(getf, setf, None, docs)
1370818
def _get_example_length(example):
  """"""
  length = tf.maximum(tf.shape(example[0])[0], tf.shape(example[1])[0])
  return length
1296745
def _float_generator(descriptor, bitwidth):
    ''
    return gen.IterValueGenerator(descriptor.name, values.get_floats(bitwidth))
1141610
def cast(self, dtype: tf.DType) -> 'TensorFluent':
        '''
        '''
        if self.dtype == dtype:
            return self
        t = tf.cast(self.tensor, dtype)
        scope = self.scope.as_list()
        batch = self.batch
        return TensorFluent(t, scope, batch=batch)
896037
def dictlist_convert_to_float(dict_list: Iterable[Dict], key: str) -> None:
    
    for d in dict_list:
        try:
            d[key] = float(d[key])
        except ValueError:
            d[key] = None
1106894
def get_shape_str(tensors):
    
    if isinstance(tensors, (list, tuple)):
        for v in tensors:
            assert isinstance(v, (tf.Tensor, tf.Variable)), "Not a tensor: {}".format(type(v))
        shape_str = ",".join(
            map(lambda x: str(x.get_shape().as_list()), tensors))
    else:
        assert isinstance(tensors, (tf.Tensor, tf.Variable)), "Not a tensor: {}".format(type(tensors))
        shape_str = str(tensors.get_shape().as_list())
    return shape_str
1010522
def _item_to_tf_feature(item, key_name):
  """"""
  v = item
  if isinstance(v, (list, tuple)) and not v:
    raise ValueError(
        "Feature {} received an empty list value, so is unable to infer the "
        "feature type to record. To support empty value, the corresponding "
        "FeatureConnector should return a numpy array with the correct dtype "
        "instead of a Python list.".format(key_name)
    )

  # Handle strings/bytes first
  if isinstance(v, (six.binary_type, six.string_types)):
    v = [tf.compat.as_bytes(v)]
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
  elif (isinstance(v, (tuple, list)) and
        all(isinstance(x, (six.binary_type, six.string_types)) for x in v)):
    v = [tf.compat.as_bytes(x) for x in v]
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
  elif (isinstance(v, np.ndarray) and
        (v.dtype.kind in ("U", "S") or v.dtype == object)):  # binary or unicode
    v = [tf.compat.as_bytes(x) for x in v.flatten()]
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=v))

  # Use NumPy for numeric types
  v = np.array(v).flatten()  # Convert v into a 1-d array

  if np.issubdtype(v.dtype, np.integer):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=v))
  elif np.issubdtype(v.dtype, np.floating):
    return tf.train.Feature(float_list=tf.train.FloatList(value=v))
  else:
    raise ValueError(
        "Value received: {}.\n"
        "tf.train.Feature does not support type {} for feature key {}. "
        "This may indicate that one of the FeatureConnectors received an "
        "unsupported value as input.".format(repr(v), repr(type(v)), key_name)
    )
399560
def _parse_example_spec(self):
    """"""
    height, width = image_util.get_expected_image_size(self.module_spec)
    input_shape = [height, width, 3]
    return {self.key: tf_v1.FixedLenFeature(input_shape, tf.float32)}
997343
public org.tensorflow.framework.CollectionDef.FloatListOrBuilder getFloatListOrBuilder() {
    if (kindCase_ == 4) {
       return (org.tensorflow.framework.CollectionDef.FloatList) kind_;
    }
    return org.tensorflow.framework.CollectionDef.FloatList.getDefaultInstance();
  }