2017-02-21 81 views
0

我该如何解决这个问题?这是我第一次为Tensortflow。我尝试从tensortflow教程中复制训练和评估模型,但它似乎不起作用。有人能帮我解决我的问题吗?谢谢!'numpy.ndarray'对象没有属性'train'

http://pastebin.com/NCQKNyKy

import tensorflow as tf 
sess = tf.InteractiveSession() 

import numpy as np 
from numpy import genfromtxt 

def weight_variable(shape): 
    initial = tf.truncated_normal(shape, stddev=0.1) 
    return tf.Variable(initial) 

def bias_variable(shape): 
    initial = tf.constant(0.1, shape=shape) 
    return tf.Variable(initial) 

def conv2d(x, W): 
    return tf.nn.conv2d(x, W, strides=[1, 1, 3*3, 1], padding='VALID') 


data = genfromtxt('circle_deeplearn_data_small.txt',delimiter=',') 
out = genfromtxt('circle_deeplearn_output_small.txt',delimiter=',') 

x = tf.placeholder(tf.float32, shape =[None, 3*3*15]) # size of x 
y_ = tf.placeholder(tf.float32, shape =[None, 1]) # size of output 




W_conv1 = weight_variable([1,3*3,1,15]) 
b_conv1 = bias_variable([15]) 

x_image = tf.reshape(x,[-1,1,3*3*15,1]) 

h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1) 


W_fc1 = weight_variable([1 * 1 * 15 , 1]) 
b_fc1 = bias_variable([1]) 

h_conv1_flat = tf.reshape(h_conv1 , [-1,1 * 1 * 15]) 
h_fc1 = tf.nn.relu(tf.matmul(h_conv1_flat , W_fc1) + b_fc1) 
y_conv = h_fc1 
keep_prob = tf.placeholder(tf.float32) 
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 



#Adam 

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_)) 
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) 
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
#sess.run(tf.global_variables_initializer())  
sess.run(tf.initialize_all_variables()) 
for i in range(20000): 
    batch = data.train.next_batch(50) 
    if i%100 == 0: 
     train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0}) 
     print("step %d, training accuracy %g"%(i, train_accuracy)) 
    train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) 
print("test accuracy %g"%accuracy.eval(feed_dict={x: data, y_: out, keep_prob: 1.0})) 

这是结果:

AttributeError: 'numpy.ndarray' object has no attribute 'train' 
+1

仍然主要在Python 2.7中...你能编辑你的排队,以包括错误发生的地方,甚至更好的完整回溯消息? – jkalden

回答

0

这不是很清楚你正在尝试做的。出现该问题是因为数据是在该线中产生的numpy的阵列

data = genfromtxt('circle_deeplearn_data_small.txt',delimiter=',') 

当试图使用数据的方法列车,它不存在,在下面的行

batch = data.train.next_batch(50) 

发生错误相反,您需要将数据提供给tensorflow。

1

这里data只是一个numpy数组。你可能需要写你自己的火车数据迭代器

0

你可能会尝试使用numpy.reshape将你的数据从2维变成3维。例如,如果您有20个样本和100个特征,那么就有一个(20,100)数据矩阵,并使用小于5的小批次。然后,您可以使用np.reshape(data,[10,5,-1])重新构造一个(10,5,40)矩阵。 *“-1”意思是你留下numpy来为你的数组计数,数组的总数是2万。因此,在这个例子中:10 * 5 * 40 = 20000。