评估测试数据准确性时的Tensorflow错误

时间:2017-06-03 02:44:19

标签: tensorflow

我使用了272个功能的数据集,包含1770个示例和3个类,我已经能够训练数据但是使用准确度而不是测试数据,我收到错误。我的代码出错了什么?

InvalidArgumentError (see above for traceback): Incompatible shapes: [770] 
vs. [1000][[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/cpu:0"](ArgMax, ArgMax_1)]]

这是我的代码:

import tensorflow as tf
import pandas as pd
import numpy as np
from random import shuffle

training_set = tf.contrib.learn.datasets.base.load_csv_without_header(
  filename="35.csv",
  target_dtype=np.int32,
  features_dtype=np.float32)


n_nodes_hl1=500
n_nodes_hl2=500
n_nodes_hl3=500
n_classes=3
batch_size=100


data=np.array(training_set.data)

training_data=data[:1000]
#print(np.shape(training_data))
test_data=data[1000:]
#print(np.shape(test_data))
target=np.array(training_set.target)

training_target=target[:1000]
#print(np.shape(training_target))

test_target=target[1000:]
#print(np.shape(test_target))

y_labels=[]
for i in training_target:
  if i==0:
    y_labels.append([1,0,0])
  elif i==1:
    y_labels.append([0,1,0])
  else:
    y_labels.append([0,0,1])

training_target=y_labels
#print(target)
x_labels=[]
for i in test_target:
 if i==0:
    x_labels.append([1,0,0])
 elif i==1:
    x_labels.append([0,1,0])
 else:
    x_labels.append([0,0,1])
test_target=y_labels

x=tf.placeholder('float',[None,272])
y=tf.placeholder('float')

def neural_network_model(data):
 hidden_1_layer={'weights':tf.Variable(tf.random_normal([272,n_nodes_hl1])),
                'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
 hidden_2_layer=
      {'weights':tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])),
                'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
 hidden_3_layer=
      {'weights':tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])),
                'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
 output_layer=
      {'weights':tf.Variable(tf.random_normal([n_nodes_hl3,n_classes])),
                'biases':tf.Variable(tf.random_normal([n_classes]))}                    




l1=tf.add(tf.matmul(data,hidden_1_layer['weights']),
     hidden_1_layer['biases'])
l1=tf.nn.relu(l1)

l2=tf.add(tf.matmul(l1,hidden_2_layer['weights']),hidden_2_layer['biases'])
l2=tf.nn.relu(l2)

l3=tf.add(tf.matmul(l2,hidden_3_layer['weights']),hidden_3_layer['biases'])
l3=tf.nn.relu(l3)

output=tf.matmul(l3,output_layer['weights'])+output_layer['biases']
return output

def train_neural_network(x):
 prediction=neural_network_model(x)


 cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits
            (logits=prediction,labels=y))

 optimizer=tf.train.AdamOptimizer().minimize(cost)
 hm_epochs=20
 with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(hm_epochs):
        epoch_loss=0
        i=0
        while(i<len(training_data[0])):
            start=i
            end=i+batch_size


            batch_x=np.array(training_data[start:end])

            batch_y=np.array(training_target[start:end])

            _,c=sess.run([optimizer,cost], feed_dict={x:batch_x,y:batch_y})
            epoch_loss+=c
            i+=batch_size
        print('Epoch',epoch,'completed out of',hm_epochs,'loss',epoch_loss)
    correct=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
    accuracy=tf.reduce_mean(tf.cast(correct,'float'))
    print('Accuracy:',accuracy.eval(session=sess,feed_dict=
                       {x:test_data,y:test_target}))
train_neural_network(x)

0 个答案:

没有答案