一 第一个神经网络实例
参照《TensorFlow自然语言处理-图姗 加格内达拉》 书籍学习,经整理形成文档。
1.1 神经网络步骤:
- 准备数据
- 定义 tensorflow 图
- 运行神经网络
中间还有许多小步骤,会逐一介绍
1.2 代码实例:
import struct
import gzip
import os
from six.moves.urllib.request import urlretrieve
import numpy as np
# import matplotlib as plt
import matplotlib.pyplot as plt
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
def maybe_download(url, filename, expected_bytes, force=False):
"""Download a file if not present, and make sure it's the right size."""
if force or not os.path.exists(filename):
print('Attempting to download:', filename)
filename, _ = urlretrieve(url + filename, filename)
print('\nDownload Complete!')
statinfo = os.stat(filename)
if statinfo.st_size == expected_bytes:
print('Found and verified', filename)
else:
raise Exception(
'Failed to verify ' + filename + '. Can you get to it with a browser?')
return filename
def read_mnist(fname_img, fname_lbl):
print('\nReading files %s and %s' % (fname_img, fname_lbl))
with gzip.open(fname_img) as fimg:
magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16))
print(num, rows, cols)
img = (np.frombuffer(fimg.read(num * rows * cols), dtype=np.uint8).reshape(num, rows * cols)).astype(np.float32)
print('(Images) Returned a tensor of shape ', img.shape)
img = (img - np.mean(img)) / np.std(img)
with gzip.open(fname_lbl) as flbl:
# flbl.read(8) reads upto 8 bytes
magic, num = struct.unpack(">II", flbl.read(8))
lbl = np.frombuffer(flbl.read(num), dtype=np.int8)
print('(Labels) Returned a tensor of shape: %s' % lbl.shape)
print('Sample labels: ', lbl[:10])
return img, lbl
# Download data if needed
url = 'http://yann.lecun.com/exdb/mnist/'
# training data
maybe_download(url, 'train-images-idx3-ubyte.gz', 9912422)
maybe_download(url, 'train-labels-idx1-ubyte.gz', 28881)
# testing data
maybe_download(url, 't10k-images-idx3-ubyte.gz', 1648877)
maybe_download(url, 't10k-labels-idx1-ubyte.gz', 4542)
# Read the training and testing data
train_inputs, train_labels = read_mnist('train-images-idx3-ubyte.gz', 'train-labels-idx1-ubyte.gz')
test_inputs, test_labels = read_mnist('t10k-images-idx3-ubyte.gz', 't10k-labels-idx1-ubyte.gz')
WEIGHTS_STRING = 'weights'
BIAS_STRING = 'bias'
batch_size = 100
img_width, img_height = 28,28
input_size = img_height * img_width
num_labels = 10
# resets the default graph Otherwise raises an error about already initialized variables
tf.compat.v1.reset_default_graph()
tf_inputs = tf.compat.v1.placeholder(shape=[batch_size, input_size], dtype=tf.float32, name = 'inputs')
tf_labels = tf.compat.v1.placeholder(shape=[batch_size, num_labels], dtype=tf.float32, name = 'labels')
# Defining the Tensorflow variables
def define_net_parameters():
with tf.compat.v1.variable_scope('layer1'):
tf.compat.v1.get_variable(WEIGHTS_STRING, shape=[input_size, 500],
initializer=tf.random_normal_initializer(0, 0.02))
tf.compat.v1.get_variable(BIAS_STRING, shape=[500],
initializer=tf.random_uniform_initializer(0, 0.01))
with tf.compat.v1.variable_scope('layer2'):
tf.compat.v1.get_variable(WEIGHTS_STRING, shape=[500, 250],
initializer=tf.random_normal_initializer(0, 0.02))
tf.compat.v1.get_variable(BIAS_STRING, shape=[250],
initializer=tf.