【问题标题】:What is causing this AttributeError when fitting a classifier with tensor flow?使用 tensorflow 拟合分类器时导致此 AttributeError 的原因是什么?
【发布时间】:2017-05-06 17:58:51
【问题描述】:

我正在尝试编辑一些示例张量代码流,以使用一些数据训练和测试卷积神经网络。我目前有以下代码设置张量流并从文件中获取我的图像数据:

mnist_classifier = learn.Estimator(
      model_fn=cnn_model_fn, model_dir="/tmp/mnist_convnet_model")


tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
      tensors=tensors_to_log, every_n_iter=50)


testImages = []
testLabels = []
for filename in os.listdir('images'):
    im = cv2.imread('images/' + filename)
    testImages.append(im)
    testLabels.append(np.int32(1.0))

for filename in os.listdir('badImages'):
    im = cv2.imread('badImages/' + filename)
    testImages.append(im)
    testLabels.append(np.int32(0.0))

然后我尝试使用以下行拟合模型:

 mnist_classifier.fit(
      x=testImages,
      y=testLabels,
      batch_size=10,
      steps=20000,
      monitors=[logging_hook])

但这会在运行时崩溃并出现以下错误:

if x_is_dict else check_array(x, x.dtype)
AttributeError: 'list' object has no attribute 'dtype'

似乎是说我的 testImages var 的结构/格式存在一些问题,但我已经确认它是正确的类型 - numpy.ndarray 的 numpy.ndarrays。有什么想法吗?

【问题讨论】:

    标签: python numpy machine-learning tensorflow


    【解决方案1】:

    错误表明x 是list,因此没有dtype。在fit 命令中我看到了

    x=testImages,
    

    及更早的

    testImages = []   # and list append's
    

    你在哪里确认这是一个数组?

    【讨论】:

    • 在我附加到 testImages 之后,我打印了它的类型,它说 bumpy.ndarray,不过会再次检查一下
    【解决方案2】:

    我认为您的 x 应该是 numpy array 类型,但看起来您使用的是列表

    >>> import numpy as np
    >>> x= np.array([1,2,3])
    >>> x.dtype
    dtype('int64')
    >>> y=[1,2,3]
    >>> y.dtype
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'list' object has no attribute 'dtype'
    

    【讨论】:

      猜你喜欢
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 2020-12-23
      相关资源
      最近更新 更多