### Weight Initialization Methods for Neural Networks Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Demonstrates two weight initialization strategies for a neural network: the default method, which scales weights based on input connections (recommended), and a large weight method for comparison. This allows for experimenting with different weight distributions. ```python import network2 net = network2.Network([784, 100, 10]) # Default: weights ~ N(0, 1/sqrt(n_in)) - recommended net.default_weight_initializer() # Large weights: weights ~ N(0, 1) - for comparison with Chapter 1 net.large_weight_initializer() ``` -------------------------------- ### Initialize Feedforward Network in Python Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Initializes a feedforward neural network with specified layer sizes. Weights and biases are randomly initialized from a Gaussian distribution. This code demonstrates creating a network and accessing its basic properties. ```python import network import mnist_loader # Create a 3-layer network: 784 input, 30 hidden, 10 output neurons net = network.Network([784, 30, 10]) # Network attributes: print(net.num_layers) # 3 print(net.sizes) # [784, 30, 10] print(len(net.biases)) # 2 (one per non-input layer) print(len(net.weights))# 2 (connections between layers) ``` -------------------------------- ### Load Data for GPU Training (Theano) Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Loads the MNIST dataset and converts it into Theano shared variables. This function is optimized for GPU computation, automatically transferring the data to the GPU if available, which significantly speeds up training for large datasets. ```python import network3 # Load data as Theano shared variables training_data, validation_data, test_data = network3.load_data_shared() # Returns tuples of (shared_x, shared_y) for each dataset # Data is automatically copied to GPU if available ``` -------------------------------- ### Quadratic and Cross-Entropy Cost Functions Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Implements and demonstrates the use of `QuadraticCost` and `CrossEntropyCost` classes. These classes provide methods to calculate the cost and the delta error term required for backpropagation, supporting different loss functions for neural networks. ```python import network2 import numpy as np # Quadratic cost: C = 0.5 * ||a - y||^2 a = np.array([[0.8], [0.2]]) # Network output y = np.array([[1.0], [0.0]]) # Target output z = np.array([[1.0], [0.5]]) # Weighted input cost = network2.QuadraticCost.fn(a, y) # 0.02 delta = network2.QuadraticCost.delta(z, a, y) # Error for backprop # Cross-entropy cost: C = -sum(y*log(a) + (1-y)*log(1-a)) cost = network2.CrossEntropyCost.fn(a, y) # ~0.223 delta = network2.CrossEntropyCost.delta(z, a, y) # Simpler gradient: (a - y) ``` -------------------------------- ### Train Neural Network with SGD in Python Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Trains a neural network using mini-batch stochastic gradient descent (SGD) with backpropagation. The function supports specifying training data, epochs, mini-batch size, learning rate, and optional testing data for evaluation after each epoch. ```python import network import mnist_loader # Load data training_data, validation_data, test_data = mnist_loader.load_data_wrapper() # Create and train network net = network.Network([784, 30, 10]) net.SGD( training_data=list(training_data), # List of (x, y) tuples epochs=30, # Number of training epochs mini_batch_size=10, # Samples per mini-batch eta=3.0, # Learning rate test_data=list(test_data) # Optional: evaluate each epoch ) # Output: # Epoch 0: 9129 / 10000 # Epoch 1: 9256 / 10000 # ... # Epoch 29: 9487 / 10000 ``` -------------------------------- ### Describe Theano-based Activation Functions Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Lists and briefly describes common activation functions available in the network3 library, which utilizes Theano for computation. These include sigmoid, tanh, ReLU, and linear activation. ```python # Theano-based activations (network3.py) # sigmoid - standard logistic function # tanh - hyperbolic tangent, outputs in [-1, 1] # ReLU - rectified linear unit: max(0, z) # linear - identity function: z ``` -------------------------------- ### Implement Sigmoid and Sigmoid Prime Functions (NumPy) Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Provides NumPy-based implementations for the sigmoid activation function and its derivative (sigmoid prime). These are fundamental for neural network backpropagation. ```python import numpy as np # Using numpy-based sigmoid (network.py, network2.py) def sigmoid_np(z): return 1.0 / (1.0 + np.exp(-z)) def sigmoid_prime(z): return sigmoid_np(z) * (1 - sigmoid_np(z)) z = np.array([-2.0, -1.0, 0.0, 1.0, 2.0]) print(sigmoid_np(z)) # [0.119, 0.269, 0.5, 0.731, 0.881] print(sigmoid_prime(z)) # [0.105, 0.197, 0.25, 0.197, 0.105] ``` -------------------------------- ### Save and Load Neural Network State Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Shows how to serialize a trained neural network to a JSON file and then load it back. This process preserves the network's architecture, weights, biases, and cost function, enabling continued training or inference without retraining from scratch. ```python import network2 # Create and train network net = network2.Network([784, 30, 10]) # ... training code ... # Save to file net.save("trained_network.json") # Load from file loaded_net = network2.load("trained_network.json") # Network architecture, weights, biases, and cost function are restored ``` -------------------------------- ### Train Convolutional Neural Network (Theano) Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Constructs and trains a complete Convolutional Neural Network (CNN) architecture using Theano for GPU acceleration. The network is defined as a list of layers, including convolutional, pooling, fully connected, and softmax layers, with support for activation functions and dropout. ```python import network3 from network3 import Network, ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer from network3 import ReLU # Load data training_data, validation_data, test_data = network3.load_data_shared() mini_batch_size = 10 # Define CNN architecture net = Network([ ConvPoolLayer( filter_shape=(20, 1, 5, 5), image_shape=(mini_batch_size, 1, 28, 28), activation_fn=ReLU ), ConvPoolLayer( filter_shape=(40, 20, 5, 5), image_shape=(mini_batch_size, 20, 12, 12), activation_fn=ReLU ), FullyConnectedLayer( n_in=40*4*4, n_out=100, activation_fn=ReLU, p_dropout=0.5 ), SoftmaxLayer(n_in=100, n_out=10, p_dropout=0.5) ], mini_batch_size) ``` -------------------------------- ### Load MNIST Data with Python Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Loads and preprocesses the MNIST handwritten digit dataset. It provides data in two formats: raw numpy arrays and tuples formatted for neural network training, including one-hot encoded labels. ```python import mnist_loader # Load raw data as numpy arrays training_data, validation_data, test_data = mnist_loader.load_data() # training_data[0].shape = (50000, 784) - images # training_data[1].shape = (50000,) - labels (0-9) # Load data formatted for neural network training training_data, validation_data, test_data = mnist_loader.load_data_wrapper() # training_data: list of 50,000 tuples (x, y) # x: (784, 1) numpy array - input image # y: (10, 1) numpy array - one-hot encoded label # validation_data/test_data: list of 10,000 tuples (x, y) # x: (784, 1) numpy array - input image # y: integer 0-9 - digit label ``` -------------------------------- ### Train Neural Network using SGD Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Trains a neural network using the Stochastic Gradient Descent (SGD) algorithm. It takes training, validation, and test data, along with parameters like epochs, mini-batch size, learning rate (eta), and L2 regularization (lmbda). ```python net.SGD( training_data=training_data, epochs=60, mini_batch_size=mini_batch_size, eta=0.03, # Learning rate validation_data=validation_data, test_data=test_data, lmbda=0.1 # L2 regularization ) ``` -------------------------------- ### Train Network with L2 Regularization and Monitoring Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Trains a neural network using Stochastic Gradient Descent (SGD) with L2 regularization. It monitors evaluation and training accuracy and cost over a specified number of epochs. The function returns lists containing these metrics for each epoch. ```python evaluation_cost, evaluation_accuracy, training_cost, training_accuracy = net.SGD( training_data=list(training_data), epochs=30, mini_batch_size=10, eta=0.5, # Learning rate lmbda=5.0, # L2 regularization parameter evaluation_data=list(validation_data), monitor_evaluation_accuracy=True, monitor_evaluation_cost=True, monitor_training_accuracy=True, monitor_training_cost=True ) ``` -------------------------------- ### Expand MNIST Training Data with Image Shifting Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Expands the MNIST training dataset by creating translated versions of each image. Each original image is shifted one pixel up, down, left, and right, resulting in a larger dataset for potentially improved model training. ```python import cPickle import gzip import numpy as np # Load original data f = gzip.open("../data/mnist.pkl.gz", 'rb') training_data, validation_data, test_data = cPickle.load(f) f.close() # Expand by shifting images expanded_pairs = [] for x, y in zip(training_data[0], training_data[1]): expanded_pairs.append((x, y)) # Original image = np.reshape(x, (-1, 28)) # Create 4 shifted versions (up, down, left, right) for d, axis, idx_pos, idx in [(1, 0, "first", 0), (-1, 0, "first", 27), (1, 1, "last", 0), (-1, 1, "last", 27)]: new_img = np.roll(image, d, axis) if idx_pos == "first": new_img[idx, :] = np.zeros(28) else: new_img[:, idx] = np.zeros(28) expanded_pairs.append((np.reshape(new_img, 784), y)) # 50,000 * 5 = 250,000 training images print(f"Expanded from 50,000 to {len(expanded_pairs)} training images") ``` -------------------------------- ### Implement SVM Classifier for MNIST Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Implements a Support Vector Machine (SVM) classifier using scikit-learn for MNIST digit classification. It loads data, trains an SVC model, and predicts on test data, providing accuracy metrics. ```python import mnist_loader from sklearn import svm # Load raw data training_data, validation_data, test_data = mnist_loader.load_data() # Train SVM classifier clf = svm.SVC() clf.fit(training_data[0], training_data[1]) # Test predictions predictions = clf.predict(test_data[0]) num_correct = sum(int(a == y) for a, y in zip(predictions, test_data[1])) print(f"SVM Baseline: {num_correct} / {len(test_data[1])} correct") # Typical result: ~9435 / 10000 (94.35% accuracy) ``` -------------------------------- ### Neural Network Feedforward and Evaluation in Python Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Performs a feedforward pass through the network to compute output activations for a given input. Also includes an evaluation method to count correct classifications on a dataset, useful for assessing network performance. ```python import network import numpy as np net = network.Network([784, 30, 10]) # Single prediction input_image = np.random.randn(784, 1) # 28x28 image as column vector output = net.feedforward(input_image) # (10, 1) array of activations predicted_digit = np.argmax(output) # Index of highest activation # Evaluate on test data test_data = [(np.random.randn(784, 1), np.random.randint(10)) for _ in range(100)] correct_count = net.evaluate(test_data) print(f"Accuracy: {correct_count} / {len(test_data)}") ``` -------------------------------- ### Network with Cost Functions and Regularization in Python Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Implements an improved neural network supporting multiple cost functions, such as cross-entropy and quadratic cost. It also incorporates L2 regularization to help prevent overfitting and improve generalization. ```python import network2 import mnist_loader # Load data training_data, validation_data, test_data = mnist_loader.load_data_wrapper() # Create network with cross-entropy cost (default) net = network2.Network([784, 30, 10], cost=network2.CrossEntropyCost) # Alternative: quadratic cost function net_quadratic = network2.Network([784, 30, 10], cost=network2.QuadraticCost) ``` -------------------------------- ### Define Convolutional Neural Network Layers (Theano) Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt Defines various layers commonly used in Convolutional Neural Networks (CNNs), including `ConvPoolLayer` for convolutional and pooling operations, `FullyConnectedLayer` for dense layers, and `SoftmaxLayer` for the output classification layer. Supports different activation functions and dropout for regularization. ```python import network3 from network3 import ConvPoolLayer, FullyConnectedLayer, SoftmaxLayer from network3 import sigmoid, tanh, ReLU mini_batch_size = 10 # Convolutional + Max Pooling layer conv_layer = ConvPoolLayer( filter_shape=(20, 1, 5, 5), # 20 filters, 1 input map, 5x5 kernel image_shape=(mini_batch_size, 1, 28, 28), # Batch, channels, height, width poolsize=(2, 2), # 2x2 max pooling activation_fn=ReLU # ReLU activation ) # Fully connected layer with dropout fc_layer = FullyConnectedLayer( n_in=20*12*12, # Flattened conv output n_out=100, # Hidden units activation_fn=sigmoid, # Sigmoid activation p_dropout=0.5 # 50% dropout during training ) # Output layer with softmax softmax_layer = SoftmaxLayer( n_in=100, # From hidden layer n_out=10, # 10 digit classes p_dropout=0.5 ) ``` -------------------------------- ### Implement Average Darkness Classifier Source: https://context7.com/mnielsen/neural-networks-and-deep-learning/llms.txt A naive baseline classifier that determines digit classification based on the average pixel intensity (darkness) of the training data for each digit. It calculates average darkness per class and then classifies test images by finding the nearest average darkness. ```python import mnist_loader from collections import defaultdict training_data, validation_data, test_data = mnist_loader.load_data() # Compute average darkness per digit class digit_counts = defaultdict(int) darknesses = defaultdict(float) for image, digit in zip(training_data[0], training_data[1]): digit_counts[digit] += 1 darknesses[digit] += sum(image) avgs = {digit: darknesses[digit] / digit_counts[digit] for digit in digit_counts} # Classify by nearest average darkness def guess_digit(image, avgs): darkness = sum(image) distances = {k: abs(v - darkness) for k, v in avgs.items()} return min(distances, key=distances.get) num_correct = sum(int(guess_digit(img, avgs) == digit) for img, digit in zip(test_data[0], test_data[1])) print(f"Average Darkness: {num_correct} / {len(test_data[1])} correct") # Typical result: ~2225 / 10000 (22.25% accuracy) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.