在TensorF中输入外部数据集

2024-03-28 15:59:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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

现在,我试着用外部数据运行一个类似的模型,我用python来训练分类器。在

我使用的数据集包括矩阵X(11527x1200)包含11527个图像的像素值,Y(1200×26)包含一个热编码标签。在

我面临的问题是,给定新的数据,权重不会随着迭代而变化,而且无论系统执行多少次迭代,我都只能使用10945的单一精度值。在

原始代码,来自tensor flows网站(这个很好用):

# -*- 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)

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

^{pr2}$

Tags: 数据runimportimgdatatftensorflowbatch