Compared weights stored in h5 and in memory

I have 3 pieces of information. py scripts, and output of script and h5 that was saved.
As you can save model is saved right after printing out the layer’s weights after training.
Then I looked at what is saved using h5dump and compared the weights in python script output. None of the weights seem to match.

py script:

import tensorflow as tf
from tensorflow.keras import layers
import pandas as pd
import numpy as np
import os
#from tensorflow.keras import datasets, layers, models
#from tensorflow.keras.utils import to_categorical

def pack_features_vector(features, labels):
  """Pack the features into a single array."""
  features = tf.stack(list(features.values()), axis=1)
  return features, labels

train_dataset_url = "https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv"
train_dataset_fp = tf.keras.utils.get_file(fname=os.path.basename(train_dataset_url),
                                           origin=train_dataset_url)
print("Local copy of the dataset file: {}".format(train_dataset_fp))

# column order in CSV file
column_names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species']

feature_names = column_names[:-1]
label_name = column_names[-1]

print("Features: {}".format(feature_names))
print("Label: {}".format(label_name))

class_names = ['Iris setosa', 'Iris versicolor', 'Iris virginica']

batch_size = 32

train_dataset = tf.data.experimental.make_csv_dataset(
    train_dataset_fp,
    batch_size,
    column_names=column_names,
    label_name=label_name,
    num_epochs=1)

features, labels = next(iter(train_dataset))
print(features)

train_dataset = train_dataset.map(pack_features_vector)
features, labels = next(iter(train_dataset))
print(features[:5])

# create model.

model = tf.keras.Sequential([
  tf.keras.layers.Dense(10, activation=tf.nn.relu, input_shape=(4,)),  # input shape required
  tf.keras.layers.Dense(10, activation=tf.nn.relu),
  tf.keras.layers.Dense(3)
])

for i in model.layers:
        print("------")
        print(i, "\n", i.input_shape, "\n", i.output_shape, "\n", i.get_weights())

model.save("iris.h5")
predictions = model(features)
predictions[:5]

print("predictions: ", predictions)

print("Prediction: {}".format(tf.argmax(predictions, axis=1)))
print("    Labels: {}".format(labels))

script output:

Local copy of the dataset file: /root/.keras/datasets/iris_training.csv
Features: ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
Label: species
OrderedDict([('sepal_length', <tf.Tensor: shape=(32,), dtype=float32, numpy=
array([5.4, 6. , 7.9, 6.1, 6.4, 4.9, 7.6, 4.4, 5.3, 4.4, 5.1, 5.4, 6.3,
       5.4, 6.8, 6.6, 4.5, 4.9, 5.8, 6. , 5. , 7.7, 6.1, 5.1, 6.7, 6.5,
       5. , 7.3, 4.9, 6.7, 5.7, 5.1], dtype=float32)>), ('sepal_width', <tf.Tensor: shape=(32,), dtype=float32, numpy=
array([3.7, 3. , 3.8, 2.8, 2.8, 3.1, 3. , 3. , 3.7, 2.9, 3.5, 3.9, 2.7,
       3.4, 3.2, 3. , 2.3, 2.4, 4. , 2.2, 3.4, 2.6, 3. , 3.8, 3.1, 3. ,
       2.3, 2.9, 3.1, 3. , 3.8, 3.7], dtype=float32)>), ('petal_length', <tf.Tensor: shape=(32,), dtype=float32, numpy=
array([1.5, 4.8, 6.4, 4. , 5.6, 1.5, 6.6, 1.3, 1.5, 1.4, 1.4, 1.3, 4.9,
       1.5, 5.9, 4.4, 1.3, 3.3, 1.2, 5. , 1.6, 6.9, 4.9, 1.9, 5.6, 5.8,
       3.3, 6.3, 1.5, 5.2, 1.7, 1.5], dtype=float32)>), ('petal_width', <tf.Tensor: shape=(32,), dtype=float32, numpy=
array([0.2, 1.8, 2. , 1.3, 2.2, 0.1, 2.1, 0.2, 0.2, 0.2, 0.3, 0.4, 1.8,
       0.4, 2.3, 1.4, 0.3, 1. , 0.2, 1.5, 0.4, 2.3, 1.8, 0.4, 2.4, 2.2,
       1. , 1.8, 0.1, 2.3, 0.3, 0.4], dtype=float32)>)])
