TensorFlow二进制分类器输出3个类而不是2个类的预测?

时间:2017-05-04 22:35:31

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

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

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

我感到困惑的地方是# Accuracy Predictions部分预测的最后一步。 为什么我的分类会得到3个课程?为什么我的准确性如此差,这么简单的分类?我对这种基于模型的机器学习相当新,所以我确定它和' #39;我做了一些语法错误或假设。 我的代码中是否有错误?

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)

enter image description here

1 个答案:

答案 0 :(得分:1)

您的模型不进行分类

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

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

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

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