(batch_size, learning_rate, num_training_iters, validation_steps)
Runs the training.
Runs the training.
Runs the training .
def train(batch_size, learning_rate, num_training_iters, validation_steps):
  """Runs the training."""
  print('Loading data')
  training_data, validation_data, test_data = dataset.load()
  print('Loaded dataset with {} training, {} validation and {} test examples.'.
        format(
            len(training_data[0]), len(validation_data[0]), len(test_data[0])))

  assert len(training_data[0]) % batch_size == 0
  assert len(validation_data[0]) % batch_size == 0
  assert len(test_data[0]) % batch_size == 0

  def build_iterator(data, infinite=True):
    """Build the iterator for inputs."""
    index = 0
    size = len(data[0])
    while True:
      if index + batch_size > size:
        if infinite:
          index = 0
        else:
          return
      yield data[0][index:index + batch_size], data[1][index:index + batch_size]
      index += batch_size

  train_iter = build_iterator(training_data)
  model = model_lib.Model([30])

  for i in range(num_training_iters):
    train_x, train_y = next(train_iter)
    model.train(train_x, train_y, learning_rate)
    if (i + 1) % validation_steps == 0:
      validation_iter = build_iterator(validation_data, infinite=False)
      correct_predictions = 0
      for valid_x, valid_y in validation_iter:
        correct_predictions += model.evaluate(valid_x, valid_y)
      print('{}/{} correct validation predictions.'.format(
          correct_predictions, len(validation_data[0])))
def train ( batch_size , learning_rate , num_training_iters , validation_steps ) : print ( 'Loading data' ) training_data , validation_data , test_data = dataset . load ( ) print ( 'Loaded dataset with {} training, {} validation and {} test examples.' . format ( len ( training_data [ 0 ] ) , len ( validation_data [ 0 ] ) , len ( test_data [ 0 ] ) ) ) assert len ( training_data [ 0 ] ) % batch_size == 0 assert len ( validation_data [ 0 ] ) % batch_size == 0 assert len ( test_data [ 0 ] ) % batch_size == 0 def build_iterator ( data , infinite = True ) : """Build the iterator for inputs.""" index = 0 size = len ( data [ 0 ] ) while True : if index + batch_size > size : if infinite : index = 0 else : return yield data [ 0 ] [ index : index + batch_size ] , data [ 1 ] [ index : index + batch_size ] index += batch_size train_iter = build_iterator ( training_data ) model = model_lib . Model ( [ 30 ] ) for i in range ( num_training_iters ) : train_x , train_y = next ( train_iter ) model . train ( train_x , train_y , learning_rate ) if ( i + 1 ) % validation_steps == 0 : validation_iter = build_iterator ( validation_data , infinite = False ) correct_predictions = 0 for valid_x , valid_y in validation_iter : correct_predictions += model . evaluate ( valid_x , valid_y ) print ( '{}/{} correct validation predictions.' . format ( correct_predictions , len ( validation_data [ 0 ] ) ) )