machine-learning - 预测分类器 tensorflow 中的概率

标签 machine-learning tensorflow classification

嘿,我对 tensorflow 还很陌生。我正在构建一个分类模型,基本上分类为 0/1。有没有办法预测输出为1的概率。这里可以使用predict_proba吗?它在 tflearn.dnn 中被广泛使用,但在我的例子中找不到任何引用。

def main():
    train_x,test_x,train_y,test_y = load_csv_data() 
    x_size = train_x.shape[1] 
    y_size = train_y.shape[1] 

    print(x_size)
    print(y_size)
    # variables
    X = tf.placeholder("float", shape=[None, x_size])
    y = tf.placeholder("float", shape=[None, y_size])
    weights_1 = initialize_weights((x_size, h_size))
    weights_2 = initialize_weights((h_size, y_size))
    # Forward propagation
    y_pred = forward_propagation(X, weights_1, weights_2)
    predict = tf.argmax(y_pred, dimension=1)
    # Backward propagation
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_pred))
    updates_sgd = tf.train.GradientDescentOptimizer(sgd_step).minimize(cost)
    # Start tensorflow session 

    with tf.Session() as sess:

        init = tf.global_variables_initializer()
        steps = 1
        sess.run(init)
        x  = np.arange(steps)
        test_acc = []
        train_acc = []
        print("Step, train accuracy, test accuracy")
        for step in range(steps):
            # Train with each example
            batch_size = len(train_x)
            avg_cost = 0  
            print(batch_size)
            for i in range(len(train_x)):
                _, c = sess.run([updates_sgd,cost], feed_dict={X: train_x[i: i + 1], y: train_y[i: i + 1]})
                print(c)
                avg_cost +=  c/batch_size 

            train_accuracy = np.mean(np.argmax(train_y, axis=1) ==
                                     sess.run(predict, feed_dict={X: train_x, y: train_y}))
            test_accuracy = np.mean(np.argmax(test_y, axis=1) ==
                                    sess.run(predict, feed_dict={X: test_x, y: test_y}))

            print(avg_cost)
            print("%d, %.2f%%, %.2f%%"
                  % (step + 1, 100. * train_accuracy, 100. * test_accuracy))

            test_acc.append(100. * test_accuracy)
            train_acc.append(100. * train_accuracy)

        predict = tf.argmax(y_pred,1)
        test_data = load_test_data( )   
        print(test_data)
        pred = predict.eval(feed_dict={X:test_data})
        print(pred)
        for x in range(0,100):
            print(pred[x])
        print(np.unique(pred))
main()

最佳答案

这里你取概率的argmax:

predict = tf.argmax(y_pred, dimension=1)

如果您仅返回“y_pred”,您应该获得概率。

关于machine-learning - 预测分类器 tensorflow 中的概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47841952/

相关文章:

python - 将多种算法与 sklearn 管道进行比较

tensorflow - CTC 丢失 InvalidArgumentError : sequence_length(b) <= time

tensorflow - Tensorflow 中基于 CuDnnGRU 的 RNN 实现的简单示例

tensorflow - ArcFace 严格来说是损失函数还是激活函数?

tensorflow - 在 TensorFlow 中构建具有固定权重的层

excel - Rapidminer 神经网络多项式

python - 无法将随机变量传递给 Tensorflow 中的 tf.image.central_crop()

python - 如何以正确的方式缩放和预测单个样本

machine-learning - 对于任何分布,弱学习器真的必须有 < 1/2 的误差吗?

machine-learning - 机器学习实践 : Writing algorithms yourself or using Weka?