通过张量流对数据进行分类,但精度值未发生变化

时间:2018-12-15 06:27:26

标签: python tensorflow

在开始之前和结束之后检查体重,偏倚都会改变。 损失值不断减少

问题是准确性值从未动过

这是我的代码,其中有一部分

我的代码中存在一个漏洞,未经编辑,此段内容不存在,但我认为您只能阅读此段。

这段代码只是在制造重量和偏见

class variableDataSet:

    def __set_Random_Weight__(self):
        self.W1 = np.random.rand(2, 5)
        self.W5 = np.random.rand(5, 2)

    def __set_Random_bias__(self):
        self.b = np.random.rand(5)
        self.b_out = np.random.rand(2)

    def create_Datas(self):
        self.__set_Random_Weight__()
        self.__set_Random_bias__()

这是我的训练数据集

set_x_data ()和 set_y_data ()是创建x_data和y_data这是训练数据集

x_data创建类似array(50〜1000,1)的外部联接array(0.5〜10,0.5)

accuracy_rate ()只是从x_data创建y_data的过程

classify_Data_Set ()是对训练数据和验证数据进行细化和分类

我们假设x_data和y_data是正确的

class trainingDataSet:

    idealTargetArea = 1.0
    idealTargetDistance = 100.0
    tryCount = 5

    def __set_x_data__(self):
        #distance
        self.distance = np.arange(50, 1000, 1.0)
        #target area
        self.area = np.arange(0.5, 10, 0.5)
        temp_data = np.zeros(shape=(self.area.size, self.distance.size, 2))

        for i in range(self.distance.size):
            for j in  range(self.area.size):
                temp_data[j][i] = np.array([self.distance[i], self.area[j]])

        self.x_data = temp_data

    def __accuracy_rate__(self, inputData, givenAccuracy):
        idealAccuracyWithGivenArea = givenAccuracy * (inputData[1] / self.idealTargetArea)
        coefficient = (self.idealTargetDistance / inputData[0]) ** 2
        seeArea = coefficient * inputData[1]
        ratio = seeArea * inputData[1]

        return idealAccuracyWithGivenArea * ratio

    def __set_y_data__(self, x_data, givenAccuracy, efficiency):
        pos = posibility.Posibility()
        self.y_data = np.zeros(shape=(np.size(x_data, 0), np.size(x_data, 1), 2))

        for i in range(np.size(x_data, 0)):
            for j in range(np.size(x_data, 1)):
                accuracy = self.__accuracy_rate__(x_data[i][j], givenAccuracy)
                correct = pos.result(self.tryCount, accuracy, int(efficiency * self.tryCount))

                if(correct >= efficiency):
                    temp = np.array([0, 1])
                else:
                    temp = np.array([1, 0])

                self.y_data[i][j] = temp

    def __classify_Data_Set__(self):
        r1 = np.random.permutation(self.x_data.shape[0])
        r2 = np.random.permutation(self.x_data.shape[1])

        input1 = r1[int(r1.size * 0.7):]
        input2 = r2[int(r2.size * 0.7):]

        x_validation = np.zeros(shape=(input1.size, input2.size, self.x_data.shape[2]))
        y_validation = np.zeros(shape=(input1.size, input2.size, self.y_data.shape[2]))

        for i in range(input1.size):
            for j in range(input2.size):
                x_validation[i][j] = np.array([self.x_data[input1[i]][input2[j]]])
                y_validation[i][j] = np.array([self.y_data[input1[i]][input2[j]]])

        self.x_validation = x_validation
        self.y_validation = y_validation

        input1 = r1[:int(r1.size * 0.7)]
        input2 = r2[:int(r2.size * 0.7)]

        x_data = np.zeros(shape=(input1.size, input2.size, self.x_data.shape[2]))
        y_data = np.zeros(shape=(input1.size, input2.size, self.y_data.shape[2]))

        for i in range(input1.size):
            for j in range(input2.size):
                x_data[i][j] = np.array([self.x_data[input1[i]][input2[j]]])
                y_data[i][j] = np.array([self.y_data[input1[i]][input2[j]]])

        self.x_data = x_data
        self.y_data = y_data

    def create_Datas(self, accuracy, efficiency):
        self.accuracy = accuracy
        self.efficiency = efficiency

        self.__set_x_data__()
        self.__set_y_data__(self.x_data, self.accuracy, self.efficiency)

        self.__classify_Data_Set__()
        self.__write_Data__()

