在使用张量流拟合分类器时导致此AttributeError的原因是什么?

时间:2017-05-06 17:58:51

标签: python numpy machine-learning tensorflow

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

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.ndarray。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我认为您的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'

答案 1 :(得分:0)

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

x=testImages,

和更早的

testImages = []   # and list append's

你在哪里确认这是一个阵列?