基于Tens的二值分类中精度值的变化与损失值的不变

2024-04-24 22:43:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用一个深层神经网络结构来根据二进制标签值-0和+1进行分类。这是我在tensorflow中实现的代码。这个问题也是从a previous question中的讨论中提出的

import tensorflow as tf
import numpy as np
from preprocess import create_feature_sets_and_labels

train_x,train_y,test_x,test_y = create_feature_sets_and_labels()

x = tf.placeholder('float', [None, 5])
y = tf.placeholder('float')

n_nodes_hl1 = 500
n_nodes_hl2 = 500
# n_nodes_hl3 = 500

n_classes = 1
batch_size = 100

def neural_network_model(data):

    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([5, n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}

    # hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
    #                   'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}

    # output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
    #                   'biases':tf.Variable(tf.random_normal([n_classes]))}

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),
                    'biases':tf.Variable(tf.random_normal([n_classes]))}


    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.nn.relu(l2)

    # l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])
    # l3 = tf.nn.relu(l3)

    # output = tf.transpose(tf.add(tf.matmul(l3, output_layer['weights']), output_layer['biases']))
    output = tf.add(tf.matmul(l2, output_layer['weights']), output_layer['biases'])
    return output



def train_neural_network(x):
    prediction = tf.sigmoid(neural_network_model(x))
    cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(prediction, y))
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    hm_epochs = 10

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        for epoch in range(hm_epochs):
            epoch_loss = 0
            i = 0
            while i < len(train_x):
                start = i
                end = i + batch_size
                batch_x = np.array(train_x[start:end])
        batch_y = np.array(train_y[start:end])

        _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                              y: batch_y})
        epoch_loss += c
        i+=batch_size

            print('Epoch', epoch, 'completed out of', hm_epochs, 'loss:', epoch_loss)

        # correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
        # accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
        predicted_class = tf.greater(prediction,0.5)
        correct = tf.equal(predicted_class, tf.equal(y,1.0))
        accuracy = tf.reduce_mean( tf.cast(correct, 'float') )

        # print (test_x.shape)
        # accuracy = tf.nn.l2_loss(prediction-y,name="squared_error_test_cost")/test_x.shape[0]
        print('Accuracy:', accuracy.eval({x: test_x, y: test_y}))

train_neural_network(x) 

具体地说,(从上一个问题继续讨论)我删除了一个层-hidden_3_layer。改变

prediction = neural_network_model(x)

prediction = tf.sigmoid(neural_network_model(x))

并根据尼尔的回答添加了predicted_class, correct, accuracy部分。我还把csv中的所有-1改为0。在

这是我的线索:

^{pr2}$

正如你所见,损失并没有减少。所以我不知道它是否还在正常工作。在

以下是多次重新运行的结果。结果摇摆不定:

('Epoch', 0, 'completed out of', 10, 'loss:', 26.513012945652008)
('Epoch', 1, 'completed out of', 10, 'loss:', 26.513012945652008)
('Epoch', 2, 'completed out of', 10, 'loss:', 26.513012945652008)
('Epoch', 3, 'completed out of', 10, 'loss:', 26.513012945652008)
('Epoch', 4, 'completed out of', 10, 'loss:', 26.513012945652008)
('Epoch', 5, 'completed out of', 10, 'loss:', 26.513012945652008)
('Epoch', 6, 'completed out of', 10, 'loss:', 26.513012945652008)
('Epoch', 7, 'completed out of', 10, 'loss:', 26.513012945652008)
('Epoch', 8, 'completed out of', 10, 'loss:', 26.513012945652008)
('Epoch', 9, 'completed out of', 10, 'loss:', 26.513012945652008)
('Accuracy:', 0.60124224)

另一个:

('Epoch', 0, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 1, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 2, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 3, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 4, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 5, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 6, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 7, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 8, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 9, 'completed out of', 10, 'loss:', 22.873702049255371)
('Accuracy:', 1.0)

还有一个:

('Epoch', 0, 'completed out of', 10, 'loss:', 23.163824260234833)
('Epoch', 1, 'completed out of', 10, 'loss:', 22.88000351190567)
('Epoch', 2, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 3, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 4, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 5, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 6, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 7, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 8, 'completed out of', 10, 'loss:', 22.873702049255371)
('Epoch', 9, 'completed out of', 10, 'loss:', 22.873702049255371)
('Accuracy:', 0.99627328)

我也看到了0.0-\

---------------编辑-------------------------------

关于数据和数据处理的一些细节。我使用的是来自雅虎的IBM每日股票数据!为20年(几乎)的时期融资。这相当于大约5200行条目。在

以下是我的处理方法:

import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import csv
import pickle

def create_feature_sets_and_labels(test_size = 0.2):
    df = pd.read_csv("ibm.csv")
    df = df.iloc[::-1]
    features = df.values
    testing_size = int(test_size*len(features))
    train_x = list(features[1:,1:6][:-testing_size])
    train_y = list(features[1:,7][:-testing_size])
    test_x = list(features[1:,1:6][-testing_size:])
    test_y = list(features[1:,7][-testing_size:])
    scaler = MinMaxScaler(feature_range=(-5,5))
    train_x = scaler.fit_transform(train_x)
    train_y = scaler.fit_transform(train_y)
    test_x = scaler.fit_transform(test_x)
    test_y = scaler.fit_transform(test_y)

    return train_x, train_y, test_x, test_y

