<jupyter_start><jupyter_text>--> Importations<jupyter_code>import tensorflow as tf
import numpy as np<jupyter_output><empty_output><jupyter_text>--> Import dataset<jupyter_code>from sklearn.preprocessing import StandardScaler
fashion_mnist = tf.keras.datasets.fashion_mnist
(images, targets), (_, _) = fashion_mnist.load_data()
images = images[:10000]
targets = targets[:10000]
images = images.reshape(-1, 784)
images = images.astype(float)
scaler = StandardScaler()
images = scaler.fit_transform(images)<jupyter_output><empty_output><jupyter_text>--> Create a model using subclassing<jupyter_code>class CustomModel(tf.keras.Model): #On customize notre model grace au subclassing
    
    #Ici on voit qu'on est plus flexible au niveau des definitions de couche compare au modele Sequential(). En effet, on peut ici
    #definir nos couches sans leurs donner d'ordre particulier...
    def __init__(self):
        super(CustomModel, self).__init__()                                                      #Initialise la classe parente
        self.first_layer = tf.keras.layers.Dense(64, activation="linear", name="first_layer")    #Ajout des layers
        self.output_layer = tf.keras.layers.Dense(10, activation="softmax", name="output_layer") #Attention ne pas appeler la variable output
        
    #C'est ici qu'on joint les differentes couches "a la main".
    def call(self, x):
        prev = self.first_layer(x)
        #prev = tf.nn.sigmoid(prev)   #On peut directement ajouter des fonctions d'activations et modifier des valeurs
        out = self.output_layer(prev)
        return out                    #On peut aussi return prev pour voir l'etat de la premiere couche

model = CustomModel()
prediction = model.predict(images[0:1])
print("Predictions :\n", prediction)<jupyter_output>Predictions :
 [[0.01869108 0.04386951 0.00327567 0.00123411 0.6297261  0.00246293
  0.01765561 0.00465648 0.18932012 0.08910844]]<jupyter_text>--> Compile and train our model the normal way<jupyter_code>model.compile(
    loss="sparse_categorical_crossentropy",
    optimizer="sgd",
    metrics=["accuracy"]
)
history = model.fit(images, targets, epochs=10)<jupyter_output>Epoch 1/10
313/313 [==============================] - 0s 497us/step - loss: 0.3697 - accuracy: 0.8732
Epoch 2/10
313/313 [==============================] - 0s 598us/step - loss: 0.3745 - accuracy: 0.8748
Epoch 3/10
313/313 [==============================] - 0s 540us/step - loss: 0.3568 - accuracy: 0.8767
Epoch 4/10
313/313 [==============================] - 0s 582us/step - loss: 0.3446 - accuracy: 0.8817
Epoch 5/10
313/313 [==============================] - 0s 531us/step - loss: 0.3560 - accuracy: 0.8766
Epoch 6/10
313/313 [==============================] - 0s 537us/step - loss: 0.3382 - accuracy: 0.8811
Epoch 7/10
313/313 [==============================] - 0s 656us/step - loss: 0.3416 - accuracy: 0.8807
Epoch 8/10
313/313 [==============================] - 0s 545us/step - loss: 0.3309 - accuracy: 0.8859
Epoch 9/10
313/313 [==============================] - 0s 521us/step - loss: 0.3357 - accuracy: 0.8843
Epoch 10/10
313/313 [==============================] - 0s 504us/step - loss: 0.342[...]
{'fraction_non_alphanumeric': 0.2037153652392947, 'fraction_numerical': 0.10579345088161209, 'mean_word_length': 3.971830985915493, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '440665', 'license': 'MIT', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1021, 'n_words': 264, 'path': 'Notebooks/Subclassing/main.ipynb', 'repo_name': 'hanzopgp/TensorFlowTest', 'size': '3176'}