【问题标题】:How should I configure my Neural Network to accept a column of numpy.ndarrays as input?我应该如何配置我的神经网络以接受一列 numpy.ndarrays 作为输入?
【发布时间】:2020-12-17 19:20:35
【问题描述】:

我有一个包含 10k 行和以下列的数据框:

      array                                     target
[1,5,6,1,3,etc...]                                5
[3,3,1,0,5,etc...]                                10
[0,0,1,1,7,etc...]                                3
        .                                         .
        .                                         .
        .                                         .

每个数组有 33222 个元素,所以我有 10,000 行,每行有一个 33,222 长的 numpy.array,我想将其输入到神经网络中以预测目标变量。

这是 NN 的配置方式:

x = df.loc[:, 'array']
y = df.loc[:, 'target']

model = Sequential()
model.add(Dense(12, activation='linear'))
model.add(Dense(1, activation='linear'))

model.compile(loss='mse',
                optimizer='adam',
                metrics=['accuracy', 'mse', 'mae'])
                   
model.fit(x, y, epochs=10, batch_size=1, verbose=1)

我来了

"ValueError: Please provide as model inputs either a single array or a list of arrays"

我之前没有尝试过使用数组作为 NN 的输入,因此我也非常感谢有关此类问题的最佳层选择和配置的任何建议。

【问题讨论】:

  • 你能在你的问题中添加 x.shape 和 y.shape 吗?

标签: tensorflow machine-learning keras deep-learning neural-network


【解决方案1】:

除非我有误解,否则我认为您的数据结构是导致问题的原因。这一行:

x = df.loc[:, 'array']

正在返回“数组”列,但该列中的每个值都是另一个数组,而不是一个值。而是尝试x = np.matrix(df.loc[:, 'array'].tolist())

【讨论】:

    【解决方案2】:

    一个实现神经网络的小例子

    代码:

    import tensorflow as tf
    import numpy as np
    
    # Generating random data
    np.random.seed(100)
    x = tf.constant(np.random.randint(50, size =(10000,33222)), dtype = tf.float32)
    y = tf.constant(np.random.randint(50, size =(10000,)), dtype = tf.float32)
    print(x.shape) # (10000, 33222)
    print(y.shape) # (10000,)
    
    def create_model():
        model = tf.keras.Sequential()
        model.add(tf.keras.Input(shape=(33222,)))
        model.add(tf.keras.layers.Dense(12, activation='relu'))
        model.add(tf.keras.layers.Dense(1, activation='linear'))
        model.compile(loss='mse',
                    optimizer='adam',
                    metrics=['accuracy', 'mse', 'mae'])
        return model
    model = create_model()
    
    model.fit(x,y, epochs = 5)
    

    输出:

    Epoch 1/5
    313/313 [==============================] - 1s 3ms/step - loss: 33659.8986 - accuracy: 0.0204 - mse: 33659.8986 - mae: 54.9263
    Epoch 2/5
    313/313 [==============================] - 1s 3ms/step - loss: 794.5642 - accuracy: 0.0198 - mse: 794.5642 - mae: 24.1756
    Epoch 3/5
    313/313 [==============================] - 1s 3ms/step - loss: 795.9055 - accuracy: 0.0196 - mse: 795.9055 - mae: 24.1960
    Epoch 4/5
    313/313 [==============================] - 1s 3ms/step - loss: 767.0253 - accuracy: 0.0201 - mse: 767.0253 - mae: 23.6541
    Epoch 5/5
    313/313 [==============================] - 1s 3ms/step - loss: 775.2028 - accuracy: 0.0196 - mse: 775.2028 - mae: 23.8585
    

    还有一些要记住的事情:

    1. 始终对隐藏层使用非线性激活函数。即(relu、elu 等)
    2. 添加的层数越多,模型学习复杂特征的能力就越强。但是,您的模型可能会过度拟合,并且需要大量时间来训练。
    3. 逐步增加您的神经元和层数。
    4. 使用验证和测试集来查看模型的优劣。
    5. 使用正则化避免模型过拟合。

    关于数据集的注释。

    1. 您不应该使用具有这么多功能的数据集。
    2. 使用数据集中的选择性特征以避免过度拟合。

    【讨论】:

    • 谢谢,这很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2017-07-05
    • 2018-11-26
    • 2017-06-13
    • 2017-09-16
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 2017-06-14
    相关资源
    最近更新 更多