期望dense_1有两个维度,但是有形状的数组(308,1,6)

时间:2022-12-29 22:54:17

I'm trying to use Conv1D for the first time for multiclass classification of time series data and my model keeps throwing this error when I use it.

我第一次尝试使用Conv1D对时间序列数据进行多类分类,我的模型在使用时不断抛出这个错误。

import numpy as np
import os

import keras
from keras.models import Sequential
from keras.layers import Conv1D, Dense, TimeDistributed, MaxPooling1D, Flatten

# fix random seed for reproducibility
np.random.seed(7)

dataset1 = np.genfromtxt(os.path.join('data', 'norm_cellcycle_384_17.txt'), delimiter=',', dtype=None)
data = dataset1[1:]

# extract columns
genes = data[:,0]
y_all = data[:,1].astype(int)
x_all = data[:,2:-1].astype(float)

# deleted this line when using sparse_categorical_crossentropy
# 384x6
y_all = keras.utils.to_categorical(y_all)

# 5
num_classes = np.unique(y_all).shape[0]

# split entire data into train set and test set
validation_split = 0.2

val_idx = np.random.choice(range(x_all.shape[0]), int(validation_split*x_all.shape[0]), replace=False)
train_idx = [x for x in range(x_all.shape[0]) if x not in val_idx]

x_train = x_all[train_idx]
y_train = y_all[train_idx]

# 308x17x1
x_train = x_train[:, :, np.newaxis]
# 308x1
y_train = y_train[:,np.newaxis]

x_test = x_all[val_idx]
y_test = y_all[val_idx]

# deleted this line when using sparse_categorical_crossentropy
y_test = keras.utils.to_categorical(y_test)

# 76x17x1
x_test = x_test[:, :, np.newaxis]
# 76x1
y_test = y_test[:,np.newaxis]


print(x_train.shape[0],'train samples')
print(x_test.shape[0],'test samples')


# Create Model 
# number of filters for 1D conv
nb_filter = 4
filter_length = 5

window = x_train.shape[1]
model = Sequential()

model.add(Conv1D(filters=nb_filter,kernel_size=filter_length,activation="relu", input_shape=(window,1)))
model.add(MaxPooling1D())
model.add(Conv1D(nb_filter=nb_filter, filter_length=filter_length, activation='relu'))
model.add(MaxPooling1D())
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))
model.summary()
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=25, batch_size=2, validation_data=(x_test, y_test))

I don't know why I get this error. When I use binary_crossentropy loss and no one hot encoding for y_all, my model works. But it fails when I use one hot encoding for y_all with categorical_crossentropy loss. When I don't use one hot encoding, keras throws an error making me change y_all to one a binary matrix. I don't even know where the (1,6) are coming from in the array.

我不知道为什么会出现这个错误。当我使用binary_cross熵损失并没有对y_all进行热编码时,我的模型是有效的。但是当我对y_all使用热编码时,它失败了,因为它具有categorical_crossentropy loss。当我不使用一个热编码时,keras抛出一个错误,使我将y_all更改为一个二进制矩阵。我甚至不知道(1,6)来自数组的哪里。

ValueError: Error when checking model target: expected dense_1 to have 2 dimensions, but got array with shape (308, 1, 6)

检查模型目标时的错误:期望dense_1有2个维度,但有形状的数组(308,1,6)

Please help! I've been stuck on this for many hours! Already went through all the related questions but still doesn't make sense.

请帮助!我已经被这个问题困扰了好几个小时了!已经回答了所有相关的问题,但仍然没有意义。

Update: I now use sparse_categorical_crossentropy because it has integer support. I deleted the to_categorical lines from the above code and I get this new error:

更新:我现在使用sparse_categorical_cross熵,因为它有整数支持。我从上面的代码中删除了to_categorical行,我得到了一个新的错误:

InvalidArgumentError (see above for traceback): Received a label value of 5 which is outside the valid range of [0, 5). Label values: 2 5
[[Node: SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits = SparseSoftmaxCrossEntropyWithLogits[T=DT_FLOAT, Tlabels=DT_INT64, _device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_1, Cast)]]

