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_sequential_weight_loading_group_name_with_incorrect_length(self): if h5py is None: return h5_path = self._save_model_dir("test.h5") num_hidden = 5 input_dim = 3 num_classes = 2 with 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="rmsprop", metrics=[keras.metrics.categorical_accuracy], ) f_ref_model = h5py.File(h5_path, "w") 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, use_bias=False, input_dim=input_dim, name="d1")) model.add(keras.layers.Dense(num_classes, name="d2")) model.compile( loss=keras.losses.MSE, optimizer="rmsprop", metrics=[keras.metrics.categorical_accuracy], ) with self.assertRaises( ValueError, msg="Weight count mismatch for layer #0 (named d1). " "Layer expects 1 weight(s). Received 2 saved weight(s)", ): 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( keras.backend.get_value(ref_model.layers[1].kernel), keras.backend.get_value(model.layers[1].kernel), )
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 test_sequential_weight_loading_group_name_with_incorrect_length(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 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='rmsprop', metrics=[keras.metrics.categorical_accuracy]) f_ref_model = h5py.File(h5_path, 'w') 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, use_bias=False, input_dim=input_dim, name='d1')) model.add(keras.layers.Dense(num_classes, name='d2')) model.compile(loss=keras.losses.MSE, optimizer='rmsprop', metrics=[keras.metrics.categorical_accuracy]) with self.assertRaisesRegex( ValueError, r'Layer #0 \(named \"d1\"\) expects 1 ' r'weight\(s\), but the saved weights have 2 ' r'element\(s\)\.'): 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( keras.backend.get_value(ref_model.layers[1].kernel), keras.backend.get_value(model.layers[1].kernel))