张量流量在训练期间没有改善

问题描述 投票:0回答:2

我开始研究神经网络。所以我开始使用TensorFlow在Python中编写一些简单的神经网络。我正在尝试使用MNIST数据库构建一个。

我遇到的问题是:训练损失函数时不会减少。它停留在60000,这是训练图像的数量。

我意识到它所做的预测都充满了零。这是代码(我也是这个平台的新手,所以如果帖子中有错误我很抱歉):

# -*- coding: utf-8 -*- 
from keras.datasets import mnist # subroutines for fetching the MNIST dataset
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from keras.utils import np_utils # utilities for one-hot encoding of ground truth values

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = np.reshape(x_train,[60000,784])
y_train = np_utils.to_categorical(y_train, 10) # One-hot encode the labels


x_test = np.reshape(x_test,[10000,784])
y_test = np_utils.to_categorical(y_test, 10) # One-hot encode the labels


input = tf.placeholder(tf.float32, name='Input')
output = tf.placeholder(tf.float32, name = 'Output')

syn0 = tf.Variable(2*tf.random_uniform([784,10],seed=1)-1, name= 'syn0')
bias = tf.Variable(2*tf.random_uniform([10],seed=1)-1, name= 'syn0')

syn0 = tf.Variable(tf.zeros([784,10]))
bias = tf.Variable(tf.zeros([10]))

init = tf.global_variables_initializer()

#model

l1 = tf.sigmoid((tf.matmul(input,syn0) + bias),name='layer1')
error = tf.square(l1-output,name='error')
loss = tf.reduce_sum(error, name='cost')



#optimizer
with tf.name_scope('trainning'):
    optimizer = tf.train.GradientDescentOptimizer(0.1)
    train = optimizer.minimize(loss)


#session
sess = tf.Session()
sess.run(init)


#trainning
for i in range (100):
    _,lossNow =  sess.run([train,loss],{input: x_train,output: y_train})
    print(lossNow)

#print debug 
print("Finally, the coeficients are: " , sess.run(tf.transpose(syn0)))
print()
pred = sess.run(l1,{input: x_test,output: y_test})
print("Next prediction: " ,  pred)
print()
print("Final Loss: ", sess.run(loss,{input: x_test,output: y_test}))


#print graph

sess.close

经过几次迭代后,这就是我得到的:

[[ 150000.]]
[[ 60000.]]
[[ 60000.]]
[[ 60000.]]
[[ 60000.]]

似乎损失陷入困境。我试图更改learning_rate,我添加了更多图层,但我得到了相同的结果。

希望你能帮我!谢谢你! :d

python tensorflow neural-network deep-learning
2个回答
0
投票

我想这里有两个问题。首先,您总结了集合中的所有60000个数据点来计算损失函数,而不是使用小批量。这使得你的损失功能非常陡峭,最小的非常平坦。其次是你已经找到了局部最小的损失函数,并且因为你已经锁定了函数的陡峭程度。

还有一个问题是你使用sigmoid而不是softmax。如果检查预测值,则它们都是零。对于sigmoid,您可以进行这种预测,因为所有输出都是独立的,并且没有像softmax那样的归一化(softmax的输出总和始终为1)。


0
投票

对于训练,你是否尝试执行节点“l1”是session.run(),它是实际进行计算的那个。对于训练也是必要的,你的错误和丢失将取决于“l1”的输出,如果你没有在会话中执行,那么丢失将无法正确进行。

error = tf.square(l1-output,name ='error')

在这一行中看到你用l1输出计算错误,这里的输出是基本事实但是“l1”没有任何值,除非你通过session.run()下的图计算它。

你可以尝试使用以下命令并检查输出

预测,_,lossNow = sess.run([l1,train,loss],{input:x_train,output:y_train})

另外,在计算错误(l1-output)时不要使用“ - ”符号,请使用tens qflowxswpoi等张量运算符

© www.soinside.com 2019 - 2024. All rights reserved.