AttributeError:'Tensor'对象没有属性'shape'

时间:2016-07-03 06:38:26

标签: python tensorflow

堆栈跟踪

Traceback (most recent call last):
  File "main.py", line 6, in <module>
    connection.start_socket(8089, callback=handler.message_processor)
  File "/mnt/d/workspace/SketchRecognitionWithTensorFlow/src/main/python/connection/python_socket_server.py", line 13, in start_socket
    process_message(connection, callback=callback)
  File "/mnt/d/workspace/SketchRecognitionWithTensorFlow/src/main/python/connection/python_socket_server.py", line 38, in process_message
    result = callback(general_proto)
  File "/mnt/d/workspace/SketchRecognitionWithTensorFlow/src/main/python/recognition/proto_handler.py", line 39, in message_processor
    return train_shape(general_proto.template)
  File "/mnt/d/workspace/SketchRecognitionWithTensorFlow/src/main/python/recognition/proto_handler.py", line 23, in train_shape
    rec.add_training_data(recognition_template.interpretation.label, recognition_template.shape)
  File "/mnt/d/workspace/SketchRecognitionWithTensorFlow/src/main/python/recognition/recognition_manager.py", line 98, in add_training_data
    self.recognizers[label].train(label, points)
  File "/mnt/d/workspace/SketchRecognitionWithTensorFlow/src/main/python/recognition/simple/recognizer.py", line 78, in train
    self.classifier.fit(x=reshaped_tensor, y=target, steps=1)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 173, in fit
    input_fn, feed_fn = _get_input_fn(x, y, batch_size)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 67, in _get_input_fn
    x, y, n_classes=None, batch_size=batch_size)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/io/data_feeder.py", line 117, in setup_train_data_feeder
    X, y, n_classes, batch_size, shuffle=shuffle, epochs=epochs)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/io/data_feeder.py", line 239, in __init__
    self.X.shape, None if self.y is None else self.y.shape, n_classes,
AttributeError: 'Tensor' object has no attribute 'shape'

以下是一些代码示例:

def create_classifier(self):
        hiddenLayers = [self.num_points, self.num_points * 2, 10]
        self.classifier = tf.contrib.learn.DNNClassifier(hidden_units=hiddenLayers)

label是一个字符串 点列表是x,y值数组的列表 例如:

[[1,2],[2,3],[3,4],...]


def train(self, label, point_list):
    points = self.resample(point_list, self.num_points)
    utils.strip_ids_from_points(points)
    value_class = 1 if label == self.label else 0
    target = tf.reshape(tf.constant(value_class), [1])
    print 'training classifier to recognize value as: [' + str(value_class) + '] label is ' + label + ' class is ' + self.label
    point_tensor = tf.convert_to_tensor(points, dtype=tf.float32)
    reshaped_tensor = tf.reshape(point_tensor, [1, self.num_points * 2])
    print reshaped_tensor
    print target
    self.classifier.fit(x=reshaped_tensor, y=target, steps=1)

我不知道为什么说张量没有形状属性或如何解决这个问题。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:2)

由于没有可接受的答案,我自己来自谷歌:
通过this answer获得信用,其中包括: 自TensorFlow 1.0起,tf.Tensor现在具有tf.Tensor.shape属性,该属性返回与tf.Tensor.get_shape()相同的值。

答案 1 :(得分:1)

堆栈跟踪对于没有行号的代码片段没有多大帮助。但我可以告诉你,当我看到那个错误时,是因为我在numpy数组上使用了tf.reshape而不是真正的张量流张量。当我尝试确定形状时,生成的对象抛出了该错误。希望有所帮助。

答案 2 :(得分:0)

很难从提供的代码中看到,特别是因为我无法弄清楚你的错误发生在哪里。但是为了它的价值,我之前遇到了一个相关的问题:

在我看来,就像一些代码试图处理tf-tensor一样,就像它是一个np-array一样。张量器没有shape属性,因为它们的形状存储为更复杂的对象。如果你想获得这些信息,你已经习惯了np,你必须打电话给my_tensor.get_shape().as_list()。但是,由于您的代码(您发布的代码)没有尝试访问shape上的任何Tensor属性,因此我不确定如何使用此信息来解决您的问题。