简体   繁体   English

ValueError:输入形状的预期轴 -1 的值为 51948,但接收到的输入形状为(无,52)

[英]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.

相关问题 “ValueError:……与图层不兼容:输入形状的预期轴 -1 的值为 8,但接收到的输入形状为(无、7、169)” - “ValueError: …incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)” ValueError: ...与图层不兼容:输入形状的预期轴 -1 的值为 20,但接收到的输入形状为 (None, 20, 637) - ValueError: ...incompatible with the layer: expected axis -1 of input shape to have value 20 but received input with shape (None, 20, 637) ValueError:密集层的输入 0 与层不兼容:预期轴 -1 的值为 8,但接收到形状为 [None, 1] 的输入 - ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 to have value 8 but received input with shape [None, 1] ValueError: 层序 7 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 5,但接收到的形状(无,21) - ValueError: Input 0 of layer sequential_7 incompatible with the layer: expected axis -1 of input shape to have value 5 but received shape (None, 21) 层密集的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但收到的输入形状为 (None, 1) - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1) ValueError:层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到的输入具有形状 - ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape 层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 8,但收到的输入具有形状(无,71) - Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 71) 输入形状的预期轴 -1 的值为 28,但收到的输入形状为 (None, 28, 28, 5) - expected axis -1 of input shape to have value 28, but received input with shape (None, 28, 28, 5) 'ValueError:层顺序的输入0与层不兼容:输入形状的预期轴-1具有值3但接收到输入 - 'ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input 层密集_18 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3500,但接收到形状为 [None, 7] 的输入 - Input 0 of layer dense_18 is incompatible with the layer: expected axis -1 of input shape to have value 3500 but received input with shape [None, 7]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM