Tensorflow:在训练中使用softmax,得到结果W,b值总是为零?

时间:2017-02-18 13:40:53

标签: csv tensorflow

我试着写一个张量流代码来第一次训练样本,但我似乎在每一步训练后权重因子W和b总是为零。

训练数据非常简单,当00.3,y = 1时,10000个样本(x,y)。我从csv文件导入了这些数据。 在csv文件中记录的跟踪数据如下所示(总共有10000个数据):

    0.487801884,1;
    0.457740109,1;
    0.092949029,-1;
    0.704023173,1;
    0.07851864,-1;

但是当我运行这个代码并在每一步中打印W和b时,我发现W,b总是为零,看起来他们没有受过训练。培训结果:

    W= [[ 0.  0.]]
    b= [ 0.  0.]
    Epoch: 0000000001 cost= 0.821999985 W= [[ 0.  0.]] b= [ 0.  0.]
    Optimization Finished!
    Accuracy: 1.0

我很困惑,有人能帮我找到问题所在吗?非常感谢你! 代码附在此处:

#coding=utf-8
import tensorflow as tf
import numpy
import os
import csv
#training data sotred in csv file
filename=open('D:\Program Files (x86)\logistic\sample.csv','r')
reader=csv.reader(filename)

t_X,t_Y=[],[]

for i in reader:
    t_X.append(i[0])
    t_Y.append(i[1])

t_X=numpy.asarray(t_X)
t_Y=numpy.asarray(t_Y)
t_XT=numpy.transpose([t_X])
t_YT=numpy.transpose([t_Y])

#Parameters
learning_rate = 0.01
training_epochs = 1
batch_size=50
display_step = 1

#Input
n_samples = t_X.shape[0]

#print "n_samples:",n_samples
x = tf.placeholder(tf.float32, [None, 1])
y = tf.placeholder(tf.float32, [None, 1])

#Weight
W = tf.Variable(tf.zeros([1, 2]))
b = tf.Variable(tf.zeros([2]))

#model
pred = tf.nn.softmax(tf.matmul(x, W) + b)
cost = tf.reduce_mean(tf.square(y-pred))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(training_epochs):
        avg_cost=0
        total_batch=int(n_samples/batch_size)

        i=0
        #read training data and transfer it into (m,n)
        for anc in range(total_batch):
            m=numpy.asarray([t_X[i],t_X[i+1],t_X[i+2],t_X[i+3],t_X[i+4]])
            n=numpy.asarray([t_Y[i],t_Y[i+1],t_Y[i+2],t_Y[i+3],t_Y[i+4]])
            m=numpy.transpose([m])
            n=numpy.transpose([n])

            _,c=sess.run([optimizer,cost], feed_dict={x: m, y: n})

            i=i+batch_size
            avg_cost += c/total_batch

        if (epoch+1)%display_step==0:           
            print ("Epoch:",'%010d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost),"W=",sess.run(W),"b=",sess.run(b))

    print ("Optimization Finished!")

    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
    # Calculate accuracy
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print("Accuracy:", accuracy.eval(feed_dict={x: t_XT, y: t_YT})

1 个答案:

答案 0 :(得分:0)

Jep,你可以期待这个权重初始化的问题:

#Weight
W = tf.Variable(tf.zeros([1, 2]))
b = tf.Variable(tf.zeros([2]))

你的体重应该随机初始化;)