compile_fit.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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,
  33. validation_steps=2)
  34. network.evaluate(ds_val)
  35. sample = next(iter(ds_val))
  36. x = sample[0]
  37. y = sample[1] # one-hot
  38. pred = network.predict(x) # [b, 10]
  39. # convert back to number
  40. y = tf.argmax(y, axis=1)
  41. pred = tf.argmax(pred, axis=1)
  42. print(pred)
  43. print(y)