[英]ValueError: expected axis -1 of input shape to have value 51948 but received input with shape (None, 52)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow.keras as keras
dataset = pd.read_csv('C:\\Users\\Maxie\\MyStuff\\FinalDatasetEng.csv')
inputs = dataset.iloc[:, 2:54].values
targets = dataset.iloc[:, 55].values
from sklearn.model_selection import train_test_split
inputs_train, inputs_test, targets_train, targets_test = train_test_split(inputs, targets,
test_size = 0.20, random_state = 0)
import keras
from keras.models import Sequential
from keras.layers import Dense
model = keras.Sequential([
# input layer
keras.layers.Flatten(input_shape=(inputs.shape[0], inputs.shape[1])),
# 1st dense layer
keras.layers.Dense(520, activation='relu'),
# 2nd dense layer
keras.layers.Dense(208, activation='relu'),
# 3rd dense layer
keras.layers.Dense(52, activation='relu'),
# output layer
keras.layers.Dense(4, activation='softmax')
])
optimiser = keras.optimizers.Adam(learning_rate=0.0001)
model.compile(optimizer=optimiser,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(inputs_train, targets_train, validation_data=(inputs_test, targets_test),
batch_size=32, epochs=50)
This is my code, I am getting this error: ValueError: Input 0 of layer dense_20 is incompatible with the layer: expected axis -1 of input shape to have value 51948 but received input with shape (None, 52).这是我的代码,我收到此错误:ValueError:dense_20 层的输入 0 与该层不兼容:输入形状的预期轴 -1 具有值 51948,但接收到形状的输入(无,52)。 Anyone please help me resolve this issue.
任何人都请帮我解决这个问题。
You have a 1D array as your features input, But you Flattened number of samples and number of features that gives the model 51948 input features (999 samples input.shape[0]
* 52 Features input.shape[1]
= 51948).您有一个一维数组作为您的特征输入,但是您将样本数量和特征数量展平,从而提供 model 51948 个输入特征(999 个样本
input.shape[0]
* 52 个特征input.shape[1]
= 51948)。 So your model expects an array of 51948 input, but you have passed inputs_train
which has 52 columns.因此,您的 model 需要一个包含 51948 个输入的数组,但您已经传递了具有 52 列的
inputs_train
。
Inference:推理:
You should not to flatten your input in the case you have 1D array as input features.如果您将一维数组作为输入要素,则不应展平您的输入。 Your input features is an array of 52 columns ans 999 samples.
您的输入特征是 52 列和 999 个样本的数组。 Instead of
Flatten
layer, use of an InputLayer
.代替
Flatten
层,使用InputLayer
。
So, modified code should be like this:所以,修改后的代码应该是这样的:
model = keras.Sequential([
# input layer
#change this line to input layer and set the input shape to the shape of your input features
#keras.layers.Flatten(input_shape=(inputs.shape[0], inputs.shape[1])),
keras.layers.InputLayer(input_shape=(inputs.shape[1],)),
# 1st dense layer
keras.layers.Dense(520, activation='relu'),
# 2nd dense layer
keras.layers.Dense(208, activation='relu'),
# 3rd dense layer
keras.layers.Dense(52, activation='relu'),
# output layer
keras.layers.Dense(4, activation='softmax')
])
optimiser = keras.optimizers.Adam(learning_rate=0.0001)
model.compile(optimizer=optimiser,
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(inputs_train, targets_train, validation_data=(inputs_test, targets_test),
batch_size=32, epochs=50)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.