<jupyter_start><jupyter_text>Using numpy array as inputs<jupyter_code>(X_train_1,y_train_1),(X_test_1,y_test_1) = tf.keras.datasets.mnist.load_data()
X_train_1 = X_train_1.astype(np.float32)/255.
X_train_1 = np.expand_dims(X_train_1,axis=-1)
X_test_1 = X_test_1.astype(np.float32)/255.
X_test_1 = np.expand_dims(X_test_1,axis=-1)
y_train_1 = tf.keras.utils.to_categorical(y_train_1,10)
y_test_1 = tf.keras.utils.to_categorical(y_test_1,10)

model_1 = build_cnn()
model_1.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model_1.fit(X_train_1,y_train_1,epochs=epochs,batch_size=batch,validation_data=(X_test_1,y_test_1),verbose=2)
model_1.evaluate(X_test_1,y_test_1)<jupyter_output>10000/10000 [==============================] - 0s 25us/sample - loss: 0.0705 - acc: 0.9782<jupyter_text>Using tf.data.Dataset as inputs<jupyter_code>def preprocessing(img,label):
    img = tf.cast(img,tf.float64)
    img = img/255.
    img = tf.expand_dims(img,axis=-1)
    label = tf.one_hot(label,10,dtype=tf.int32)
    return img,label
(X_train_2,y_train_2),(X_test_2,y_test_2) = tf.keras.datasets.mnist.load_data()

train_ds = tf.data.Dataset.from_tensor_slices((X_train_2,y_train_2)).map(preprocessing,num_parallel_calls=tf.data.experimental.AUTOTUNE).shuffle(256).batch(batch).repeat(epochs).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
test_ds = tf.data.Dataset.from_tensor_slices((X_test_2,y_test_2)).map(preprocessing,num_parallel_calls=tf.data.experimental.AUTOTUNE).batch(batch).repeat(epochs).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

model_2 = build_cnn()
model_2.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model_2.fit(train_ds,epochs=epochs,steps_per_epoch=len(X_train_2)//batch,validation_data=test_ds,validation_steps=5,verbose=2)
model_2.evaluate(test_ds)<jupyter_output>400/400 [==============================] - 3s 8ms/step - loss: 0.0688 - acc: 0.9783<jupyter_text>tensorflow distributed training with Keras<jupyter_code>from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
(X_train_3,y_train_3),(X_test_3,y_test_3) = tf.keras.datasets.mnist.load_data()

mirrored = tf.distribute.MirroredStrategy()
print('\nnumber of replicas in sync: {}'.format(mirrored.num_replicas_in_sync))

batch_per_replica = batch
global_batch = batch_per_replica * mirrored.num_replicas_in_sync
print('global batch: {}'.format(global_batch))
train_ds_dist = tf.data.Dataset.from_tensor_slices((X_train_3,y_train_3)).map(preprocessing,num_parallel_calls=tf.data.experimental.AUTOTUNE).shuffle(256).batch(global_batch).repeat(epochs).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
test_ds_dist = tf.data.Dataset.from_tensor_slices((X_test_3,y_test_3)).map(preprocessing,num_parallel_calls=tf.data.experimental.AUTOTUNE).batch(global_batch).repeat(epochs).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

with mirrored.scope():
    model_3 = build_cnn()
    model_3.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model_3.fit(train_ds_dist,epochs=epochs,steps_per_epoch=len(X_train_3)//batch,validation_data=test_ds_dist,validation_steps=5,verbose=2)
model_3.evaluate(test_ds_dist)<jupyter_output>400/400 [==============================] - 3s 8ms/step - loss: 0.0690 - acc: 0.9781
{'fraction_non_alphanumeric': 0.1636473429951691, 'fraction_numerical': 0.04468599033816425, 'mean_word_length': 9.618589743589743, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '620639', 'license': 'MIT', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1332, 'n_words': 103, 'path': 'Code appendix/Exploring distributed training with keras and tensorflow data module.ipynb', 'repo_name': 'sungsujaing/ML_DL_articles_resources', 'size': '3312'}