| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import os
- os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
- import tensorflow as tf
- from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
- def preprocess(x, y):
- """
- x is a simple image, not a batch
- """
- x = tf.cast(x, dtype=tf.float32) / 255.
- x = tf.reshape(x, [28*28])
- y = tf.cast(y, dtype=tf.int32)
- y = tf.one_hot(y, depth=10)
- return x,y
- batchsz = 128
- (x, y), (x_val, y_val) = datasets.mnist.load_data()
- print('datasets:', x.shape, y.shape, x.min(), x.max())
- db = tf.data.Dataset.from_tensor_slices((x,y))
- db = db.map(preprocess).shuffle(60000).batch(batchsz)
- ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
- ds_val = ds_val.map(preprocess).batch(batchsz)
- sample = next(iter(db))
- print(sample[0].shape, sample[1].shape)
- network = Sequential([layers.Dense(256, activation='relu'),
- layers.Dense(128, activation='relu'),
- layers.Dense(64, activation='relu'),
- layers.Dense(32, activation='relu'),
- layers.Dense(10)])
- network.build(input_shape=(None, 28*28))
- network.summary()
- network.compile(optimizer=optimizers.Adam(lr=0.01),
- loss=tf.losses.CategoricalCrossentropy(from_logits=True),
- metrics=['accuracy']
- )
- network.fit(db, epochs=3, validation_data=ds_val, validation_freq=2)
-
- network.evaluate(ds_val)
- network.save_weights('weights.ckpt')
- print('saved weights.')
- del network
- network = Sequential([layers.Dense(256, activation='relu'),
- layers.Dense(128, activation='relu'),
- layers.Dense(64, activation='relu'),
- layers.Dense(32, activation='relu'),
- layers.Dense(10)])
- network.compile(optimizer=optimizers.Adam(lr=0.01),
- loss=tf.losses.CategoricalCrossentropy(from_logits=True),
- metrics=['accuracy']
- )
- network.load_weights('weights.ckpt')
- print('loaded weights!')
- network.evaluate(ds_val)
|