首页 文章

'numpy.ndarray'对象没有属性'train'

提问于
浏览
1

我怎么能解决这个问题?这是我第一次参加Tensortflow . 我尝试从tensortflow教程复制训练和评估模型,但它似乎不起作用 . 有人可以帮我解决我的问题吗?谢谢!

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'

4 回答

  • 1

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

  • 1

    目前还不清楚你要做什么 . 出现此问题的原因是数据是在此行中生成的numpy数组

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

    当您尝试在以下行中使用不存在的方法数据列时,会发生此错误

    batch = data.train.next_batch(50)
    

    相反,您需要将数据提供给tensorflow .

  • 3

    我遇到了同样的问题 . 实际上,它知道数据的结构,这就是我遇到这个问题的原因 . 来自 tensorflow lib的Te数据集在单个文件中压缩,并在文件中分隔为 train, testvalidation set . 这就是为什么当我们调用 dataset.train.next_batch() 它确实有效 . 您拥有的数据集的压缩方式与's why it doesn'工作方式不同 . 您必须以自己的方式配置数据集,以便批处理系统和循环 .

  • 0

    您可以尝试使用numpy.reshape将数据从2维转换为3维 . 例如,如果您有20个样本和100个特征,那么(20,100)数据矩阵并使用5的小批量大小 . 然后您可以使用np.reshape(data,[10,5,-1])重新形成( 10,5,40)矩阵 .

    *“ - 1”意味着你留下numpy为你计算数组,数组的总数是20,000 . 因此,在这个例子中:10 * 5 * 40 = 20,000 .

相关问题