compile_fit.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import tensorflow as tf
  2. from tensorflow.keras import datasets, layers, optimizers, Sequential, metrics
  3. def preprocess(x, y):
  4. """
  5. x is a simple image, not a batch
  6. """
  7. x = tf.cast(x, dtype=tf.float32) / 255.
  8. x = tf.reshape(x, [28*28])
  9. y = tf.cast(y, dtype=tf.int32)
  10. y = tf.one_hot(y, depth=10)
  11. return x,y
  12. batchsz = 128
  13. (x, y), (x_val, y_val) = datasets.mnist.load_data()
  14. print('datasets:', x.shape, y.shape, x.min(), x.max())
  15. db = tf.data.Dataset.from_tensor_slices((x,y))
  16. db = db.map(preprocess).shuffle(60000).batch(batchsz)
  17. ds_val = tf.data.Dataset.from_tensor_slices((x_val, y_val))
  18. ds_val = ds_val.map(preprocess).batch(batchsz)
  19. sample = next(iter(db))
  20. print(sample[0].shape, sample[1].shape)
  21. network = Sequential([layers.Dense(256, activation='relu'),
  22. layers.Dense(128, activation='relu'),
  23. layers.Dense(64, activation='relu'),
  24. layers.Dense(32, activation='relu'),
  25. layers.Dense(10)])
  26. network.build(input_shape=(None, 28*28))
  27. network.summary()
  28. network.compile(optimizer=optimizers.Adam(lr=0.01),
  29. loss=tf.losses.CategoricalCrossentropy(from_logits=True),
  30. metrics=['accuracy']
  31. )
  32. network.fit(db, epochs=5, validation_data=ds_val, validation_freq=2)
  33. network.evaluate(ds_val)
  34. sample = next(iter(ds_val))
  35. x = sample[0]
  36. y = sample[1] # one-hot
  37. pred = network.predict(x) # [b, 10]
  38. # convert back to number
  39. y = tf.argmax(y, axis=1)
  40. pred = tf.argmax(pred, axis=1)
  41. print(pred)
  42. print(y)