AssertionError:传入张量形状必须为4-D

时间:2018-12-26 22:54:08

标签: python tensorflow classification tflearn

我正在尝试做一个分类器,并且不断出现这个错误,这确实让我感到困惑。因为我真的是机器学习的新手,所以我无法在互联网上找到任何东西。

错误

AssertionError: Incoming Tensor shape must be 4-D

代码

IMG_SIZE = 64
    tf.reset_default_graph()
    convnet = input_data(shape=[1,IMG_SIZE,IMG_SIZE,1],name='input')
    convnet = conv_2d(convnet, 32, 5, activation='relu')
    convnet = max_pool_2d(convnet, 5)
    convnet = conv_2d(convnet, 64, 5, activation='relu')
    convnet = max_pool_2d(convnet, 5)
    convnet = conv_2d(convnet, 128, 5, activation='relu')
    convnet = max_pool_2d(convnet, 5)
    convnet = conv_2d(convnet, 64, 5, activation='relu')
    convnet = max_pool_2d(convnet, 5)
    convnet = conv_2d(convnet, 32, 5, activation='relu')
    convnet = max_pool_2d(convnet, 5)
    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=LR, loss='categorical_crossentropy', name='targets')
    model = tflearn.DNN(convnet, tensorboard_dir='log', tensorboard_verbose=0)
    model.fit({'input': X_train}, {'targets': y_train}, n_epoch=10,
              validation_set=({'input': X_test}, {'targets': y_test}),
              snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

如果我给convnet = input_data(shape=[None,IMG_SIZE,IMG_SIZE,1],name='input') 它给了我这个错误

Exception in thread Thread-3:

Traceback (most recent call last):
  File "C:\Users\zeele\Miniconda3\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\zeele\Miniconda3\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tflearn\data_flow.py", line 187, in fill_feed_dict_queue
    data = self.retrieve_data(batch_ids)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tflearn\data_flow.py", line 222, in retrieve_data
    utils.slice_array(self.feed_dict[key], batch_ids)
  File "C:\Users\zeele\Miniconda3\lib\site-packages\tflearn\utils.py", line 187, in slice_array
    return X[start]
TypeError: 'generator' object is not subscriptable

1 个答案:

答案 0 :(得分:0)

编辑:错误是由于此原因

train = training_data[:-5000]
test = testing_data[-5000:]

X_train = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
#The error was here
y_train = (i[1] for i in train)

X_test = np.array([i[0] for i in test]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
#The error was here
y_test = (i[1] for i in test)

在Y_train中列出列表后,它开始工作,如下所示。

train = training_data[:-5000]

test = testing_data[-5000:]

X_train = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)

y_train = [i[1] for i in train]

X_test = np.array([i[0] for i in test]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
y_test = [i[1] for i in test]