为什么我的神经网络现在既不会报错,但是也不会运行下去了?卡在114行了
·
下图是我要运行的一个简单的神经网络,从 train_classes_5_2.tfrecords文件中读入,并且通过tf.train.shuffle_batch()函数获得image_batch和label_batch,然后想通过sess.run([image_batch,label_batch])来获取batch_ximg,batch_ylabel,并且把batch_ximg,batch_ylabel传入feeds字典,再通过sess.run(optm,feed_dict=feeds)来对优化器进行优化,同通过sess.run(cost,feed_dict=feeds)来对损失值进行更新,但是却卡在print("3333333333333")和print("444444444444")之间,既不报错,也不运行,是在读取文件里的内容吗?
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#import tensorflow.examples.tutorials.mnist.input_data
import input_data
mnist = input_data.read_data_sets('data',one_hot=True)
#导入数据和网络构建
n_hidden_1=256
n_hidden_2=128
#n_input=784
#*3是表示彩色图的通道数
n_input = 224*224*3
#分类
n_classes=5
#从tfrecord文件中读取
#队列读取
def readtfrecord(filename):
#创建一个队列来维护输入文件列表
filename_queue=tf.train.string_input_producer([filename])
reader=tf.TFRecordReader()
#从文件中读出一个样例
_,serialized_example=reader.read(filename_queue)
#解析读入的一个样例,解析多个用parse_example
features=tf.parse_single_example(
serialized_example,
features={
#tf.FixedLenFeature结果为Tensor
#tf.VarLenFeature结果为SparseTensor,用于处理稀疏数据
'label': tf.FixedLenFeature([],tf.int64),
#'img_raw': tf.FixedLenFeature([],tf.string)
'image': tf.FixedLenFeature([],tf.string),
'height':tf.FixedLenFeature([],tf.int64),
'width':tf.FixedLenFeature([],tf.int64),
'channels':tf.FixedLenFeature([],tf.int64),
})
img=tf.decode_raw(features['image'],tf.uint8)
img=tf.reshape(img,[224,224,3])
img=tf.cast(img,tf.float32)
label=tf.cast(features['label'],tf.float32)
return img,label
img,label=readtfrecord('train_classes_5_2.tfrecords')
#产生用于训练的批次
min_after_dequeue=1000
batch_size=128
capacity=min_after_dequeue+3*batch_size
image_batch,label_batch=tf.train.shuffle_batch(
[img,label],batch_size=batch_size,
capacity=capacity,min_after_dequeue=min_after_dequeue,
#若队列中没有足够的项目,则允许最终批次更小
allow_smaller_final_batch=True)
x=tf.placeholder("float",[None,n_input])
y=tf.placeholder("float",[None,n_classes])
#正态分布的标准差
stddev = 0.1
weights = {
#高斯初始化
'w1':tf.Variable(tf.random_normal([n_input,n_hidden_1],stddev=stddev)),
'w2':tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2],stddev=stddev)),
'out':tf.Variable(tf.random_normal([n_hidden_2,n_classes],stddev=stddev))
}
biases={
#偏置项也可以零值初始化
'b1':tf.Variable(tf.random_normal([n_hidden_1])),
'b2':tf.Variable(tf.random_normal([n_hidden_2])),
'out':tf.Variable(tf.random_normal([n_classes]))
}
print("网络已经准备好")
#前向传播
def qiangxiangchuanbo(X,weights,biases):
layer_1=tf.nn.sigmoid(tf.add(tf.matmul(X,weights['w1']),biases['b1']))
layer_2=tf.nn.sigmoid(tf.add(tf.matmul(layer_1,weights['w2']),biases['b2']))
return (tf.matmul(layer_2,weights['out']) + biases['out'])
#prediction
pred = qiangxiangchuanbo(x,weights,biases)
#损失和优化
#平均LOST 损失函数:交叉熵函数softmax_cross_entropy_with_logits(labels=pred,logits=y))
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred,labels=y))
#优化器 学习率0.001 梯度下降
optm=tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss=cost)
#准确率
#argmax中第二个参数0表示的是按列比较返回最大值的索引,
# 1表示按行比较返回最大值的索引。
#对比这两个矩阵或者向量的相等的元素。
corr=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
#将corr的数据类型转换成float然后再除以n
accr=tf.reduce_mean(tf.cast(corr,"float"))
#初始化
init=tf.global_variables_initializer()
print("功能已准备好")
training_epochs=100
#batch_size=64
display_step=10
sess=tf.Session()
sess.run(init)
coord=tf.train.Coordinator()
threads=tf.train.start_queue_runners(sess=sess,coord=coord)
print("1111111111111111111111111")
#迭代
for epoch in range(training_epochs):
print("22222222222222222222222")
avg_cost=0.
#total_batch=int(mnist.train.num_examples/batch_size)
total_batch=100
for i in range(total_batch):
print("33333333333333333333333333333")
batch_ximg,batch_ylabel=sess.run([image_batch,label_batch]) #114行
print("444444444444444444444444444444444")
feeds={x:batch_x,y:batch_y}
#sess.run(optm,feed_dict=feeds)
sess.run(optm,feed_dict=feeds)
#avg_cost += sess.run(cost,feed_dict=feeds)
avg_cost += sess.run(cost,feed_dict=feeds)
avg_cost = avg_cost / total_batch
#显示在显示器上
if (epoch-1)%display_step==0:
print("Epoch :%03d/%03d cost: %.9f" % (epoch,training_epochs,avg_cost))
#train_feeds={x:img_batches,y:label_batches}
train_acc=sess.run(accr,feed_dict=feeds)
print("Train Accuracy: %.3f" % (train_acc))
#test_feeds={x:mnist.test.images,y:mnist.test.labels}
#test_acc=sess.run(accr,feed_dict=test_feeds)
#print("Test Accuracy: %.3f" % (test_acc))
coord.request_stop()
coord.join(threads)
sess.close()
print("结束")
更多推荐
所有评论(0)