代码对网上mnist.py文件进行了简单的修改实现!
import os
import gzip
from urllib.request import urlopen
import numpy as np
import tensorflow as tf
def load_data(fpath='D:\\Science\\tensorflow-adversarial-master\\mnist.npz'):
if not os.path.exists(fpath):
print('Downloading mnist dataset')
_mkdata(fpath)
db = np.load(fpath)
X_train, y_train = db['x_train'], db['y_train']
X_test, y_test = db['x_test'], db['y_test']
y_train=to_categorial(y_train,10)
y_test=to_categorial(y_test,10)
X_train=np.reshape(X_train,(X_train.shape[0], X_train.shape[1]*X_train.shape[2]))
X_test=np.reshape(X_test,(X_test.shape[0], X_test.shape[1]*X_test.shape[2]))
return (X_train, y_train), (X_test, y_test)
def _mkdata(fpath):
_download('/var/tmp/mnist_X_train.gz',
'http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz')
_download('/var/tmp/mnist_y_train.gz',
'http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz')
_download('/var/tmp/mnist_X_test.gz',
'http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz')
_download('/var/tmp/mnist_y_test.gz',
'http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz')
X_train = _extract_images('/var/tmp/mnist_X_train.gz')
y_train = _extract_labels('/var/tmp/mnist_y_train.gz')
y_train = np.expand_dims(y_train, 1)
X_test = _extract_images('/var/tmp/mnist_X_test.gz')
y_test = _extract_labels('/var/tmp/mnist_y_test.gz')
y_test = np.expand_dims(y_test, 1)
np.savez_compressed(fpath, X_train=X_train, y_train=y_train,
X_test=X_test, y_test=y_test)
def _read32(bytestream):
dt = np.dtype(np.uint32).newbyteorder('>')
return np.frombuffer(bytestream.read(4), dtype=dt)[0]
def _extract_images(fpath):
with gzip.GzipFile(fpath) as bytestream:
magic = _read32(bytestream)
if magic != 2051:
raise ValueError('Invalid magic number {0} in MNIST image file: {1}'.format(magic, f.name))
num_images = _read32(bytestream)
rows = _read32(bytestream)
cols = _read32(bytestream)
buf = bytestream.read(rows*cols*num_images)
data = np.frombuffer(buf, dtype=np.uint8)
data = data.reshape(num_images, rows, cols)
return data
def _extract_labels(fpath):
with gzip.GzipFile(fpath) as bytestream:
magic = _read32(bytestream)
if magic != 2049:
raise ValueError('Invalid magic number {0} in MNIST label file: {1}'.format(magic, f.name))
num_items = _read32(bytestream)
buf = bytestream.read(num_items)
labels = np.frombuffer(buf, dtype=np.uint8)
return labels
def _download(fpath, url):
if os.path.exists(fpath):
return
workdir = os.path.dirname(fpath) or '.'
os.makedirs(workdir, exist_ok=True)
with urlopen(url) as ret, open(fpath, 'wb') as w:
w.write(ret.read())
def to_categorial(y,n_classes):
y_std=np.zeros([len(y),n_classes])
for i in range(len(y)):
y_std[i,y[i]]=1.0
return y_std
if __name__ == '__main__':
#load_data()
(X_train, y_train), (X_test, y_test) = load_data()
x=tf.placeholder(tf.float32,[None,784])
W=tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))
y=tf.nn.softmax(tf.matmul(x,W)+b)
y_=tf.placeholder(tf.float32,[None,10])
z=y_*tf.log(y)
cross=tf.reduce_sum(y_*tf.log(y),[1])
cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
init_op=tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
batch_xs, batch_ys = X_train[0:200],y_train[0:200]
#sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
print(sess.run(cross_entropy,feed_dict={x: batch_xs, y_: batch_ys}))
1)我们来确定z的维度,即y_*tf.log(y)返回的维度:
z=y_*tf.log(y)
...
sess.run((z, feed_dict={x: batch_xs, y_: batch_ys}).shape)
运行结果:
所以我们可以确定这里的”*”是点乘运算,而不是矩阵乘法,即(N,10)和(N,10)的矩阵想乘仍旧是(N,10),因为对应的每个位置进行相应的点乘运算即可!
2)现在我们来确定cross的维度,即tf.reduce_sum(y_*tf.log(y),[1])的维度:
cross=tf.reduce_sum(y_*tf.log(y),[1])
...
print(sess.run(cross,feed_dict={x: batch_xs, y_: batch_ys}).shape)
运行结果:
我们也可以来看一下其中的值:
cross=tf.reduce_sum(y_*tf.log(y),[1])
...
print(sess.run(cross,feed_dict={x: batch_xs, y_: batch_ys}))
运行结果:
3)现在来看cross_entropy的值:
cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
...
print(sess.run(cross_entropy,feed_dict={x: batch_xs, y_: batch_ys}))
运行结果: