Tensorflow使用估算器和相同的通用模型

时间:2016-08-31 07:44:44

标签: python tensorflow

我正在使用dbpedia训练集(small_train,small_test)。使用bag_of_words_model的估算器可以得到0.70

的准确度

Tensorflow版本0.9

为什么以下代码的行为与估算器不同?

在估算器中graph_action's _run_with_monitors通过运行train_op来执行同样的操作

outputs = session.run(tensors, feed_dict=feed_dict)

虽然下面实施的相同模型的准确度为0.67

  with tf.Graph().as_default():
    sess = tf.Session()
    tf.set_random_seed(1618)
    keys_placeholder = tf.placeholder(tf.int64, shape=(None,),name='keys')
    keys = tf.identity(keys_placeholder) 
    labels_placeholder = tf.placeholder(tf.int64, shape=(None,),name='labels')
    input_placeholder = tf.placeholder(tf.int64, shape=(None, 10),name='input')
    inputs = {'key': keys_placeholder.name, 'inputs_': input_placeholder.name}
    tf.add_to_collection('inputs', json.dumps(inputs))

    target = tf.one_hot(labels_placeholder, 15, 1, 0)
    word_vectors = learn.ops.categorical_variable(input_placeholder, n_classes=n_words,
    embedding_size=EMBEDDING_SIZE, name='words')

    features = tf.reduce_max(word_vectors, reduction_indices=1)
    prediction, loss = learn.models.logistic_regression(features, target)
    prediction = tf.argmax(prediction, 1)

    train_op = tf.contrib.layers.optimize_loss(
      loss, tf.contrib.framework.get_global_step(),
      optimizer='Adam', learning_rate=0.01)

    outputs = {'key': keys.name, 'prediction': prediction.name}
    tf.add_to_collection('outputs', json.dumps(outputs))
    init = tf.initialize_all_variables()
    sess.run(init)

    for step in xrange(100):
      fill_feed_dict = {
        keys_placeholder: np.array(range(len(x_train))),
        labels_placeholder: y_train,
        input_placeholder: x_train }
      start_time = time.time()
      _, loss_value = sess.run([train_op, loss],
         feed_dict=fill_feed_dict)

      duration = time.time() - start_time

    print("Done Training")
    y_predicted = sess.run(prediction, feed_dict={input_placeholder: x_test})
    score = metrics.accuracy_score(y_test, y_predicted)
    print('Accuracy: {0:f}'.format(score))

我正在阅读如下数据: -

  global n_words
  training_set  =  pandas.read_csv('dbpedia_data/dbpedia_csv/train_small.csv', header=None)
  testing_set = pandas.read_csv('dbpedia_data/dbpedia_csv/test_small.csv', header=None)
  x_train = training_set[2]
  y_train = training_set[0]
  x_test = testing_set[2]
  y_test = testing_set[0]

  # Process vocabulary
  np.random.seed(1)
  vocab_processor = learn.preprocessing.VocabularyProcessor(MAX_DOCUMENT_LENGTH)
  x_train = np.array(list(vocab_processor.fit_transform(x_train)))
  x_test = np.array(list(vocab_processor.transform(x_test)))
  n_words = len(vocab_processor.vocabulary_)

0 个答案:

没有答案