ValueError: Cannot feed value of shape TensorFlow

ValueError: Cannot feed value of shape TensorFlow

我在使用 TFLearn / TensorFlow 时遇到一些问题。我已将 np.reshape 调整为合适的尺寸,但因错误而崩溃:

这个错误发生在训练代码的第17行:

ValueError: Cannot feed value of shape (48, 1) for Tensor 'TargetsData/Y:0', which has shape '(?, 2)'

参考线:

model.fit(X, Y, n_epoch=250, validation_set=(W,Z), show_metric=True)

我的训练代码如下:

import deepneuralnet as net
import numpy as np
from tflearn.data_utils import image_preloader
import os

model = net.model
train_path = os.path.abspath('train')
print(train_path)
X, Y = image_preloader(target_path=train_path, image_shape=(100, 100),
 mode='folder', grayscale=False, categorical_labels=True, normalize=True)
X = np.reshape(X, (-1, 100, 100, 3))

validate_path = os.path.abspath('validate')
W, Z = image_preloader(target_path=validate_path, image_shape=(100, 100),
 mode='folder', grayscale=False, categorical_labels=True, normalize=True)
W = np.reshape(W, (-1, 100, 100, 3))
model.fit(X, Y, n_epoch=250, validation_set=(W,Z), show_metric=True)
model.save('./ZtrainedNet/final-model.tfl')

神经网络是:

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.metrics import Accuracy

acc = Accuracy()
network = input_data(shape=[None, 100, 100, 3])
# Conv layers ------------------------------------
network = conv_2d(network, 64, 3, strides=1, activation='relu')
network = max_pool_2d(network, 2, strides=2)
network = conv_2d(network, 64, 3, strides=1, activation='relu')
network = max_pool_2d(network, 2, strides=2)
network = conv_2d(network, 64, 3, strides=1, activation='relu')
network = conv_2d(network, 64, 3, strides=1, activation='relu')
network = conv_2d(network, 64, 3, strides=1, activation='relu')
network = max_pool_2d(network, 2, strides=2)
# Fully Connected Layers -------------------------
network = fully_connected(network, 1024, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 1024, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='momentum',
 loss='categorical_crossentropy',
learning_rate=0.001, metric=acc)
model = tflearn.DNN(network)

我的理解是跟softmax有关系吗?不过我不确定。

你的Y值也是one-hot编码的吗?我只是想猜测为什么 Y 的形状是 (?, 2)。 如果您可以在训练集中共享一些样本标签,那就太好了。

原来是子文件夹搞砸了。 2 对应于我拥有的子文件夹的数量,我认为我设置正确,但在 "train."

中只有 1 个子文件夹