Keras输入值错误 [英] Keras input value error

查看:245
本文介绍了Keras输入值错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以(样本,时间步长,特征)张量形式的ECG数据,例如(2464,15000,1).

我的目标是将标签分类为1到5.我对目标进行了热编码,使其具有尺寸(2464,5).

在我开始LSTM之前,我想尝试一种基本方法.使用以下顺序模型:

 def build_model():  
        model = models.Sequential()                 
        model.add(layers.Dense(16, activation='relu',input_shape=(X_train.shape[1],X_train.shape[2]))) 
        model.add(layers.Dense(16, activation='relu'))           
        model.add(layers.Dense(y_train.shape[1], activation='softmax'))

        model.compile(loss='sparse_categorical_crossentropy',
                       optimizer='rmsprop',
                       metrics=['mae'])
        return model

    model=build_model()
    history=model.fit(X_train,
                      y_train,
                      epochs=20,
                      batch_size=512,
                      validation_data=(X_val,y_val))

不幸的是,我收到一个值错误:

文件 "C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ keras \ engine \ training.py", _standardize_input_data中的第113行 'with shape'+ str(data_shape))

ValueError:检查目标时出错:预期density_13具有3 尺寸,但数组的形状为(2464,5)

我搜索了与此问题相关的其他主题.在大多数情况下,在我的情况5中,输出层不符合预期的目标.因此,这应该是正确的.另一个问题通常是目标不是一个热编码的目标.但这也完成了.该错误告诉我具有三个维度(基于输入?).但是,如果我添加一个维度以获取目标维度(2464,5,1),则ValueError会发生变化,期望与输入数据具有相同的维度,而我必须使用softmax层将其缩小为y目标.

ValueError:检查目标时出错:预期density_23的形状为(15000,1),但数组的形状为(5,1)

我很困惑.你能给我一个提示吗?

额外:我也尝试过使输入变平,但是也得到了(不同的)ValueError(... shape(1,)但得到了形状为(5,)的数组).在这里扁平化是正确的方法吗?

感谢您的帮助.

解决方案

这里的问题实际上是您的损失函数.由于您已经有1个热编码的 y 向量,因此您不应该使用稀疏分类交叉熵,而应该使用分类交叉熵.试试这个

分类交叉熵

就尺寸而言,让我们只创建一些虚拟数据.

X_train = np.zeros((2464, 150, 1))
y_train = np.zeros((2464,))

X_val = np.zeros((2464, 150, 1))
y_val = np.zeros((2464,))

X_train_r = X_train.reshape(2464,150,)
X_val_r = X_val.reshape(2464,150,)
input_shape = (150,)

print(X_train_r.shape)
print(X_val_r.shape)

(2464,150)
(2464,150)

现在,我们将为标签获得一个热编码输出.

num_classes = 5
# Convert class vectors to binary class matrices. This uses 1 hot encoding.
y_train_binary = keras.utils.to_categorical(y_train, num_classes)
y_val_binary = keras.utils.to_categorical(y_val, num_classes)

print(y_train_binary.shape)
print(y_val_binary.shape)

(2464,5)
(2464,5)

现在,我们将按照以下步骤制作模型

model = Sequential()                 
model.add(Dense(16, activation='relu',input_shape=input_shape)) 
model.add(Dense(16, activation='relu'))           
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.summary()

history=model.fit(X_train_r,
                  y_train_binary,
                  epochs=5,
                  batch_size=8,
                  validation_data=(X_val_r, y_val_binary))

这将为您工作.

稀疏分类交叉熵

如果要使用稀疏分类交叉熵,则不应使用1-hot编码标签.像这样

X_train = np.zeros((2464, 150, 1))
y_train = np.zeros((2464,))

X_val = np.zeros((2464, 150, 1))
y_val = np.zeros((2464,))

X_train_r = X_train.reshape(2464,150,)
X_val_r = X_val.reshape(2464,150,)
input_shape = (150,)

num_classes = 5

model = Sequential()                 
model.add(Dense(16, activation='relu',input_shape=input_shape)) 
model.add(Dense(16, activation='relu'))           
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss='sparse_categorical_crossentropy',
                       optimizer='rmsprop',
                       metrics=['mae'])

model.summary()

history=model.fit(X_train_r,
                  y_train,
                  epochs=5,
                  batch_size=8,
                  validation_data=(X_val_r, y_val))

I have ECG data which are in tensor-form of (samples, timesteps, features) with e.g. (2464,15000,1).

I aim to classify labels in the range 1 to 5. I one hot encoded the target to have the dimensions (2464,5).

Before I start with LSTM I wanted to try a basic approach. With following sequential model:

 def build_model():  
        model = models.Sequential()                 
        model.add(layers.Dense(16, activation='relu',input_shape=(X_train.shape[1],X_train.shape[2]))) 
        model.add(layers.Dense(16, activation='relu'))           
        model.add(layers.Dense(y_train.shape[1], activation='softmax'))

        model.compile(loss='sparse_categorical_crossentropy',
                       optimizer='rmsprop',
                       metrics=['mae'])
        return model

    model=build_model()
    history=model.fit(X_train,
                      y_train,
                      epochs=20,
                      batch_size=512,
                      validation_data=(X_val,y_val))

Unfortunately, I get a value error:

File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 113, in _standardize_input_data 'with shape ' + str(data_shape))

ValueError: Error when checking target: expected dense_13 to have 3 dimensions, but got array with shape (2464, 5)

I searched for other topics with the problem. Most of the case, the output layer does not fit the expected targets, in my case 5. So that should be correct. The other issue was often that the target was not one hot encoded. But that is also done. The error tells me to have three dimensions (based on the input?). But if I add one dimension to gain a target dimension of (2464,5,1), the ValueError change, expecting the same dimension as the input data, while I have to reduce it to the y-target with the softmax layer.

ValueError: Error when checking target: expected dense_23 to have shape (15000, 1) but got array with shape (5, 1)

I get confused. Could you give me a hint?

Extra: I also tried to flatten the input, but get also a (different) ValueError (...shape (1,) but got array with shape (5,)). Is flattening here the correct approach?

Thanks for your help.

解决方案

The problem here is actually your loss function. Since you already have 1-hot encoded y vectors then you should not be using the sparse categorical cross entropy, but rather you should use categorical cross entropy. Try this

Categorical cross entropy

Let's just make some dummy data for dimensions sake.

X_train = np.zeros((2464, 150, 1))
y_train = np.zeros((2464,))

X_val = np.zeros((2464, 150, 1))
y_val = np.zeros((2464,))

X_train_r = X_train.reshape(2464,150,)
X_val_r = X_val.reshape(2464,150,)
input_shape = (150,)

print(X_train_r.shape)
print(X_val_r.shape)

(2464, 150)
(2464, 150)

Now we will get our one hot encoded outputs for our labels.

num_classes = 5
# Convert class vectors to binary class matrices. This uses 1 hot encoding.
y_train_binary = keras.utils.to_categorical(y_train, num_classes)
y_val_binary = keras.utils.to_categorical(y_val, num_classes)

print(y_train_binary.shape)
print(y_val_binary.shape)

(2464, 5)
(2464, 5)

Now we will make our model as follows

model = Sequential()                 
model.add(Dense(16, activation='relu',input_shape=input_shape)) 
model.add(Dense(16, activation='relu'))           
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.summary()

history=model.fit(X_train_r,
                  y_train_binary,
                  epochs=5,
                  batch_size=8,
                  validation_data=(X_val_r, y_val_binary))

This will work for you.

Sparse categorical cross entropy

If you want to use sparse categorical cross entropy then you should not use 1-hot encoded labels. Like this

X_train = np.zeros((2464, 150, 1))
y_train = np.zeros((2464,))

X_val = np.zeros((2464, 150, 1))
y_val = np.zeros((2464,))

X_train_r = X_train.reshape(2464,150,)
X_val_r = X_val.reshape(2464,150,)
input_shape = (150,)

num_classes = 5

model = Sequential()                 
model.add(Dense(16, activation='relu',input_shape=input_shape)) 
model.add(Dense(16, activation='relu'))           
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss='sparse_categorical_crossentropy',
                       optimizer='rmsprop',
                       metrics=['mae'])

model.summary()

history=model.fit(X_train_r,
                  y_train,
                  epochs=5,
                  batch_size=8,
                  validation_data=(X_val_r, y_val))

这篇关于Keras输入值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