这是我的假设

隐藏层的活动函数为S型,而最后一层的活动函数为softmax

class fireDecision:

    SCALE_X = 1000

    def __init__(self):
        self.sess = tf.Session()

    def Session(self):
        return self.sess

    def algorithm(self, x):
        x = x / self.SCALE_X

        hidden_layer1 = tf.sigmoid(tf.matmul(x, self.W1) + self.b)
        out_layer = tf.nn.softmax(tf.matmul(hidden_layer1, self.W5) + self.b_out)

        return out_layer

这是教练代码

损失函数是交叉熵,优化器是梯度下降

class trainer:

    def __loss__(self, hypothesis, y):
        cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(hypothesis), reduction_indices=[1]))

        return cross_entropy

    def __train__(self, loss):
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
        train = optimizer.minimize(loss)

        return train

    def __accuracy__(self, hypothesis, y):
        correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        return accuracy

    def check_accuracy(self, x, y, epoch, hypothesis, sess):
        accuracy_test = self.__accuracy__(hypothesis, y)

        self.accuracy[epoch] = sess.run(accuracy_test, feed_dict={x:self.x_validation, y:self.y_validation})
        print("accuracy : " + str(self.accuracy[epoch]))

    def training(self, network, data_set, train_Epoch):
        self.set_training_data_set(data_set)
        self.set_validation_data_set(data_set)

        //network is fireDecision class
        sess = network.Session()

        x= tf.placeholder(tf.float32, shape=[None, 2], name='x')
        y= tf.placeholder(tf.float32, shape=[None, 2], name='y')

        hypothesis = network.algorithm(x)
        loss = self.__loss__(hypothesis, y)
        train = self.__train__(loss)

        epochs = train_Epoch//I was input epoch 2000
        self.losses = np.zeros(epochs)
        self.accuracy = np.zeros(epochs)

        loop = self.x_data.shape[0] * self.x_data.shape[1]
        x_data_input = np.zeros((loop, 2))
        y_data_input = np.zeros((loop, 2))

        print("Training Step")
        for step in range(epochs):
            print("Step : " + str(step))
            count = 0

            for i in range(self.x_data.shape[0]):
                for j in range(self.x_data.shape[1]):
                    x_data_input[count] = self.x_data[i][j]
                    y_data_input[count] = self.y_data[i][j]
                    count += 1

            h, _, l = sess.run([hypothesis, train, loss], feed_dict={x:x_data_input, y:y_data_input})

            self.losses[step] = l
            print("Loss : " + str(self.losses[step]))

            self.check_accuracy(x, y, step, hypothesis, sess)

如果要运行我的代码,这是我代码的空洞

import numpy as np

class rwFromDirectory:
    def __init(self):
        pass

    def write_data(self, file_path, write_data):
        np.savetxt(file_path, write_data)

    def read_data(self, file_path):
        return np.loadtxt(file_path).astype(np.float32)

import rwFromDirectory
import numpy as np

class variableDataSet:

    def __init__(self):
        self.W1 = None
        self.W5 = None

        self.b = None
        self.b_out = None

        self.rw = rwFromDirectory.rwFromDirectory()

    def __set_Random_Weight__(self):
        self.W1 = np.random.rand(2, 5)
        self.W5 = np.random.rand(5, 2)

    def __set_Random_bias__(self):
        self.b = np.random.rand(5)
        self.b_out = np.random.rand(2)

    def create_Datas(self):
        self.__set_Random_Weight__()
        self.__set_Random_bias__()

        self.write_Datas()

    def set_Datas(self, W1, W5, b, b_out):
        self.W1 = W1
        self.W5 = W5
        self.b = b
        self.b_out = b_out

    def write_Datas(self):
        self.rw.write_data('./model_data/W1', self.W1)
        self.rw.write_data('./model_data/W5', self.W5)

        self.rw.write_data('./model_data/b', self.b)
        self.rw.write_data('./model_data/b_out', self.b_out)

    def read_Datas(self):
        self.W1 = self.rw.read_data('./model_data/W1')
        self.W5 = self.rw.read_data('./model_data/W5')

        self.b = self.rw.read_data('./model_data/b')
        self.b_out = self.rw.read_data('./model_data/b_out')

    def get_W1(self):
        return self.W1

    def get_W5(self):
        return self.W5

    def get_b(self):
        return self.b

    def get_b_out(self):
        return self.b_out

