<jupyter_start><jupyter_text>Part 2: An Untrained networkWe're going to start with a basic neural network that is designed to learn how to label colors. Don't worry, you don't need to know how the neural network works yet, but what we're going to learn first is about the need for *training* the network. We're going to start with a blank, untrained network, and see how it does. You could think of this network as a newborn baby who opens its eyes for the first time, not knowing the names of anything yet, but able to receive red, green, and blue color information. We're going to see how the baby does when no one has taught it any names yet.Go ahead and click Run.<jupyter_code>from keras.layers import Activation, Dense, Dropout
from keras.models import Sequential
import keras.optimizers, keras.utils, numpy
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer

def train():
    """
    Creates a blank, untrained neural network.
    """

    # We always need a couple of output neurons. Later we'll use actual colors.
    numColors = 2
    colorLabels = [ 'none1', 'none2' ]
    
    # Hyperparameters to define the network shape.
    numFullyConnectedPerceptrons = numColors * 4
    
    model = Sequential([
        # Layer 1: Fully connected layer with ReLU activation.
        Dense(numFullyConnectedPerceptrons, activation='relu', kernel_initializer='TruncatedNormal', input_shape=(3,)),

        # Outputs: SoftMax activation to get probabilities by color.
        Dense(numColors, activation='softmax')
    ])

    print(model.summary())

    # Compile for categorization.
    model.compile(
        optimizer = keras.optimizers.SGD(lr = 0.01, momentum = 0.9, decay = 1e-6, nesterov = True),
        loss = 'categorical_crossentropy',
        metrics = [ 'accuracy' ])

    return (model, colorLabels)
    
(colorModel, colorLabels) = train()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_9 (Dense)              (None, 8)                 32        
_________________________________________________________________
dense_10 (Dense)             (None, 2)                 18        
=================================================================
Total params: 50
Trainable params: 50
Non-trainable params: 0
_________________________________________________________________
None<jupyter_text>Above you can see a text summary of the shape of the neural network. Ready for a bit of complication? We're using a 1980s-style two-layer network. The first layer uses 4 perceptrons per color, and is connected to a second layer that has one output predictor neuron per color.Don't worry, we'll learn more about what this means as we go along.Just like in part 1, we can use RGB color sliders to set a color. But this time we get to see what the neural network predicts for each color. The color names are in one list like: `['none1', 'none2']` And the next list shows the percentage likelihood of each of those colors being correct, like: `[49.5 50.5]` Go ahead and play with the sliders and make whatever colors you want. Keep an eye on the percentages. We'll discuss below.<jupyter_code>from IPython.core.display import display, HTML
from ipywidgets import interact
def displayColor(r, g, b):
    rInt = min(255, max(0, int(r * 255.0)))
    gInt = min(255, max(0, int(g * 255.0)))
    bInt = min(255, max(0, int(b * 255.0)))
    hexColor = "#%02X%02X%02X" % (rInt, gInt, bInt)
    display(HTML('<div style="width: 50%; height: 50px; background: ' + hexColor + ';"></div>'))

@interact(r = (0.0, 1.0, 0.01), g = (0.0, 1.0, 0.01), b = (0.0, 1.0, 0.01))
def getPredictionsFromModel(r, g, b):
    testColor = numpy.array([ (r, g, b) ])
    predictions = colorModel.predict(testColor, verbose=0)  # Predictions shape (1, numColors)
    predictions *= 100.0
    print(colorLabels)
    numpy.set_printoptions(precision=1, suppress=True)
    print(predictions[0])
    displayColor(r, g, b)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11805890873973926, 'fraction_numerical': 0.025350072428778368, 'mean_word_length': 3.8118466898954706, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '282233', 'license': 'MIT', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1139, 'n_words': 465, 'path': 'Part02_An_Untrained_Network.ipynb', 'repo_name': 'erikma/ColorMatching', 'size': '4142'}