import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
n_inputs=28*28
n_hidden1=300
n_hidden2=100
n_classes=10
X=tf.placeholder(tf.float32,shape=(None,n_inputs))
y=tf.placeholder(tf.int64,shape=(None,n_classes))
hidden1=tf.layers.dense(X,n_hidden1,activation=tf.nn.relu)
hidden2=tf.layers.dense(hidden1,n_hidden2,activation=tf.nn.relu)
outputs=tf.layers.dense(hidden2,n_classes)
cross_entropy=tf.nn.softmax_cross_entropy_with_logits(labels=y,
logits=outputs)
loss=tf.reduce_mean(cross_entropy)
optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_op=optimizer.minimize(loss)
correct=tf.equal(tf.argmax(y,1),tf.argmax(outputs,1))
accuracy_score=tf.reduce_mean(tf.cast(correct,tf.float32))
with tf.Session() as sess:
tf.global_variables_initializer().run()
mnist=input_data.read_data_sets('D:/Mechine/Project/MNIST_data/',one_hot=True)
#有改动,原本为'MNIST_data/',发生错误(由于连接方在一段时间后...),故直接登录mnist下载数据集压缩包,保存到代码中路径下
X_train,y_train=mnist.train.images,mnist.train.labels
X_test,y_test=mnist.test.images,mnist.test.labels
for t in range(50000):
i=np.random.randint(0,len(X_train))
X_i=X_train[i].reshape(1,-1)
y_i=y_train[i].reshape(1,-1)
sess.run(train_op,feed_dict={X:X_i,y:y_i})
accuracy=accuracy_score.eval(feed_dict={X:X_test,y:y_test})
print('accuracy={}'.format(accuracy))
以下内容参考自TensorFlow Core v1.15.0官方文档
tf.placeholder:Inserts a placeholder for a tensor that will be always fed.
tf.placeholder(dtype, shape=None, name=None)
tf.layers.dense :Functional interface for the densely-connected layer. (deprecated)
tf.layers.dense(inputs, units, activation=None)
tf.nn.softmax_cross_entropy_with_logits:Computes softmax cross entropy between logits and labels. (deprecated)
tf.nn.softmax_cross_entropy_with_logits(labels=None, logits=None)
tf.math.reduce_mean:Computes the mean of elements across dimensions of a tensor.If axis is None, all dimensions are reduced, and a tensor with a single element is returned.
tf.math.reduce_mean(input_tensor, axis=None)
tf.train.GradientDescentOptimizer:Optimizer that implements the gradient descent algorithm.
tf.train.GradientDescentOptimizer(learning_rate)
tf.train.GradientDescentOptimizer.minimize:Add operations to minimize loss by updating var_list.
minimize(loss)
tf.cast:Casts a tensor to a new type.
tf.cast(x, dtype, name=None)
tf.Session:A class for running TensorFlow operations.
tf.Session.run:Runs operations and evaluates tensors in fetches.
run(fetches, feed_dict=None)
fetches:A single graph element, a list of graph elements, or a dictionary whose values are graph elements or lists of graph elements (described above).
feed_dict:A dictionary that maps graph elements to values (described above).
A graph element can be one of the following types: tf.Operation,tf.Tensor,tf.SparseTensor,get_tensor_handle,string
tf.keras.backend.eval:Evaluates the value of a variable.
tf.keras.backend.eval(x)
import numpy as np
from tensorflow import keras
from sklearn.preprocessing import StandardScaler
#读数据集
mnist = keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data('D:/Mechine/Project/MNIST_data/mnist.npz') #同理,前往官网下载数据集
#归一化处理,非常重要!
scaler = StandardScaler()
scaled_x_train = scaler.fit_transform(x_train.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
scaled_x_test = scaler.transform(x_test.astype(np.float32).reshape(-1,1)).reshape(-1,28,28)
#创建模型
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28,28)))
model.add(keras.layers.Dense(300,activation='relu'))
model.add(keras.layers.Dense(100,activation='relu'))
model.add(keras.layers.Dense(10,activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])
history = model.fit(scaled_x_train,y_train,epochs=5)
print(history.history)
loss,accuracy = model.evaluate(x_test,y_test)
print('accuracy={}'.format(accuracy))
以下内容参考自TensorFlow Core v2.1.0官方文档
tf.keras.datasets.mnist.load_data:Loads the MNIST dataset.
tf.keras.datasets.mnist.load_data(path=‘mnist.npz’)
Arguments:path where to cache the dataset locally (relative to ~/.keras/datasets).
Returns:Tuple of Numpy arrays: (x_train, y_train), (x_test, y_test).
tf.keras.Sequential:Linear stack of layers.
tf.keras.Sequential(layers=None, name=None)
Methods:
add:Adds a layer instance on top of the layer stack.
add(layer)
compile:Configures the model for training.
compile(optimizer=‘rmsprop’, loss=None, metrics=None)
optimizer:String (name of optimizer) or optimizer instance.
loss:String (name of objective function), objective function or tf.keras.losses.Loss instance.
metrics:List of metrics to be evaluated by the model during training and testing. Typically you will use metrics=[‘accuracy’].
fit:Trains the model for a fixed number of epochs (iterations on a dataset).
fit(x=None, y=None, epochs=1)
tf.keras.layers.Flatten:Flattens the input. Does not affect the batch size.
tf.keras.layers.Flatten(input_shape=None)
tf.keras.layers.Dense:Just your regular densely-connected NN layer.
tf.keras.layers.Dense(units, activation=None)
[1]王磊,王晓东著.机器学习算法导论[M].北京:清华大学出版社,2019:217.
[2]TensorFlow Core v2.1.0.
[3]TensorFlow Core v1.15.0.