class Posibility:
    def __init__(self):
        pass

    def __factorial__(self, n):
        result = 1
        for i in range(n):
            result *= (i + 1)

        return result

    def __Combination__(self, tryCount, hopeResult):
        result = self.__factorial__(tryCount) / (self.__factorial__(tryCount - hopeResult) * self.__factorial__(hopeResult))

        return result

    def result(self, tryCount, probability, hopeResult):
        result = self.__Combination__(tryCount, hopeResult) * pow(probability, hopeResult) * pow((1 - probability), (tryCount - hopeResult))

        return result

import posibility
import numpy as np
import rwFromDirectory

class trainingDataSet:

    idealTargetArea = 1.0
    idealTargetDistance = 100.0
    tryCount = 5

    def __init__(self):
        self.x_data = None
        self.y_data = None

        self.accuracy = None
        self.efficiency = None

        self.rw = rwFromDirectory.rwFromDirectory()

    def __set_x_data__(self):
        #distance
        self.distance = np.arange(50, 1000, 1.0)
        #target area
        self.area = np.arange(0.5, 10, 0.5)
        temp_data = np.zeros(shape=(self.area.size, self.distance.size, 2))

        for i in range(self.distance.size):
            for j in  range(self.area.size):
                temp_data[j][i] = np.array([self.distance[i], self.area[j]])

        self.x_data = temp_data

    def __accuracy_rate__(self, inputData, givenAccuracy):
        idealAccuracyWithGivenArea = givenAccuracy * (inputData[1] / self.idealTargetArea)
        coefficient = (self.idealTargetDistance / inputData[0]) ** 2
        seeArea = coefficient * inputData[1]
        ratio = seeArea * inputData[1]

        return idealAccuracyWithGivenArea * ratio

    def __set_y_data__(self, x_data, givenAccuracy, efficiency):
        pos = posibility.Posibility()
        self.y_data = np.zeros(shape=(np.size(x_data, 0), np.size(x_data, 1), 2))

        for i in range(np.size(x_data, 0)):
            for j in range(np.size(x_data, 1)):
                accuracy = self.__accuracy_rate__(x_data[i][j], givenAccuracy)
                correct = pos.result(self.tryCount, accuracy, int(efficiency * self.tryCount))

                if(correct >= efficiency):
                    temp = np.array([0, 1])
                else:
                    temp = np.array([1, 0])

                self.y_data[i][j] = temp

    def __classify_Data_Set__(self):
        r1 = np.random.permutation(self.x_data.shape[0])
        r2 = np.random.permutation(self.x_data.shape[1])

        input1 = r1[int(r1.size * 0.7):]
        input2 = r2[int(r2.size * 0.7):]

        x_validation = np.zeros(shape=(input1.size, input2.size, self.x_data.shape[2]))
        y_validation = np.zeros(shape=(input1.size, input2.size, self.y_data.shape[2]))

        for i in range(input1.size):
            for j in range(input2.size):
                x_validation[i][j] = np.array([self.x_data[input1[i]][input2[j]]])
                y_validation[i][j] = np.array([self.y_data[input1[i]][input2[j]]])

        self.x_validation = x_validation
        self.y_validation = y_validation

        input1 = r1[:int(r1.size * 0.7)]
        input2 = r2[:int(r2.size * 0.7)]

        x_data = np.zeros(shape=(input1.size, input2.size, self.x_data.shape[2]))
        y_data = np.zeros(shape=(input1.size, input2.size, self.y_data.shape[2]))

        for i in range(input1.size):
            for j in range(input2.size):
                x_data[i][j] = np.array([self.x_data[input1[i]][input2[j]]])
                y_data[i][j] = np.array([self.y_data[input1[i]][input2[j]]])

        self.x_data = x_data
        self.y_data = y_data

    def __write_Data__(self):
        size = np.array([[self.x_data.shape[0], self.x_data.shape[1], self.x_data.shape[2]], [self.y_data.shape[0], self.y_data.shape[1], self.y_data.shape[2]]])
        self.rw.write_data('./model_data/xy_size', size)

        temp_Array = self.x_data
        temp_Array = temp_Array.reshape((self.x_data.size))
        self.rw.write_data('./model_data/x_data', temp_Array)

        temp_Array = self.y_data
        temp_Array = temp_Array.reshape((self.y_data.size))
        self.rw.write_data('./model_data/y_data', temp_Array)

        array = np.array([self.accuracy, self.efficiency])
        self.rw.write_data('./model_data/etc', array)

        size = np.array([[self.x_validation.shape[0], self.x_validation.shape[1], self.x_validation.shape[2]], [self.y_validation.shape[0], self.y_validation.shape[1], self.y_validation.shape[2]]])
        self.rw.write_data('./model_data/xy_validation_size', size)

        temp_Array = self.x_validation
        temp_Array = temp_Array.reshape((self.x_validation.size))
        self.rw.write_data('./model_data/x_validation', temp_Array)

        temp_Array = self.y_validation
        temp_Array = temp_Array.reshape((self.y_validation.size))
        self.rw.write_data('./model_data/y_validation', temp_Array)

    def create_Datas(self, accuracy, efficiency):
        self.accuracy = accuracy
        self.efficiency = efficiency

        self.__set_x_data__()
        self.__set_y_data__(self.x_data, self.accuracy, self.efficiency)

        self.__classify_Data_Set__()
        self.__write_Data__()

    def read_Datas(self):
        xy_size = self.rw.read_data('./model_data/xy_size')

        array = self.rw.read_data('./model_data/x_data')
        self.x_data = array.reshape((int(xy_size[0][0]), int(xy_size[0][1]), int(xy_size[0][2])))

        array = self.rw.read_data('./model_data/y_data')
        self.y_data = array.reshape((int(xy_size[1][0]), int(xy_size[1][1]), int(xy_size[1][2])))

        array = self.rw.read_data('./model_data/etc')
        self.accuracy = array[0]
        self.efficiency = array[1]

        xy_validation_size = self.rw.read_data('./model_data/xy_validation_size')

        array = self.rw.read_data('./model_data/x_validation')
        self.x_validation = array.reshape((int(xy_validation_size[0][0]), int(xy_validation_size[0][1]), int(xy_validation_size[0][2])))

        array = self.rw.read_data('./model_data/y_validation')
        self.y_validation = array.reshape((int(xy_validation_size[0][0]), int(xy_validation_size[0][1]), int(xy_validation_size[0][2])))

    def get_x_data(self):
        return self.x_data

    def get_y_data(self):
        return self.y_data

    def get_accuracy(self):
        return self.accuracy

    def get_efficiency(self):
        return self.efficiency

    def get_x_validation(self):
        return self.x_validation

    def get_y_validation(self):
        return self.y_validation

