预测不同类别与测试数据的大小

我不明白为什么 tf.estimator.predict 会根据 test_data 的大小预测不同的类别。

test_data = list()for i in range(8, 10):    test_data.append(mpimg.imread(FLAGS.input + 'test-' + str(i) + '.png'))test_data = np.asarray(test_data)test_data = test_data[:, :, :, :3].astype(np.float32)...classifier = tf.estimator.Estimator(model_fn=custom_model_fn, model_dir=FLAGS.modelPath)pred_input = tf.estimator.inputs.numpy_input_fn(x = { 'x': test_data }, shuffle = False, num_epochs = 1)

根据上面的代码,test_data 包含 test-8.pngtest-9.png,它们分别对应标签 89

通过模型的预测,test_data 推断出的标签分别为 39

但是当我使用大小为3的 test_data 时,

for i in range(7, 10):    test_data.append(mpimg.imread(FLAGS.input + 'test-' + str(i) + '.png'))

test_data 的大小扩展到3,并且包含 test-7.pngtest-8.pngtest-9.png,它们分别对应标签 789

但是 test_data 推断出的标签分别为 789

我想知道为什么在第一个和第二个代码中,test-8.png 的预期类别不同。此外,我不明白为什么 test_data 的大小会影响实际的预测结果。

可能代码中存在错误,如果您能告诉我是什么错误,我将不胜感激。

if __name__ == '__main__':   import argparse   import scipy.io   import matplotlib.image as mpimg   tf.estimator.RunConfig(       ...   )   if FLAGS.isTrain:      mat_data = scipy.io.loadmat(FLAGS.inputMat)      data = mat_data['X']      labels = mat_data['y']      data = data.reshape(data.shape[0] * data.shape[1] * data.shape[2], data.shape[3]).T      data = data/np.float32(255)      labels = labels.reshape(labels.shape[0]) % 10      train_data = data[:500000]      train_labels = make_onehot_vector(labels[:500000].astype(np.float32))      ...      classifier = tf.estimator.Estimator(custom_model_fn, model_dir = FLAGS.modelPath)      tensors_to_log = {"probabilities" : "softmax_tensor"}      logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=2000)      train_input = tf.estimator.inputs.numpy_input_fn( x = {"x":train_data}, y = train_labels, batch_size = FLAGS.batch_size, num_epochs=FLAGS.num_epoch, shuffle = True)      train_spec = tf.estimator.TrainSepc(input_fn=train_input, max_stpes=FLAGS.num_stpes)      ...      tf.estimator.train_and_evaluate(classifier, train_spec, eval_spec)      classifier.export_savedmodel(FLAGS.modelPath, serving_input_receiver_fn=serving_input_receiver_fn)    else:      test_data = list()      for i in range(8, 10):          test_data.append(mpimg.imread(FLAGS.input + 'test-' + str(i) + '.png'))      test_data = np.asarray(test_data)      test_data = test_data[:, :, :, :3].astype(np.float32)      test_data = test_data.reshape(-1, 32*32*3)      classifier = tf.estimator.Estimator(model_fn = custom_model_fn, model_dir = FLAGS.modelPath)      pred_input = tf.estimator.inputs.numpy_input_fn(x = {"x": test_Data}, y = None, shuffle=False, num_epochs = 1)      pred_result = classifier.predict(input_fn = pred_input)      pred_list = list(pred_result)      print(pred_list)
def custom_model_fn(features, labels, mode):    input_layer = tf.reshape(features['x'], [-1, 32, 32, 3])    isTrain = (mode == tf.estimator.ModeKeys.TRAIN)    L1 = cnn(input_layer, 32, [5,5], [2,2], 2, phase=isTrain)    ...    L5 = cnn(L4, 196, [5,5], [2,2], 2, phase=isTrain)    L5_flat = tf.reshape(L5, [-1, 196 * 3 * 3])    L6 = dense_batch_relu(L5_flat, isTrain, 1024, 'L6')    logits = tf.layers.dense(inputs=L6, units = 10, activation=None)    predictions = {"classes": tf.argmax(input=logits, axis=1), "probabilities": tf.nn.softmax(logits, name="softmax_tensor")}    if mode == tf.estimator.ModeKeys.PREDICT:        export_outputs = {"predict_output": tf.estimator.export.PredictOutput(predictions)}        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_outputs)    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels)) + tf.losses.get_regularization_loss()    if mode == tf.estimator ModeKeys.TRAIN:       optimizer = tf.train.AdamOptimizer(5e-4)       train_op = optimizer.minimize(loss=loss, global_step = tf.train.get_global_step())       return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)    Y = tf.argmax(labels, 1)    eval_metric_ops = {"acc" : tf.metrics.accuracy(labels=Y, predictions=predictions['classes'])}    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
def serving_input_receiver_fn():    inputs = {        'x':tf.placeholder(tf.float32, [None, 32*32*3]),    }    return tf.estimator.export.ServingInputReceiver(inputs, inputs)

在这个代码中,为什么测试数据越小,预测值越不准确,而数据量越大,预测值越准确?


回答:

我在 dense_batch_relu 中使用了 tf.layers.batch_normalization

def dense_batch_relu(x, phase, unit, scope, dropout_rate=0.3):   with tf.variable_scope(scope):      reg = tf.contrib.layers.l2_regularizer(scale = 5e-3)      l1 = tf.layers.dense(x, unit, activation = None, kernel_regularizer=reg)      l2 = tf.layers.batch_normalization(inputs=l1, training=phase)      l3 = tf.layers.dropout(l2, dropout_rate, training=phase)      return tf.nn.relu(l3, 'relu')

但是,我没有在 tf.estimator.ModeKeys.TRAIN 中添加 tf.conrol_dependencies

所以,我将代码修改如下:

if mode == tf.estimator ModeKeys.TRAIN:   update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)   with tf.control_dependencies(update_ops):       optimizer = tf.train.AdamOptimizer(5e-4)       train_op = optimizer.minimize(loss=loss, global_step = tf.train.get_global_step())       return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

然后,它运行得很好。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注