save_load_weight.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import os
  2. os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
  3. import tensorflow as tf
  4. from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
  5. def preprocess(x, y):
  6. """
  7. x is a simple image, not a batch
  8. """
  9. x = tf.cast(x, dtype=tf.float32) / 255.
  10. x = tf.reshape(x, [28*28])
  11. y = tf.cast(y, dtype=tf.int32)
  12. y = tf.one_hot(y, depth=10)
  13. return x,y
  14. batchsz = 128
  15. (x, y), (x_val, y_val) = datasets.mnist.load_data()
  16. print('datasets:', x.shape, y.shape, x.min(), x.max())
  17. db = tf.data.Dataset.from_tensor_slices((x,y))
  18. db = db.map(preprocess).shuffle(60000).batch(batchsz)
  19. ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
  20. ds_val = ds_val.map(preprocess).batch(batchsz)
  21. sample = next(iter(db))
  22. print(sample[0].shape, sample[1].shape)
  23. network = Sequential([layers.Dense(256, activation='relu'),
  24. layers.Dense(128, activation='relu'),
  25. layers.Dense(64, activation='relu'),
  26. layers.Dense(32, activation='relu'),
  27. layers.Dense(10)])
  28. network.build(input_shape=(None, 28*28))
  29. network.summary()
  30. network.compile(optimizer=optimizers.Adam(lr=0.01),
  31. loss=tf.losses.CategoricalCrossentropy(from_logits=True),
  32. metrics=['accuracy']
  33. )
  34. network.fit(db, epochs=3, validation_data=ds_val, validation_freq=2)
  35. network.evaluate(ds_val)
  36. network.save_weights('weights.ckpt')
  37. print('saved weights.')
  38. del network
  39. network = Sequential([layers.Dense(256, activation='relu'),
  40. layers.Dense(128, activation='relu'),
  41. layers.Dense(64, activation='relu'),
  42. layers.Dense(32, activation='relu'),
  43. layers.Dense(10)])
  44. network.compile(optimizer=optimizers.Adam(lr=0.01),
  45. loss=tf.losses.CategoricalCrossentropy(from_logits=True),
  46. metrics=['accuracy']
  47. )
  48. network.load_weights('weights.ckpt')
  49. print('loaded weights!')
  50. network.evaluate(ds_val)