<jupyter_start><jupyter_text>Neural Network Example本程序将使用 TensorFlow v2.0 搭建具有两个全连接层的神经网络,是用 Mnist 数据集进行训练. 神经网络的结构 Mnist 数据集![MNIST Dataset](http://neuralnetworksanddeeplearning.com/images/mnist_100_digits.png)<jupyter_code>from __future__ import absolute_import, division, print_function

import tensorflow as tf
from tensorflow.keras import Model, layers
import numpy as np

print(tf.__version__)
# Mnist 数据集参数
num_classes = 10 # 输出为0~9共10个类别
num_features = 784 # 输入图片尺寸为28x28,展开成28x28=784的向量

# 训练参数
learning_rate = 0.1 # 学习率
training_steps = 2000
batch_size = 256
display_step = 100 # 每训练100次,输出1次训练信息

# 网络参数
n_hidden_1 = 128 # 第1个全连接层神经元数量
n_hidden_2 = 256 # 第2个全连接层神经元数量
# 使用 karas 导入 mnist 数据集
from tensorflow.keras.datasets import mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
# 载入图片的像素值为0-255,将其转换为 float32 类型,用与后面数据归一化处理
x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)
# 将 28x28 的图片,展开成 28x28=784 的向量
x_train, x_test = x_train.reshape([-1, num_features]), x_test.reshape([-1, num_features])
# 对图片做归一化处理 [0, 255] to [0, 1]
x_train, x_test = x_train / 255., x_test / 255.
# Use tf.data API to shuffle and batch data.
train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_data = train_data.repeat().shuffle(5000).batch(batch_size).prefetch(1)
# 创建 TF 模型
class NeuralNet(Model):
    # 设置两个全连接层
    def __init__(self):
        # Q: 这里为什么要这么写?
        super(NeuralNet, self).__init__()
        # 第1个全连接层
        self.fc1 = layers.Dense(n_hidden_1, activation=tf.nn.relu)
        # 第2个全连接层
        self.fc2 = layers.Dense(n_hidden_2, activation=tf.nn.relu)
        # 输出层
        self.out = layers.Dense(num_classes, activation=tf.nn.softmax)
    
    # 设置前向传播过程
    def call(self, x, is_training=False):
        x = self.fc1(x)
        x = self.fc2(x)
        # tf cross entropy expect logits without softmax, so only
        # apply softmax when not training.
        if not is_training:
            x = tf.nn.softmax(x)
        return x

# 构建神经网络
neural_net = NeuralNet()
# 损失函数使用交叉熵(Cross-Entropy Loss).
# Note that this will apply 'softmax' to the logits.
# Q: 交叉熵的原理,tf中交叉熵函数参数说明?
def cross_entropy_loss(x, y):
    # Convert labels to int 64 for tf cross-entropy function.
    y = tf.cast(y, tf.int64)
    # Apply softmax to logits and compute cross-entropy.
    loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=x)
    # Average loss across the batch.
    return tf.reduce_mean(loss)

# 计算精确度
def accuracy(y_pred, y_true):
    # Predicted class is the index of highest score in prediction vector (i.e. argmax).
    correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.cast(y_true, tf.int64))
    return tf.reduce_mean(tf.cast(correct_prediction, tf.float32), axis=-1)

# Stochastic gradient descent optimizer.
optimizer = tf.optimizers.SGD(learning_rate)
# Optimization process. 
def run_optimization(x, y):
    # Wrap computation inside a GradientTape for automatic differentiation.
    with tf.GradientTape() as g:
        # Forward pass.
        pred = neural_net(x, is_training=True)
        # Compute loss.
        loss = cross_entropy_loss(pred, y)
        
    # Variables to update, i.e. trainable variables.
    trainable_variables = neural_net.trainable_variables

    # Compute gradients.
    gradients = g.gradient(loss, trainable_variables)
    
    # Update W and b following gradients.
    optimizer.apply_gradients(zip(gradients, trainable_variables))
# Run training for the given number of steps.
for step, (batch_x, batch_y) in enumerate(train_data.take(training_steps), 1):
    # Run the optimization to update W and b values.
    run_optimization(batch_x, batch_y)
    
    if step % display_step == 0:
        pred = neural_net(batch_x, is_training=True)
        loss = cross_entropy_loss(pred, batch_y)
        acc = accuracy(pred, batch_y)
        print("step: %i, loss: %f, accuracy: %f" % (step, loss, acc))
# 在验证集上测试模型
pred = neural_net(x_test, is_training=False)
print("Test Accuracy: %f" % accuracy(pred, y_test))
# 可视化预测值
import matplotlib.pyplot as plt
# Predict 5 images from validation set.
n_images = 5
test_images = x_test[:n_images]
predictions = neural_net(test_images)

# Display image and model prediction.
for i in range(n_images):
    plt.imshow(np.reshape(test_images[i], [28, 28]), cmap='gray')
    plt.show()
    print("Model prediction: %i" % np.argmax(predictions.numpy()[i]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10581891709728151, 'fraction_numerical': 0.026286227813974387, 'mean_word_length': 3.8762322015334063, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '380541', 'license': 'MIT', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1840, 'n_words': 389, 'path': 'tensorflow2.0/neutral_networks/neural_network.ipynb', 'repo_name': 'YujieShui/up-up-tensorflow', 'size': '4451'}