gpt4 book ai didi

python - TensorFlow:训练时参数不更新

转载 作者:行者123 更新时间:2023-11-28 21:47:50 27 4
gpt4 key购买 nike

我正在使用 TensorFlow 实现分类模型

我面临的问题是,当我运行训练步骤时,我的权重和误差没有更新。结果,我的网络不断返回相同的结果。

我基于 MNIST example 开发了我的模型来自 TensorFlow 网站。

import numpy as np
import tensorflow as tf
sess = tf.InteractiveSession()

#load dataset
dataset = np.loadtxt('char8k.txt', dtype='float', comments='#', delimiter=",")
Y = np.asmatrix( dataset[:,0] )
X = np.asmatrix( dataset[:,1:1201] )

m = 11527
labels = 26

# y is update to 11527x26
Yt = np.zeros((m,labels))

for i in range(0,m):
index = Y[0,i] - 1
Yt[i,index]= 1

Y = Yt
Y = np.asmatrix(Y)

#------------------------------------------------------------------------------

#graph settings

x = tf.placeholder(tf.float32, shape=[None, 1200])
y_ = tf.placeholder(tf.float32, shape=[None, 26])


Wtest = tf.Variable(tf.truncated_normal([1200,26], stddev=0.001))
W = tf.Variable(tf.truncated_normal([1200,26], stddev=0.001))
b = tf.Variable(tf.zeros([26]))
sess.run(tf.initialize_all_variables())

y = tf.nn.softmax(tf.matmul(x,W) + b)

cross_entropy = -tf.reduce_sum(y_*tf.log(y))

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
Wtest = W


for i in range(10):
print("iteracao:")
print(i)
Xbatch = X[np.random.randint(X.shape[0],size=100),:]
Ybatch = Y[np.random.randint(Y.shape[0],size=100),:]
train_step.run(feed_dict={x: Xbatch, y_: Ybatch})
print("atualizacao de pesos")
print(Wtest==W)#monitora atualizaçao dos pesos

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print("precisao:Y")
print accuracy.eval(feed_dict={x: X, y_: Y})
print(" ")
print(" ")

最佳答案

问题可能出在您如何初始化权重矩阵 W。如果将其初始化为全零,则所有神经元将在每一步中遵循相同的梯度,这将导致网络无法训练。换线

W = tf.Variable(tf.zeros([1200,26]))

...用类似的东西

W = tf.Variable(tf.truncated_normal([1200,26], stddev=0.001))

...应该让它开始训练。

This question在 CrossValidated 网站上有一个很好的解释为什么你不应该将所有的权重初始化为零。

关于python - TensorFlow:训练时参数不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35961434/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com