我希望你们身体健康。我正在处理Mnist数据集并通过实现函数next_batch(batch,data,label)成功运行了我的代码,但我认为它不能很好地工作,因为经过1个时期后我会不断遭受相同的损失。请大家帮我我要分享我的训练功能和下一批功能。谢谢
代码
layer1_neuron=500
layer2_neuron=500
layer3_neuron=500
number_of_class=10
batch_size=200
#my neural network
def conv2d(data,weights):
return tf.nn.conv2d(data,weights,strides=[1,1,1,1],padding='SAME')
def maxpool2d(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
def Convo_neural_network(x_train):
weights={
'conv1':tf.Variable(tf.random_normal([5,5,1,32])), #[5 and 5 of convolution , 1 input , 32 output]
'conv2': tf.Variable(tf.random_normal([5,5,32,64])), #[5 and 5 of convolution , 32 input , 64 output]
'fc':tf.Variable(tf.random_normal([7*7*64,1024])), #[After convolution we compress images into features thats why our image become]
# 7*7 now 64 is the conv2 output thats why we are multiplying it now fully connected
#layer gives us 1024 output
'output': tf.Variable(tf.random_normal([1024,number_of_class])) # 1025 input we got from fully connected layer(fc) and atlast we got
}
biases={
'conv1':tf.Variable(tf.random_normal([32])),#here we will gives number of ouputs
'conv2':tf.Variable(tf.random_normal([64])),#here we will gives number of ouputs
'fc':tf.Variable(tf.random_normal([1024])),#here we will gives number of ouputs
'output':tf.Variable(tf.random_normal([10]))#here we will gives number of ouputs
}
x_train=tf.reshape(x_train,[-1,28,28,1])
conv1=conv2d(x_train,weights['conv1'])
conv1=maxpool2d(conv1)
conv2=conv2d(conv1,weights['conv2'])
conv2=maxpool2d(conv2)
#now before injecting our data to fully connected layer we have to reshape our data into 7*7*64 because we are compressing our data
fc=tf.reshape(conv2,[-1,7*7*64])
fc=tf.nn.relu(tf.matmul(fc,weights['fc'])+biases['fc'])
output= tf.matmul(fc,weights['output']+biases['output'])
return output
# for splitting out batches of data
def next_batch(num, data, labels):
'''
Return a total of `num` random samples and labels.
'''
idx = np.arange(0 , len(data))
np.random.shuffle(idx)
idx = idx[:num]
data_shuffle = [data[ i] for i in idx]
labels_shuffle = [labels[ i] for i in idx]
return np.asarray(data_shuffle), np.asarray(labels_shuffle)
def traning_neuralNetwork(x_test,x_train,y_test,y_train):
x=tf.placeholder('float',[None,784]) #28 * 28 is 784 (shape of the data)
y=tf.placeholder('float')
total_epochs=10
total_loss=0
epoch_loss=0
batch_size=100
num_batch = int(np.ceil(48000/batch_size))
prediction=[]
prediction=Convo_neural_network(x)
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
optimizer=tf.train.AdamOptimizer().minimize(cost)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range (total_epochs):
total_loss=0
for _ in range (num_batch):
x_train,y_train=next_batch(batch_size,x_train,y_train)
_,epoch_loss=sess.run([optimizer,cost],feed_dict={x:x_train,y:y_train})
total_loss+=epoch_loss
print('Epoch ',epoch, " loss = ",total_loss)
print("Traning Complete!")
correct=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct,'float'))
print('accuracy',accuracy.eval({x : x_test,y : y_test}))
输出
Epoch 0 loss = 40313040.96320662
Epoch 1 loss = 110.52409030497074
Epoch 2 loss = 110.52409036457539
Epoch 3 loss = 110.52409060299397
Epoch 4 loss = 110.52409039437771
Epoch 5 loss = 110.52409066259861
Epoch 6 loss = 110.52409003674984
Epoch 7 loss = 110.52409045398235
Epoch 8 loss = 110.5240905880928
Epoch 9 loss = 110.524090513587
Traning Complete!
accuracy 0.346