pandas Keras 神经网络错误:使用序列设置数组元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44573784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:47:43  来源:igfitidea点击:

Keras Neural Network Error: Setting an Array Element with a Sequence

pythonpython-2.7pandasneural-networkkeras

提问by Ashley O

I'm loading dummy data into a neural network, but I'm receiving an error I can't seem to debug:

我正在将虚拟数据加载到神经网络中,但我收到了一个似乎无法调试的错误:

Here is my data, visualized:

这是我的数据,可视化:

 df:
Label          Mar
0    | [[.332, .326], [.058, .138]]
0    | [[.234, .246], [.234, .395]]
1    | [[.084, .23], [.745, .923]], 

I'm trying to use the 'Mar' column to predict the 'Label' column (I know this data makes no sense, its just similar to my real data). Here is my neural network code:

我正在尝试使用 'Mar' 列来预测 'Label' 列(我知道这个数据没有意义,它只是类似于我的真实数据)。这是我的神经网络代码:

model = Sequential()
model.add(Dense(3, input_dim=(1), activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
X = df['Mar']
Y = pd.get_dummies(df['Label'])
model.fit(X, Y, epochs=150, batch_size=10)

Here is the code to create my sample data:

这是创建我的示例数据的代码:

Sample = [{'Label': 0, 'Mar': [[.332, .326], [.058, .138]]},
 {'Label': 0, 'Mar': [[.234, .246], [.013, .592]]},
 {'Label': 1,  'Mar': [[.084, .23], [.745, .923]]}]

df = pd.DataFrame(Sample)

When I get to the final row of this code, I get this error:

当我到达此代码的最后一行时,出现此错误:

Epoch 1/150
-----------------------------------------------------------------------
ValueError                            Traceback (most recent call last)
<ipython-input-271-3d2506918d89> in <module>()
----> 1 model.fit(X, Y, epochs=150, batch_size=10)

/usr/local/lib/python2.7/site-packages/keras/models.pyc in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
    854                               class_weight=class_weight,
    855                               sample_weight=sample_weight,
--> 856                               initial_epoch=initial_epoch)
    857 
    858     def evaluate(self, x, y, batch_size=32, verbose=1,

/usr/local/lib/python2.7/site-packages/keras/engine/training.pyc in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
   1496                               val_f=val_f, val_ins=val_ins, shuffle=shuffle,
   1497                               callback_metrics=callback_metrics,
-> 1498                               initial_epoch=initial_epoch)
   1499 
   1500     def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

/usr/local/lib/python2.7/site-packages/keras/engine/training.pyc in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch)
   1150                 batch_logs['size'] = len(batch_ids)
   1151                 callbacks.on_batch_begin(batch_index, batch_logs)
-> 1152                 outs = f(ins_batch)
   1153                 if not isinstance(outs, list):
   1154                     outs = [outs]

/usr/local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.pyc in __call__(self, inputs)
   2227         session = get_session()
   2228         updated = session.run(self.outputs + [self.updates_op],
-> 2229                               feed_dict=feed_dict)
   2230         return updated[:len(self.outputs)]
   2231 

/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    776     try:
    777       result = self._run(None, fetches, feed_dict, options_ptr,
--> 778                          run_metadata_ptr)
    779       if run_metadata:
    780         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    952             np_val = subfeed_val.to_numpy_array()
    953           else:
--> 954             np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
    955 
    956           if (not is_tensor_handle_feed and

/usr/local/lib/python2.7/site-packages/numpy/core/numeric.pyc in asarray(a, dtype, order)
    529 
    530     """
--> 531     return array(a, dtype, copy=False, order=order)
    532 
    533 

ValueError: setting an array element with a sequence.

I now suspect it has something to do with my input columns being list, not np arrays? However, I've tried making them into arrays first and I'm still getting the same error. Would really love and appreciate help!!

我现在怀疑它与我的输入列是列表有关,而不是 np 数组?但是,我尝试先将它们放入数组中,但仍然遇到相同的错误。真的很喜欢并感谢帮助!

EditI've tried one hot encoding the label field, as I found somewhere online that that may help. It hasn't helped at this point

编辑我已经尝试了一种热编码标签字段,因为我在网上找到了可能有帮助的地方。这在这一点上没有帮助

采纳答案by maxymoo

A couple of issues here,

这里有几个问题,

  1. The input is the wrong shape
  2. The input is a mixture of arrays and lists.
  1. 输入是错误的形状
  2. 输入是数组和列表的混合。

One possible solution would be to use keras.layers.Flattento reshape your data, and pd.Series.tolist()to uniformize the data type of the input array:

一种可能的解决方案是用于keras.layers.Flatten重塑数据,并pd.Series.tolist()统一输入数组的数据类型:

model = Sequential()
model.add(Flatten(input_shape=(2,2)))
model.add(Dense(3, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
X = df['Mar'].tolist()
Y = df['Label']
model.fit(X, Y, epochs=150, batch_size=10)