if __name__ == "__main__":
    train_x, train_y, test_x, test_y = create_feature_sets_and_labels()
    with open('stockdata.pickle', 'wb') as f:
        pickle.dump([train_x, train_y, test_x, test_y], f)

第0列是日期。所以这不是一个特性。第7列也不是。我使用sklearnMinMaxScaler()在-5到5的范围内规范化数据。在

------------编辑2-------------------

我注意到,当数据以非标准化形式呈现时,系统不会改变其精度。在


Tags: oftestlayersizetftrainrandomout
1条回答
网友
1楼 · 发布于 2024-04-24 22:43:31

一旦您在ML培训任务中将数据预处理成错误的形状或范围,其余的数据流就会出错。在问题中的代码中,您以不同的方式多次执行此操作。在

把事情按顺序处理。第一个问题是预处理。你的目标应该是:

  • X值(输入特征)以表格形式,每行为例,每列为特征。数值应该是数值和比例,以便与神经网络一起使用。测试和训练数据需要以相同的比例缩放-这并不意味着使用相同的.fit_transform,因为这符合缩放器。

  • Y值(输出标签)在表格形式下,每一行都是与X的同一行相匹配的示例,每列是一个输出的真实值。对于分类问题,值通常为0和1,,并且不应重新缩放,因为它们代表类成员身份。

重新编写create_feature_sets_and_labels函数可以正确地执行以下操作:

def create_feature_sets_and_labels(test_size = 0.2):
    df = pd.read_csv("ibm.csv")
    df = df.iloc[::-1]
    features = df.values
    testing_size = int(test_size*len(features))

    train_x = np.array(features[1:,1:6][:-testing_size]).astype(np.float32)
    train_y = np.array(features[1:,7][:-testing_size]).reshape(-1, 1).astype(np.float32)

    test_x = np.array(features[1:,1:6][-testing_size:]).astype(np.float32)
    test_y = np.array(features[1:,7][-testing_size:]).reshape(-1, 1).astype(np.float32)

    scaler = MinMaxScaler(feature_range=(-5,5))

    scaler.fit(train_x)

    train_x = scaler.transform(train_x)
    test_x = scaler.transform(test_x)

    return train_x, train_y, test_x, test_y

与您版本的重要区别:

  • 使用类型转换np.array,而不是{}(细微差别)

  • y值是表格式的[n_examples, n_outputs](主要区别在于,您的行向量形状会导致以后出现许多问题)

  • Scaler只适合一次,然后应用于特征(主要区别在于,如果单独缩放训练和测试数据,则无法预测任何有意义的东西)

  • Scaler是而不是应用于输出(分类器的主要区别在于,您希望训练和测试值为0,1,以获得有意义的训练和报告准确性)

此数据的培训代码也存在一些问题:

  • y = tf.placeholder('float')应该是y = tf.placeholder('float', [None, 1])。这对处理没有影响,但是当y是错误的形状时,会正确地抛出错误。这个错误早就成为事情出问题的线索了。

  • n_nodes_hl1 = 500n_nodes_hl2 = 500可能要低得多,而网络实际上会更好地工作,例如n_nodes_hl1 = 10和{}-这主要是因为权重的初始值比较大,您可以选择缩小权重,对于更复杂的数据,您可能希望这样做。在这种情况下,减少隐藏神经元的数量更为简单。

  • 正如我们在评论中所讨论的,train_neural_网络功能的开始应该如下所示:

    ^{pr2}$

    。这是一个很大的区别。通过使用sigmoid_cross_entropy_with_logits,您已经承诺使用输出层的预转换值进行培训。但您仍然希望预测值能够测量准确度(或用于网络的任何其他用途,其中您希望读取预测值)。

  • 为了一致地度量损失,您希望每个示例都有平均损失,所以您需要将每个批次的平均值和除以批次数:'loss:', epoch_loss/(len(train_x)/batch_size)

如果我做了所有这些修正,并用更多的时间段运行这个,比如50,那么我得到一个典型的0.7的损失和{}的准确度测量值-这是合理可靠的,但是由于起始权重的变化,确实会移动一点。准确度不是很稳定,而且可能会受到过度拟合的影响,这是你根本不允许的(你应该阅读一些帮助测量和管理过度拟合的技术,这是可靠地培训NNs的重要部分)

0.5的值可能不正确。通过修改网络体系结构或元参数,可以对其进行改进。我可以得到0.43的训练损失和高达0.83测试精度,例如在隐藏层中用tf.nn.relu替换{},并运行500个周期。在

为了了解更多关于神经网络的知识,在训练时要测量什么,以及在你的模型中什么是值得改变的,你将需要更深入地研究这个主题。在

相关问题 更多 >