import variableDataSet
import numpy as np
import tensorflow as tf

class fireDecision:

    SCALE_X = 1000
    set_test = False
    set_train = False

    def __init__(self):
        self.sess = tf.Session()

    def __read_Data__(self):
        v = variableDataSet.variableDataSet()
        v.read_Datas()

        return v

    def __set_Train__(self):
        v = self.__read_Data__()

        self.W1 = tf.Variable(v.get_W1(), dtype = tf.float32)
        self.W5 = tf.Variable(v.get_W5(), dtype = tf.float32)
        self.b = tf.Variable(v.get_b(), dtype = tf.float32)
        self.b_out = tf.Variable(v.get_b_out(), dtype = tf.float32)

        self.sess.run(tf.global_variables_initializer())

    def __set_Test__(self):
        v = self.__read_Data__()

        self.W1 = v.get_W1()
        self.W5 = v.get_W5()
        self.b = v.get_b()
        self.b_out = v.get_b_out()

    def write_data(self):
        v = variableDataSet.variableDataSet()

        W1 = self.sess.run(self.W1)
        W5 = self.sess.run(self.W5)
        b = np.array([self.sess.run(self.b)])
        b_out = np.array([self.sess.run(self.b_out)])

        v.set_Datas(W1, W5, b, b_out)
        v.write_Datas()

    def Session(self):
        return self.sess

    def algorithm(self, x):
        if(not self.set_train):
            self.set_test = False
            self.set_train = True
            self.__set_Train__()

        x = x / self.SCALE_X

        hidden_layer1 = tf.sigmoid(tf.matmul(x, self.W1) + self.b)
        out_layer = tf.nn.softmax(tf.matmul(hidden_layer1, self.W5) + self.b_out)

        return out_layer

    def decision(self, distance, targetArea):
        self.input = np.array([[distance/self.SCALE_X, targetArea]])

        if(not self.set_test):
            self.set_test = True
            self.set_train = False
            self.__set_Test__()

        return self.sess.run(self.algorithm(self.input))

