【问题标题】:TensorFlow binary classifier outputs predictions for 3 classes instead of 2?TensorFlow 二元分类器输出 3 个类别而不是 2 个类别的预测?
【发布时间】:2017-10-03 06:39:53
【问题描述】:

当我打印出预测时,输出包括 3 个单独的类 0, 1, and 2,但我只在训练集中给它 2 个单独的类 0 and 1。我不确定为什么会这样。我正在尝试详细说明来自TensorFlow Machine Learning Cookbook 的教程。这是基于第 2 章的最后一个示例(如果有人可以访问它)。请注意,有一些错误,但这可能是文本中旧版本之间的不兼容。

无论如何,我在构建模型时都在尝试开发一种非常严格的结构,以便将其嵌入肌肉记忆中。我为一组计算的每个tf.Session 预先实例化tf.Graph,并设置要使用的线程数。请注意,我将TensorFlow 1.0.1Python 3.6.1 一起使用,因此如果您使用的是旧版本的Python,f"formatstring{var}" 将不起作用。

我感到困惑的是# Accuracy Predictions 部分下预测的最后一步。 为什么我的分类有 3 个类,为什么这么简单的分类我的准确率这么差?我对这种基于模型的机器学习相当陌生,所以我确信这是一些语法我所做的错误或假设。 我的代码有错误吗?

import numpy as np
import tensorflow as tf 
import matplotlib.pyplot as plt
import multiprocessing

# Set number of CPU to use
tf_max_threads = tf.ConfigProto(intra_op_parallelism_threads=multiprocessing.cpu_count())

# Data
seed= 0
size = 50
x = np.concatenate((np.random.RandomState(seed).normal(-1,1,size),
                    np.random.RandomState(seed).normal(2,1,size)
                   )
                  )
y = np.concatenate((np.repeat(0, size), 
                    np.repeat(1, size)
                   )
                  )

# Containers
loss_data = list()
A_data = list()

# Graph
G_6 = tf.Graph()
n = 25

# Containers
loss_data = list()
A_data = list()

# Iterations
n_iter = 5000

# Train / Test Set
tr_ratio = 0.8
tr_idx = np.random.RandomState(seed).choice(x.size, round(tr_ratio*x.size), replace=False)
te_idx = np.array(list(set(range(x.size)) - set(tr_idx)))


# Build Graph
with G_6.as_default():
    # Placeholders
    pH_x = tf.placeholder(tf.float32, shape=[None,1], name="pH_x")
    pH_y_hat = tf.placeholder(tf.float32, shape=[None,1], name="pH_y_hat")

    # Train Set
    x_train = x[tr_idx].reshape(-1,1)
    y_train = y[tr_idx].reshape(-1,1)
    # Test Set
    x_test= x[te_idx].reshape(-1,1)
    y_test = y[te_idx].reshape(-1,1)

    # Model
    A = tf.Variable(tf.random_normal(mean=10, stddev=1, shape=[1], seed=seed), name="A")
    model = tf.multiply(pH_x, A)

    # Loss
    loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=model, labels=pH_y_hat))
    with tf.Session(graph=G_6, config=tf_max_threads) as sess:
        sess.run(tf.global_variables_initializer())
        # Optimizer
        op = tf.train.GradientDescentOptimizer(0.03)
        train_step = op.minimize(loss)
        # Train linear model 
        for i in range(n_iter):
            idx_random = np.random.RandomState(i).choice(x_train.size, size=n)
            x_tr = x[idx_random].reshape(-1,1)
            y_tr = y[idx_random].reshape(-1,1)

            sess.run(train_step, feed_dict={pH_x:x_tr, pH_y_hat:y_tr})

            # Iterations
            A_iter = sess.run(A)[0]
            loss_iter = sess.run(loss, feed_dict={pH_x:x_tr, pH_y_hat:y_tr}).mean()
            # Append
            loss_data.append(loss_iter)
            A_data.append(A_iter)

