【问题标题】:AttributeError: 'Tensor' object has no attribute 'shape'AttributeError:“张量”对象没有属性“形状”
【发布时间】:2016-07-03 06:38:26
【问题描述】:

堆栈跟踪

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)

标签是一个字符串 点列表是 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)

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

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    由于没有公认的答案,而且我自己来自 Google:
    归功于 this answer 的 mrry,内容如下:
    从 TensorFlow 1.0 开始,tf.Tensor 现在有一个tf.Tensor.shape 属性,它返回与tf.Tensor.get_shape() 相同的值。

    【讨论】:

    • 那么解决办法是什么??
    • @YanKingYin 我没有看到问题——你卡在哪里了?如果此答案对您没有帮助,请打开另一个问题
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      很难从提供的代码中看出,尤其是因为我无法弄清楚您的错误发生在哪里。但是对于它的价值,我之前遇到过一个相关的问题:

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

      【讨论】:

        猜你喜欢
        • 2016-12-04
        • 2020-10-22
        • 2019-02-20
        • 2017-12-06
        • 2018-07-02
        相关资源
        最近更新 更多