def run_benchmark(self, gpus=0): input_dim_1 = 40 input_dim_2 = 60 input_shape = (self.num_samples, input_dim_1, 60) x, y = generate_text_input_data(input_shape) # build the model: a single LSTM model = Sequential() model.add(LSTM(128, input_shape=(input_dim_1, input_dim_2))) model.add(Dense(input_dim_2), activation='softmax') optimizer = RMSprop(lr=0.01) if keras.backend.backend() is "tensorflow" and gpus > 1: model = multi_gpu_model(model, gpus=gpus) model.compile(loss='categorical_crossentropy', optimizer=optimizer) # create a distributed trainer for cntk if keras.backend.backend() is "cntk" and gpus > 1: start, end = cntk_gpu_mode_config(model, x.shape[0]) x = x[start: end] y = y[start: end] time_callback = timehistory.TimeHistory() model.fit(x, y, batch_size=self.batch_size, epochs=self.epochs, callbacks=[time_callback]) self.total_time = 0 for i in range(1, self.epochs): self.total_time += time_callback.times[i]
def run_benchmark(self, gpus=0, inference=False, use_dataset_tensors=False): print("Running model ", self.test_name) keras.backend.set_learning_phase(True) input_dim_1 = 40 input_dim_2 = 60 input_shape = (self.num_samples, input_dim_1, 60) x_train, y_train = generate_text_input_data(input_shape) x_train = x_train.astype('float32') y_train = y_train.astype('float32') # build the model: a single LSTM model = Sequential() model.add( LSTM(128, input_shape=(input_dim_1, input_dim_2), unroll=True)) optimizer = RMSprop(lr=0.01) if use_dataset_tensors: # Create the dataset and its associated one-shot iterator. dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)) dataset = dataset.repeat() dataset = dataset.shuffle(10000) dataset = dataset.batch(self.batch_size) iterator = dataset.make_one_shot_iterator() # Model creation using tensors from the get_next() graph node. inputs, targets = iterator.get_next() if use_dataset_tensors: input_tensor = keras.layers.Input(tensor=inputs) model.add(Dense(input_dim_2)) predictions = model(input_tensor) model = keras.models.Model(input_tensor, predictions) else: model.add(Dense(input_dim_2, activation='softmax')) # use multi gpu model for more than 1 gpu if (keras.backend.backend() == 'tensorflow' or keras.backend.backend() == 'mxnet') and gpus > 1: model = multi_gpu_model(model, gpus=gpus) if use_dataset_tensors: model.compile(loss=crossentropy_from_logits, optimizer=optimizer, metrics=['accuracy'], target_tensors=[targets]) else: model.compile(loss='categorical_crossentropy', optimizer=optimizer) time_callback = timehistory.TimeHistory() if use_dataset_tensors: model.fit(epochs=self.epochs, steps_per_epoch=15, callbacks=[time_callback]) else: model.fit(x_train, y_train, batch_size=self.batch_size, epochs=self.epochs, callbacks=[time_callback]) self.total_time = 0 for i in range(1, self.epochs): self.total_time += time_callback.times[i] if keras.backend.backend() == "tensorflow": keras.backend.clear_session()
def run_benchmark(self, gpus=0, inference=False, use_dataset_tensors=False, epochs=20): # prepare logging # file name: backend_data_format_dataset_model_batch_size_gpus.log log_file = keras.backend.backend( ) + '_' + keras.backend.image_data_format( ) + '_lstm_synthetic_batch_size_' + str( self.batch_size) + '_' + str(gpus) + 'gpus.log' # nopep8 logging.basicConfig(level=logging.INFO, filename=log_file) self.epochs = epochs print("Running model ", self.test_name) keras.backend.set_learning_phase(True) input_dim_1 = 40 input_dim_2 = 60 input_shape = (self.num_samples, input_dim_1, 60) x_train, y_train = generate_text_input_data(input_shape) x_train = x_train.astype('float32') y_train = y_train.astype('float32') # build the model: a single LSTM model = Sequential() model.add( LSTM(128, input_shape=(input_dim_1, input_dim_2), unroll=True)) optimizer = RMSprop(lr=0.01) if use_dataset_tensors: # Create the dataset and its associated one-shot iterator. dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)) dataset = dataset.repeat() dataset = dataset.shuffle(10000) dataset = dataset.batch(self.batch_size) iterator = dataset.make_one_shot_iterator() # Model creation using tensors from the get_next() graph node. inputs, targets = iterator.get_next() if use_dataset_tensors: input_tensor = keras.layers.Input(tensor=inputs) model.add(Dense(input_dim_2)) predictions = model(input_tensor) model = keras.models.Model(input_tensor, predictions) else: model.add(Dense(input_dim_2, activation='softmax')) # use multi gpu model for more than 1 gpu if (keras.backend.backend() == 'tensorflow' or keras.backend.backend() == 'mxnet') and gpus > 1: model = multi_gpu_model(model, gpus=gpus, cpu_merge=False) if use_dataset_tensors: model.compile(loss=crossentropy_from_logits, optimizer=optimizer, metrics=['accuracy'], target_tensors=[targets]) else: model.compile(loss='categorical_crossentropy', optimizer=optimizer) time_callback = TimeHistory() if use_dataset_tensors: history_callback = model.fit(epochs=self.epochs, steps_per_epoch=15, callbacks=[time_callback]) else: history_callback = model.fit(x_train, y_train, batch_size=self.batch_size, epochs=self.epochs, callbacks=[time_callback]) log = LoggingMetrics(history_callback, time_callback) log.save_metrics_to_log(logging) if keras.backend.backend() == "tensorflow": keras.backend.clear_session()