【问题标题】:Pandas DataFrame and KerasPandas DataFrame 和 Keras
【发布时间】:2017-10-08 04:12:15
【问题描述】:

我正在尝试使用 Keras 在 Python 中执行情感分析。为此,我需要对我的文本进行词嵌入。当我尝试将数据拟合到我的模型时出现问题:

model_1 = Sequential()
model_1.add(Embedding(1000,32, input_length = X_train.shape[0]))
model_1.add(Flatten())
model_1.add(Dense(250, activation='relu'))
model_1.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

我的火车数据的形状是

(4834,)

并且是 Pandas 系列对象。当我尝试拟合我的模型并使用其他一些数据对其进行验证时,我收到此错误:

model_1.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=2, batch_size=64, verbose=2)

ValueError:检查模型输入时出错:预期 embedding_1_input 具有形状 (None, 4834) 但得到了具有形状的数组 (4834, 1)

如何重塑我的数据以使其适合 Keras?我一直在尝试使用 np.reshape,但我无法使用该功能放置 None 元素。

提前致谢

【问题讨论】:

    标签: python pandas keras


    【解决方案1】:

    使用 tf.data.Dataset.from_tensor_slices 从 pandas 数据帧中读取值。

    请参阅https://www.tensorflow.org/tutorials/load_data/pandas_dataframe 以了解如何在 TF2.x 中正确执行此操作

    【讨论】:

      【解决方案2】:

      您需要特定版本的 Pandas 才能使用。如果您使用当前版本(截至 2018 年 8 月 20 日),这将失败。

      回滚你的 Pandas 和 Keras(pip 卸载 ....),然后像这样安装特定版本

      python -m pip install pandas==0.19.2
      

      【讨论】:

        【解决方案3】:

        https://pypi.org/project/keras-pandas/

        最简单的方法是使用 keras_pandas 包将 pandas 数据框适合 keras。下面显示的代码是包文档中的一般示例。

        from keras import Model
        from keras.layers import Dense
        
        from keras_pandas.Automater import Automater
        from keras_pandas.lib import load_titanic
        
        observations = load_titanic()
        
        # Transform the data set, using keras_pandas
        categorical_vars = ['pclass', 'sex', 'survived']
        numerical_vars = ['age', 'siblings_spouses_aboard', 'parents_children_aboard', 'fare']
        text_vars = ['name']
        
        auto = Automater(categorical_vars=categorical_vars, numerical_vars=numerical_vars, text_vars=text_vars,
         response_var='survived')
        X, y = auto.fit_transform(observations)
        
        # Start model with provided input nub
        x = auto.input_nub
        
        # Fill in your own hidden layers
        x = Dense(32)(x)
        x = Dense(32, activation='relu')(x)
        x = Dense(32)(x)
        
        # End model with provided output nub
        x = auto.output_nub(x)
        
        model = Model(inputs=auto.input_layers, outputs=x)
        model.compile(optimizer='Adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
        
        # Train model
        model.fit(X, y, epochs=4, validation_split=.2)
        

        【讨论】:

        • 好像2018年keras_pandas的开发就停止了,只支持到1.11版本的tensorflow。然而,最新版本的 tensorflow 是 2.3。
        【解决方案4】:

        None 是进入训练的预期行数,因此您无法定义它。 Keras 还需要一个 numpy 数组作为输入,而不是 pandas 数据框。首先使用df.values 将df 转换为numpy 数组,然后执行np.reshape((-1, 4834))。请注意,您应该使用np.float32。如果您在 GPU 上进行训练,这一点很重要。

        【讨论】:

          猜你喜欢
          • 2017-12-12
          • 1970-01-01
          • 2019-07-17
          • 2022-07-28
          • 2023-03-14
          • 2019-01-31
          • 2021-01-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多