Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TensorFlow-MNIST数据中的训练精度没有提高_Tensorflow_Machine Learning_Mnist_Kaggle - Fatal编程技术网

TensorFlow-MNIST数据中的训练精度没有提高

TensorFlow-MNIST数据中的训练精度没有提高,tensorflow,machine-learning,mnist,kaggle,Tensorflow,Machine Learning,Mnist,Kaggle,我用tensorflow编写了一个程序来处理Kaggle的数字识别器问题。程序可以正常运行,但训练精度总是很低,约为10%,例如: step 0, training accuracy 0.11 step 100, training accuracy 0.13 step 200, training accuracy 0.21 step 300, training accuracy 0.12 step 400, training accuracy 0.07 step 500, training ac

我用tensorflow编写了一个程序来处理Kaggle的数字识别器问题。程序可以正常运行,但训练精度总是很低,约为10%,例如:

step 0, training accuracy 0.11
step 100, training accuracy 0.13
step 200, training accuracy 0.21
step 300, training accuracy 0.12
step 400, training accuracy 0.07
step 500, training accuracy 0.08
step 600, training accuracy 0.15
step 700, training accuracy 0.05
step 800, training accuracy 0.08
step 900, training accuracy 0.12
step 1000, training accuracy 0.05
step 1100, training accuracy 0.09
step 1200, training accuracy 0.12
step 1300, training accuracy 0.1
step 1400, training accuracy 0.08
step 1500, training accuracy 0.11
step 1600, training accuracy 0.17
step 1700, training accuracy 0.13
step 1800, training accuracy 0.11
step 1900, training accuracy 0.13
step 2000, training accuracy 0.07
……
以下是我的代码:

def weight_变量(形状):
初始值=tf.截断的_法线(形状,标准差=0.1)
返回tf.变量(初始值)
def偏差_变量(形状):
初始=tf.常数(0.1,形状=形状)
返回tf.变量(初始值)
def conv2d(x,w):
返回tf.nn.conv2d(x,w,步长=[1,1,1,1],padding='SAME')
def max_pool_2x2(x):
#ksize=[批次,高度,宽度,通道],跨步=[批次,跨步,跨步,通道]
返回tf.nn.max_pool(x,ksize=[1,2,2,1],步长=[1,2,2,1],padding='SAME')
x=tf.placeholder(tf.float32,[None,784])
y=tf.占位符(tf.float32,[None,10])
keep_prob=tf.placeholder(tf.float32)
x_image=tf.placeholder(tf.float32,[None,28,28,1])
w_conv1=权重_变量([5,5,1,32])
b_conv1=偏差_变量([32])
h_conv1=tf.nn.relu(conv2d(x_图像,w_conv1)+b_conv1)
h_池1=最大池2(h_池1)
w_conv2=权重_变量([5,5,32,64])
b_conv2=偏差_变量([64])
h_conv2=tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
h_池2=最大池2×2(h_池2)
w_fc1=权重_变量([7*7*641024])
b_fc1=偏差_变量([1024])
h_pool2_flat=tf.重塑(h_pool2,[-1,7*7*64])
h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1)
#辍学
keep_prob=tf.placeholder(tf.float32)
h_fc1_drop=tf.nn.drop(h_fc1,keep_prob)
#softmax
w_fc2=重量_变量([1024,10])
b_fc2=偏差_变量([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop,w_fc2)+b_fc2)
交叉熵=tf.reduce\u均值(-tf.reduce\u和(y*tf.log(y\u conv),reduce\u指数=[1]))
列车步长=tf.列车AdamOptimizer(10e-4).最小化(交叉熵)
正确的预测=tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
准确度=tf.reduce_平均值(tf.cast(正确的预测,tf.float32))
def get_批次(i、尺寸、序列、标签):
startIndex=(i*尺寸)%42000
endIndex=开始索引+大小
批次X=序列[startIndex:endIndex]
批次Y=标签[startIndex:endIndex]
返回批次X,批次Y
数据=pd.read\U csv('train.csv'))
列车数据=数据下降(['label'],轴=1)
列车数据=列车数据.values.aType(数据类型=np.float32)
列车数据=列车数据。重塑(42000,28,28,1)
label_data=data['label'].tolist()
label\u data=tf.one\u hot(label\u data,depth=10)
label\u data=tf.Session().run(label\u data).astype(dtype=np.float64)
批量大小=100
tf.global_variables_initializer().run()
对于范围内的i(20000):
批次x、批次y=获取批次(i、批次大小、序列数据、标签数据)
如果i%100==0:
训练精度=精度.eval(feed\u dict={x\u image:batch\u x,y\u:batch\u y,keep\u prob:1.0})
打印(“步骤%d,训练精度%g”%(i,训练精度))
train_step.run(feed_dict={x_image:batch_x,y_:batch_y,keep_prob:0.9})

我不知道我的程序出了什么问题。

我建议您更改
偏差变量
函数-不确定
tf.variable(tf.constant)
的行为,另外我们通常将偏差初始化为零,而不是0.1:

def bias_变量(形状):
返回tf.zero((shape),dtype=tf.float32)

如果这没有帮助,请尝试使用
stddev=0.01

初始化权重,非常感谢您的回答!我将weights and bias的STDEV更改为0.01,程序在大约第3000步之前正常运行,精度约为0.99。然而,在第3000步之后,精度突然变为0.1左右。这非常奇怪。@Lixudong这是另一个不同的问题-请接受此答案并打开一个新帖子如果答案解决了您的问题,请接受它-见-谢谢