简体   繁体   中英

Tensorflow Iterator next value

I was trying the follow exercise on Machine Learning Crash Course, https://developers.google.com/machine-learning/crash-course/introduction-to-neural-networks/programming-exercise , and had an doubt about the Tensorfow library.

Opening the colab source, there is the folowing code:

def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):
    """Trains a neural net regression model.

    Args:
      features: pandas DataFrame of features
      targets: pandas DataFrame of targets
      batch_size: Size of batches to be passed to the model
      shuffle: True or False. Whether to shuffle the data.
      num_epochs: Number of epochs for which data should be repeated. None = repeat indefinitely
    Returns:
      Tuple of (features, labels) for next data batch
    """

    # Convert pandas data into a dict of np arrays.
    features = {key:np.array(value) for key,value in dict(features).items()}                                             

    # Construct a dataset, and configure batching/repeating.
    ds = Dataset.from_tensor_slices((features,targets)) # warning: 2GB limit
    ds = ds.batch(batch_size).repeat(num_epochs)

    # Shuffle the data, if specified.
    if shuffle:
      ds = ds.shuffle(10000)

    # Return the next batch of data.
    features, labels = ds.make_one_shot_iterator().get_next()
    return features, labels

I'm calling this function with:

my_input_fn(training_examples, training_targets["median_house_value"], batch_size=10)

And getting this result:

({'households': <tf.Tensor 'IteratorGetNext_14:0' shape=(?,) dtype=float64>,
  'housing_median_age': <tf.Tensor 'IteratorGetNext_14:1' shape=(?,) dtype=float64>,
  'latitude': <tf.Tensor 'IteratorGetNext_14:2' shape=(?,) dtype=float64>,
  'longitude': <tf.Tensor 'IteratorGetNext_14:3' shape=(?,) dtype=float64>,
  'median_income': <tf.Tensor 'IteratorGetNext_14:4' shape=(?,) dtype=float64>,
  'population': <tf.Tensor 'IteratorGetNext_14:5' shape=(?,) dtype=float64>,
  'rooms_per_person': <tf.Tensor 'IteratorGetNext_14:6' shape=(?,) dtype=float64>,
  'total_bedrooms': <tf.Tensor 'IteratorGetNext_14:7' shape=(?,) dtype=float64>,
  'total_rooms': <tf.Tensor 'IteratorGetNext_14:8' shape=(?,) dtype=float64>},
 <tf.Tensor 'IteratorGetNext_14:9' shape=(?,) dtype=float64>)

I noted that each time I run this, the IteratorGetNext is incremented. But the question is, how it knows the next number if everytime I call this function a new Dataset is created?

in general case you should call this function only once. After you call my_input_fn , some nodes with corresponding names will be added to current default graph. After you call this function second time, the standard name resolution algorithm will figure out that there already are nodes with such names, so names will be extended with suffix _{idx}.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM