简体   繁体   English

AttributeError:“ TensorSliceDataset”对象没有属性“ dtype”

[英]AttributeError: 'TensorSliceDataset' object has no attribute 'dtype'

Here's what I did: 这是我所做的:

def prepare_data(self, features, labels):
  assert features.shape[0] == labels.shape[0]
  print("DEBUG: features: shape = " + str(features.shape) \
    + " , dtype(0,0) = " + str(type(features[0,0])))
  print("DEBUG: labels: shape = " + str(labels.shape) \
    + ", dtype(0) = " + str(type(labels[0])))
  dataset = tf.data.Dataset.from_tensor_slices( (features, labels) )
  iterator = dataset.make_one_shot_iterator()
  return dataset, iterator

... ...

self.train_features = np.asarray(train_features_list)
self.train_labels = np.asarray(train_labels_list)
self.train_data, self.train_it = \
    self.prepare_data(self.train_features, self.train_labels)

hidden1 = tf.layers.dense(self.train_data,
    self.input_layer_size * 40,
    activation=tf.nn.relu,
    name='hidden1')

And this is what I've got: 这就是我所拥有的:

DEBUG: features: shape = (4000, 3072) , dtype(0,0) = <class 'numpy.uint8'>
DEBUG: labels: shape = (4000,), dtype(0) = <class 'numpy.int64'>
...
AttributeError: 'TensorSliceDataset' object has no attribute 'dtype'

With the error location pointing to this code in tensorflow/python/layers/core.py: 在tensorflow / python / layers / core.py中将错误位置指向此代码:

layer = Dense(units,
            activation=activation,
            use_bias=use_bias,
            kernel_initializer=kernel_initializer,
            bias_initializer=bias_initializer,
            kernel_regularizer=kernel_regularizer,
            bias_regularizer=bias_regularizer,
            activity_regularizer=activity_regularizer,
            kernel_constraint=kernel_constraint,
            bias_constraint=bias_constraint,
            trainable=trainable,
            name=name,
            dtype=inputs.dtype.base_dtype,
            _scope=name,
            _reuse=reuse)

Could you tell me what am I doing wrong here? 你能告诉我我在做什么错吗?

Your tf.layers.dense accepts tensor as input, but you are feeding it a tf data object. 您的tf.layers.dense接受张量作为输入,但是您正在向其提供tf数据对象。 That is why its probably throwing this error. 这就是为什么它可能引发此错误的原因。

I have modified your code with an example and that does not throw an error. 我已经用示例修改了您的代码,并且不会引发错误。 Also, the dense layer will expect 2 dimensions as input so I included the batch in your function so that it is 2 dim. 另外,密集层将以2维为输入,因此我将批处理包含在函数中,以使其为2个暗淡的。

def prepare_data(features, labels):
  assert features.shape[0] == labels.shape[0]
  print("DEBUG: features: shape = " + str(features.shape) \
    + " , dtype(0,0) = " + str(type(features[0,0])))
  print("DEBUG: labels: shape = " + str(labels.shape) \
    + ", dtype(0) = " + str(type(labels[0])))
  dataset = tf.data.Dataset.from_tensor_slices( (features, labels) )
  iterator = dataset.batch(1).make_one_shot_iterator() # Modified here
  return iterator # Returned only the iterator

train_features = np.random.randn(4000, 3072) 
train_labels = np.random.randn(4000)
train_it = prepare_data(train_features, train_labels)

input_data, input_label = train_it.get_next() # Getting the input feature from the iterator
hidden1 = tf.layers.dense(input_data, 40, activation=tf.nn.relu, name='hidden1') # Used 40 as an example

Result: 结果:

DEBUG: features: shape = (4000, 3072) , dtype(0,0) = <class 'numpy.float64'>
DEBUG: labels: shape = (4000,), dtype(0) = <class 'numpy.float64'> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tensorflow 2.0.0: AttributeError: &#39;TensorSliceDataset&#39; 对象没有属性 &#39;as_numpy_iterator&#39; - Tensorflow 2.0.0: AttributeError: 'TensorSliceDataset' object has no attribute 'as_numpy_iterator' AttributeError: 'TensorSliceDataset' object 没有属性 'get_shape' - AttributeError: 'TensorSliceDataset' object has no attribute 'get_shape' AttributeError:类型对象&#39;object&#39;没有属性&#39;dtype&#39; - AttributeError: type object 'object' has no attribute 'dtype' AttributeError:“列表”对象没有属性“ dtype” - AttributeError: 'list' object has no attribute 'dtype' AttributeError:&#39;CommandQueue&#39;对象没有属性&#39;dtype&#39; - AttributeError: 'CommandQueue' object has no attribute 'dtype' AttributeError: 'float' object 没有属性 'dtype' - AttributeError: 'float' object has no attribute 'dtype' AttributeError: &#39;int&#39; 对象没有属性 &#39;dtype&#39; - AttributeError: 'int' object has no attribute 'dtype' AttributeError:“ NoneType”对象没有属性“ dtype” - AttributeError: 'NoneType' object has no attribute 'dtype' AttributeError: 'df' object 没有属性 'dtype' - AttributeError: 'df' object has no attribute 'dtype' 估算器API:AttributeError:&#39;NoneType&#39;对象没有属性&#39;dtype&#39; - Estimator API: AttributeError: 'NoneType' object has no attribute 'dtype'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM