Home > other >  Hi I'm drogozhang can ask me if you have any questions
Hi I'm drogozhang can ask me if you have any questions

Time:10-06

Rt I will immediately reply!

CodePudding user response:



Minst deep learning program not convergence

Is about tensorflow problem, I am tensorflow beginners, copied from the book minst learning program, but after running, no matter how many batches, learning the basic success rate constant,

I made many attempts to remove the regularization, remove the moving average, or not, the batch_size changed to 2, the observed variable operation condition, the input x is correct, but the output of the neural network in many cases in x y y under the condition of different two results are identical, thus softmax result is the same, a mystery and could not find the cause of this situation, the code and run situation here are posted, the great god, please help me find the reason, big New Year's day, hope everything goes well with you all a happy Spring Festival,

The import struct
The import numpy as np
The import matplotlib. Pyplot as PLT
The from matplotlib. Widgets import the Slider, the Button
The import tensorflow as tf
The import time

# encapsulate MNIST operating in a class, easy to use, after
The class MyMinst () :

Def decode_idx3_ubyte (self, idx3_ubyte_file) :
With the open (idx3_ubyte_file, 'rb') as f:
Print (' parse the file: 'idx3_ubyte_file)
Fb_data=(https://bbs.csdn.net/topics/f.read)

Offset=0
Fmt_header='& gt; Iiii '# in big-endian read four unsinged int32
Magic_number, num_images num_rows, num_cols=struct. Unpack_from (fmt_header fb_data, offset)
Print (' idex3 magic number: {}, picture number: {} '. The format (magic_number, num_images))
Offset +=struct. Calcsize (fmt_header)
Fmt_image='& gt; '+ STR (num_rows * num_cols) +' B '

Images=np. Empty ((num_images, num_rows * num_cols)) have modified #
For I in range (num_images) :
Im=struct. Unpack_from (fmt_image fb_data, offset)
Images. [I]=np array (im) # here expressed in a one-dimensional array images, np, array (im). Reshape ((num_rows num_cols))
Offset +=struct. Calcsize (fmt_image)
Return images'
Def decode_idx1_ubyte (self, idx1_ubyte_file) :
With the open (idx1_ubyte_file, 'rb') as f:
Print (' parse the file: 'idx1_ubyte_file)
Fb_data=(https://bbs.csdn.net/topics/f.read)

Offset=0
Fmt_header='& gt; Ii '# in big-endian read two unsinged int32
Magic_number, label_num=struct. Unpack_from (fmt_header fb_data, offset)
Print (' idex1 magic number: {}, tag number: {} '. The format (magic_number, label_num))
Offset +=struct. Calcsize (fmt_header)
Labels=np. Empty (shape=[0, 10], dtype=float) # neural network need to make 10 label float array

Fmt_label='& gt; B '# read one byte at a time
For I in range (label_num) :
N=struct. Unpack_from (fmt_label fb_data, offset)
Labels=np. Append (labels, [[0,0,0,0,0,0,0,0,0,0]], the axis=0)
Labels [I] [n]=1
Offset +=struct. Calcsize (fmt_label)
The return labels

Def __init__ (self) :
Fixed training file location #
The self. The img=self. Decode_idx3_ubyte ("/home/zhangyl/Downloads/mnist/train - images. Idx3 does - ubyte ")
The self. The result=self. Decode_idx1_ubyte ("/home/zhangyl/Downloads/mnist/train - labels. Idx1 - ubyte ")

Print (self. The result [0])
Print (self. The result [1000])
Print (self. The result [25000])
Fixed validation file location #
Self. Validate_img=self. Decode_idx3_ubyte ("/home/zhangyl/Downloads/mnist/t10k - images. Idx3 does - ubyte ")
Self. Validate_result=self. Decode_idx1_ubyte ("/home/zhangyl/Downloads/mnist/t10k - labels. Idx1 - ubyte ")
Each batch # read the starting position of the training data
Self. Train_read_addr=0
Each batch # read training data batchsize
Self. Train_batchsize=100
Each batch # read the original position of the validation data
Self. Validate_read_addr=0
Each batch # read validation data batchsize
Self. Validate_batchsize=100
# define variables used to return to the batch data
Self. Train_img_batch=self. Img
Self. Train_result_batch=self. The result
Self. Validate_img_batch=self. Validate_img
Self. Validate_result_batch=self. Validate_result

Def get_next_batch_traindata (self) :
N=len (self. Img) # for parameters appropriate constraints,
If self. Train_read_addr + self. Train_batchsize & lt;=n:
Self. Train_img_batch=self. Img [self train_read_addr: self. Train_read_addr + self. Train_batchsize]
Self. Train_result_batch=self. The result [self train_read_addr: self. Train_read_addr + self. Train_batchsize]
Self. Train_read_addr +=self. Train_batchsize # change starting position
If self. Train_read_addr==n:
Self. Train_read_addr=0
The else:
Self. Train_img_batch=self. Img [self train_read_addr: n]
Self. Train_img_batch. Append (self. Img [0: self. Train_read_addr + self. Train_batchsize -n])
Self. Train_result_batch=self. The result [self train_read_addr: n]
Self. Train_result_batch. Append (self. The result [0: self. Train_read_addr + self. Train_batchsize -n])
Self. Train_read_addr=self. Train_read_addr + self. Train_batchsize - n # change the starting position, there is no consider batchsize is greater than in the case of n
Return self. Train_img_batch, self train_result_batch # test using a temporary variable returns whether it is feasible to


Def set_train_read_addr (self, addr) :
Self. Train_read_addr=addr
Def set_train_batchsize (self, batchsize) :
Self. Train_batchsize=batchsize
If batchsize & lt; 1:
Self. Train_batchsize=1
Def set_validate_read_addr (self, addr) :
Self. Validate_read_addr=addr
Def set_validate_batchsize (self, batchsize) :
Self. Validate_batchsize=batchsize
If batchsize<1:
Self. Validate_batchsize=1

Myminst=myminst () # minst class instances
Batch_size=2 # set each round training Batch size
Initial vector learning_rate=0.8 #
Vector learning_rate_decay=0.999 # attenuation
Max_steps=300000 # biggest steps training

# define storage training rounds of several variables, when using tensorflow training neural networks,
# generally will represent the number of train wheel variables through trainable parameter is set to not training
Training_step=tf. Variable (0, trainable=False)

# define get hidden layer and output layer of the forward propagation calculation, the activation function used relu ()
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related