Python tensorflow.Placeholder() Examples

The following are 14 code examples of tensorflow.Placeholder(). 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 tensorflow , or try the search function .
Example #1
Source File: eranlayers.py    From eran with Apache License 2.0 6 votes vote down vote up
def eran_input(shape, name=None):
    """
    adds a tf.Placeholder to the graph. The shape will be augmented with None at the beginning as batch size
    
    Arguments
    ---------
    shape : list or tuple
        the shape of the Placeholder, has 1 to 3 entries
    name : str
        optional name for the Placeholder operation  
    
    Return
    ------
    output : tf.Tensor
        tensor associated with the Placeholder operation
    """
    assert len(shape) < 4, "shape should have less than 4 entries (batch size is taken care of)"
    batch_shape = [None]
    for s in shape:
        batch_shape.append(s)
    
    return tf.placeholder(tf.float64, batch_shape, name=name) 
Example #2
Source File: inference_demo.py    From Gun-Detector with Apache License 2.0 6 votes vote down vote up
def export(sess, input_pl, output_tensor, input_file_pattern, output_dir):
  """Exports inference outputs to an output directory.

  Args:
    sess: tf.Session with variables already loaded.
    input_pl: tf.Placeholder for input (HWC format).
    output_tensor: Tensor for generated outut images.
    input_file_pattern: Glob file pattern for input images.
    output_dir: Output directory.
  """
  if output_dir:
    _make_dir_if_not_exists(output_dir)

  if input_file_pattern:
    for file_path in tf.gfile.Glob(input_file_pattern):
      # Grab a single image and run it through inference
      input_np = np.asarray(PIL.Image.open(file_path))
      output_np = sess.run(output_tensor, feed_dict={input_pl: input_np})
      image_np = data_provider.undo_normalize_image(output_np)
      output_path = _file_output_path(output_dir, file_path)
      PIL.Image.fromarray(image_np).save(output_path) 
Example #3
Source File: inference_demo.py    From g-tensorflow-models with Apache License 2.0 6 votes vote down vote up
def export(sess, input_pl, output_tensor, input_file_pattern, output_dir):
  """Exports inference outputs to an output directory.

  Args:
    sess: tf.Session with variables already loaded.
    input_pl: tf.Placeholder for input (HWC format).
    output_tensor: Tensor for generated outut images.
    input_file_pattern: Glob file pattern for input images.
    output_dir: Output directory.
  """
  if output_dir:
    _make_dir_if_not_exists(output_dir)

  if input_file_pattern:
    for file_path in tf.gfile.Glob(input_file_pattern):
      # Grab a single image and run it through inference
      input_np = np.asarray(PIL.Image.open(file_path))
      output_np = sess.run(output_tensor, feed_dict={input_pl: input_np})
      image_np = data_provider.undo_normalize_image(output_np)
      output_path = _file_output_path(output_dir, file_path)
      PIL.Image.fromarray(image_np).save(output_path) 
Example #4
Source File: inference_demo.py    From multilabel-image-classification-tensorflow with MIT License 6 votes vote down vote up
def export(sess, input_pl, output_tensor, input_file_pattern, output_dir):
  """Exports inference outputs to an output directory.

  Args:
    sess: tf.Session with variables already loaded.
    input_pl: tf.Placeholder for input (HWC format).
    output_tensor: Tensor for generated outut images.
    input_file_pattern: Glob file pattern for input images.
    output_dir: Output directory.
  """
  if output_dir:
    _make_dir_if_not_exists(output_dir)

  if input_file_pattern:
    for file_path in tf.gfile.Glob(input_file_pattern):
      # Grab a single image and run it through inference
      input_np = np.asarray(PIL.Image.open(file_path))
      output_np = sess.run(output_tensor, feed_dict={input_pl: input_np})
      image_np = data_provider.undo_normalize_image(output_np)
      output_path = _file_output_path(output_dir, file_path)
      PIL.Image.fromarray(image_np).save(output_path) 
Example #5
Source File: input_ops.py    From exoplanet-ml with Apache License 2.0 5 votes vote down vote up
def build_feature_placeholders(config):
  """Builds tf.Placeholder ops for feeding model features and labels.

  Args:
    config: ConfigDict containing the feature configurations.

  Returns:
    features: A dictionary containing "time_series_features" and "aux_features",
        each of which is a dictionary of tf.Placeholders of features from the
        input configuration. All features have dtype float32 and shape
        [batch_size, length].
  """
  batch_size = None  # Batch size will be dynamically specified.
  features = {"time_series_features": {}, "aux_features": {}}
  for feature_name, feature_spec in config.items():
    placeholder = tf.placeholder(
        dtype=tf.float32,
        shape=[batch_size, feature_spec.length],
        name=feature_name)

    if feature_spec.is_time_series:
      features["time_series_features"][feature_name] = placeholder
    else:
      features["aux_features"][feature_name] = placeholder

  return features 
Example #6
Source File: input_ops.py    From exoplanet-ml with Apache License 2.0 5 votes vote down vote up
def build_labels_placeholder():
  """Builds a tf.Placeholder op for feeding model labels.

  Returns:
    labels: An int64 tf.Placeholder with shape [batch_size].
  """
  batch_size = None  # Batch size will be dynamically specified.
  return tf.placeholder(dtype=tf.int64, shape=[batch_size], name="labels") 
Example #7
Source File: input_ops.py    From Gun-Detector with Apache License 2.0 5 votes vote down vote up
def build_feature_placeholders(config):
  """Builds tf.Placeholder ops for feeding model features and labels.

  Args:
    config: ConfigDict containing the feature configurations.

  Returns:
    features: A dictionary containing "time_series_features" and "aux_features",
        each of which is a dictionary of tf.Placeholders of features from the
        input configuration. All features have dtype float32 and shape
        [batch_size, length].
  """
  batch_size = None  # Batch size will be dynamically specified.
  features = {"time_series_features": {}, "aux_features": {}}
  for feature_name, feature_spec in config.items():
    placeholder = tf.placeholder(
        dtype=tf.float32,
        shape=[batch_size, feature_spec.length],
        name=feature_name)

    if feature_spec.is_time_series:
      features["time_series_features"][feature_name] = placeholder
    else:
      features["aux_features"][feature_name] = placeholder

  return features 
Example #8
Source File: input_ops.py    From Gun-Detector with Apache License 2.0 5 votes vote down vote up
def build_labels_placeholder():
  """Builds a tf.Placeholder op for feeding model labels.

  Returns:
    labels: An int64 tf.Placeholder with shape [batch_size].
  """
  batch_size = None  # Batch size will be dynamically specified.
  return tf.placeholder(dtype=tf.int64, shape=[batch_size], name="labels") 
Example #9
Source File: network_helpers.py    From AlphaToe with MIT License 5 votes vote down vote up
def get_deterministic_network_move(session, input_layer, output_layer, board_state, side, valid_only=False,
                                   game_spec=None):
    """Choose a move for the given board_state using a deterministic policy. A move is selected using the values from
    the output_layer and selecting the move with the highest score.

    Args:
        session (tf.Session): Session used to run this network
        input_layer (tf.Placeholder): Placeholder to the network used to feed in the board_state
        output_layer (tf.Tensor): Tensor that will output the probabilities of the moves, we expect this to be of
            dimesensions (None, board_squares).
        board_state: The board_state we want to get the move for.
        side: The side that is making the move.

    Returns:
        (np.array) It's shape is (board_squares), and it is a 1 hot encoding for the move the network has chosen.
    """
    np_board_state = np.array(board_state)
    np_board_state = np_board_state.reshape(1, *input_layer.get_shape().as_list()[1:])
    if side == -1:
        np_board_state = -np_board_state

    probability_of_actions = session.run(output_layer,
                                         feed_dict={input_layer: np_board_state})[0]

    if valid_only:
        available_moves = game_spec.available_moves(board_state)
        available_moves_flat = [game_spec.tuple_move_to_flat(x) for x in available_moves]
        for i in range(game_spec.board_squares()):
            if i not in available_moves_flat:
                probability_of_actions[i] = 0

    move = np.argmax(probability_of_actions)
    one_hot = np.zeros(len(probability_of_actions))
    one_hot[move] = 1.
    return one_hot 
Example #10
Source File: layers.py    From graph_level_drug_discovery with Apache License 2.0 5 votes vote down vote up
def graph_gather(atoms, membership_placeholder, batch_size):
  """
  Parameters
  ----------
  atoms: tf.Tensor
    Of shape (n_atoms, n_feat)
  membership_placeholder: tf.Placeholder
    Of shape (n_atoms,). Molecule each atom belongs to.
  batch_size: int
    Batch size for deep model.

  Returns
  -------
  tf.Tensor
    Of shape (batch_size, n_feat)
  """

  # WARNING: Does not work for Batch Size 1! If batch_size = 1, then use reduce_sum!
  assert batch_size > 1, "graph_gather requires batches larger than 1"

  # Obtain the partitions for each of the molecules
  activated_par = tf.dynamic_partition(atoms, membership_placeholder,
                                       batch_size)

  # Sum over atoms for each molecule
  sparse_reps = [
      tf.reduce_sum(activated, 0, keep_dims=True) for activated in activated_par
  ]

  # Get the final sparse representations
  sparse_reps = tf.concat(axis=0, values=sparse_reps)

  return sparse_reps 
