Wednesday, 30 December 2020

keras 3 save/load model

//main.py
from tensorflow.keras.models import load_model

"""
#create model
model = Sequential([
    Dense(units=16, input_shape=(1,), activation='relu'),
    Dense(units=32, activation='relu'),
    Dense(units=2, activation='softmax')
])
model.summary()

#training & validation
model.compile(optimizer=Adam(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x=scaled_train_samples, y=train_lables, validation_split=0.1, batch_size=10, epochs=30, shuffle=True, verbose=2)

#save model
model.save('models/model1.h5')
"""

#load model
model = load_model('models/model1.h5')
model.summary()
print( model.get_weights())
print(model.optimizer) 

------------------------------
#logs
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
dense (Dense)                (None, 16)                32
_________________________________________________________________
dense_1 (Dense)              (None, 32)                544
_________________________________________________________________
dense_2 (Dense)              (None, 2)                 66
=================================================================
Total params: 642
Trainable params: 642
Non-trainable params: 0
_________________________________________________________________
[array([[ 0.26625147, -0.30418316, -0.13632423, -0.4079098 , -0.08543348,
         0.5374129 ,  0.4584436 , -0.28098437, -0.52589685, -0.4887119 ,
         0.01660078, -0.10933313, -0.04969841, -0.10267085,  0.7305957 ,
         0.38774747]], dtype=float32), array([-0.09283859,  0.        ,  0.        ,  0.        ,  0.        ,
       -0.09529305, -0.13439798,  0.        ,  0.        ,  0.        ,
        0.20418786,  0.        ,  0.        ,  0.        , -0.13027988,
       -0.1161219 ], dtype=float32), array([[ 0.26004615, -0.31933463,  0.21335247,  0.06417851, -0.12653175,
        -0.40698838, -0.2899054 , -0.3321916 , -0.51734823,  0.2656369 ,
         0.25500387, -0.23773687,  0.09429104, -0.5213961 ,  0.5374189 ,
         0.47588807,  0.15414259, -0.26182094, -0.06989337,  0.16001046,
         0.05894524,  0.5102477 ,  0.10094702, -0.20431465, -0.03044271,
        -0.25523925, -0.40259373,  0.07096949, -0.3661572 ,  0.40184933,
        -0.20758483,  0.01585205],
       [ 0.2500454 , -0.31888163,  0.12653747, -0.34026104,  0.207423  ,
         0.22553   , -0.21539871,  0.10445303, -0.30555665, -0.28052825,
         0.0283457 ,  0.21818611,  0.24136344, -0.13501498,  0.01192671,
        -0.10412639,  0.2694293 , -0.22594748, -0.06913146, -0.30933705,
         0.00475213, -0.09345153,  0.20352694, -0.163133  ,  0.13384211,
        -0.19982849, -0.28636962, -0.31544378,  0.1858199 ,  0.1552572 ,
         0.01639351,  0.2738203 ],
     ...
       [ 0.50497067, -0.30627   ],
       [ 0.3936801 , -0.17796172],
       [-0.65509355,  0.02540744],
       [-0.10199237, -0.04108612],
       [ 0.7816102 , -0.721409  ]], dtype=float32), array([ 0.09544881, -0.09544882], dtype=float32)]
<tensorflow.python.keras.optimizer_v2.adam.Adam object at 0x00000145A451F8E0>

loaded model prediction on test data set is still accurate
reference:

No comments:

Post a Comment