Keras错误检查目标时出错:预期activation_1具有2个维度,但得到了具有形状的数组(10,5,95)

时间:2018-02-15 02:09:12

标签: keras

我正在尝试使用keras创建一个简单的RNN但是我收到了这个错误。 输入是由二进制类表示的字母流。形状是(10,5,55)。 10批次,每次5个字母,共95个字符。

我猜它与输入反馈的错误输入有关,但我不知道如何处理它。

Traceback (most recent call last):
  File "07_rnn.py", line 90, in <module>
    model.fit(x, y, epochs=3, batch_size=BATCHSIZE)
  File "/home/dmabelin/.local/lib/python3.5/site-packages/keras/models.py", line 965, in fit
    validation_steps=validation_steps)
  File "/home/dmabelin/.local/lib/python3.5/site-packages/keras/engine/training.py", line 1593, in fit
    batch_size=batch_size)
  File "/home/dmabelin/.local/lib/python3.5/site-packages/keras/engine/training.py", line 1430, in _standardize_user_data
    exception_prefix='target')
  File "/home/dmabelin/.local/lib/python3.5/site-packages/keras/engine/training.py", line 110, in _standardize_input_data
    'with shape ' + str(data_shape))
ValueError: Error when checking target: expected activation_1 to have 2 dimensions, but got array with shape (10, 5, 95)

代码

import numpy as np
import glob
from keras.models import Sequential
from keras.layers import LSTM, Dense, Activation
from keras.optimizers import Adam
from keras.utils.np_utils import to_categorical
CHARMAP = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=!@#$%^&*()_+`~[]\{}|;':\",./<>?"

SEQLEN = 5
BATCHSIZE = 10
ALPHASIZE = len(CHARMAP)
INTERNALSIZE = 128
FILES = "shakespeare/*.txt"
LEARNING_RATE = 0.001


## Data related stuff

def char_to_value(char):
    idx = CHARMAP.find(char)
    if idx >= 0:
        return idx
    else:
        return 0

def char_to_class_map(char):
    value = char_to_value(char)
    return to_categorical(value,ALPHASIZE)

def value_to_char(value):
    return CHARMAP[value]

# iterate every single file
def get_file_data(pattern, index):
    paths = glob.glob(pattern)
    length = len(paths)

    if index < length:
        data = []
        with open(paths[index], "r") as file:
            for line in file:
                line_values = [char_to_class_map(l) for l in line]
                data = data + list(line_values)
        return data
    else:
        return None

# get batch data in file
def build_line_data(file_data, seqlen, batch_index, batch_count):
    length = len(file_data)
    start = batch_index * batch_count
    end = start+seqlen
    x = []
    y = []
    while end+1 <= length and len(x) < batch_count:
        x_line = file_data[start:end]
        y_line = file_data[start+1:end+1]
        x.append(x_line)
        y.append(y_line)
        start = start + 1
        end = start + seqlen
    x = np.array(x)
    y = np.array(y)
    return x,y


def create_model():
    model = Sequential()
    model.add(LSTM(INTERNALSIZE,input_shape=(SEQLEN, ALPHASIZE)))
    model.add(Dense(ALPHASIZE))
    model.add(Activation('softmax'))
    #adam optimizer
    optimizer = Adam(lr=LEARNING_RATE)
    model.compile(loss='categorical_crossentropy', optimizer=optimizer)
    return model

print('before create_model')
model = create_model()
print('after create_model')
for i in range(1):
    print('before get file data')
    file_data = get_file_data(FILES, i)
    print('after get file data')
    idx = 0
    while True:
        print('before build line data')
        x,y = build_line_data(file_data, SEQLEN, idx ,BATCHSIZE)
        print('after build line data')
        print('before fit')
        model.fit(x, y, epochs=3, batch_size=BATCHSIZE)
        print('after fit')
        idx = idx + 1
        if 0 == len(x):
            break
        if idx > 10:
            break

github链接:https://github.com/djaney/ml-studies/blob/master/07_rnn.py

编辑: return_sequences = LSTM中的True修复它。

1 个答案:

答案 0 :(得分:1)

你想要预测什么?如果它是序列到序列模型,那么return_sequences=true是正确的方法。 出错的原因是您的目标是3维(batchsize,sequence_length,features),而LSTM图层仅在序列的最后一个步骤输出(batchsize,features) return_sequences=false。 因此,根据您的应用,您必须更改目标的形状或设置return_sequences=true,就像您已经做过的那样。