张量流 - 我的输入数组中可以有元组的条目吗?这样做会抛出'Unable to get element from the feed as bytes.'



最终我的问题是:是否有可能在样本上运行tensorflow的DNNClassifier,其中一个条目是元组或元组集合?

我想在一堆样本上运行一个分类器。大多数样本都是数字,但我也希望其中一个条目是一组数字,例如样本是<1,2,3,4,(1.5, 1.6, 1.4), 2>。其实我想要一套奇怪的条目可能不同大小的元组,像一个样本是& lt; 1, 2, 3, 4,(1.5, 1.6, 1.4), 2>和第二个示例是& lt; 11日12日13日,14日(0.5,0.1,00,0.1,0.2,0.0),12>不同长度的只是一个条目(在这里,指数4)每个样本(并最终每个条目是3种不同的数字),但我想任何工作,不只是一个数字列表。

编辑:如果有更好的方法来做到这一点,就像tensorflow中的不同事物一样,Matlab中的一个特定函数可以处理我在样本中想要的多元组条目,请建议!

import tensorflow as tf
import numpy as np
trainX = np.array([
            [19, 1] ,
            [10, 3] ,
            [0, 0] ,
            [11, 14] ,
            ])
print(trainX)
print(trainX.shape)
trainY = np.array([ 1, 1, 0, 1 ])
print(trainY)
print(trainY.shape)
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=2)]
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
                                            n_classes=3, 
                                            hidden_units=[10])
classifier.fit(x=trainX, y=trainY, steps=2000)
print("Done.")
exit()

然后把trainX改成这个,

trainX = np.array([
            [19, (1)] ,
            [10, (3)] ,
            [0, (0)] ,
            [11, (14)] ,
            ])

但这:

trainX = np.array([
            [19, (1, 1)] ,
            [10, (3, 9)] ,
            [0, (0, 0)] ,
            [11, (14, 6)] ,
            ])

由于numpy错误而失败:

Traceback (most recent call last):
  File "./supersimple.py", line 8, in <module>
    [11, (14, 6)] ,
ValueError: setting an array element with a sequence.

如果你改变np。通过添加'dtype = np '将数组类型添加到对象。或者只是将元组移动到前面(我相信这可以让numpy立即决定这是一个对象类型),像这样:

trainX = np.array([
            [(1, 1), 19] ,
            [(3, 9), 10] ,
            [(0, 0), 0] ,
            [(14, 6), 11] ,
            ])

在tensorflow中失败:

tensorflow.python.framework.errors.InternalError: Unable to get element from the feed as bytes.

总输出/堆栈跟踪如下:

$ python3 ./supersimple.py 
[[(1, 1) 19]
[(3, 9) 10]
[(0, 0) 0]
[(14, 6) 11]]
(4, 2)
[1 1 0 1]
(4,)
WARNING:tensorflow:Change warning: default value of `enable_centered_bias` will change after 2016-10-09. It will be disabled by default.Instructions for keeping existing behaviour:
Explicitly set `enable_centered_bias` to 'True' if you want to keep existing behaviour.
WARNING:tensorflow:Using default config.
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 972, in _do_call
    return fn(*args)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 954, in _run_fn
    status, run_metadata)
  File "/usr/lib/python3.4/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/errors.py", line 463, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.InternalError: Unable to get element from the feed as bytes.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "./supersimple.py", line 22, in <module>
    classifier.fit(x=trainX, y=trainY, steps=2000)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py", line 435, in fit
    max_steps=max_steps)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 333, in fit
    max_steps=max_steps)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 708, in _train_model
    max_steps=max_steps)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/graph_actions.py", line 285, in _monitored_train
    None)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/monitored_session.py", line 368, in run
    run_metadata=run_metadata)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/monitored_session.py", line 521, in run
    run_metadata=run_metadata)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/monitored_session.py", line 488, in run
    return self._sess.run(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/monitored_session.py", line 619, in run
    run_metadata=run_metadata)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/monitored_session.py", line 488, in run
    return self._sess.run(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InternalError: Unable to get element from the feed as bytes.

您可能需要将动态长度元组视为稀疏张量(如果可行的话,使用length =动态元组的最大长度)。

然后转换为嵌入/一个热,这样它就是一个密集张量,因为DNNClassifier需要输入作为密集张量。

相关内容

最新更新