<jupyter_start><jupyter_text>Intermediate Neural Network in TensorFlow In this notebook, we improve on our [introductory shallow net](https://github.com/jonkrohn/DLTFpT/blob/master/notebooks/shallow_net_in_tensorflow.ipynb) by incorporating the theory we've covered since. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/jonkrohn/DLTFpT/blob/master/notebooks/intermediate_net_in_tensorflow.ipynb)  Load dependencies<jupyter_code>import tensorflow
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from matplotlib import pyplot as plt<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code>(X_train, y_train), (X_valid, y_valid) = mnist.load_data()<jupyter_output><empty_output><jupyter_text>Preprocess data<jupyter_code>X_train = X_train.reshape(60000, 784).astype('float32')
X_valid = X_valid.reshape(10000, 784).astype('float32')
X_train /= 255
X_valid /= 255
n_classes = 10
y_train = to_categorical(y_train, n_classes)
y_valid = to_categorical(y_valid, n_classes)<jupyter_output><empty_output><jupyter_text>Design neural network architecture<jupyter_code>model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(784,)))
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()
(64*64)
(64*64)+64<jupyter_output><empty_output><jupyter_text>Configure model<jupyter_code># N.B.: learning rate is order of magnitude quicker relative to shallow net
model.compile(loss='categorical_crossentropy', optimizer=SGD(lr=0.1), metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Train!<jupyter_code># N.B.: number of epochs is order of magnitude smaller relative to shallow net
model.fit(X_train, y_train, batch_size=128, epochs=20, verbose=1, validation_data=(X_valid, y_valid))<jupyter_output><empty_output><jupyter_text>Performing inference<jupyter_code>valid_0 = X_valid[0].reshape(1, 784)
model.predict(valid_0)
model.predict_classes(valid_0)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1197632058287796, 'fraction_numerical': 0.028688524590163935, 'mean_word_length': 9.61352657004831, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 3, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '453912', 'license': 'MIT', 'n_tokens_mistral': 836, 'n_tokens_neox': 785, 'n_words': 132, 'path': 'notebooks/intermediate_net_in_tensorflow.ipynb', 'repo_name': 'ewbolme/DLTFpT', 'size': '2196'}