【问题标题】:'numpy.ndarray' object has no attribute 'train'“numpy.ndarray”对象没有属性“train”
【发布时间】:2017-07-10 19:02:01
【问题描述】:

我该如何解决这个问题?这是我第一次使用 Tensortflow。我尝试从 tensorflow 教程中复制训练和评估模型,但它似乎不起作用。有人可以帮我解决我的问题吗?谢谢!

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'

【问题讨论】:

  • 仍然主要在 python 2.7 中...您可以编辑您的问题以包含发生错误的行,或者更好地包含完整的回溯消息吗?

标签: numpy tensorflow


【解决方案1】:

我也遇到过同样的问题。实际上,这不是问题。从字面上看,我不知道数据的结构,这就是我遇到这个问题的原因。来自tensorflow 的数据集被压缩在一个文件中,并在一个文件中分隔为train, test 和validation 集。这就是为什么当我们调用dataset.train.next_batch() 时它确实有效。您拥有的数据集没有以同样的方式压缩,这就是它不起作用的原因。您必须以自己的方式配置数据集,批处理系统和循环也是如此。

【讨论】:

    【解决方案2】:

    您可以尝试使用 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。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        目前还不清楚您要做什么。出现问题是因为data是这一行生成的numpy数组

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

        当你尝试使用方法train of data时出现错误,在下面一行中不存在

        batch = data.train.next_batch(50)
        

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

        【讨论】:

          猜你喜欢
          • 2018-01-29
          • 2020-09-22
          • 2020-02-21
          • 2020-12-08
          • 2019-12-10
          • 2020-05-10
          • 2019-11-04
          • 2017-05-24
          • 2021-06-14
          相关资源
          最近更新 更多