import rwFromDirectory
import numpy as np
import tensorflow as tf

class trainer:

    def __init__(self):
        self.rw = rwFromDirectory.rwFromDirectory()

    def __loss__(self, hypothesis, y):
        cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(hypothesis), reduction_indices=[1]))

        return cross_entropy

    def __train__(self, loss):
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
        train = optimizer.minimize(loss)

        return train

    def set_training_data_set(self, trainingDataSet):
        self.x_data = trainingDataSet.get_x_data()
        self.y_data = trainingDataSet.get_y_data()

    def __set_validation__(self):
        x_validation_ = np.zeros((self.x_validation.shape[0] * self.x_validation.shape[1], self.x_validation.shape[2]))
        y_validation_ = np.zeros((self.y_validation.shape[0] * self.y_validation.shape[1], self.y_validation.shape[2]))

        for i in range(self.x_validation.shape[0]):
            count = 0
            for j in range(self.x_validation.shape[1]):
                x_validation_[count] = self.x_validation[i][j]
                y_validation_[count] = self.y_validation[i][j]
                count += 1

        self.x_validation = x_validation_
        self.y_validation = y_validation_

    def set_validation_data_set(self, trainingDataSet):
        self.x_validation = trainingDataSet.get_x_validation()
        self.y_validation = trainingDataSet.get_y_validation()

        self.__set_validation__()

    def __accuracy__(self, hypothesis, y):
        correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        return accuracy

    def check_accuracy(self, x, y, epoch, hypothesis, sess):
        accuracy_test = self.__accuracy__(hypothesis, y)

        self.accuracy[epoch] = sess.run(accuracy_test, feed_dict={x:self.x_validation, y:self.y_validation})
        print("accuracy : " + str(self.accuracy[epoch]))

    def training(self, network, data_set, train_Epoch):
        self.set_training_data_set(data_set)
        self.set_validation_data_set(data_set)

        sess = network.Session()

        x= tf.placeholder(tf.float32, shape=[None, 2], name='x')
        y= tf.placeholder(tf.float32, shape=[None, 2], name='y')

        hypothesis = network.algorithm(x)
        loss = self.__loss__(hypothesis, y)
        train = self.__train__(loss)

        epochs = train_Epoch
        self.losses = np.zeros(epochs)
        self.accuracy = np.zeros(epochs)

        loop = self.x_data.shape[0] * self.x_data.shape[1]
        x_data_input = np.zeros((loop, 2))
        y_data_input = np.zeros((loop, 2))

        print("Training Step")
        for step in range(epochs):
            print("Step : " + str(step))
            count = 0

            for i in range(self.x_data.shape[0]):
                for j in range(self.x_data.shape[1]):
                    x_data_input[count] = self.x_data[i][j]
                    y_data_input[count] = self.y_data[i][j]
                    count += 1

            h, _, l = sess.run([hypothesis, train, loss], feed_dict={x:x_data_input, y:y_data_input})

            self.losses[step] = l
            print("Loss : " + str(self.losses[step]))

            self.check_accuracy(x, y, step, hypothesis, sess)

        self.write_result(network)

    def write_result(self, network):
        network.write_data()
        self.rw.write_data('./model_data/losses', self.losses)
        self.rw.write_data('./model_data/accuracy', self.accuracy)

您可以以此运行代码

import fireDecision
fireDecision = fireDecision.fireDecision()

import trainingDataSet
trainingDataSet = trainingDataSet.trainingDataSet()
trainingDataSet.create_Datas(0.8, 0.6)

import variableDataSet
variableDataSet = variableDataSet.variableDataSet()
variableDataSet.create_Datas()

import trainer
trainer = trainer.trainer()

trainer.training(fireDecision, trainingDataSet, 2000)

0 个答案:

没有答案