实施具有多个输出的神经网络时遇到keras错误

Encountering error for keras when implementing neural network with multiple outputs

我正在实现一个国际象棋 AI,其中输出是位置和要移动的棋子。但是,当我按照 the keras API documentation 上的多输出教程进行操作时,它 returns 错误

Failed to find data adapter that can handle input:(<class 'list'> containing values of types {'(<class \'list\'> containing values of types {\'(<class \\'list\\'> containing values of types {\\'(<class \\\\'list\\\\'> containing values of types {"<class \\\\'int\\\\'>"})\\'})\'})'}), (<class 'dict'> containing {"<class 'str'>"} keys and {"<class 'numpy.ndarray'>"} values)

抱歉,如果复制的部分太长,我只是想确保更容易找到问题所在。

以下代码的可重现部分:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

board_inputs = keras.Input(shape=(8, 8, 12))


conv1= layers.Conv2D(10, 3, activation='relu')
conv2 = layers.Conv2D(10, 3, activation='relu')
pooling1 = layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding="valid", data_format=None,)
pooling2 = layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding="valid", data_format=None,)
flatten = keras.layers.Flatten(data_format=None)


x = conv1(board_inputs)
x = pooling1(x)
x = conv2(x)
x = flatten(x)
piece_output = layers.Dense(12,name = 'piece')(x)
alpha_output = layers.Dense(7,name = 'alpha')(x)
numbers_output = layers.Dense(7,name = 'number')(x)


model = keras.Model(inputs=board_inputs, outputs=[piece_output,alpha_output,numbers_output], name="chess_ai_v3")
model.compile(
    loss=keras.losses.mse,
    optimizer=keras.optimizers.Adam(),
    metrics=None,
)

keras.utils.plot_model(model, "multi_input_and_output_model.png", show_shapes=True)
history = model.fit(
    trans_data[:len(trans_data)],
    {"piece": pieces[:len(trans_data)], "alpha": alphas[:len(trans_data)],"number": numbers[:len(trans_data)]},
    epochs=2,
    batch_size=32,
)
# history = model.fit(trans_data[:len(trans_data)],batch_size=64, epochs=1000,verbosity = 2)

更新: 我仍然遇到网络问题,我测试了为答案设置的每个值数组。它们在单独执行时都可以正常运行,有谁知道可能导致这种情况的任何问题吗?

您的数据格式似乎很奇怪。看看

我没问题运行这个例子

import numpy as np
from tensorflow.keras import layers, models


board_inputs = layers.Input(shape=(8, 8, 12))
conv1= layers.Conv2D(10, 3, activation='relu')
conv2 = layers.Conv2D(10, 3, activation='relu')
pooling1 = layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding="valid", data_format=None,)
pooling2 = layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding="valid", data_format=None,)
flatten = layers.Flatten(data_format=None)


x = conv1(board_inputs)
x = pooling1(x)
x = conv2(x)
x = flatten(x)
piece_output = layers.Dense(12,name = 'piece')(x)
alpha_output = layers.Dense(7,name = 'alpha')(x)
numbers_output = layers.Dense(7,name = 'number')(x)


model = models.Model(inputs=board_inputs, outputs=[piece_output,alpha_output,numbers_output], name="chess_ai_v3")
model.compile(loss='mse', optimizer='adam')
model.summary()

X = np.random.uniform(0,1, (100,8,8,12))
y = {"piece": np.random.uniform(0,1,(100,12)), 
     "alpha": np.random.uniform(0,1,(100,7)),
     "number": np.random.uniform(0,1,(100,7))}

history = model.fit(X,y, epochs=2, batch_size=32)