Tensorflow真实的例子

时间:2017-04-10 08:54:47

标签: python tensorflow

我已根据此Tensorflow example 编写了代码。我遇到的问题是我得到的准确性没有任何意义(它是1或0)所以我的问题是我在这里缺少的?

import tensorflow as tf
import  numpy as np
import  csv
import os


#defining  batch fuuntion

def batch(iterable, n=1):
    l = len(iterable)
    for ndx in range(0, l, n):
        yield iterable[ndx:min(ndx + n, l)]


os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
Training_File = 'Training.csv'
Test_File     = 'Test.csv'
numberOFClasses = 19
batchSize = 19

# read training data
filePointer  = open(Training_File, 'r', newline='')
reader = csv.reader(filePointer)
Training_Data   = []
Training_Labels = []
row = next(reader)
len(row)
#### Getting Training_Data and labels
for  row in reader:
    Training_Data.append(row[:-2])
    Training_Labels.append(row[-1])
# close TrainingFile  and  getting Data and labels from Test
len(Training_Data)
filePointer.close();

filePointer =open(Test_File, 'r', newline='')
reader  =   csv.reader(filePointer)
Test_Data = []
Test_Labels=[]
row = next(reader)

for row in reader:
    Test_Data.append(row[:-2])
    Test_Labels.append(row[-1])
len(Test_Labels)
filePointer.close()
len(Training_Data[0])



x = tf.placeholder('float',[None,len(row[:-2])])
w = tf.Variable(tf.zeros([len(row[:-2]),numberOFClasses]))
b = tf.Variable(tf.zeros([numberOFClasses]))
model = tf.add(tf.matmul(x,w),b)
y_ = tf.placeholder(tf.float32,[None,numberOFClasses])
y =  tf.nn.softmax(model)

cross_entropy= -tf.reduce_sum(y_*tf.log(y),reduction_indices=[1])
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
index =0
batch_xs = []
batch_ys = []
batch_txs= []
batch_tys= []
# Training processing


for i in batch(Training_Data,batchSize):
    batch_xs.append(i)
for i in batch(Training_Labels,batchSize):
    batch_ys.append(i)

for i in batch(Test_Data,batchSize):
    batch_txs.append(i)
for i in batch(Test_Labels,batchSize):
    batch_tys.append(i)


#print(np.reshape(batch_ys[len],(1,batchSize)))
for i in range(len(batch_xs) -1 ):
    sess.run(train_step,feed_dict={x:batch_xs[i],y_:np.reshape(batch_ys[i],(1,batchSize))})



correct_prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
for i in range(len(batch_txs) -1):
    print(sess.run(accuracy,feed_dict={x:batch_txs[i],y_:np.reshape(batch_tys[i],(1,batchSize))}))

更新 我已经改变了批次的大小:

.............................................
numberOFClasses = 19

batchSize = 19 * 3
....................................
for i in range(int(len(batch_xs)/batchSize) ):
    print(sess.run(train_step,feed_dict={x:batch_xs[i],y_:np.reshape(batch_ys[i],(batchSize,numberOFClasses))}))



correct_prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
for i in range(len(batch_txs) -1):
    print(sess.run(accuracy,feed_dict={x:batch_txs[i],y_:np.reshape(batch_tys[i],(1,batchSize))}))

结果仍然相同,所以我只是没有得到我在这里缺少的东西

2ndUpdate

运行这部分代码:     对于范围内的j(len(batch_xs)-1):         打印(sess.run(train_step,feed_dict = {X:batch_xs [j]时,Y_:np.reshape(batch_ys [j]时,(numberOFClasses,3))}))

发出了巨大的错误消息,但我想这部分是相关的:

InvalidArgumentError (see above for traceback): Incompatible shapes: [19,3] vs. [57,19]
 [[Node: mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_1_0, Log)]]

因此,由于我的批量大小是树的倍数,所以我应该得到57个预测 - > Y_。

塑造Y_ [57,1]的喂养

表示范围内的j(len(batch_xs)-1):     打印(sess.run(train_step,feed_dict = {X:batch_xs [j]时,Y_:np.reshape(batch_ys [j]时,(BATCHSIZE,1))}))

打印输出None作为返回值但没有错误(我猜)确定。

但是运行准确度部分会在开头提到1和0。

测试和训练数据和标签100%正确!

这是CSV文件结尾的一部分:

enter image description here

2 个答案:

答案 0 :(得分:1)

它可能不是您的问题或错误的来源,但我认为所有出现的行[: - 2]应该被行[: - 1]替换,如果您想要获取所有索引但只有一个(Python排除了end

范围内给出的索引row[begin:end]

你应该:

y_ = tf.placeholder(tf.float32,[None,numberOFClasses])
...
sess.run(train_step,feed_dict={x:batch_xs[i],y_:np.reshape(batch_ys[i],(batchSize, numberOFClasses ))})
...
print(sess.run(accuracy,feed_dict={x:batch_txs[i],y_:np.reshape(batch_tys[i],(batchSize, numberOFClasses ))}))

无论如何,您应该绝对使用batch_size != numberOFClasses,因为它会抛出一个错误,您可以使用它来理解代码中的错误。如果你没有,你会丢失异常消息,但错误仍然存​​在,隐藏(你的网络仍然没有学到你想要的东西)。当你得到重新检查问题的错误外观,并试图理解为什么(看看形状是什么,应该是什么)

答案 1 :(得分:0)

从您的代码示例中,无法准确判断(至少缺少batch()和batchSize),但我的猜测是您有一批大小(无论是否有意),以及因此,您可以获得一个精度(如果样本被正确预测)或零(如果样本被错误分类)。为了获得有意义的准确度,您需要评估更大的批次。