TLearn 错误地将形状作为输入

TFlearn error feeding a shape as an input

我正在创建一个神经网络来玩 Tic Tac Toe。我正在将 tflearn 用于神经网络。这是我使用的训练数据

[[[1, 1, 1, 0, -1, -1, -1, 0, 0], 6], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 3], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 5],
                     [[1, 1, 1, 0, -1, -1, -1, 0, 0], 2], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 7], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 1],
                     [[0, 0, 1, -1, 1, 0, 1, -1, -1], 4], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 3], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 8],
                     [[0, 0, 1, -1, 1, 0, 1, -1, -1], 5], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 9], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 7],
                     [[0, -1, 1, 0, 1, 0, 1, -1, -1], 9], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 3], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 2],
                     [[0, -1, 1, 0, 1, 0, 1, -1, -1], 5], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 8], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 7],
                     [[1, -1, -1, 0, 1, 0, -1, 0, 1], 2], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 1], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 3],
                     [[1, -1, -1, 0, 1, 0, -1, 0, 1], 5], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 7], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 9],
                     [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 1], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 5], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 3],
                     [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 2], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 7], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 8]]

它包含当前棋盘状态,9个数字的列表和第1个数字的位置。我将电路板和位置分离到数据和标签中。当我将数据输入神经网络时出现此错误

ValueError: Cannot feed value of shape (30, 9) for Tensor u'input/X:0', which has shape '(?, 30, 9)'

这是我用来创建和训练模型的代码

def create_model():
network = input_data(shape=(None, 30, 9), name='input')

network = fully_connected(network, 128, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 256, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 256, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 128, activation='relu')
network = dropout(network, 0.8)

network = fully_connected(network, 9, activation='linear')
network = regression(network, optimizer='adam', learning_rate=0.01, loss='mean_square', name='targets')

model = tflearn.DNN(network, tensorboard_dir='log')

return model

def train_model():
training_data = [[[1, 1, 1, 0, -1, -1, -1, 0, 0], 6], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 3], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 5],
                 [[1, 1, 1, 0, -1, -1, -1, 0, 0], 2], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 7], [[1, 1, 1, 0, -1, -1, -1, 0, 0], 1],
                 [[0, 0, 1, -1, 1, 0, 1, -1, -1], 4], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 3], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 8],
                 [[0, 0, 1, -1, 1, 0, 1, -1, -1], 5], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 9], [[0, 0, 1, -1, 1, 0, 1, -1, -1], 7],
                 [[0, -1, 1, 0, 1, 0, 1, -1, -1], 9], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 3], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 2],
                 [[0, -1, 1, 0, 1, 0, 1, -1, -1], 5], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 8], [[0, -1, 1, 0, 1, 0, 1, -1, -1], 7],
                 [[1, -1, -1, 0, 1, 0, -1, 0, 1], 2], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 1], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 3],
                 [[1, -1, -1, 0, 1, 0, -1, 0, 1], 5], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 7], [[1, -1, -1, 0, 1, 0, -1, 0, 1], 9],
                 [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 1], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 5], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 3],
                 [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 2], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 7], [[-1, 1, -1, 0, 1, 0, -1, 1, 0], 8]]
x = []
y = []

for i in training_data:
    x.append(i[0])
    y.append(i[1])

model = create_model()
model.fit({'input': x}, {'targets': y}, n_epoch=10, snapshot_step=500, show_metric=True, run_id='openai_learning')

在你的第 2 行你写了

network = input_data(shape=(None, 30, 9), name='input')

这将创建一个具有指定形状的 TensorFlow 占位符,即 (None, 30, 9),其中 None 表示批量大小。

然而,当您在此行中提供输入时

 model.fit({'input': x}, {'targets': y}, n_epoch=10, snapshot_step=500, show_metric=True, run_id='openai_learning')

您提供的形状 (30, 9) 与 input_data 函数创建的占位符形状不匹配。所以我建议你导入 numpy 并在 model.fit

之前添加这一行
x = np.reshape(x, (-1, 30, 9))

这会将您的数组重塑为占位符预期的形状。这是 (batch_size, 30, 9)