Tflearn训练批次错误提示 "object of type 'Tensor' has no len()"

4
我刚接触TensorFlow,正在使用Tflearn训练图像以分类眼睛状态。目前我有400张训练图像和200张验证图像。我在脚本中使用image_preloader来获取自定义图像输入。我认为它成功地加载了图像,显示为:

tflearn.data_utils.ImagePreloader object at 0x7fa28f3a5310

但在训练期间,在划分和获取批次时出现问题,导致Traceback错误。
Traceback (most recent call last):
  File "tflearn_custom.py", line 181, in <module>
    model.fit(x,y,validation_set=({'input':test_x},{'targets':test_y}),n_epoch=10,batch_size=10)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/models/dnn.py", line 215, in fit
    callbacks=callbacks)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.py", line 285, in fit
    self.summ_writer, self.coord)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.py", line 709, in initialize_fit
    self.n_train_samples = len(get_dict_first_element(feed_dict))
TypeError: object of type 'Tensor' has no len()

以下是我的代码:

import tflearn 
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout,fully_connected
from tflearn.layers.estimator import regression 
import tensorflow as tf
from tflearn.data_utils import image_preloader

test_filename='/path_to_validating_set/'
train_filename='/path_to_training_set/'


x, y = image_preloader(train_filename, image_shape=(128, 128),  mode='folder',  grayscale=True, categorical_labels=True,   normalize=True)
test_x, test_y = image_preloader(test_filename, image_shape=(128, 128),   mode='folder', grayscale=True, categorical_labels=True,   normalize=True)

convnet =input_data(shape=[None, 128,128,1], name='input')  
convnet = conv_2d(convnet, 32, 2, activation='relu') 

convnet = max_pool_2d(convnet,2)

convnet = conv_2d(convnet, 64, 2, activation='relu')

convnet = max_pool_2d(convnet,2)

convnet = fully_connected(convnet, 1024, activation='relu')  

convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 2, activation='softmax')



convnet = regression(convnet, optimizer='adam', learning_rate=0.01, loss='categorical_crossentropy', name='targets')

model= tflearn.DNN(convnet)


model.fit(x,y,validation_set=({'input':test_x},{'targets':test_y}),n_epoch=5,batch_size=5)

我正在使用TensorFlow 1.0。我已经尝试搜索类似的问题,但没有解决它。

你确定你正在将正确类型的输入传递给validation_set吗?文档要求提供 tuple ... 元组保存数据和目标(以与X_inputs和Y_targets相同的类型提供)。此外,它还接受浮点数(<1),以对训练数据执行数据拆分。 - patrick
@patrick 我也尝试了这个,model.fit(x,y,n_epoch=10,validation_set=0.1,batch_size=10),但是还是出现了同样的错误。 - dp01
1个回答

5

我认为问题在于feed_dict是一个张量,而类型为Tensor的对象没有长度。您可以使用feed_dict.shape[0]来获取其长度。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接