#             Log
            if (i + 1) % 1000 == 0:
                print(f"Step #{i + 1}:\tA = {A_iter}", f"Loss = {to_precision(loss_iter)}", sep="\t")
                print()   

        # Accuracy Predictions
        A_result = sess.run(A)
        y_ = tf.squeeze(tf.round(tf.nn.sigmoid_cross_entropy_with_logits(logits=model, labels=pH_y_hat)))

        correct_predictions = tf.equal(y_, pH_y_hat)
        accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))
        print(sess.run(y_, feed_dict={pH_x:x_train, pH_y_hat:y_train}))
        print("Training:",
              f"Accuracy = {sess.run(accuracy, feed_dict={pH_x:x_train, pH_y_hat:y_train})}", 
              f"Shape = {x_train.shape}", sep="\t")

        print("Testing:",
              f"Accuracy = {sess.run(accuracy, feed_dict={pH_x:x_test, pH_y_hat:y_test})}", 
              f"Shape = {x_test.shape}", sep="\t")

# Plot path
with plt.style.context("seaborn-whitegrid"):
    fig, ax = plt.subplots(nrows=3, figsize=(6,6))
    pd.Series(loss_data,).plot(ax=ax[0], label="loss", legend=True)
    pd.Series(A_data,).plot(ax=ax[1], color="red", label="A", legend=True)
    ax[2].hist(x[:size], np.linspace(-5,5), label="class_0", color="red")
    ax[2].hist(x[size:], np.linspace(-5,5), label="class_1", color="blue")

    alphas = np.linspace(0,0.5, len(A_data))
    for i in range(0, len(A_data), 100):
        alpha = alphas[i]
        a = A_data[i]
        ax[2].axvline(a, alpha=alpha, linestyle="--", color="black")
    ax[2].legend(loc="upper right")
    fig.suptitle("training-process", fontsize=15, y=0.95)

输出结果:

Step #1000: A = 6.72    Loss = 1.13

Step #2000: A = 3.93    Loss = 0.58

Step #3000: A = 2.12    Loss = 0.319

Step #4000: A = 1.63    Loss = 0.331

Step #5000: A = 1.58    Loss = 0.222

[ 0.  0.  1.  0.  0.  0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  2.
  0.  0.  2.  0.  2.  0.  0.  0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  2.  0.  0.  0.  0.  0.  0.  0.  1.  0.
  1.  0.  1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.
  0.  0.  0.  0.  0.  0.  0.  0.]
Training:   Accuracy = 0.475    Shape = (80, 1)
Testing:    Accuracy = 0.5  Shape = (20, 1)

【问题讨论】:

    标签: python machine-learning tensorflow neural-network classification


    【解决方案1】:

    你的模型不做分类

    您有一个线性回归模型,即您的输出变量 (model = tf.multiply(pH_x, A)) 为每个输入输出一个具有任意范围的单个标量值。这通常是 预测 模型所需要的,它需要预测一些数值,而不是分类器。

    之后,您将其视为包含典型的 n 元分类器输出(例如,通过传递 sigmoid_cross_entropy_with_logits)但它与该函数的期望不匹配 - 在这种情况下,模型变量的“形状”应该每个输入数据点是 多个 值(例如,在您的情况下为 2 个),每个值对应于与每个类的概率相对应的某个度量;然后通常传递给 softmax 函数以对其进行归一化。

    或者,您可能需要一个二进制分类器模型,该模型根据类输出单个值 0 或 1 - 但是,在这种情况下,您需要矩阵乘法后的逻辑函数之类的东西;这需要一个不同的损失函数,比如简单的均方差,而不是 sigmoid_cross_entropy_with_logits。

    目前编写的模型似乎是两个不同的、不兼容的教程的混合体。

    【讨论】:

    • 好吧,这是有道理的。一些区别是:(1)教程中pH_xpH_y_hat的形状是[1, None]而不是[None,1],但我无法让它工作; (2)modeltf.add(pH_x, A) 而不是tf.multiply(pH_x, A),我认为这是一个错误 b/c 我认为这是转换后的逻辑回归; (3) 他的预测部分使用tf.squeeze(tf.round(tf.nn.nsigmoid(tf.add(pH_x, A)))),所以我可以尝试,但我认为logits 会通过,这就是我使用其他函数的原因。感谢您的洞察力。我会修复它,然后接受它。
    猜你喜欢
    • 1970-01-01
    • 2020-05-22
    • 2020-01-19
    • 1970-01-01
    • 2020-03-12
    • 2016-12-04
    • 2018-05-22
    • 2020-11-17
    • 2021-01-26
    相关资源
    最近更新 更多