Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between num_epochs and steps?

In tensorflow get started code:

import tensorflow as tf
import numpy as np

features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)
x = np.array([1., 2., 3., 4.])
y = np.array([0., -1., -2., -3.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x}, y, batch_size=4, num_epochs=1000)
estimator.fit(input_fn=input_fn, steps=1000)
estimator.evaluate(input_fn=input_fn)

I know what batch_size means, but what do num_epochs and steps mean respectively when there are only 4 training examples?

like image 684
iamabug Avatar asked Feb 04 '23 15:02

iamabug


2 Answers

An epoch means using the whole data you have.

A step means using a single batch data.

So, n_steps = Number of data in single epoch // batch_size.

According to https://www.tensorflow.org/api_docs/python/tf/contrib/learn/Trainable,

  • steps: Number of steps for which to train model. If None, train forever. 'steps' works incrementally. If you call two times fit(steps=10) then training occurs in total 20 steps. If you don't want to have incremental behaviour please set max_steps instead. If set, max_steps must be None.

  • batch_size: minibatch size to use on the input, defaults to first dimension of x. Must be None if input_fn is provided.

like image 89
J-min Avatar answered Feb 07 '23 18:02

J-min


num_epochs indicates how many times will the input_fn return the whole batch

and steps indicates how many times the function should run.

For the method of the object estimator here, it will stop either it run more than "steps" times or the input_fn ceases to provide data, as according to Tensorflow API:

For each step, calls input_fn, which returns one batch of data. Evaluates until: - steps batches are processed, or - input_fn raises an end-of-input exception (OutOfRangeError or StopIteration).

like image 22
William Avatar answered Feb 07 '23 19:02

William