tensorflow:将n_classes更改为2会产生“ValueError:Shapes(?,1)和(?,)不兼容;”

时间:2016-10-28 22:57:22

标签: python machine-learning neural-network tensorflow python-3.4

我在https://www.tensorflow.org/versions/r0.11/tutorials/tflearn/index.html#tf-contrib-learn-quickstart的教程中使用了我自己的数据。他们使用Iris数据集;我有自己的数据,但它应该仍然有效。我收到了一个不兼容的形状错误。

错误:

Traceback (most recent call last):
  File "./kidTraining_stripped.py", line 36, in <module>
classifier.fit(x = training_set.data, y = training_set.target, steps = 2000)  ########## THROWS UP HERE.
  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 662, in _train_model
train_op, loss_op = self._get_train_ops(features, targets)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 963, in _get_train_ops
_, loss, train_op = self._call_model_fn(features, targets, ModeKeys.TRAIN)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 944, in _call_model_fn
return self._model_fn(features, targets, mode=mode, params=self.params)
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py", line 258, in _dnn_classifier_model_fn
weight=_get_weight_tensor(features, weight_column_name))
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/contrib/losses/python/losses/loss_ops.py", line 329, in sigmoid_cross_entropy
logits.get_shape().assert_is_compatible_with(multi_class_labels.get_shape())
  File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/framework/tensor_shape.py", line 750, in assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (?, 1) and (?,) are incompatible

我的节目非常相似;我只是将变量名称更改为准确:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
import numpy as np

# Data sets
KID_TRAINING = "kid_data_training_nn.csv"
KID_TEST = "kid_data_test_nn.csv"

# load datasets
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(filename = KID_TRAINING, target_dtype = np.int, features_dtype=np.float32)

test_set = tf.contrib.learn.datasets.base.load_csv_with_header(filename=KID_TEST, target_dtype = np.int, features_dtype=np.float32)

# specify that all features have real-value data
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=11)]

# build 3 layer DNN with 10, 20, 10 units respectively
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=2, model_dir="./kid_model")

# Fit model
print("It blows up here, so this is x and y:")
print(training_set.data)
print(training_set.data.dtype)
print(training_set.data.shape)

# (18, 11)

print(training_set.target)
print(training_set.target.dtype)
print(training_set.target.shape)
# (18,)

classifier.fit(x = training_set.data, y = training_set.target, steps = 2000)  ########## THROWS UP HERE.

这些数据对我来说并不好笑;我已经将它剥离到4列,使浮点只有2个小数点,等等,尝试重塑张量,似乎没有任何工作。当我在获得classifier.fit之前打印出形状时,它们似乎兼容,(18,11)和(18,)。但是当我运行fit函数时,错误说形状是?当我粘贴数据时,它变得难以理解,但是这里的例子是训练数据的前3行:

18 11 NT ASD

0 132 1 15.28333 2.66667 3.48333 2.26667 3.33333 1.2 0.98333 1.35 0

0 138 1 9.95 1.25 2.73333 0.83333 1.95 0.83333 1 1.35 0

有18行数据,只有2个类,11列(不包括末尾的标签)。测试数据类似,只有我只有6个样本,所以第一行的第一列包含6个。

怎么了?我需要不同形状的数据吗?我需要重塑它吗?根据我的输入数据,程序中的条目是否不正确?希望有人可以提供帮助。

======= 编辑:我知道它与n_classes有关;它似乎取任何大于2的值,除了我只有2个类,所以我希望我的模型实际使用正确的参数。

0 个答案:

没有答案