Keras模型密集输入形状投掷误差
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keras模型密集输入形状投掷误差相关的知识,希望对你有一定的参考价值。
我有一个特征向量与形状X_train.shape
作为(52, 54)
当我训练keras模型时,它会将错误抛给我:
ValueError: Error when checking model input: expected dense_109_input to have shape (None, 52) but got array with shape (52, 54)
我已经尝试了几乎所有我能想到的以及扫描堆栈溢出但我的问题仍然存在。代码如下:
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
##### Reading CSV #####
data = pd.read_csv('Dataset/Emotion_data.csv')
X = data.ix[:, 4:]
y = data['label']
##### Normalizing #####
featureName = list(X)
for name in featureName:
X[name] = (X[name] - min(X[name]))/(max(X[name]) - min(X[name]))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=3)
##### Model #####
model = Sequential()
model.add(Dense(100, input_shape=(54,), activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(1, activation='softmax'))
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
model.fit(X_train, y_train)
prediction = model.predict(X_test)
print(accuracy_score(y_test, prediction))
如果有人对数据头感兴趣
In[42]: X_train.head()
Out[42]:
tempo total_beats average_beats chroma_stft_mean chroma_stft_std
35 0.438961 0.480897 0.505383 0.504320 0.938452
34 0.520000 0.552580 0.500670 0.581778 0.680247
63 0.477551 0.361328 0.334990 0.705472 0.357676
27 0.477551 0.345419 0.309433 0.492245 0.728405
43 0.520000 0.530305 0.495715 0.306097 0.663995
chroma_stft_var chroma_cq_mean chroma_cq_std chroma_cq_var
35 0.932494 0.975206 0.394472 0.366960
34 0.657810 0.654770 0.550766 0.522269
63 0.333977 0.495473 0.618748 0.591578
27 0.707998 0.644147 0.628125 0.601222
43 0.640980 0.591299 0.639918 0.613379
chroma_cens_mean ... zcr_var harm_mean harm_std harm_var
35 0.964034 ... 0.381363 0.021468 0.426776 0.225840
34 0.755071 ... 0.213207 0.021598 0.115191 0.031476
63 0.704930 ... 0.197960 0.021620 0.350194 0.163286
27 0.715832 ... 0.247092 0.022253 0.319208 0.140714
43 0.784991 ... 0.221276 0.021777 0.656981 0.471881
perc_mean perc_std perc_var frame_mean frame_std frame_var
35 0.362241 0.673257 0.467421 0.343459 0.174215 0.048846
34 0.365434 0.152561 0.031588 0.091940 0.088991 0.018342
63 0.340043 0.320664 0.116833 0.097610 0.077334 0.015154
27 0.372315 0.604247 0.380492 0.995443 1.000000 1.000000
43 0.377154 0.529161 0.296033 0.122519 0.089255 0.018417
[5 rows x 54 columns]
答案
您没有在第一层中正确定义输入形状
model.add(Dense(100, input_shape=(54,), activation='relu'))
尝试将第一层中的代码更改为
model.add(Dense(100, input_shape=(52, 54), activation'relu))
以上是关于Keras模型密集输入形状投掷误差的主要内容,如果未能解决你的问题,请参考以下文章