import tensorflow as tf
# load data set
Mnist=tf. Keras. Datasets. Mnist
# load data, the data load was divided into training set and test set
# training set data x_train shape for,28,28 (60000)
# training set label y_train data shape (60000)
# test set data x_test shape for,28,28 (10000)
# test data set label y_test shape for (10000)
(x_train y_train), (x_test, y_test)=mnist. Load_data ()
# was carried out on the training set and testing set of data normalization processing, help to improve model training speed
X_train, x_test=x_train/255.0, x_test/255.0
# the training set and testing set of tags to hot coding alone
Y_train=tf. Keras. Utils. To_categorical (y_train, num_classes=10)
Y_test=tf. Keras. Utils. To_categorical (y_test, num_classes=10)
# create a dataset object, use the dataset object to manage the data
Mnist_train=tf. Data. The Dataset. From_tensor_slices ((x_train y_train))
# training cycle is set to 1 (all the training set data training once referred to as a cycle)
Mnist_train=mnist_train. Repeat (1)
# batch size is set to 32 (every training model was introduced into 32 data)
Mnist_train=mnist_train. Batch (32)
# create a dataset object, use the dataset object to manage the data
Mnist_test=tf. Data. The Dataset. From_tensor_slices ((x_test y_test))
# training cycle is set to 1 (all the training set data training once referred to as a cycle)
Mnist_test=mnist_test. Repeat (1)
# batch size is set to 32 (every training model was introduced into 32 data)
Mnist_test=mnist_test. Batch (32)
# model definition
# use first Flatten the data from 3 d to 2 d,,28,28 (60000) - & gt; (60000784)
# set the input data shape input_shape don't need to contain the number of data, can be (28, 28)
The model=tf. Keras. Models. Sequential ([
Tf. Keras. The layers. Flatten (input_shape=(28, 28)),
Tf. Keras. The layers. Dense (10, activation='softmax')
])
# the optimizer to define
Optimizer=tf. Keras. Optimizers. SGD (0.1)
Use the average value of the #
Train_loss=tf. Keras. Metrics. Scheme (name='train_loss')
# training accuracy calculation
Train_accuracy=tf. Keras. Metrics. CategoricalAccuracy (name='train_accuracy')
Use the average value of the #
Test_loss=tf. Keras. Metrics. Scheme (name='test_loss')
# test calculation accuracy
Test_accuracy=tf. Keras. Metrics. CategoricalAccuracy (name='test_accuracy')
# we can use @ tf. The function decorator to python code into tensorflow figure said, used to accelerate the code running speed
# define a training model of function
@ tf. The function
Def train_step (data, label) :
# fixed writing, the use of tf. GradientTape () to calculate the gradient
With tf. GradientTape () as tape:
# incoming data acquisition model predicted results
Predictions=model (data)
Label # contrast and calculating the loss predictions
Loss=tf. Keras. Losses. MSE (label, predictions)
# incoming loss and the model parameters, adjust the weight values
Gradients=tape. Gradient (loss model. Trainable_variables)
# make adjustment weights
Optimizer. Apply_gradients (zip (gradients, model. Trainable_variables))
# calculate average loss
Train_loss (loss)
# to calculate the average accuracy
Train_accuracy (label, predictions)
# we can use @ tf. The function decorator to python code into tensorflow figure said, used to accelerate the code running speed
# define a function of the model test
@ tf. The function
Def test_step (data, label) :
# incoming data acquisition model predicted results
Predictions=model (data)
Label # contrast and calculating the loss predictions
T_loss=tf. Keras. Losses. MSE (label, predictions)
# calculate average loss
Test_loss (t_loss)
# to calculate the average accuracy
Test_accuracy (label, predictions)
10 # training cycle (all the training set data training once referred to as a cycle)
EPOCHS=10
For epoch in range (EPOCHS) :
# training set loop 60000/32=1875
For the image, the label in mnist_train:
# each loop into a batch of training data and the label model
Train_step (image, label)
# test set cycle 10000/32=312.5 - & gt; 313
For test_image test_label mnist_test in:
# each loop into a batch of data and the label test
Test_step (test_image test_label)
# print the results
Template='Epoch {}, Loss: {: 3}, Accuracy: {: 3}, Test Loss: {: 3}, the Test Accuracy: 3} {:'
Print (template. The format (epoch + 1,
Train_loss. Result (),
Train_accuracy. Result (),
Test_loss. Result (),
Test_accuracy. Result ()))
Is this code bosses appear this kind of problem how to solve?
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
Traceback (the most recent call last) :
The File "C: \ Users \ lenovo \ Anaconda3 \ lib \ urllib \ request py", line 1254, in do_open
Practice equest (the req. Get_method (), the req. The selector, the req. Data, headers)
The File "C: \ Users \ lenovo \ Anaconda3 \ lib \ HTTP \ client py", line 1107, in the request
Self. _send_request (method, url, body, headers)
The File "C: \ Users \ lenovo \ Anaconda3 \ lib \ HTTP \ client py", line 1152, in _send_request
Self. Endheaders (body)
The File "C: \ Users \ lenovo \ Anaconda3 \ lib \ HTTP \ client py", line 1103, in endheaders
Self. _send_output (message_body)
The File "C: \ Users \ lenovo \ Anaconda3 \ lib \ HTTP \ client py", line 934, in _send_output
The self. The send (MSG)
The File "C: \ Users \ lenovo \ Anaconda3 \ lib \ HTTP \ client py", line 877, in the send
The self. The connect ()
The File "C: \ Users \ lenovo \ Anaconda3 \ lib \ HTTP \ client py", line 1253, in the connect
Super (). The connect ()
The File "C: \ Users \ lenovo \ Anaconda3 \ lib \ HTTP \ client py", line 849, in the connect
(self. The host, the self. The port), the self. The timeout, the self. Source_address)
The File "C: \ Users \ lenovo \ Anaconda3 \ lib \ socket py", line 694, in create_connection
For res in getaddrinfo (host, port, 0, SOCK_STREAM) :
The File "C: \ Users \ lenovo \ Anaconda3 \ lib \ socket py", line 733, getaddrinfo in
For res in _socket. Getaddrinfo (host, port, family type, proto, flags) :
Socket. Gaierror: [11002] Errno getaddrinfo failed
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull