tensorflow ValueError: features should be a dictionary of `Tensor`s. Given type: <class 'tensorflow.python.framework.ops.Tensor'>

依然范特西╮ 提交于 2019-12-03 13:55:15

Google developer blog Introducing TensorFlow Feature Columns

This article can make you understand! I just add 3 lines in the def parser(record). as below: my_features = {} for idx, names in enumerate(feature_names): my_features[names] = parsed['features'][idx]

import tensorflow as tf

tf.logging.set_verbosity(tf.logging.INFO)

feature_names = [
    'SepalLength',
    'SepalWidth',
    'PetalLength',
    'PetalWidth'
]


def my_input_fn(is_shuffle=False, repeat_count=1):
    dataset = tf.data.TFRecordDataset(['csv.tfrecords'])  # filename is a list

    def parser(record):
        keys_to_features = {
            'label': tf.FixedLenFeature((), dtype=tf.int64),
            'features': tf.FixedLenFeature(shape=(4,), dtype=tf.float32),
        }
        parsed = tf.parse_single_example(record, keys_to_features)
        my_features = {}
        for idx, names in enumerate(feature_names):
            my_features[names] = parsed['features'][idx]
        return my_features, parsed['label']

    dataset = dataset.map(parser)
    if is_shuffle:
        # Randomizes input using a window of 256 elements (read into memory)
        dataset = dataset.shuffle(buffer_size=256)
    dataset = dataset.batch(32)
    dataset = dataset.repeat(repeat_count)
    iterator = dataset.make_one_shot_iterator()
    features, labels = iterator.get_next()
    return features, labels

feature_columns = [tf.feature_column.numeric_column(k) for k in feature_names]

classifier = tf.estimator.DNNClassifier(
    feature_columns=feature_columns,  # The input features to our model
    hidden_units=[10, 10],  # Two layers, each with 10 neurons
    n_classes=3,
    model_dir='lalalallal')  # Path to where checkpoints etc are stored

classifier.train(input_fn=lambda: my_input_fn(is_shuffle=True, repeat_count=100))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!