gpt4 book ai didi

tensorflow - ValueError : Error when checking input: expected dense_1_input to have shape (3, )但得到形状为(2,)的数组

转载 作者:行者123 更新时间:2023-11-30 10:00:16 25 4
gpt4 key购买 nike

我是机器学习新手。我正在尝试训练人工神经网络,拟合方法会生成有关数组形状的错误“ValueError:检查输入时出错:预期dense_1_input具有形状(3,)但得到形状为(2,)的数组”。知道我有 3 个输入(年龄、性别、标签)。该数据集包含 3356 行。前 5 行数据的图像为 here

如下代码所示:

import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import LabelEncoder, OneHotEncoder

# Importing the dataset
dataset = pd.read_csv('Train_data1.csv')

X = dataset.iloc[:, 1:3].values # age, sex
y = dataset.iloc[:, 3].values #label

# Encoding categorical data
# Encoding the Independent Variable
labelencoder_X = LabelEncoder()
X[:, 1] = labelencoder_X.fit_transform(X[:, 1])
labelencoder_X_1 = LabelEncoder()
# Encoding the Dependent Variable
labelencoder_y = LabelEncoder()
y = labelencoder_y.fit_transform(y)
print(X.shape) #after run : (3356, 2)
print(y.shape) #after run : (3356,)

# Splitting the dataset into the Training set and Test set

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Feature Scaling

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
print(X_train.shape)
print(y_train.shape)

# Initialising the ANN
model = Sequential()
model.add(Dense(10, input_dim = 3, activation = 'relu'))
model.add(Dense(10, activation = 'relu'))
model.add(Dense(2, activation = 'softmax'))

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

# Fitting the ANN to the Training set
model.fit(X_train, y_train, batch_size = 10, epochs = 100)

运行后的错误:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-fc00dae1105e> in <module>
1 # Fitting the ANN to the Training set
----> 2 model.fit(X_train, y_train, batch_size = 10, epochs = 100)

~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False

~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
749 feed_input_shapes,
750 check_batch_axis=False, # Don't enforce the batch size.
--> 751 exception_prefix='input')
752
753 if y is not None:

~\Anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
136 ': expected ' + names[i] + ' to have shape ' +
137 str(shape) + ' but got array with shape ' +
--> 138 str(data_shape))
139 return data
140

ValueError: Error when checking input: expected dense_1_input to have shape (3,) but got array with shape (2,)

最佳答案

标签是 Y。X 的维度为 2。因此将 input_dim = 3 更改为 input_dim = 2y_train = to_categorical(y_train)

关于tensorflow - ValueError : Error when checking input: expected dense_1_input to have shape (3, )但得到形状为(2,)的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59305008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com