最终我的问题是:是否可以在其中一个条目是一个元组或一组元组的样本上运行tensorflow的DNNClassifier?
我想要的是对一堆样本运行分类器。大多数样本都是数字,但我也希望其中一个条目是一组数字,例如样本为<1,2,3,4,(1.5,1.6,1.4),2>。我实际上希望奇怪的条目是一组可能不同大小的元组,例如一个样本是 <1, 2, 3, 4, (1.5, 1.6, 1.4), 2> ,第二个样本是 <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.
因此,如果您通过在数组末尾添加 'dtype = np.object' 将 np.array 类型更改为对象,或者只是将元组移到前面(我相信这允许 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.
最佳答案
您可能需要将动态长度元组视为稀疏张量(如果可行,长度=动态元组的最大长度)。
然后转换为 embedding/one hot,使其成为稠密张量,因为 DNNClassifier 需要输入作为稠密张量。
关于python - tensorflow - 我的输入数组中可以有元组条目吗?这样做会抛出 'Unable to get element from the feed as bytes.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40386921/