TensorFlow错误:TypeError:“int”对象不可迭代-我找不到问题所在

g6baxovj  于 2023-03-03  发布在  其他
关注(0)|答案(2)|浏览(120)

首先,我是ML的新手,所以这可能是一个愚蠢的错误:)
我下载了这个数据集:https://www.kaggle.com/datasets/muhammadtalharasool/simple-gender-classification
转为可变性别_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 -这应该是正确的,但仍然不起作用

o8x7eapl

o8x7eapl1#

我发现当我替换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)])

v2g6jxz6

v2g6jxz62#

问题出在函数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)])

相关问题