cross_entry的设置tensorflow 权重更新为nan的问题

cross_entry的设置tensorflow 权重更新为nan的问题

当用cifar10时,cross_entry=tf.reduce_mean(-tf.reduce_sum(y*tf.log(ylog),reduction_indices=1))无法更新权重,cross_entry=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=fc_3))能更新权重

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 22 13:41:31 2019

@author: Administrator
"""
import sys
path=r'G:\python\example\models-master\tutorials\image\cifar10'
sys.path.append(path)
import tensorflow as tf
import cifar10_input
import numpy as np
data_dir=r'G:\python\tmp\cifar10_data\cifar-10-batches-bin'
batch_size=128
max_step=3000

x=tf.placeholder(tf.float32,shape=[None,24,24,3])
y=tf.placeholder(tf.float32,[None,10])

image_train,label_train=cifar10_input.inputs(eval_data=False,data_dir=data_dir,batch_size=batch_size)
image_test,label_test=cifar10_input.inputs(eval_data=True,data_dir=data_dir,batch_size=batch_size)

#卷积层第一层
weight_1=tf.Variable(tf.random_normal(shape=[5,5,3,64],stddev=0.1))
bias_1=tf.Variable(tf.zeros([64]))
conv_1=tf.nn.conv2d(x,weight_1,[1,1,1,1],padding='SAME')+bias_1
pool_1=tf.nn.max_pool(tf.nn.relu(conv_1),ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME')
#卷积层第二层
weight_2=tf.Variable(tf.random_normal(shape=[5,5,64,128,],stddev=0.1))
bias_2=tf.Variable(tf.zeros([128]))
conv_2=tf.nn.conv2d(pool_1,weight_2,[1,1,1,1],padding='SAME')
pool_2=tf.nn.max_pool(tf.nn.relu(conv_2),ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME')
fc_x=tf.reshape(pool_2,[batch_size,6*6*128])
#全连接层层第一层
weight_3=tf.Variable(tf.random_normal(shape=[6*6*128,384],stddev=0.1))
bias_3=tf.Variable(tf.zeros([384]))
fc_1=tf.nn.relu(tf.matmul(fc_x,weight_3)+bias_3)
#全连接层层第二层
weight_4=tf.Variable(tf.random_normal(shape=[384,192],stddev=0.1))
bias_4=tf.Variable(tf.zeros([192]))
fc_2=tf.nn.relu(tf.matmul(fc_1,weight_4)+bias_4)
#全连接层层第三层
weight_5=tf.Variable(tf.random_normal(shape=[192,10],stddev=0.1))
bias_5=tf.Variable(tf.zeros([10]))
fc_3=tf.matmul(fc_2,weight_5)+bias_5
ylog=tf.nn.softmax(fc_3)
cross_entry=tf.reduce_mean(-tf.reduce_sum(y*tf.log(ylog),reduction_indices=1))
#cross_entry=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=fc_3))
train_op=tf.train.AdamOptimizer(0.001).minimize(cross_entry)
correct_pre=tf.equal(tf.argmax(ylog,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct_pre,tf.float32))
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)
tf.train.start_queue_runners(sess=sess)
for i in range(max_step):
    image_batch,label_batch=sess.run([image_train,label_train])
    x_batch,y_batch=image_batch,np.eye(10,dtype=float)[label_batch]
    _,loss=sess.run([train_op,cross_entry],feed_dict={x:x_batch,y:y_batch})
    if (i+1)%10==0: print('Epoch:',i+1,'Loss:',loss)
    if (i+1)%200==0:
        acc=sess.run(accuracy,feed_dict={x:x_batch,y:y_batch})
        print('Epoch:',i+1,'Acc:',acc)

num_examples=10000
num_item=num_examples//batch_size
total_true=0
for i in range(num_item):
    image_batch,label_batch=sess.run([image_test,label_test])
    x_batch,y_batch=image_batch,np.eye(10)[label_batch]
    predict=sess.run(correct_pre,feed_dict={x:x_batch,y:y_batch})
    total_true+=tf.reduce_sum(predict)
print('Test accuracy:', total_true/(num_item*batch_size))

用mnist时就可以

import matplotlib.pyplot as plt
import sys
path = r'G:\python\example\models-master\research\slim'
sys.path.append(path)
import tensorflow as tf
from scipy.stats import norm
import numpy as np
import tensorflow.contrib.slim as slim
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets(
'G:/python/example/MNIST_data/')#,one_hot=True)
batch_size=128
max_step=3000

x=tf.placeholder(tf.float32,shape=[batch_size,784])
y=tf.placeholder(tf.float32,[batch_size,10])

#卷积层第一层
x_image = tf.reshape(x, shape=[-1, 28, 28, 1])
weight_1=tf.Variable(tf.random_normal(shape=[5,5,1,64],stddev=0.1))
bias_1=tf.Variable(tf.zeros([64]))
conv_1=tf.nn.conv2d(x_image,weight_1,[1,1,1,1],padding='SAME')+bias_1
pool_1=tf.nn.max_pool(tf.nn.relu(conv_1),ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME')
#卷积层第二层
weight_2=tf.Variable(tf.random_normal(shape=[5,5,64,128,],stddev=0.1))
bias_2=tf.Variable(tf.zeros([128]))
conv_2=tf.nn.conv2d(pool_1,weight_2,[1,1,1,1],padding='SAME')
pool_2=tf.nn.max_pool(tf.nn.relu(conv_2),ksize=[1,3,3,1],strides=[1,2,2,1],padding='SAME')
fc_x=tf.reshape(pool_2,[batch_size,7*7*128])
#全连接层层第一层
weight_3=tf.Variable(tf.random_normal(shape=[7*7*128,384],stddev=0.1))
bias_3=tf.Variable(tf.zeros([384]))
fc_1=tf.nn.relu(tf.matmul(fc_x,weight_3)+bias_3)
#全连接层层第二层
weight_4=tf.Variable(tf.random_normal(shape=[384,192],stddev=0.1))
bias_4=tf.Variable(tf.zeros([192]))
fc_2=tf.nn.relu(tf.matmul(fc_1,weight_4)+bias_4)
#全连接层层第三层
weight_5=tf.Variable(tf.random_normal(shape=[192,10],stddev=0.1))
bias_5=tf.Variable(tf.zeros([10]))
fc_3=tf.matmul(fc_2,weight_5)+bias_5
ylog=tf.nn.softmax(fc_3)
cross_entry=tf.reduce_mean(-tf.reduce_sum(y*tf.log(ylog),reduction_indices=1))
#cross_entry=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=fc_3))
train_op=tf.train.AdamOptimizer(0.001).minimize(cross_entry)
correct_pre=tf.equal(tf.argmax(ylog,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct_pre,tf.float32))
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)

for i in range(max_step):
    image_batch,label_batch=mnist.train.next_batch(batch_size)
    x_batch,y_batch=image_batch,np.eye(10,dtype=float)[label_batch]
    _,loss=sess.run([train_op,cross_entry],feed_dict={x:x_batch,y:y_batch})
    if (i+1)%10==0: print('Epoch:',i+1,'Loss:',loss)
    if (i+1)%200==0:
        acc=sess.run(accuracy,feed_dict={x:x_batch,y:y_batch})
        print('Epoch:',i+1,'Acc:',acc)

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/86597991
今日推荐