TensorFlow 错误:TypeError:'int' 对象不可迭代 - 我找不到问题出在哪里

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

首先我是机器学习的新手,所以这可能是一个愚蠢的错误:)

我下载了这个数据集: https://www.kaggle.com/datasets/muhammadtalharasool/simple-gender-classification

转为变量 gender_2

使用 OneHotCode:

gender_one_hot_2 = pd.get_dummies(gender_2)
gender_one_hot_2.head()

gender_one_HOT = gender_one_hot_2.drop("Unnamed: 9", axis = 1)

归一化数据:

cf = make_column_transformer((MinMaxScaler(), [' Age', ' Height (cm)', ' Weight (kg)']),
                             (OneHotEncoder(handle_unknown= "ignore"),[' Occupation_ Accountant', ' Occupation_ Analyst',
       ' Occupation_ Architect', ' Occupation_ Business Analyst',
       ' Occupation_ Business Consultant', ' Occupation_ CEO',
       ' Occupation_ Doctor', ' Occupation_ Engineer',
       ' Occupation_ Graphic Designer', ' Occupation_ IT Manager',
       ' Occupation_ Lawyer', ' Occupation_ Marketing Specialist',
       ' Occupation_ Nurse', ' Occupation_ Project Manager',
       ' Occupation_ Sales Representative', ' Occupation_ Software Engineer',
       ' Occupation_ Teacher', ' Occupation_ Writer', ' Occupation_Accountant',
       ' Occupation_Analyst', ' Occupation_Architect',
       ' Occupation_Business Analyst', ' Occupation_CEO', ' Occupation_Doctor',
       ' Occupation_Engineer', ' Occupation_Graphic Designer',
       ' Occupation_IT Manager', ' Occupation_Lawyer',
       ' Occupation_Marketing Specialist', ' Occupation_Nurse',
       ' Occupation_Project Manager', ' Occupation_Sales Representative',
       ' Occupation_Software Developer', ' Occupation_Teacher',
       ' Occupation_Writer', " Education Level_ Associate's Degree",
       " Education Level_ Bachelor's Degree",
       ' Education Level_ Doctorate Degree',
       " Education Level_ Master's Degree" ,
       " Education Level_Associate's Degree",
       " Education Level_Bachelor's Degree",
       ' Education Level_Doctorate Degree', " Education Level_Master's Degree",
       ' Marital Status_ Divorced', ' Marital Status_ Married',
       ' Marital Status_ Single', ' Marital Status_ Widowed',
       ' Marital Status_Divorced', ' Marital Status_Married',
       ' Marital Status_Single', ' Favorite Color_ Black',
       ' Favorite Color_ Blue', ' Favorite Color_ Green',
       ' Favorite Color_ Grey', ' Favorite Color_ Orange',
       ' Favorite Color_ Pink', ' Favorite Color_ Purple',
       ' Favorite Color_ Red', ' Favorite Color_ Yellow',
       ' Favorite Color_Black', ' Favorite Color_Blue',
       ' Favorite Color_Green', ' Favorite Color_Grey',
       ' Favorite Color_Orange', ' Favorite Color_Pink',
       ' Favorite Color_Purple', ' Favorite Color_Red',
       ' Favorite Color_Yellow'])
)
X = gender_one_HOT.drop(" Income (USD)", axis = 1)
y = gender_one_HOT[" Income (USD)"]

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

cf.fit(X_train)

X_train_normal_2 = cf.transform(X_train)
X_test_normal_2 = cf.transform(X_test)

然后我插入到NN:

tf.random.set_seed(42)

model_gender_2 = tf.keras.Sequential(tf.keras.layers.Dense(60 ,input_shape=139),
                                     tf.keras.layers.Dense(30),
                                     tf.keras.layers.Dense(1)
                                     )

model_gender_2.compile(loss = tf.keras.losess.mae,
                       optimizer = tf.keras.optimizers.Adam(learning_rate = 0.01),
                       metrics = ["mae"])
model_gender_2.fit(X_train_normal_2, y_train, epochs = 120)


but I got this output: 

TypeError                                 Traceback (most recent call last)
<ipython-input-103-e0b5ab8c49e8> in <module>
      1 tf.random.set_seed(42)
      2 
----> 3 model_gender_2 = tf.keras.Sequential(tf.keras.layers.Dense(60 ,input_shape=139),
      4                                      tf.keras.layers.Dense(30),
      5                                      tf.keras.layers.Dense(1)

3 frames
/usr/local/lib/python3.8/dist-packages/keras/engine/base_layer.py in __init__(self, trainable, name, dtype, dynamic, **kwargs)
    450                 else:
    451                     batch_size = None
--> 452                 batch_input_shape = (batch_size,) + tuple(kwargs["input_shape"])
    453             self._batch_input_shape = batch_input_shape
    454 

TypeError: 'int' object is not iterable

实际上我找不到错误我尝试更改第一层的输入但它没有工作:/

我尝试插入不同的输入形状,但没有用

我试图将第一层的输入形状更改为 139 - 这应该是正确的但仍然不起作用

tensorflow machine-learning deep-learning
2个回答
0
投票

我发现当我替换 input_shape 时它可以工作,无论如何我昨天试过了但它没有工作所以这就是我插入 input_shape 的原因。看起来 TensorFlow 一直在逗我:))

model_gender_2 = tf.keras.Sequential([tf.keras.layers.Dense(60), tf.keras.layers.Dense(30), tf.keras.layers.Dense(1)])


0
投票

问题出在功能

tf.keras.Sequential
上。它需要一个层列表而不是每个层作为参数。所以,你传递输入的方式是错误的。尝试这样做:

model_gender_2 = tf.keras.Sequential()
model_gender_2.add(tf.keras.layers.Dense(60, input_shape=(139,))
model_gender_2.add(tf.keras.layers.Dense(30)
model_gender_2.add(tf.keras.layers.Dense(1)

或者这个

model_gender_2 = tf.keras.Sequential([tf.keras.layers.Dense(60, input_shape=139),
                                      tf.keras.layers.Dense(30),
                                      tf.keras.layers.Dense(1)])
© www.soinside.com 2019 - 2024. All rights reserved.