[endnoteref: 1] [[endnoteref: 1]] [[endnoteref: 2]] [[endnoteref: 2]] [[endnoteref: 2]] [[endnoteref: 2]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endnoteref: 3]] [[endno

Requested sample of data:

要求的样本数据:

,Main,Gp,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17
YDL179w,1,-0.75808,-0.90319,-0.98935,-0.73995,-0.67193,-0.12777,-0.95307,-1.01656,0.79730,2.11688,1.98537,0.61591,0.56603,-0.13684,-0.52228,-0.05068,0.78823,
YLR079w,1,-0.48845,-0.70828,-0.47688,-0.65814,-0.45374,-0.47302,-0.71214,-1.02839,0.24048,3.11376,1.28952,0.44874,0.04379,-0.31104,-0.30332,-0.34575,0.82285,
YER111c,1,-0.42218,0.23887,1.84427,-0.02083,-0.61105,-0.65827,-0.79992,-0.39857,-0.09166,2.03314,1.58457,0.68744,0.14443,-0.72910,-1.46097,-0.82353,-0.51662,
YBR200w,1,0.09824,0.55258,-0.89641,-1.19111,-1.11744,-0.76133,0.09824,2.16120,1.46126,1.03148,0.67537,-0.33155,-0.60170,-1.39987,-0.42978,-0.15963,0.81045,
YPL209c,2,-0.65282,-0.32055,2.53702,2.00538,0.60982,0.51014,-0.55314,-1.01832,-0.78573,0.01173,0.07818,-0.05473,-0.22087,0.24432,-0.28732,-1.11801,-0.98510,
YJL074c,2,-0.81087,-0.19448,1.72941,0.59002,-0.53069,-0.25051,-0.92294,-0.92294,-0.53069,0.08570,1.87884,1.97223,0.45927,-0.36258,-0.34390,-1.07237,-0.77351,
YNL233w,2,-0.43997,0.66325,2.85098,0.74739,-0.42127,-0.47736,-0.79524,-0.80459,-0.48671,-0.21558,1.25226,1.01852,-0.10339,-0.56151,-0.96353,-0.46801,-0.79524,
YLR313c,2,-0.46611,0.42952,3.01689,1.13856,0.01902,-0.44123,-0.66514,-0.98856,-0.59050,-0.47855,0.84002,0.39220,0.50416,-0.50342,-0.82685,-0.64026,-0.73977,
YGR041w,2,-0.57187,-0.26687,1.10561,-0.38125,-0.68624,-0.26687,-0.87687,-1.18186,-0.80062,0.60999,2.09686,1.82998,1.14374,0.11437,-0.80062,-0.87687,-0.19062,

1 个解决方案

#1


0  

So I noticed that even though I know there are 5 classes in this dataset as seen by the unique values obtained for y_all, for some reason Keras to_categorical thinks there are 6 classes.

因此我注意到,尽管我知道这个数据集中有5个类,如y_all的唯一值所示,出于某种原因,Keras to_categorical认为有6个类。

# 384x6 
y_all = keras.utils.to_categorical(y_all)

# 5 
num_classes = np.unique(y_all).shape[0]

I don't know why that is. Keeping this in mind I changed this line of code and my model began to run:

我不知道为什么。记住这一点,我修改了这一行代码,我的模型开始运行:

model.add(Dense(num_classes, activation='softmax'))

模型。add(密度(num_classes,激活= ' softmax '))

to

model.add(Dense(num_classes+1, activation='softmax'))

模型。add(密度(num_classes + 1,激活= ' softmax '))

I still don't know why to_categorical behaves this way. Anyone know?

我还是不知道为什么to_categorical会这样。有人知道吗?

#1


0  

So I noticed that even though I know there are 5 classes in this dataset as seen by the unique values obtained for y_all, for some reason Keras to_categorical thinks there are 6 classes.

因此我注意到,尽管我知道这个数据集中有5个类,如y_all的唯一值所示,出于某种原因,Keras to_categorical认为有6个类。

# 384x6 
y_all = keras.utils.to_categorical(y_all)

# 5 
num_classes = np.unique(y_all).shape[0]

I don't know why that is. Keeping this in mind I changed this line of code and my model began to run:

我不知道为什么。记住这一点,我修改了这一行代码,我的模型开始运行:

model.add(Dense(num_classes, activation='softmax'))

模型。add(密度(num_classes,激活= ' softmax '))

to

model.add(Dense(num_classes+1, activation='softmax'))

模型。add(密度(num_classes + 1,激活= ' softmax '))

I still don't know why to_categorical behaves this way. Anyone know?

我还是不知道为什么to_categorical会这样。有人知道吗?