为什么我的递归神经网络给我一个错误?

时间:2019-02-08 03:10:05

标签: python numpy keras deep-learning recurrent-neural-network

您好,我在制作一个使用openaigym玩卡特尔的递归神经网络时遇到此值错误-

Traceback (most recent call last):
  File "C:/Users/Tejas/Desktop/ML Laboratory/Deep Learning/Neural Networks/4. Sentdex/Part - 3/Gym.py", line 147, in train_model
    model.fit(X, y, batch_size=64, epochs = 5)

  File "C:\Users\Tejas\Anaconda3\envs\tensorflow_gpuenv\lib\site-packages\keras\engine\training.py", line 952, in fit
    batch_size=batch_size)

  File "C:\Users\Tejas\Anaconda3\envs\tensorflow_gpuenv\lib\site-packages\keras\engine\training.py", line 789, in _standardize_user_data
    exception_prefix='target')

  File "C:\Users\Tejas\Anaconda3\envs\tensorflow_gpuenv\lib\site-packages\keras\engine\training_utils.py", line 102, in standardize_input_data
    str(len(data)) + ' arrays: ' + str(data)[:200] + '...')

ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 19570 arrays: [array([[0],
       [1]]), array([[1],
       [0]]), array([[0],
       [1]]), array([[1],
       [0]]), array([[1],
       [0]]), array([[1],
       [0]]), array([[1],
       [0]]), array([[0],
     ...

这是我制作的递归神经网络模型。我认为必须在此定义中进行一些更改-

def neural_network_model(input_size):
    model = Sequential()
    model.add(CuDNNLSTM(128, input_shape=(input_size, 1), return_sequences=True))
    model.add(Dropout(0.8))

    model.add(CuDNNLSTM(256, return_sequences=True))
    model.add(Dropout(0.8))

    model.add(CuDNNLSTM(512))
    model.add(Dropout(0.8))

    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.8))

    model.add(Dense(2, activation='softmax'))
    return model

然后训练该函数执行的模型-

def train_model(training_data, model=False):

    X = np.array([i[0] for i in training_data]).reshape(-1,len(training_data[0][0]),1)
    y = [i[1] for i in training_data]
    print(len(X[0]))
    if not model:
        model = neural_network_model(input_size = len(X[0]))
        model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
        model.fit(X, y, batch_size=64, epochs = 5)
    return model.

我不明白为什么会出现此错误。我曾尝试更改输入形状和许多其他东西,但没有一个解决我的问题。如果您认为完整的代码有帮助,可以从这里获取-Full Code

1 个答案:

答案 0 :(得分:0)

您的目标y是一个列表,但它应该是一个numpy数组。在拟合模型之前,请尝试使用y = np.array(y)