def train(sess, loss, x_train, y_train, init_all=False, evaluate=None, feed=None, args=None, rng=None, var_list=None, fprop_args=None, optimizer=None, devices=None, x_batch_preprocessor=None, use_ema=False, ema_decay=.998, run_canary=None, loss_threshold=1e5, dataset_train=None, dataset_size=None): """ Run (optionally multi-replica, synchronous) training to minimize `loss` :param sess: TF session to use when training the graph :param loss: tensor, the loss to minimize :param x_train: numpy array with training inputs or tf Dataset :param y_train: numpy array with training outputs or tf Dataset :param init_all: (boolean) If set to true, all TF variables in the session are (re)initialized, otherwise only previously uninitialized variables are initialized before training. :param evaluate: function that is run after each training iteration (typically to display the test/validation accuracy). :param feed: An optional dictionary that is appended to the feeding dictionary before the session runs. Can be used to feed the learning phase of a Keras model for instance. :param args: dict or argparse `Namespace` object. Should contain `nb_epochs`, `learning_rate`, `batch_size` :param rng: Instance of numpy.random.RandomState :param var_list: Optional list of parameters to train. :param fprop_args: dict, extra arguments to pass to fprop (loss and model). :param optimizer: Optimizer to be used for training :param devices: list of device names to use for training If None, defaults to: all GPUs, if GPUs are available all devices, if no GPUs are available :param x_batch_preprocessor: callable Takes a single tensor containing an x_train batch as input Returns a single tensor containing an x_train batch as output Called to preprocess the data before passing the data to the Loss :param use_ema: bool If true, uses an exponential moving average of the model parameters :param ema_decay: float or callable The decay parameter for EMA, if EMA is used If a callable rather than a float, this is a callable that takes the epoch and batch as arguments and returns the ema_decay for the current batch. :param loss_threshold: float Raise an exception if the loss exceeds this value. This is intended to rapidly detect numerical problems. Sometimes the loss may legitimately be higher than this value. In such cases, raise the value. If needed it can be np.inf. :param dataset_train: tf Dataset instance. Used as a replacement for x_train, y_train for faster performance. :param dataset_size: integer, the size of the dataset_train. :return: True if model trained """ # Check whether the hardware is working correctly canary.run_canary() if run_canary is not None: warnings.warn("The `run_canary` argument is deprecated. The canary " "is now much cheaper and thus runs all the time. The " "canary now uses its own loss function so it is not " "necessary to turn off the canary when training with " " a stochastic loss. Simply quit passing `run_canary`." "Passing `run_canary` may become an error on or after " "2019-10-16.") args = _ArgsWrapper(args or {}) fprop_args = fprop_args or {} # Check that necessary arguments were given (see doc above) # Be sure to support 0 epochs for debugging purposes if args.nb_epochs is None: raise ValueError("`args` must specify number of epochs") if optimizer is None: if args.learning_rate is None: raise ValueError("Learning rate was not given in args dict") assert args.batch_size, "Batch size was not given in args dict" if rng is None: rng = np.random.RandomState() if optimizer is None: optimizer = tf.train.AdamOptimizer(learning_rate=args.learning_rate) else: if not isinstance(optimizer, tf.train.Optimizer): raise ValueError("optimizer object must be from a child class of " "tf.train.Optimizer") grads = [] xs = [] preprocessed_xs = [] ys = [] if dataset_train is not None: assert x_train is None and y_train is None and x_batch_preprocessor is None if dataset_size is None: raise ValueError("You must provide a dataset size") data_iterator = dataset_train.make_one_shot_iterator().get_next() x_train, y_train = sess.run(data_iterator) devices = infer_devices(devices) for device in devices: with tf.device(device): # x = tf.placeholder(x_train.dtype, (None,) + x_train.shape[1:]) # y = tf.placeholder(y_train.dtype, (None,) + y_train.shape[1:]) x = tf.placeholder(tf.float32, (None,) + x_train.shape[1:]) y = tf.placeholder(tf.float32, (None,) + y_train.shape[1:]) xs.append(x) ys.append(y) if x_batch_preprocessor is not None: x = x_batch_preprocessor(x) # We need to keep track of these so that the canary can feed # preprocessed values. If the canary had to feed raw values, # stochastic preprocessing could make the canary fail. preprocessed_xs.append(x) loss_value = loss.fprop(x, y, **fprop_args) print("loss_value", loss_value) grads.append(optimizer.compute_gradients( loss_value, var_list=var_list)) print("grads:", grads) num_devices = len(devices) print("num_devices: ", num_devices) grad = avg_grads(grads) # Trigger update operations within the default graph (such as batch_norm). with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): train_step = optimizer.apply_gradients(grad) epoch_tf = tf.placeholder(tf.int32, []) batch_tf = tf.placeholder(tf.int32, []) if use_ema: if callable(ema_decay): ema_decay = ema_decay(epoch_tf, batch_tf) ema = tf.train.ExponentialMovingAverage(decay=ema_decay) with tf.control_dependencies([train_step]): train_step = ema.apply(var_list) # Get pointers to the EMA's running average variables avg_params = [ema.average(param) for param in var_list] # Make temporary buffers used for swapping the live and running average # parameters tmp_params = [tf.Variable(param, trainable=False) for param in var_list] # Define the swapping operation param_to_tmp = [tf.assign(tmp, param) for tmp, param in safe_zip(tmp_params, var_list)] with tf.control_dependencies(param_to_tmp): avg_to_param = [tf.assign(param, avg) for param, avg in safe_zip(var_list, avg_params)] with tf.control_dependencies(avg_to_param): tmp_to_avg = [tf.assign(avg, tmp) for avg, tmp in safe_zip(avg_params, tmp_params)] swap = tmp_to_avg batch_size = args.batch_size assert batch_size % num_devices == 0 device_batch_size = batch_size // num_devices if init_all: sess.run(tf.global_variables_initializer()) else: initialize_uninitialized_global_variables(sess) for epoch in xrange(args.nb_epochs): if dataset_train is not None: nb_batches = int(math.ceil(float(dataset_size) / batch_size)) else: # Indices to shuffle training set index_shuf = list(range(len(x_train))) # Randomly repeat a few training examples each epoch to avoid # having a too-small batch while len(index_shuf) % batch_size != 0: index_shuf.append(rng.randint(len(x_train))) nb_batches = len(index_shuf) // batch_size rng.shuffle(index_shuf) # Shuffling here versus inside the loop doesn't seem to affect # timing very much, but shuffling here makes the code slightly # easier to read x_train_shuffled = x_train[index_shuf] y_train_shuffled = y_train[index_shuf] prev = time.time() for batch in range(nb_batches): if dataset_train is not None: x_train_shuffled, y_train_shuffled = sess.run(data_iterator) start, end = 0, batch_size else: # Compute batch start and end indices start = batch * batch_size end = (batch + 1) * batch_size # Perform one training step diff = end - start assert diff == batch_size feed_dict = {epoch_tf: epoch, batch_tf: batch} for dev_idx in xrange(num_devices): cur_start = start + dev_idx * device_batch_size cur_end = start + (dev_idx + 1) * device_batch_size feed_dict[xs[dev_idx]] = x_train_shuffled[cur_start:cur_end] feed_dict[ys[dev_idx]] = y_train_shuffled[cur_start:cur_end] if cur_end != end and dataset_train is None: msg = ("batch_size (%d) must be a multiple of num_devices " "(%d).\nCUDA_VISIBLE_DEVICES: %s" "\ndevices: %s") args = (batch_size, num_devices, os.environ['CUDA_VISIBLE_DEVICES'], str(devices)) raise ValueError(msg % args) if feed is not None: feed_dict.update(feed) _, loss_numpy = sess.run([train_step, loss_value], feed_dict=feed_dict) if np.abs(loss_numpy) > loss_threshold: raise ValueError("Extreme loss during training: ", loss_numpy) if np.isnan(loss_numpy) or np.isinf(loss_numpy): raise ValueError("NaN/Inf loss during training") assert (dataset_train is not None or end == len(index_shuf)) # Check that all examples were used cur = time.time() _logger.info("Epoch " + str(epoch) + " took " + str(cur - prev) + " seconds") print("loss:", loss_numpy) if evaluate is not None: if use_ema: # Before running evaluation, load the running average # parameters into the live slot, so we can see how well # the EMA parameters are performing sess.run(swap) evaluate() if use_ema: # Swap the parameters back, so that we continue training # on the live parameters sess.run(swap) if use_ema: # When training is done, swap the running average parameters into # the live slot, so that we use them when we deploy the model sess.run(swap) return True
def batch_eval_multi_worker(sess, graph_factory, numpy_inputs, batch_size=None, devices=None, feed=None): """ Generic computation engine for evaluating an expression across a whole dataset, divided into batches. This function assumes that the work can be parallelized with one worker device handling one batch of data. If you need multiple devices per batch, use `batch_eval`. The tensorflow graph for multiple workers is large, so the first few runs of the graph will be very slow. If you expect to run the graph few times (few calls to `batch_eval_multi_worker` that each run few batches) the startup cost might dominate the runtime, and it might be preferable to use the single worker `batch_eval` just because its startup cost will be lower. :param sess: tensorflow Session :param graph_factory: callable When called, returns (tf_inputs, tf_outputs) where: tf_inputs is a list of placeholders to feed from the dataset tf_outputs is a list of tf tensors to calculate Example: tf_inputs is [x, y] placeholders, tf_outputs is [accuracy]. This factory must make new tensors when called, rather than, e.g. handing out a reference to existing tensors. This factory must make exactly equivalent expressions every time it is called, otherwise the results of `batch_eval` will vary depending on how work is distributed to devices. This factory must respect "with tf.device()" context managers that are active when it is called, otherwise work will not be distributed to devices correctly. :param numpy_inputs: A list of numpy arrays defining the dataset to be evaluated. The list should have the same length as tf_inputs. Each array should have the same number of examples (shape[0]). Example: numpy_inputs is [MNIST().x_test, MNIST().y_test] :param batch_size: Number of examples to use in a single evaluation batch. If not specified, this function will use a reasonable guess and may run out of memory. When choosing the batch size, keep in mind that the batch will be divided up evenly among available devices. If you can fit 128 examples in memory on one GPU and you have 8 GPUs, you probably want to use a batch size of 1024 (unless a different batch size runs faster with the ops you are using, etc.) :param devices: List of devices to run on. If unspecified, uses all available GPUs if any GPUS are available, otherwise uses CPUs. :param feed: An optional dictionary that is appended to the feeding dictionary before the session runs. Can be used to feed the learning phase of a Keras model for instance. :returns: List of numpy arrays corresponding to the outputs produced by the graph_factory """ canary.run_canary() global _batch_eval_multi_worker_cache devices = infer_devices(devices) if batch_size is None: # For big models this might result in OOM and then the user # should just specify batch_size batch_size = len(devices) * DEFAULT_EXAMPLES_PER_DEVICE n = len(numpy_inputs) assert n > 0 m = numpy_inputs[0].shape[0] for i in range(1, n): assert numpy_inputs[i].shape[0] == m out = [] replicated_tf_inputs = [] replicated_tf_outputs = [] p = None num_devices = len(devices) assert batch_size % num_devices == 0 device_batch_size = batch_size // num_devices cache_key = (graph_factory, tuple(devices)) if cache_key in _batch_eval_multi_worker_cache: # Retrieve graph for multi-GPU inference from cache. # This avoids adding tf ops to the graph packed = _batch_eval_multi_worker_cache[cache_key] replicated_tf_inputs, replicated_tf_outputs = packed p = len(replicated_tf_outputs[0]) assert p > 0 else: # This graph has not been built before. # Build it now. for device in devices: with tf.device(device): tf_inputs, tf_outputs = graph_factory() assert len(tf_inputs) == n if p is None: p = len(tf_outputs) assert p > 0 else: assert len(tf_outputs) == p replicated_tf_inputs.append(tf_inputs) replicated_tf_outputs.append(tf_outputs) del tf_inputs del tf_outputs # Store the result in the cache packed = replicated_tf_inputs, replicated_tf_outputs _batch_eval_multi_worker_cache[cache_key] = packed for _ in range(p): out.append([]) flat_tf_outputs = [] for output in range(p): for dev_idx in range(num_devices): flat_tf_outputs.append(replicated_tf_outputs[dev_idx][output]) # pad data to have # examples be multiple of batch size # we discard the excess later num_batches = int(np.ceil(float(m) / batch_size)) needed_m = num_batches * batch_size excess = needed_m - m if excess > m: raise NotImplementedError( ("Your batch size (%(batch_size)d) is bigger" " than the dataset (%(m)d), this function is " "probably overkill.") % locals()) def pad(array): """Pads an array with replicated examples to have `excess` more entries""" if excess > 0: array = np.concatenate((array, array[:excess]), axis=0) return array numpy_inputs = [pad(numpy_input) for numpy_input in numpy_inputs] orig_m = m m = needed_m for start in range(0, m, batch_size): batch = start // batch_size if batch % 100 == 0 and batch > 0: _logger.debug("Batch " + str(batch)) # Compute batch start and end indices end = start + batch_size numpy_input_batches = [ numpy_input[start:end] for numpy_input in numpy_inputs ] feed_dict = {} for dev_idx, tf_inputs in enumerate(replicated_tf_inputs): for tf_input, numpy_input in zip(tf_inputs, numpy_input_batches): dev_start = dev_idx * device_batch_size dev_end = (dev_idx + 1) * device_batch_size value = numpy_input[dev_start:dev_end] assert value.shape[0] == device_batch_size feed_dict[tf_input] = value if feed is not None: feed_dict.update(feed) flat_output_batches = sess.run(flat_tf_outputs, feed_dict=feed_dict) for e in flat_output_batches: assert e.shape[0] == device_batch_size, e.shape output_batches = [] for output in range(p): o_start = output * num_devices o_end = (output + 1) * num_devices device_values = flat_output_batches[o_start:o_end] assert len(device_values) == num_devices output_batches.append(device_values) for out_elem, device_values in zip(out, output_batches): assert len(device_values) == num_devices, (len(device_values), num_devices) for device_value in device_values: assert device_value.shape[0] == device_batch_size out_elem.extend(device_values) out = [np.concatenate(x, axis=0) for x in out] for e in out: assert e.shape[0] == m, e.shape # Trim off the examples we used to pad up to batch size out = [e[:orig_m] for e in out] assert len(out) == p, (len(out), p) return out
def train_with_PGN(sess, model, loss, train_type='naive', evaluate=None, args=None, rng=None, classifier_var_list=None, generator_var_list=None, save_dir=None, fprop_args=None, optimizer=None, use_ema=False, ema_decay=.998, loss_threshold=1e10, dataset_train=None, dataset_size=None): """ Run (optionally multi-replica, synchronous) training to minimize `loss` :param sess: TF session to use when training the graph :param loss: tensor, the loss to minimize :param evaluate: function that is run after each training iteration (typically to display the test/validation accuracy). :param args: dict or argparse `Namespace` object. Should contain `nb_epochs`, `learning_rate`, `batch_size` :param rng: Instance of numpy.random.RandomState :param var_list: Optional list of parameters to train. :param fprop_args: dict, extra arguments to pass to fprop (loss and model). :param optimizer: Optimizer to be used for training :param use_ema: bool If true, uses an exponential moving average of the model parameters :param ema_decay: float or callable The decay parameter for EMA, if EMA is used If a callable rather than a float, this is a callable that takes the epoch and batch as arguments and returns the ema_decay for the current batch. :param loss_threshold: float Raise an exception if the loss exceeds this value. This is intended to rapidly detect numerical problems. Sometimes the loss may legitimately be higher than this value. In such cases, raise the value. If needed it can be np.inf. :param dataset_train: tf Dataset instance. Used as a replacement for x_train, y_train for faster performance. :param dataset_size: integer, the size of the dataset_train. :return: True if model trained """ # Check whether the hardware is working correctly canary.run_canary() args = _ArgsWrapper(args or {}) fprop_args = fprop_args or {} # Check that necessary arguments were given (see doc above) # Be sure to support 0 epochs for debugging purposes if args.nb_epochs is None: raise ValueError("`args` must specify number of epochs") if optimizer is None: if args.learning_rate is None: raise ValueError("Learning rate was not given in args dict") assert args.batch_size, "Batch size was not given in args dict" assert dataset_train and dataset_size, "dataset_train or dataset_size was not given" if rng is None: rng = np.random.RandomState() if optimizer is None: optimizer = tf.train.AdamOptimizer(learning_rate = args.learning_rate) else: if not isinstance(optimizer, tf.train.Optimizer): raise ValueError("optimizer object must be from a child class of " "tf.train.Optimizer") grads_classifier = [] if train_type == 'PGN': grads_generator = [] xs = [] ys = [] data_iterator = dataset_train.make_one_shot_iterator().get_next() x_train, y_train = sess.run(data_iterator) devices = infer_devices() for device in devices: with tf.device(device): x = tf.placeholder(x_train.dtype, (None,) + x_train.shape[1:]) y = tf.placeholder(y_train.dtype, (None,) + y_train.shape[1:]) xs.append(x) ys.append(y) if train_type == 'PGN': loss_classifier, loss_generator = loss.fprop(x, y, **fprop_args) else: loss_classifier = loss.fprop(x, y, **fprop_args) grads_classifier.append(optimizer.compute_gradients(loss_classifier, var_list=classifier_var_list)) if train_type == 'PGN': grads_generator.append(optimizer.compute_gradients(loss_generator, var_list=generator_var_list)) num_devices = len(devices) print("num_devices: ", num_devices) grad_classifier = avg_grads(grads_classifier) if train_type == 'PGN': grad_generator = avg_grads(grads_generator) # Trigger update operations within the default graph (such as batch_norm). with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): train_step = optimizer.apply_gradients(grad_classifier) if train_type == 'PGN': with tf.control_dependencies([train_step]): train_step = optimizer.apply_gradients(grad_generator) var_list = classifier_var_list if train_type == 'PGN': var_list += generator_var_list if use_ema: ema = tf.train.ExponentialMovingAverage(decay=ema_decay) with tf.control_dependencies([train_step]): train_step = ema.apply(var_list) # Get pointers to the EMA's running average variables avg_params = [ema.average(param) for param in var_list] # Make temporary buffers used for swapping the live and running average # parameters tmp_params = [tf.Variable(param, trainable=False) for param in var_list] # Define the swapping operation param_to_tmp = [tf.assign(tmp, param) for tmp, param in safe_zip(tmp_params, var_list)] with tf.control_dependencies(param_to_tmp): avg_to_param = [tf.assign(param, avg) for param, avg in safe_zip(var_list, avg_params)] with tf.control_dependencies(avg_to_param): tmp_to_avg = [tf.assign(avg, tmp) for avg, tmp in safe_zip(avg_params, tmp_params)] swap = tmp_to_avg batch_size = args.batch_size assert batch_size % num_devices == 0 device_batch_size = batch_size // num_devices sess.run(tf.global_variables_initializer()) best_acc = 0.0 for epoch in xrange(args.nb_epochs): nb_batches = int(math.ceil(float(dataset_size) / batch_size)) prev = time.time() for batch in range(nb_batches): x_train_shuffled, y_train_shuffled = sess.run(data_iterator) start, end = 0, batch_size feed_dict = dict() for dev_idx in xrange(num_devices): cur_start = start + dev_idx * device_batch_size cur_end = start + (dev_idx + 1) * device_batch_size feed_dict[xs[dev_idx]] = x_train_shuffled[cur_start:cur_end] feed_dict[ys[dev_idx]] = y_train_shuffled[cur_start:cur_end] _, loss_classifier_numpy = sess.run([train_step, loss_classifier], feed_dict=feed_dict) if np.abs(loss_classifier_numpy) > loss_threshold: raise ValueError("Extreme loss_classifier during training: ", loss_classifier_numpy) if np.isnan(loss_classifier_numpy) or np.isinf(loss_classifier_numpy): raise ValueError("NaN/Inf loss_classifier during training") cur = time.time() _logger.info("Epoch " + str(epoch) + " took " + str(cur - prev) + " seconds") if evaluate is not None: if use_ema: sess.run(swap) r_value = evaluate(epoch) if use_ema: sess.run(swap) if use_ema: sess.run(swap) with sess.as_default(): save_path = os.path.join(save_dir,'model.joblib') save(save_path, model) return True
def train_ae(sess, loss, x_train, x_train_target, init_all=False, evaluate=None, feed=None, args=None, rng=None, var_list=None, fprop_args=None, optimizer=None, devices=None, x_batch_preprocessor=None, use_ema=False, ema_decay=.998, run_canary=None, loss_threshold=1e5, dataset_train=None, dataset_size=None): # Check whether the hardware is working correctly start_time = time.time() canary.run_canary() if run_canary is not None: warnings.warn("The `run_canary` argument is deprecated. The canary " "is now much cheaper and thus runs all the time. The " "canary now uses its own loss function so it is not " "necessary to turn off the canary when training with " " a stochastic loss. Simply quit passing `run_canary`." "Passing `run_canary` may become an error on or after " "2019-10-16.") args = _ArgsWrapper(args or {}) fprop_args = fprop_args or {} # Check that necessary arguments were given (see doc above) # Be sure to support 0 epochs for debugging purposes if args.nb_epochs is None: raise ValueError("`args` must specify number of epochs") if optimizer is None: if args.learning_rate is None: raise ValueError("Learning rate was not given in args dict") assert args.batch_size, "Batch size was not given in args dict" if rng is None: rng = np.random.RandomState() if optimizer is None: optimizer = tf.train.AdamOptimizer(learning_rate=args.learning_rate) else: if not isinstance(optimizer, tf.train.Optimizer): raise ValueError("optimizer object must be from a child class of " "tf.train.Optimizer") grads = [] xs = [] xs_t = [] preprocessed_xs = [] preprocessed_xs_t = [] #ys = [] if dataset_train is not None: assert x_train is None and x_batch_preprocessor is None if dataset_size is None: raise ValueError("You must provide a dataset size") data_iterator = dataset_train.make_one_shot_iterator().get_next() x_train, x_train_target = sess.run(data_iterator) devices = infer_devices(devices) for device in devices: with tf.device(device): x = tf.placeholder(x_train.dtype, (None, ) + x_train.shape[1:]) x_t = tf.placeholder(x_train_target.dtype, (None, ) + x_train_target.shape[1:]) #y = tf.placeholder(y_train.dtype, (None,) + y_train.shape[1:]) xs.append(x) xs_t.append(x_t) #ys.append(y) if x_batch_preprocessor is not None: x = x_batch_preprocessor(x) x_t = x_batch_preprocessor(x_t) # We need to keep track of these so that the canary can feed # preprocessed values. If the canary had to feed raw values, # stochastic preprocessing could make the canary fail. preprocessed_xs.append(x) preprocessed_xs_t.append(x_t) loss_value = loss.fprop(x, x_t, **fprop_args) grads.append( optimizer.compute_gradients(loss_value, var_list=var_list)) num_devices = len(devices) print("num_devices: ", num_devices) grad = avg_grads(grads) # Trigger update operations within the default graph (such as batch_norm). with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): train_step = optimizer.apply_gradients(grad) epoch_tf = tf.placeholder(tf.int32, []) batch_tf = tf.placeholder(tf.int32, []) if use_ema: if callable(ema_decay): ema_decay = ema_decay(epoch_tf, batch_tf) ema = tf.train.ExponentialMovingAverage(decay=ema_decay) with tf.control_dependencies([train_step]): train_step = ema.apply(var_list) # Get pointers to the EMA's running average variables avg_params = [ema.average(param) for param in var_list] # Make temporary buffers used for swapping the live and running average # parameters tmp_params = [ tf.Variable(param, trainable=False) for param in var_list ] # Define the swapping operation param_to_tmp = [ tf.assign(tmp, param) for tmp, param in safe_zip(tmp_params, var_list) ] with tf.control_dependencies(param_to_tmp): avg_to_param = [ tf.assign(param, avg) for param, avg in safe_zip(var_list, avg_params) ] with tf.control_dependencies(avg_to_param): tmp_to_avg = [ tf.assign(avg, tmp) for avg, tmp in safe_zip(avg_params, tmp_params) ] swap = tmp_to_avg batch_size = args.batch_size assert batch_size % num_devices == 0 device_batch_size = batch_size // num_devices if init_all: sess.run(tf.global_variables_initializer()) else: initialize_uninitialized_global_variables(sess) for epoch in xrange(args.nb_epochs): if dataset_train is not None: nb_batches = int(math.ceil(float(dataset_size) / batch_size)) else: # Indices to shuffle training set index_shuf = list(range(len(x_train))) # Randomly repeat a few training examples each epoch to avoid # having a too-small batch while len(index_shuf) % batch_size != 0: index_shuf.append(rng.randint(len(x_train))) nb_batches = len(index_shuf) // batch_size rng.shuffle(index_shuf) # Shuffling here versus inside the loop doesn't seem to affect # timing very much, but shuffling here makes the code slightly # easier to read x_train_shuffled = x_train[index_shuf] x_train_target_shuffled = x_train_target[index_shuf] #y_train_shuffled = y_train[index_shuf] prev = time.time() for batch in range(nb_batches): if dataset_train is not None: x_train_shuffled, x_train_target_shuffled = sess.run( data_iterator) start, end = 0, batch_size else: # Compute batch start and end indices start = batch * batch_size end = (batch + 1) * batch_size # Perform one training step diff = end - start assert diff == batch_size feed_dict = {epoch_tf: epoch, batch_tf: batch} for dev_idx in xrange(num_devices): cur_start = start + dev_idx * device_batch_size cur_end = start + (dev_idx + 1) * device_batch_size feed_dict[xs[dev_idx]] = x_train_shuffled[cur_start:cur_end] feed_dict[ xs_t[dev_idx]] = x_train_target_shuffled[cur_start:cur_end] #feed_dict[ys[dev_idx]] = y_train_shuffled[cur_start:cur_end] if cur_end != end and dataset_train is None: msg = ("batch_size (%d) must be a multiple of num_devices " "(%d).\nCUDA_VISIBLE_DEVICES: %s" "\ndevices: %s") args = (batch_size, num_devices, os.environ['CUDA_VISIBLE_DEVICES'], str(devices)) raise ValueError(msg % args) if feed is not None: feed_dict.update(feed) _, loss_numpy = sess.run([train_step, loss_value], feed_dict=feed_dict) if np.abs(loss_numpy) > loss_threshold: raise ValueError("Extreme loss during training: ", loss_numpy) if np.isnan(loss_numpy) or np.isinf(loss_numpy): raise ValueError("NaN/Inf loss during training") assert (dataset_train is not None or end == len(index_shuf)) # Check that all examples were used cur = time.time() _logger.info("Epoch " + str(epoch) + " took " + str(cur - prev) + " seconds") if evaluate is not None: if use_ema: # Before running evaluation, load the running average # parameters into the live slot, so we can see how well # the EMA parameters are performing sess.run(swap) evaluate() if use_ema: # Swap the parameters back, so that we continue training # on the live parameters sess.run(swap) if use_ema: # When training is done, swap the running average parameters into # the live slot, so that we use them when we deploy the model sess.run(swap) end_time = time.time() print("Time taken for training: ", end_time - start_time) return True
def train_with_noise(sess, loss, x_train, y_train, init_all=False, evaluate=None, feed=None, args=None, rng=None, var_list=None, fprop_args=None, optimizer=None, devices=None, x_batch_preprocessor=None, use_ema=False, ema_decay=.998, run_canary=None, loss_threshold=1e5, dataset_train=None, dataset_size=None, save=False, type="normal", datasetName="MNIST", retrain=False, discretizeColor=1): """ Run (optionally multi-replica, synchronous) training to minimize `loss` :param sess: TF session to use when training the graph :param loss: tensor, the loss to minimize :param x_train: numpy array with training inputs or tf Dataset :param y_train: numpy array with training outputs or tf Dataset :param init_all: (boolean) If set to true, all TF variables in the session are (re)initialized, otherwise only previously uninitialized variables are initialized before training. :param evaluate: function that is run after each training iteration (typically to display the test/validation accuracy). :param feed: An optional dictionary that is appended to the feeding dictionary before the session runs. Can be used to feed the learning phase of a Keras model for instance. :param args: dict or argparse `Namespace` object. Should contain `nb_epochs`, `learning_rate`, `batch_size` :param rng: Instance of numpy.random.RandomState :param var_list: Optional list of parameters to train. :param fprop_args: dict, extra arguments to pass to fprop (loss and model). :param optimizer: Optimizer to be used for training :param devices: list of device names to use for training If None, defaults to: all GPUs, if GPUs are available all devices, if no GPUs are available :param x_batch_preprocessor: callable Takes a single tensor containing an x_train batch as input Returns a single tensor containing an x_train batch as output Called to preprocess the data before passing the data to the Loss :param use_ema: bool If true, uses an exponential moving average of the model parameters :param ema_decay: float or callable The decay parameter for EMA, if EMA is used If a callable rather than a float, this is a callable that takes the epoch and batch as arguments and returns the ema_decay for the current batch. :param loss_threshold: float Raise an exception if the loss exceeds this value. This is intended to rapidly detect numerical problems. Sometimes the loss may legitimately be higher than this value. In such cases, raise the value. If needed it can be np.inf. :param dataset_train: tf Dataset instance. Used as a replacement for x_train, y_train for faster performance. :param dataset_size: integer, the size of the dataset_train. :return: True if model trained """ _, width, height, channel = list(np.shape(x_train)) # Check whether the hardware is working correctly canary.run_canary() if run_canary is not None: warnings.warn("The `run_canary` argument is deprecated. The canary " "is now much cheaper and thus runs all the time. The " "canary now uses its own loss function so it is not " "necessary to turn off the canary when training with " " a stochastic loss. Simply quit passing `run_canary`." "Passing `run_canary` may become an error on or after " "2019-10-16.") args = _ArgsWrapper(args or {}) fprop_args = fprop_args or {} # Check that necessary arguments were given (see doc above) # Be sure to support 0 epochs for debugging purposes if args.nb_epochs is None: raise ValueError("`args` must specify number of epochs") if optimizer is None: if args.learning_rate is None: raise ValueError("Learning rate was not given in args dict") assert args.batch_size, "Batch size was not given in args dict" if rng is None: rng = np.random.RandomState() if optimizer is None: optimizer = tf.train.AdamOptimizer(learning_rate=args.learning_rate) else: if not isinstance(optimizer, tf.train.Optimizer): raise ValueError("optimizer object must be from a child class of " "tf.train.Optimizer") grads = [] xs = [] preprocessed_xs = [] ys = [] if dataset_train is not None: assert x_train is None and y_train is None and x_batch_preprocessor is None if dataset_size is None: raise ValueError("You must provide a dataset size") data_iterator = dataset_train.make_one_shot_iterator().get_next() x_train, y_train = sess.run(data_iterator) devices = infer_devices(devices) for device in devices: with tf.device(device): x = tf.placeholder(x_train.dtype, (None,) + x_train.shape[1:]) y = tf.placeholder(y_train.dtype, (None,) + y_train.shape[1:]) xs.append(x) ys.append(y) if x_batch_preprocessor is not None: x = x_batch_preprocessor(x) # We need to keep track of these so that the canary can feed # preprocessed values. If the canary had to feed raw values, # stochastic preprocessing could make the canary fail. preprocessed_xs.append(x) loss_value = loss.fprop(x, y, **fprop_args) grads.append(optimizer.compute_gradients( loss_value, var_list=var_list)) num_devices = len(devices) print("num_devices: ", num_devices) grad = avg_grads(grads) # Trigger update operations within the default graph (such as batch_norm). with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): train_step = optimizer.apply_gradients(grad) epoch_tf = tf.placeholder(tf.int32, []) batch_tf = tf.placeholder(tf.int32, []) if use_ema: if callable(ema_decay): ema_decay = ema_decay(epoch_tf, batch_tf) ema = tf.train.ExponentialMovingAverage(decay=ema_decay) with tf.control_dependencies([train_step]): train_step = ema.apply(var_list) # Get pointers to the EMA's running average variables avg_params = [ema.average(param) for param in var_list] # Make temporary buffers used for swapping the live and running average # parameters tmp_params = [tf.Variable(param, trainable=False) for param in var_list] # Define the swapping operation param_to_tmp = [tf.assign(tmp, param) for tmp, param in safe_zip(tmp_params, var_list)] with tf.control_dependencies(param_to_tmp): avg_to_param = [tf.assign(param, avg) for param, avg in safe_zip(var_list, avg_params)] with tf.control_dependencies(avg_to_param): tmp_to_avg = [tf.assign(avg, tmp) for avg, tmp in safe_zip(avg_params, tmp_params)] swap = tmp_to_avg batch_size = args.batch_size assert batch_size % num_devices == 0 device_batch_size = batch_size // num_devices saver = tf.train.Saver(max_to_keep=100) startingEpoch = 0 # if retrainEpoch is not None: # startingEpoch = retrainEpoch if init_all: sess.run(tf.global_variables_initializer()) else: initialize_uninitialized_global_variables(sess) # Used for retraining the model if retrain == True: print("Retrain is in progress...") retrain = False # Set to false once it has retrained just in case we have run this script for multiple loops latestFileName = tf.train.latest_checkpoint(args.train_dir, latest_filename=None) splitFileName = latestFileName.split("-") startingEpoch = int(splitFileName[len(splitFileName)-1]) model_path = os.path.join(args.train_dir, args.filename+"-"+str(startingEpoch)) print("Trying to load trained model from: "+model_path) if os.path.exists(model_path + ".meta"): tf_model_load(sess, model_path) print("Load trained model") # x_train = x_train[0:10] feed_x_train = x_train if type == "normal": feed_x_train = convert_uniimage(x_train, discretizeColor) for epoch in xrange(startingEpoch, args.nb_epochs): tmpX = np.copy(x_train) if type == "noise": # Use it for MNIST and Fashion MNIST if datasetName == "MNIST": tmpX = np.clip(x_train+(np.random.uniform(0, 0.8, (len(x_train), width, height, channel)) - 0.4), 0, 1) # Use it for MNIST and Fashion CIFAR10 if datasetName == "CIFAR10": tmpX = np.clip(x_train+(np.random.uniform(0, 0.3, (len(x_train), width, height, channel)) - 0.15), 0, 1) feed_x_train = convert_uniimage(tmpX, discretizeColor) ################## # Showing images # ################## showImg = True showImg = False if showImg: shapeImg = (width, height, channel) if channel == 1: shapeImg = (width, height) for iii in range(len(feed_x_train)): fig = plt.figure() pixels = x_train[iii].reshape(shapeImg) sub = fig.add_subplot(1, 4, 1) plt.imshow(pixels, cmap='gray') pixels = tmpX[iii].reshape(shapeImg) sub = fig.add_subplot(1, 4, 2) plt.imshow(pixels, cmap='gray') pixels = feed_x_train[iii].reshape(shapeImg) sub = fig.add_subplot(1, 4, 3) plt.imshow(pixels, cmap='gray') # pixels = X_cur[iii].reshape((width, height, channel)) # sub = fig.add_subplot(1, 4, 4) # plt.imshow(pixels, cmap='gray') # pixels = adv_x[iii].reshape((28, 28)) - xtrain[iii].reshape((28, 28)) # print(np.mean(np.sum((adv_x[iii:iii+1] - xtrain[iii:iii+1]) ** 2, # axis=(1, 2, 3)) ** .5)) # sub = fig.add_subplot(1, 3, iii+3) # plt.imshow(pixels / abs(pixels).max() * 0.2 + 0.5, cmap='gray') plt.show() if dataset_train is not None: nb_batches = int(math.ceil(float(dataset_size) / batch_size)) else: # Indices to shuffle training set index_shuf = list(range(len(x_train))) # Randomly repeat a few training examples each epoch to avoid # having a too-small batch while len(index_shuf) % batch_size != 0: index_shuf.append(rng.randint(len(x_train))) nb_batches = len(index_shuf) // batch_size rng.shuffle(index_shuf) # Shuffling here versus inside the loop doesn't seem to affect # timing very much, but shuffling here makes the code slightly # easier to read x_train_shuffled = feed_x_train[index_shuf] y_train_shuffled = y_train[index_shuf] prev = time.time() for batch in range(nb_batches): if dataset_train is not None: x_train_shuffled, y_train_shuffled = sess.run(data_iterator) start, end = 0, batch_size else: # Compute batch start and end indices start = batch * batch_size end = (batch + 1) * batch_size # Perform one training step diff = end - start assert diff == batch_size feed_dict = {epoch_tf: epoch, batch_tf: batch} for dev_idx in xrange(num_devices): cur_start = start + dev_idx * device_batch_size cur_end = start + (dev_idx + 1) * device_batch_size feed_dict[xs[dev_idx]] = x_train_shuffled[cur_start:cur_end] feed_dict[ys[dev_idx]] = y_train_shuffled[cur_start:cur_end] if cur_end != end and dataset_train is None: msg = ("batch_size (%d) must be a multiple of num_devices " "(%d).\nCUDA_VISIBLE_DEVICES: %s" "\ndevices: %s") args = (batch_size, num_devices, os.environ['CUDA_VISIBLE_DEVICES'], str(devices)) raise ValueError(msg % args) if feed is not None: feed_dict.update(feed) _, loss_numpy = sess.run( [train_step, loss_value], feed_dict=feed_dict) if np.abs(loss_numpy) > loss_threshold: raise ValueError("Extreme loss during training: ", loss_numpy) if np.isnan(loss_numpy) or np.isinf(loss_numpy): raise ValueError("NaN/Inf loss during training") assert (dataset_train is not None or end == len(index_shuf)) # Check that all examples were used cur = time.time() _logger.info("Epoch " + str(epoch) + " took " + str(cur - prev) + " seconds") if evaluate is not None: if use_ema: # Before running evaluation, load the running average # parameters into the live slot, so we can see how well # the EMA parameters are performing sess.run(swap) if (epoch + 1) % 10 == 0 or (epoch + 1) == args.nb_epochs: evaluate() if use_ema: # Swap the parameters back, so that we continue training # on the live parameters sess.run(swap) if save and ((epoch + 1) % 50 == 0 or (epoch + 1) == args.nb_epochs): with tf.device('/CPU:0'): save_path = os.path.join(args.train_dir, args.filename) if tf.gfile.Exists(args.train_dir) == False: tf.gfile.MakeDirs(args.train_dir) saver.save(sess, save_path, global_step=(epoch + 1)) _logger.info("Reaching save point at " + str(epoch + 1) + ": " + str(save_path)) if use_ema: # When training is done, swap the running average parameters into # the live slot, so that we use them when we deploy the model sess.run(swap) return True