tf.Tensor(
[[4.7 3.2 1.3 0.2]
 [6.3 2.5 5.  1.9]
 [4.4 3.2 1.3 0.2]
 [7.7 2.8 6.7 2. ]
 [5.8 2.6 4.  1.2]], shape=(5, 4), dtype=float32)
------
<keras.layers.core.Dense object at 0x7fe62018b8d0> 
 (None, 4) 
 (None, 10) 
 [array([[-0.16934353,  0.6538881 ,  0.09529907,  0.09148753,  0.35007977,
        -0.48498237, -0.616264  ,  0.42495692, -0.54337656,  0.14560634],
       [-0.04133761, -0.35187003, -0.5649046 , -0.26850873, -0.0262723 ,
         0.6109561 , -0.39360768, -0.33579051,  0.46564436, -0.56868595],
       [ 0.26928586,  0.4964025 ,  0.26902878,  0.64027154,  0.50757873,
        -0.35445428,  0.12211812, -0.18665534,  0.17297399,  0.07234693],
       [ 0.5847622 ,  0.16729748,  0.5002092 ,  0.0327698 , -0.03720093,
        -0.4658246 ,  0.6366278 ,  0.44643772,  0.26316   ,  0.0116443 ]],
      dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]
------
<keras.layers.core.Dense object at 0x7fe62019e8d0> 
 (None, 10) 
 (None, 10) 
 [array([[ 0.25488317, -0.19811574,  0.31339538,  0.46442997,  0.46701813,
         0.32009012,  0.14943856, -0.118653  ,  0.3611651 , -0.05267757],
       [ 0.21770436,  0.30100304,  0.02044654,  0.17554712, -0.05844954,
        -0.0471037 ,  0.4999628 ,  0.5461823 , -0.38297945, -0.47540808],
       [ 0.36347872,  0.27856046,  0.30605143, -0.19340989, -0.10854504,
        -0.20153347,  0.33568484,  0.35512662,  0.2420249 ,  0.007388  ],
       [ 0.37105292, -0.19282565,  0.4251526 , -0.00805998,  0.1456933 ,
         0.08754086, -0.47610855,  0.01876432,  0.10244167, -0.09887961],
       [ 0.39265168,  0.40771556, -0.52760196, -0.46516624, -0.45102534,
        -0.3610369 , -0.5141273 , -0.41536093,  0.3469665 ,  0.3520397 ],
       [-0.08349526, -0.06482443,  0.28481692, -0.19939888,  0.43423206,
        -0.54631263,  0.05766773,  0.09323448, -0.17082131, -0.02900785],
       [ 0.52993524,  0.32083207, -0.14601195, -0.37405825, -0.40391874,
         0.10046351,  0.37597203, -0.17189291,  0.17522407,  0.5077548 ],
       [ 0.37318254,  0.12648302,  0.25008893, -0.02691913,  0.12483996,
        -0.18606123,  0.01354373, -0.28218728, -0.2563297 , -0.41760454],
       [-0.09039289,  0.15598345, -0.05856067,  0.16004056,  0.32928538,
         0.50391257,  0.38259608,  0.17491794,  0.36692405, -0.5169993 ],
       [-0.1809234 , -0.53764766, -0.0459218 ,  0.53855896, -0.16923049,
        -0.03278053, -0.42605472, -0.00554866,  0.2525249 , -0.31536007]],
      dtype=float32), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]
------
<keras.layers.core.Dense object at 0x7fe6200db7b8> 
 (None, 10) 
 (None, 3) 
 [array([[-0.27715015,  0.35010564, -0.63067424],
       [ 0.35075736,  0.12754834, -0.404687  ],
       [-0.5605073 ,  0.21351773, -0.33925647],
       [ 0.6517352 ,  0.15162307,  0.17802429],
       [ 0.5286808 ,  0.35111117,  0.02914745],
       [ 0.14262837, -0.39313567, -0.6624643 ],
       [-0.10874224,  0.45081067,  0.6237936 ],
       [-0.21501306,  0.2514338 , -0.04387879],
       [ 0.09561396,  0.6299237 , -0.4526446 ],
       [-0.27246085,  0.44971967, -0.10388595]], dtype=float32), array([0., 0., 0.], dtype=float32)]
predictions:  tf.Tensor(
[[ 1.3542574e-02  9.5328659e-01 -1.8916668e+00]
 [-8.7079895e-01  2.8846312e+00 -5.2455764e+00]
 [ 1.9318907e-02  8.8224554e-01 -1.7554586e+00]
 [-1.1404124e+00  3.7611663e+00 -6.7353735e+00]
 [-4.3975523e-01  2.1991529e+00 -4.0744286e+00]
 [-2.4423681e-04  1.0151449e+00 -2.0052242e+00]
 [-1.2026094e-02  1.0524964e+00 -2.0688508e+00]
 [-8.2208884e-01  2.8163836e+00 -5.0243940e+00]
 [-5.1455361e-01  2.4287968e+00 -4.4816799e+00]
 [-8.4206414e-01  2.9725320e+00 -5.4485259e+00]
 [-4.8059338e-01  2.3958726e+00 -4.4582477e+00]
 [-9.2836344e-01  3.1894240e+00 -5.7621212e+00]
 [-4.1600901e-01  2.1558359e+00 -4.0035191e+00]
 [-9.6293294e-01  3.1582255e+00 -5.6286354e+00]
 [-1.0713589e+00  3.5353155e+00 -6.4250231e+00]
 [-5.6884605e-01  2.5648918e+00 -4.7011642e+00]
 [-8.7955415e-01  3.0693195e+00 -5.5841455e+00]
 [-8.4293075e-03  1.0209532e+00 -2.0088677e+00]
 [ 1.1531422e-02  1.1286016e+00 -2.1406360e+00]
 [-8.6255896e-01  2.9612677e+00 -5.3611178e+00]
 [-9.7419536e-03  9.4846559e-01 -1.8676918e+00]
 [-8.2208884e-01  2.8163836e+00 -5.0243940e+00]
 [ 1.5697019e-02  1.0343369e+00 -2.0550537e+00]
 [-8.5081160e-01  2.9531438e+00 -5.3250475e+00]
 [-9.0655349e-03  1.1560876e+00 -2.2623563e+00]
 [-4.9178526e-01  2.2317708e+00 -4.0964704e+00]
 [-5.0555271e-01  2.5194461e+00 -4.6620979e+00]
 [-9.0001255e-01  2.9964993e+00 -5.4717879e+00]
 [-7.5749421e-01  2.9134338e+00 -5.2107706e+00]
 [-7.8310817e-01  2.8561511e+00 -5.2203703e+00]
 [-6.1771739e-01  2.7320302e+00 -5.0307646e+00]
 [-9.8205024e-01  2.9898508e+00 -5.2746177e+00]], shape=(32, 3), dtype=float32)
Prediction: [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
    Labels: [0 2 0 2 1 0 0 2 1 2 1 2 1 2 2 1 2 0 0 2 0 2 0 2 0 1 1 2 2 2 1 2]
root@nonroot-Standard-PC-i440FX-PIIX-1996:~/dev-learn/gpu/tflow/tensorflow/tflow-2nded/examples-misc# 

h5dump:

HDF5 "iris.h5" {
GROUP "/" {
   ATTRIBUTE "backend" {
      DATATYPE  H5T_STRING {
         STRSIZE H5T_VARIABLE;
         STRPAD H5T_STR_NULLTERM;
         CSET H5T_CSET_UTF8;
         CTYPE H5T_C_S1;
      }
      DATASPACE  SCALAR
      DATA {
      (0): "tensorflow"
      }
   }
   ATTRIBUTE "keras_version" {
      DATATYPE  H5T_STRING {
         STRSIZE H5T_VARIABLE;
         STRPAD H5T_STR_NULLTERM;
         CSET H5T_CSET_UTF8;
         CTYPE H5T_C_S1;
      }
      DATASPACE  SCALAR
      DATA {
      (0): "2.6.0"
      }
   }
   ATTRIBUTE "model_config" {
      DATATYPE  H5T_STRING {
         STRSIZE H5T_VARIABLE;
         STRPAD H5T_STR_NULLTERM;
         CSET H5T_CSET_ASCII;
         CTYPE H5T_C_S1;
      }
      DATASPACE  SCALAR
      DATA {
      (0): "{"class_name": "Sequential", "config": {"name": "sequential", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 4], "dtype": "float32", "sparse": false, "ragged": false, "name": "dense_input"}}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "batch_input_shape": [null, 4], "dtype": "float32", "units": 10, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 10, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_2", "trainable": true, "dtype": "float32", "units": 3, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}]}}"
      }
   }
   GROUP "model_weights" {
      ATTRIBUTE "backend" {
         DATATYPE  H5T_STRING {
            STRSIZE H5T_VARIABLE;
            STRPAD H5T_STR_NULLTERM;
            CSET H5T_CSET_ASCII;
            CTYPE H5T_C_S1;
         }
         DATASPACE  SCALAR
         DATA {
         (0): "tensorflow"
         }
      }
      ATTRIBUTE "keras_version" {
         DATATYPE  H5T_STRING {
            STRSIZE H5T_VARIABLE;
            STRPAD H5T_STR_NULLTERM;
            CSET H5T_CSET_ASCII;
            CTYPE H5T_C_S1;
         }
         DATASPACE  SCALAR
         DATA {
         (0): "2.6.0"
         }
      }
      ATTRIBUTE "layer_names" {
         DATATYPE  H5T_STRING {
            STRSIZE H5T_VARIABLE;
            STRPAD H5T_STR_NULLTERM;
            CSET H5T_CSET_ASCII;
            CTYPE H5T_C_S1;
         }
         DATASPACE  SIMPLE { ( 3 ) / ( 3 ) }
         DATA {
         (0): "dense", "dense_1", "dense_2"
         }
      }
      GROUP "dense" {
         ATTRIBUTE "weight_names" {
            DATATYPE  H5T_STRING {
               STRSIZE H5T_VARIABLE;
               STRPAD H5T_STR_NULLTERM;
               CSET H5T_CSET_ASCII;
               CTYPE H5T_C_S1;
            }
            DATASPACE  SIMPLE { ( 2 ) / ( 2 ) }
            DATA {
            (0): "dense/kernel:0", "dense/bias:0"
            }
         }
         GROUP "dense" {
            DATASET "bias:0" {
               DATATYPE  H5T_IEEE_F32LE
               DATASPACE  SIMPLE { ( 10 ) / ( 10 ) }
               DATA {
               (0): 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
               }
            }
            DATASET "kernel:0" {
               DATATYPE  H5T_IEEE_F32LE
               DATASPACE  SIMPLE { ( 4, 10 ) / ( 4, 10 ) }
               DATA {
               (0,0): -0.229984, 0.55905, -0.00440913, 0.539828, 0.605038,
               (0,5): -0.123624, 0.423012, -0.401372, -0.279511, -0.372577,
               (1,0): -0.521374, -0.240034, 0.547406, 0.0879287, 0.324214,
               (1,5): 0.490638, -0.476336, 0.400679, 0.177176, 0.394547,
               (2,0): -0.282348, 0.561525, 0.12279, -0.625552, -0.176907,
               (2,5): 0.26281, 0.474833, -0.220838, -0.480139, 0.0378248,
               (3,0): 0.266141, 0.642651, 0.245914, -0.28463, -0.494527,
               (3,5): 0.331186, -0.532385, 0.240047, -0.34254, -0.618691
               }
            }
         }
      }
      GROUP "dense_1" {
         ATTRIBUTE "weight_names" {
            DATATYPE  H5T_STRING {
               STRSIZE H5T_VARIABLE;
               STRPAD H5T_STR_NULLTERM;
               CSET H5T_CSET_ASCII;
               CTYPE H5T_C_S1;
            }
            DATASPACE  SIMPLE { ( 2 ) / ( 2 ) }
            DATA {
            (0): "dense_1/kernel:0", "dense_1/bias:0"
            }
         }
         GROUP "dense_1" {
            DATASET "bias:0" {
               DATATYPE  H5T_IEEE_F32LE
               DATASPACE  SIMPLE { ( 10 ) / ( 10 ) }
               DATA {
               (0): 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
               }
            }
            DATASET "kernel:0" {
               DATATYPE  H5T_IEEE_F32LE
               DATASPACE  SIMPLE { ( 10, 10 ) / ( 10, 10 ) }
               DATA {
               (0,0): -0.424829, -0.473763, -0.246853, 0.49395, -0.46109,
               (0,5): -0.323847, 0.400711, 0.185984, -0.044736, 0.0648189,
               (1,0): -0.51151, 0.100563, -0.305169, -0.466093, -0.185254,
               (1,5): 0.436662, -0.225098, -0.467152, -0.215212, -0.293005,
               (2,0): 0.0764917, 0.509988, -0.522192, -0.336315, -0.100952,
               (2,5): -0.251529, -0.492384, -0.485824, 0.0855405, 0.051747,
               (3,0): 0.313386, 0.141108, -0.0913046, 0.0728239, -0.459731,
               (3,5): 0.00487339, 0.069817, 0.208603, -0.427246, -0.164857,
               (4,0): -0.343971, 0.354207, 0.364476, 0.170104, -0.109189,
               (4,5): 0.0741416, -0.442788, 0.30364, -0.506393, -0.347792,
               (5,0): -0.178506, 0.310563, -0.0832336, 0.234843, -0.203555,
               (5,5): 0.0222743, -0.129316, -0.0299608, -0.0531686,
               (5,9): -0.366153,
               (6,0): 0.0599744, -0.31578, -0.278592, 0.157339, 0.0429568,
               (6,5): 0.104149, -0.340642, 0.493082, 0.479793, 0.155183,
               (7,0): -0.144516, 0.434003, -0.152078, -0.292525, 0.0383413,
               (7,5): 0.439195, 0.544749, -0.371441, 0.543013, 0.127611,
               (8,0): 0.07818, -0.4963, -0.540678, 0.376699, 0.272532,
               (8,5): 0.3568, -0.472329, 0.0289415, -0.1749, 0.0130953,
               (9,0): 0.375074, 0.0499882, -0.508902, -0.0186206, 0.527686,
               (9,5): 0.162656, 0.344723, 0.353479, -0.32266, 0.425488
               }
            }
         }
      }
      GROUP "dense_2" {
         ATTRIBUTE "weight_names" {
            DATATYPE  H5T_STRING {
               STRSIZE H5T_VARIABLE;
               STRPAD H5T_STR_NULLTERM;
               CSET H5T_CSET_ASCII;
               CTYPE H5T_C_S1;
            }
            DATASPACE  SIMPLE { ( 2 ) / ( 2 ) }
            DATA {
            (0): "dense_2/kernel:0", "dense_2/bias:0"
            }
         }
         GROUP "dense_2" {
            DATASET "bias:0" {
               DATATYPE  H5T_IEEE_F32LE
               DATASPACE  SIMPLE { ( 3 ) / ( 3 ) }
               DATA {
               (0): 0, 0, 0
               }
            }
            DATASET "kernel:0" {
               DATATYPE  H5T_IEEE_F32LE
               DATASPACE  SIMPLE { ( 10, 3 ) / ( 10, 3 ) }
               DATA {
               (0,0): -0.148028, 0.35095, 0.672949,
               (1,0): -0.109485, 0.527961, -0.502886,
               (2,0): 0.644735, 0.0426024, 0.647439,
               (3,0): 0.478526, -0.540785, -0.360855,
               (4,0): -0.436447, 0.214376, 0.379542,
               (5,0): 0.533146, -0.399072, 0.268073,
               (6,0): 0.382141, -0.251674, -0.115079,
               (7,0): -0.564273, -0.143216, 0.444221,
               (8,0): 0.200261, 0.308898, 0.139336,
               (9,0): 0.665718, -0.251294, -0.0679823
               }
            }
         }
      }
   }
}
}

Hi @guen_gn, You are getting different weights because you are comparing the weights before and after training the model. Before training the model, weights are randomly initialized but after training the weights are updated according to the optimization process. Please refer to this gist for working code. Thank You.