tensorflow feature_column尝试重塑特征

时间:2019-03-09 20:12:30

标签: python tensorflow mnist tensorflow-estimator

我正在尝试使用自定义估算器为MNIST数据集实现网络。
这是我的输入功能:

def input_train_fn():
  train, test = tf.keras.datasets.mnist.load_data()
  mnist_x, mnist_y = train
  mnist_y = tf.cast(mnist_y, tf.int32)
  mnist_x = tf.cast(mnist_x, tf.int32)
  features = {'image': mnist_x}
  labels = mnist_y
  dataset = tf.data.Dataset.from_tensor_slices((features, labels))
  return dataset

这是我定义模型的方式:

def my_model(features, labels, mode, params):
    # create net
    net = tf.feature_column.input_layer(features, params['feature_columns'])
    # create hidden layers
    for unit in params['hidden_units']:
        net = tf.layers.dense(net, unit, tf.nn.relu)
    # create output layer
    legits = tf.layers.dense(net, params['n_classes'], activation=None)
    # predict (if in predict mode)
    predicted_classes = tf.arg_max(legits, 1)
    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {
            'class_ids': predicted_classes,
            'probabilities': tf.nn.softmax(legits),
            'logits': legits
        }
        return tf.estimator.EstimatorSpec(mode, predictions=predictions)
    # define loss function
    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=legits)
    # evaluation metrics
    accuracy = tf.metrics.accuracy(labels=labels,
                                   predictions=predicted_classes,
                                   name='acc_op')
    metrics = {'accuracy': accuracy}
    tf.summary.scalar('accuracy', accuracy[1])
    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops=metrics)

    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
    train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
    return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)

这就是我所谓的训练函数:

feature_columns = [tf.feature_column.numeric_column('image', shape=[28, 28], dtype=tf.int32), ]
classifier = tf.estimator.Estimator(model_fn=my_model,
                       params={
                           'feature_columns': feature_columns,
                           'hidden_units': [10, 10],
                           'n_classes': 10,
                       }, model_dir='/model')
classifier.train(input_fn=input_train_fn, steps=10)

据我所知,我正在estimatorsfeature_columns这本书的所有工作,但我得到了错误:

  

ValueError:无法使用输入形状为[28,28],{{3}”的“ input_layer / image / Reshape”(op:“ Reshape”)对具有784个元素的张量进行整形以定形为[28,784](21952个元素) },并将输入张量计算为部分形状:input 2 = [28,784]。

我有什么想念的吗?
预先感谢,感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

首先,您需要生产批次。有关更多详细信息,请参见https://www.tensorflow.org/guide/datasets

...
dataset = tf.data.Dataset.from_tensor_slices((features, labels))
dataset = dataset.batch(size)
  return dataset

然后重塑图像并投射到float。 -1代表batch_size,在训练过程中将被替换。根据提供的数据类型,将标签强制转换为浮动是可选的。

    net = tf.cast(tf.reshape(features, [-1, 28*28]), tf.float32)
    labels = tf.cast(labels, tf.int64)
    net = tf.layers.dense(net, 10, tf.nn.relu)
    legits = tf.layers.dense(net, 10, activation=None)
    predicted_classes = tf.arg_max(legits, 1)
    if mode == tf.estimator.ModeKeys.PREDICT:
        predictions = {
            'class_ids': predicted_classes,
            'probabilities': tf.nn.softmax(legits),
            'logits': legits
        }
        return tf.estimator.EstimatorSpec(mode, predictions=predictions)

    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=legits)

    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(mode, loss=loss)

    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
    train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
    return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)

classifier = tf.estimator.Estimator(model_fn=my_model)

classifier.train(input_fn=lambda: input_train_fn(), steps=10)