Python错误:预期为2维,但数组的形状为(45,4,3,2,3,4,3)

问题描述 投票:0回答:1

我正在使用X_train训练形状为(104,4)且Y_train训练形状为(45,4)的模型,如下所示:

model = Sequential()
model.add(layers.Dense(50, input_dim=x_train.shape[1], activation='relu'))
model.add(layers.Dense(40, activation='relu'))
model.add(layers.Dense(30, activation='relu'))
model.add(layers.Dense(25, activation='relu'))
model.add(layers.Dense(3, activation='softmax'))

model.compile(loss='categorical_crossentropy',
          optimizer = 'adam',
          metrics=['accuracy'])
model.summary()

history = model.fit(x_train, y_train,
                batch_size = 10, epochs = 50,
                verbose = 1, validation_split = 0.3)

[当我尝试拟合我的数据时,这是引发的错误:

检查目标时发生错误:预期density_91具有2维,但数组的形状为(45,4,3,2,3,4,3)

我还尝试在第一个Dense层之前添加Flatten层,但这会导致以下错误:

输入2与flatten_3层不兼容:预期的min_ndim = 3,找到的ndim = 2

我不确定如何确定尺寸。您能帮我解决这个问题吗?谢谢!

python keras recurrent-neural-network
1个回答
0
投票

我将发布整个脚本:

import pandas as pd
import numpy as np
from keras.utils import np_utils
import keras
from keras.models import Sequential 
from keras import layers
from keras.optimizers import SGD

from sklearn.model_selection import train_test_split

from sklearn import preprocessing
from sklearn.preprocessing import LabelEncoder,OneHotEncoder

df = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",header=0)

df.columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width',"Class"]

df, cl = (df.iloc[:,0:4],df['Class'])

df_scaler = preprocessing.MinMaxScaler(feature_range=(0,1))
df1 = df_scaler.fit_transform(df)

cl1 = LabelEncoder().fit_transform(cl)

x_train, y_train, x_test, y_test = train_test_split(df1,cl1,test_size = 0.3)

y_train = np_utils.to_categorical(y_train, 3)
y_test = np_utils.to_categorical(y_test,3)

model = Sequential()
model.add(layers.Dense(50,input_dim=x_train.shape[1],activation='relu'))
model.add(layers.Dense(40,activation='relu'))
model.add(layers.Dense(30,activation='relu'))
model.add(layers.Dense(25,activation='relu'))
model.add(layers.Dense(3,activation='softmax'))

model.compile(loss='categorical_crossentropy',optimizer = 'adam',metrics=['accuracy'])

history = model.fit(x_train, y_train,batch_size = 10, epochs = 50,verbose = 1,validation_split = 0.3)

这是它给我的错误:

ValueError                                Traceback (most recent call last)
<ipython-input-95-4c538cd97d59> in <module>()
      1 history = model.fit(x_train, y_train,
      2                     batch_size = 10, epochs = 50,
----> 3                     verbose = 1, validation_split = 0.3)

2 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    129                         ': expected ' + names[i] + ' to have ' +
    130                         str(len(shape)) + ' dimensions, but got array '
--> 131                         'with shape ' + str(data_shape))
    132                 if not check_batch_axis:
    133                     data_shape = data_shape[1:]

ValueError: Error when checking target: expected dense_91 to have 2 dimensions, but got array with shape (45, 4, 3, 2, 3, 4, 3)

希望这对您有帮助,我正在学习在线课程,但我不知道为什么会出错。

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.