<jupyter_start><jupyter_text>Neural Network Example本程序将使用 TensorFlow v2.0 搭建具有两个全连接层的神经网络(不使用 Keras 定义网络). 神经网络的结构 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
import numpy as np
# Mnist 数据集参数
num_classes = 10 # 输出为0~9共10个类别
num_features = 784 # 输入图片尺寸为28x28,展开成28x28=784的向量

# 训练参数
learning_rate = 0.001 # 学习率
training_steps = 3000
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)
# 存储全连接层的权重值和偏置值

# A random value generator to initialize weights.
random_normal = tf.initializers.RandomNormal()

weights = {
    'h1': tf.Variable(random_normal([num_features, n_hidden_1])),
    'h2': tf.Variable(random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(random_normal([n_hidden_2, num_classes]))
}

biases = {
    'b1': tf.Variable(tf.zeros([n_hidden_1])),
    'b2': tf.Variable(tf.zeros([n_hidden_2])),
    'out': tf.Variable(tf.zeros([num_classes]))
}
# 创建模型
def neural_net(x):
    # 第1个全连接层
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    # 并使用 sigmoid 作为激活函数
    layer_1 = tf.nn.sigmoid(layer_1)
    
    # 第2个全连接层
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    # 也使用 sigmoid 作为激活函数
    layer_2 = tf.nn.sigmoid(layer_2)
    
    # 输出层,每一个神经元表示0~9中的一类
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    # 使用 softmax 输出后验概率
    return tf.nn.softmax(out_layer)
# Cross-Entropy loss function.
def cross_entropy(y_pred, y_true):
    # Encode label to a one hot vector.
    y_true = tf.one_hot(y_true, depth=num_classes)
    # Clip prediction values to avoid log(0) error.
    y_pred = tf.clip_by_value(y_pred, 1e-9, 1.)
    # Compute cross-entropy.
    return tf.reduce_mean(-tf.reduce_sum(y_true * tf.math.log(y_pred)))

# Accuracy metric.
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 优化器
def run_optimization(x, y):
    # Wrap computation inside a GradientTape for automatic differentiation.
    with tf.GradientTape() as g:
        pred = neural_net(x)
        loss = cross_entropy(pred, y)
        
    # Variables to update, i.e. trainable variables.
    trainable_variables = list(weights.values()) + list(biases.values())

    # Compute gradients.
    gradients = g.gradient(loss, trainable_variables)
    
    # Update W and b following gradients.
    optimizer.apply_gradients(zip(gradients, trainable_variables))
# 训练
for step, (batch_x, batch_y) in enumerate(train_data.take(training_steps), 1):
    # 运行 optimization 更新 W 和 b
    run_optimization(batch_x, batch_y)
    
    if step % display_step == 0:
        pred = neural_net(batch_x)
        loss = cross_entropy(pred, batch_y)
        acc = accuracy(pred, batch_y)
        print("step: %i, loss: %f, accuracy: %f" % (step, loss, acc))
# Test model on validation set.
pred = neural_net(x_test)
print("Test Accuracy: %f" % accuracy(pred, y_test))
# Visualize predictions.
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.12429250622594522, 'fraction_numerical': 0.03101652705456192, 'mean_word_length': 4.420858895705521, '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': '631440', 'license': 'MIT', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1873, 'n_words': 363, 'path': 'tensorflow2.0/neutral_networks/neural_network_raw.ipynb', 'repo_name': 'YujieShui/up-up-tensorflow', 'size': '4417'}