对张量流量不熟,梯度下降权重不会改变

时间:2017-10-09 16:21:02

标签: tensorflow gradient-descent

这是我的第一个问题。 这可能是一个愚蠢的问题,但我无法解决。当我在下面运行我的代码时,W和b保持不变(无论我将它们设置为1还是0)。我的train_x有很多RGB值,train_y是相应的类(0或1)。 无论采取多少步骤,它都不会移动。此外,准确度始终为1。

train_x = np.loadtxt('matrix1',dtype= int)#[[r,g,b],[r,g,b],...]
train_y = np.loadtxt('matrix2',dtype= int)#[0,0,0,0,0,...,1,1,1,1,1]
#machine learning

sess = tf.Session()

x = tf.placeholder("float", shape=[None, 3])
y_ = tf.placeholder("float", shape=[None, 1])
W = tf.Variable(tf.zeros([3,2]))#first: 3 factors(rgb) ; second: 2 categories(red, white)
b = tf.Variable(tf.zeros([2]))
y = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)

init = tf.global_variables_initializer()
sess.run(init)

for num in range(100):
    number_selected = random.sample(range(0,len(train_x)-1) , 1000)#number_selected: random selection of data for training
    sess.run(train_step, feed_dict={x: [train_x[num] for num in number_selected], y_: [[train_y[num]] for num in number_selected]})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
number_selected = random.sample(range(0,len(train_x)-1) , 10)#number_selected: random numbers for training
print(sess.run(accuracy, feed_dict={x: [train_x[num] for num in number_selected], y_: [[train_y[num]] for num in number_selected]}))
print(sess.run(W),sess.run(b))

0 个答案:

没有答案