def test_sequential_weight_loading_group_name_with_incorrect_shape(self): if h5py is None: return temp_dir = self.get_temp_dir() self.addCleanup(shutil.rmtree, temp_dir) h5_path = os.path.join(temp_dir, 'test.h5') num_hidden = 5 input_dim = 3 num_classes = 2 with tf.Graph().as_default(), self.cached_session(): ref_model = keras.models.Sequential() ref_model.add( keras.layers.Dense(num_hidden, input_dim=input_dim, name='d1')) ref_model.add(keras.layers.Dense(num_classes, name='d2')) ref_model.compile(loss=keras.losses.MSE, optimizer=optimizer_v1.RMSprop(lr=0.0001), metrics=[keras.metrics.categorical_accuracy]) f_ref_model = h5py.File(h5_path, 'w') keras.backend.set_value(ref_model.layers[1].bias, [3.5] * num_classes) hdf5_format.save_weights_to_hdf5_group(f_ref_model, ref_model.layers) f_model = h5py.File(h5_path, 'r') model = keras.models.Sequential() model.add( keras.layers.Dense(num_hidden + 5, input_dim=input_dim, name='d1')) model.add(keras.layers.Dense(num_classes, name='d2')) model.compile(loss=keras.losses.MSE, optimizer=optimizer_v1.RMSprop(lr=0.0001), metrics=[keras.metrics.categorical_accuracy]) with self.assertRaisesRegex( ValueError, r'Layer #0 \(named "d1"\), weight ' r'<tf\.Variable \'d1_1\/kernel:0\' ' r'shape=\(3, 10\) dtype=float32> has ' r'shape \(3, 10\), but the saved weight has ' r'shape \(3, 5\)\.'): hdf5_format.load_weights_from_hdf5_group_by_name( f_model, model.layers) hdf5_format.load_weights_from_hdf5_group_by_name( f_model, model.layers, skip_mismatch=True) self.assertAllClose([3.5] * num_classes, keras.backend.get_value(model.layers[1].bias))
def testAssertModelCloneSameObjectsThrowError(self): input_arr = np.random.random((1, 3)) target_arr = np.random.random((1, 3)) model_graph = tf.Graph() clone_graph = tf.Graph() # Create two models with the same layers but different optimizers. with tf.compat.v1.Session(graph=model_graph): inputs = keras.layers.Input(shape=(3, )) x = keras.layers.Dense(2)(inputs) x = keras.layers.Dense(3)(x) model = keras.models.Model(inputs, x) model.compile(loss='mse', optimizer=tf.compat.v1.train.AdadeltaOptimizer()) model.train_on_batch(input_arr, target_arr) with tf.compat.v1.Session(graph=clone_graph): inputs = keras.layers.Input(shape=(3, )) x = keras.layers.Dense(2)(inputs) x = keras.layers.Dense(4)(x) x = keras.layers.Dense(3)(x) clone = keras.models.Model(inputs, x) clone.compile(loss='mse', optimizer=optimizer_v1.RMSprop(lr=0.0001)) clone.train_on_batch(input_arr, target_arr)
def test_sequential_weight_loading_group_name_with_incorrect_shape(self): if h5py is None: return h5_path = self._save_model_dir('test.h5') num_hidden = 5 input_dim = 3 num_classes = 2 with tf.Graph().as_default(), self.cached_session(): ref_model = keras.models.Sequential() ref_model.add(keras.layers.Dense(num_hidden, input_dim=input_dim, name='d1')) ref_model.add(keras.layers.Dense(num_classes, name='d2')) ref_model.compile(loss=keras.losses.MSE, optimizer=optimizer_v1.RMSprop(lr=0.0001), metrics=[keras.metrics.categorical_accuracy]) f_ref_model = h5py.File(h5_path, 'w') keras.backend.set_value(ref_model.layers[1].bias, [3.5] * num_classes) hdf5_format.save_weights_to_hdf5_group(f_ref_model, ref_model) f_model = h5py.File(h5_path, 'r') model = keras.models.Sequential() model.add(keras.layers.Dense(num_hidden + 5, input_dim=input_dim, name='d1')) model.add(keras.layers.Dense(num_classes, name='d2')) model.compile(loss=keras.losses.MSE, optimizer=optimizer_v1.RMSprop(lr=0.0001), metrics=[keras.metrics.categorical_accuracy]) with self.assertRaises( ValueError, msg='Shape mismatch in layer #0 (named d1) for weight d1_1/kernel:0. ' 'Weight expects shape (3, 10). ' 'Received saved weight with shape (3, 5)'): hdf5_format.load_weights_from_hdf5_group_by_name(f_model, model) hdf5_format.load_weights_from_hdf5_group_by_name( f_model, model, skip_mismatch=True) self.assertAllClose([3.5] * num_classes, keras.backend.get_value(model.layers[1].bias))
def test_nested_model_with_tensor_input(self): gpus = 2 input_dim = 10 shape = (input_dim, ) num_samples = 16 num_classes = 10 if not check_if_compatible_devices(gpus=gpus): self.skipTest('multi gpu only') with tf.Graph().as_default(), self.cached_session(): input_shape = (num_samples, ) + shape x_train = np.random.randint(0, 255, input_shape) y_train = np.random.randint(0, num_classes, (input_shape[0], )) y_train = np_utils.to_categorical(y_train, num_classes) x_train = x_train.astype('float32') y_train = y_train.astype('float32') dataset = tf.compat.v1.data.Dataset.from_tensor_slices( (x_train, y_train)) dataset = dataset.repeat() dataset = dataset.batch(4) iterator = tf.compat.v1.data.make_one_shot_iterator(dataset) inputs, targets = iterator.get_next() input_tensor = keras.layers.Input(tensor=inputs) model = keras.models.Sequential() model.add(keras.layers.Dense(3, input_shape=(input_dim, ))) model.add(keras.layers.Dense(num_classes)) output = model(input_tensor) outer_model = keras.Model(input_tensor, output) parallel_model = multi_gpu_utils.multi_gpu_model(outer_model, gpus=gpus) parallel_model.compile(loss='categorical_crossentropy', optimizer=optimizer_v1.RMSprop(lr=0.0001, decay=1e-6), metrics=['accuracy'], target_tensors=[targets]) parallel_model.fit(epochs=1, steps_per_epoch=3)
def test_rmsprop(self): with self.cached_session(): self._test_optimizer(optimizer_v1.RMSprop()) self._test_optimizer(optimizer_v1.RMSprop(decay=1e-3))
def testRMSpropCompatibility(self): opt_v1 = optimizer_v1.RMSprop() opt_v2 = rmsprop.RMSprop() self._testOptimizersCompatibility(opt_v1, opt_v2)