在TensorFlow中提供外部数据集

时间:2016-03-12 23:04:56

标签: python dataset tensorflow

我已经成功测试了这里提供的mnist分类器的简单模型:https://www.tensorflow.org/versions/r0.7/tutorials/mnist/beginners/index.html#mnist-for-ml-beginners

现在,我尝试使用外部数据运行类似的模型,我已经习惯在python中训练分类器。

使用的数据集由矩阵X(11527乘1200)组成,其中包含11527个图像的像素值和包含一个热编码标签的Y(1200乘26)。

我面临的问题是,在给定新数据的情况下,权重不会随着迭代而变化,并且无论迭代次数多少,我都会坚持单个精度值10,945。系统执行。

原始代码,来自张量流网站(这个工作正常):

# -*- coding: utf-8 -*-
"""
primeiro programa feito com a biblioteca tensorflow do google
rede neural simples com objetivo de identificar caracteres
"""
import numpy as np
import tensorflow as tf
import cv2
import input_data

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #importaçao do database MNIST

### montagem do "Graph" - "arquietura da rede" - ###########################################

x = tf.placeholder("float", [None, 784])  #placeholder representam os dados que irao entrar no sistema
y_ = tf.placeholder("float", [None,10])
W = tf.Variable(tf.zeros([784,10]))  #pesos
b = tf.Variable(tf.zeros([10]))      #bias
y = tf.nn.softmax(tf.matmul(x,W) + b)  #y function
cross_entropy = -tf.reduce_sum(y_*tf.log(y)) #cost function
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)  #configuraçao do treinamento

############# INICIO DO TREINAMENTO #########################

init = tf.initialize_all_variables()  
sess = tf.InteractiveSession() 
sess.run(init)  #inicializa a sessao 
for i in range(1000): #loop de treinamento
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

################################################3#####
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) #calculo da taxa de acerto
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print "taxa de acerto"
print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})

#####################################################

#rotina de transformaçao de imagem para tensor (,784)
img = cv2.imread('digit.png',-1)
img = cv2.resize(img, (28, 28)) 

np.reshape(img, 784)
imgtensor = np.reshape(img, 784) #tensor de saida

print "caractere reconhecido"

predictvec = (y.eval(feed_dict={x: [ (imgtensor) ]})) #previsao de saida
a = sess.run(tf.arg_max(predictvec,1))
print(a+1) 
cv2.imshow('resized',img)
cv2.waitKey(0)

使用外部数据集的代码(这个代码不起作用)

# -*- coding: utf-8 -*-
"""
Created on Sat Mar 12 18:49:42 2016

@author: rachillesf
"""

import numpy as np
import tensorflow as tf

## PREPARAÇÂO DOS DADOS#######################################################

#carregando 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

# transforma y em uma matriz 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)
Y = np.asanyarray(Y)
X = np.asanyarray(X)

### montagem do "Graph" - "arquietura da rede" - ###########################################

x = tf.placeholder("float", [None, 1200])  #placeholder representam os dados que irao entrar no sistema
y_ = tf.placeholder("float", [None,26])
W = tf.Variable(tf.truncated_normal([1200,26], stddev=0.001))
b = tf.Variable(tf.zeros([26]))      #bias
y = tf.nn.softmax(tf.matmul(x,W) + b)  #funçao de y
cross_entropy = -tf.reduce_sum(y_*tf.log(y)) #cost function
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)  #configuraçao do treinamento

############# INICIO DO TREINAMENTO #########################

init = tf.initialize_all_variables()  
sess = tf.InteractiveSession() 
sess.run(init)  #inicializa a sessao 
for i in range(100): #loop de treinamento
  print(i)
  sess.run(train_step, feed_dict={x: X, y_: Y})

################################################3#####
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) #calculo da taxa de acerto
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print "taxa de acerto"
print sess.run(accuracy, feed_dict={x: X, y_: Y})

1 个答案:

答案 0 :(得分:1)

问题是你的权重和偏差被初始化为零(检查比索和偏见)。 您应该将偏差初始化为0.1并将权重加权到正态分布。