使用tf.placeholder()消耗NumPy数组时,抱怨'dict'对象没有属性'dtype'!

时间:2018-10-28 23:27:48

标签: tensorflow

在定义训练步骤的输入功能时

def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):

    # Convert pandas data into a dict of np arrays.
    features = { key:np.array(value) for key,value in dict(features).items() }                                           

##Construct a dataset, and configure batching/repeating.
# ds = Datasets.from_tensor_slices((featues, targets)) # 2GB limit

    features_placeholder = tf.placeholder(features.dtype, features.shape)
    target_placeholder = tf.placeholder(targets.dtype, targets.shape)

    ds = Datasets.from_tensor_slices(( features_placeholders, target_placeholders)) 
         # other transformations on the dataset
    ds = ds.batch(batch_size).repeat(count = num_epochs) 

    if shuffle: # Shuffle the data, if specified.
      ds = ds.shuffle(buffer_size=10000) # Randomly shuffles the elements of this dataset.

    # Return the next batch of data.
    features, labels = ds.make_one_shot_iterator().get_next() # returns A nested structure of tf.Tensor objects.

    return features, labels

我使用tf.placeholder()而不是按照here中的描述将功能和标签作为tf.constants()传递。

在训练模型时:

_ = linear_regressor.train(
    input_fn = lambda:my_input_fn(my_feature, targets),
    steps=100
)

它抛出一个错误:

在第features_placeholder = tf.placeholder(features.dtype, features.shape)

AttributeError: 'dict' object has no attribute 'dtype'

任何人都可以提供帮助,发生了什么问题以及如何纠正?

谢谢。!

0 个答案:

没有答案