I'm running a simple CNN with the mnist dataset on tensorflow 2.4, and I want to save all the weights to a .csv file, the code I've tried is as following
for layer in model_train.layers:
weights = layer.get_weights()
print(weights)
np.savetxt('weight.csv' , weights , fmt='%s', delimiter=',')
and
weight = model_train.get_weights()
np.savetxt('weight.csv' , weight , fmt='%s', delimiter=',')
and this method that I found
all of the result above end up saving the .csv file something like this that isnt saving every single weight in the layer, is there a way that can save every weight? or did I do something wrong?
My model
model_train = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(28, 28)),
tf.keras.layers.Reshape(target_shape=(28, 28, 1)),
tf.keras.layers.Conv2D(filters=12, kernel_size=(3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(10)
])
model_train.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True),
metrics=[tf.metrics.SparseCategoricalAccuracy()]
)
model_train.fit(
train_images,
train_labels,
epochs=5,
batch_size=480,
validation_split=0.2
)
model_train.save('./mnist_model.h5') # creates a HDF5 file 'my_model.h5'
weight = model_train.get_weights()
np.savetxt('weight.csv' , weight , fmt='%s', delimiter=',')