Example #11
Source File: layers_copy.py    From graph_level_drug_discovery with Apache License 2.0 5 votes vote down vote up
def graph_gather(atoms, membership_placeholder, batch_size):
  """
  Parameters
  ----------
  atoms: tf.Tensor
    Of shape (n_atoms, n_feat)
  membership_placeholder: tf.Placeholder
    Of shape (n_atoms,). Molecule each atom belongs to.
  batch_size: int
    Batch size for deep model.

  Returns
  -------
  tf.Tensor
    Of shape (batch_size, n_feat)
  """

  # WARNING: Does not work for Batch Size 1! If batch_size = 1, then use reduce_sum!
  assert batch_size > 1, "graph_gather requires batches larger than 1"

  # Obtain the partitions for each of the molecules
  activated_par = tf.dynamic_partition(atoms, membership_placeholder,
                                       batch_size)

  # Sum over atoms for each molecule
  sparse_reps = [
      tf.reduce_sum(activated, 0, keep_dims=True) for activated in activated_par
  ]

  # Get the final sparse representations
  sparse_reps = tf.concat(axis=0, values=sparse_reps)

  return sparse_reps 
Example #12
Source File: input_ops.py    From multilabel-image-classification-tensorflow with MIT License 5 votes vote down vote up
def build_feature_placeholders(config):
  """Builds tf.Placeholder ops for feeding model features and labels.

  Args:
    config: ConfigDict containing the feature configurations.

  Returns:
    features: A dictionary containing "time_series_features" and "aux_features",
        each of which is a dictionary of tf.Placeholders of features from the
        input configuration. All features have dtype float32 and shape
        [batch_size, length].
  """
  batch_size = None  # Batch size will be dynamically specified.
  features = {"time_series_features": {}, "aux_features": {}}
  for feature_name, feature_spec in config.items():
    placeholder = tf.placeholder(
        dtype=tf.float32,
        shape=[batch_size, feature_spec.length],
        name=feature_name)

    if feature_spec.is_time_series:
      features["time_series_features"][feature_name] = placeholder
    else:
      features["aux_features"][feature_name] = placeholder

  return features 
Example #13
Source File: input_ops.py    From multilabel-image-classification-tensorflow with MIT License 5 votes vote down vote up
def build_labels_placeholder():
  """Builds a tf.Placeholder op for feeding model labels.

  Returns:
    labels: An int64 tf.Placeholder with shape [batch_size].
  """
  batch_size = None  # Batch size will be dynamically specified.
  return tf.placeholder(dtype=tf.int64, shape=[batch_size], name="labels") 
Example #14
Source File: network_helpers.py    From AlphaToe with MIT License 4 votes vote down vote up
def get_stochastic_network_move(session, input_layer, output_layer, board_state, side,
                                valid_only=False, game_spec=None):
    """Choose a move for the given board_state using a stocastic policy. A move is selected using the values from the
     output_layer as a categorical probability distribution to select a single move

    Args:
        session (tf.Session): Session used to run this network
        input_layer (tf.Placeholder): Placeholder to the network used to feed in the board_state
        output_layer (tf.Tensor): Tensor that will output the probabilities of the moves, we expect this to be of
            dimesensions (None, board_squares) and the sum of values across the board_squares to be 1.
        board_state: The board_state we want to get the move for.
        side: The side that is making the move.

    Returns:
        (np.array) It's shape is (board_squares), and it is a 1 hot encoding for the move the network has chosen.
    """
    np_board_state = np.array(board_state)
    if side == -1:
        np_board_state = -np_board_state

    np_board_state = np_board_state.reshape(1, *input_layer.get_shape().as_list()[1:])
    probability_of_actions = session.run(output_layer,
                                         feed_dict={input_layer: np_board_state})[0]

    if valid_only:
        available_moves = list(game_spec.available_moves(board_state))
        if len(available_moves) == 1:
            move = np.zeros(game_spec.board_squares())
            np.put(move, game_spec.tuple_move_to_flat(available_moves[0]), 1)
            return move
        available_moves_flat = [game_spec.tuple_move_to_flat(x) for x in available_moves]
        for i in range(game_spec.board_squares()):
            if i not in available_moves_flat:
                probability_of_actions[i] = 0.

        prob_mag = sum(probability_of_actions)
        if prob_mag != 0.:
            probability_of_actions /= sum(probability_of_actions)

    try:
        move = np.random.multinomial(1, probability_of_actions)
    except ValueError:
        # sometimes because of rounding errors we end up with probability_of_actions summing to greater than 1.
        # so need to reduce slightly to be a valid value
        move = np.random.multinomial(1, probability_of_actions / (1. + 1e-6))

    return move