ValueError:使用序列设置数组元素。 (Python / SKFlow)

时间:2016-09-12 12:11:20

标签: python python-3.x pandas numpy tensorflow

这是我使用本网站多年后的第一个问题 - 如果我做错了,请纠正我......

我正在尝试使用TensorFlow的SKFlow部分训练深度神经网络分类器,这应该使这非常简单。在将数据传递给分类器ValueError方法时,我不断得到此fit

Traceback (most recent call last):
  File "./train_ann.py", line 55, in <module>
clf.fit(x_train.values, y_train.values, steps=1000)
  File "/home/maurice/Dev/pyvenv/itslb/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 235, in fit
epochs=None)
  File "/home/maurice/Dev/pyvenv/itslb/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 82, in _get_input_fn
epochs=epochs)
  File "/home/maurice/Dev/pyvenv/itslb/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/learn_io/data_feeder.py", line 116, in setup_train_data_feeder
x, y, n_classes, batch_size, shuffle=shuffle, epochs=epochs)
  File "/home/maurice/Dev/pyvenv/itslb/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/learn_io/data_feeder.py", line 241, in __init__
self.x = check_array(x, dtype=x_dtype)
  File "/home/maurice/Dev/pyvenv/itslb/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/learn_io/data_feeder.py", line 188, in check_array
array = np.array(array, dtype=dtype, order=None, copy=False)
ValueError: setting an array element with a sequence.

相关代码如下所示:

# The y_train dataframe now contains only the type_id column which contains only integers
y_train = y_train[["type_id"]]

# Convert the numpy arrays to lists
x_train["vector"] = x_train.vector.map(lambda x: x.tolist())

# Get the number of classes for the classifier
n_classes = len(list(y_train.type_id.unique()))

# Create a deep neural network classifier
clf = skflow.DNNClassifier(
    hidden_units=[2000, 2000, 1500, 1000],
    n_classes=n_classes,
    model_dir='/home/maurice/Dev/test/TFModel01'
)

# Start training the classifier
clf.fit(x_train, y_train, steps=1000)

x_train数据框看起来像这样(它只包含一列):

                                                vector
324  [1.0, 0.0, 1.0, 1.0, 0.0, 2.0, 1.0, 1.0, 2.0, ...
471  [0.0, 2.0, 0.0, 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, ...
...

y_train看起来像这样:

     type_id
887       10
507        2
...

我尝试使用解决方案这么类似的问题,但要么我做错了要么存在不同的问题。

我已尝试过的内容:

  • 不将Numpy数组转换为列表,同样的错误
  • 确保x_train.vector中的每个元素具有相同的长度 - 它具有
  • 确保x_train.vector中的每个元素属于同一类型 - 已完成
  • 将x_train.vector转换为整数 - 结果相同

修改

我做了一些更多的研究,并且能够找出如何调用引发异常的函数。

for index, row in x_train.iterrows():
    print(type(row))
    print(row)
    array = np.array(row, dtype=np.float32, order=None, copy=False)

这会再现错误。输出显示row的类型为pandas.core.series.Series - 这解释了异常。

<class 'pandas.core.series.Series'>
vector    [1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 2.0, ...
Name: 599, dtype: object
Traceback (most recent call last):
  File "./train_ann.py", line 34, in <module>
    array = np.array(row, dtype=np.float32, order=No

你们能指点我一个解决方案吗?

提前致谢!

0 个答案:

没有答案