Tensorflow精度没有增加

时间:2019-07-15 15:54:52

标签: python tensorflow machine-learning deep-learning

我有一个张量流模型,我的问题是精度没有增加,或者错误率没有降低

我已经尝试降低学习率并将辍学应用于每个图层,但结果并未显示出模型的任何改进。

如何提高模型的准确性和错误率?

----这是model.py中的模型代码

import tensorflow as tf
import os
import time
import sys
import numpy as np
from tensorflow.python.platform import gfile



class cnn(object):

    def __init__(self, sizevocab, lengthsequence, num_class, batch_size):


        self.x = tf.placeholder(tf.int32, [None, lengthsequence], name = "x")
        self.y = tf.placeholder(tf.float32, [None, num_class], name = "y")

        self.keep_drop = tf.placeholder(tf.float32)



        print("target")
        print(self.y)


        # ------for svm model------


        self.dimension = 64
        self.sizefilter = [2,3,4]
        self.numfilter = 8

        self.is_training = tf.placeholder(tf.bool,[], name= "is_training")
        self.global_step = tf.Variable(0, name = "global_step")
        self.learning_rate = 0.0001

        with tf.name_scope("embeddings"):
            # word_embedding = tf.get_variable("word_embedding", [vocabulary, dimension])

            # embedded_word_id = tf.nn.embedding_lookup(word_embedding, self.x)
            self.word_embedded = tf.Variable(tf.truncated_normal([sizevocab, self.dimension], 1, 2), name = "embedding")
            print("input")
            print(self.word_embedded)


            self.embeddedlook = tf.nn.embedding_lookup(self.word_embedded, self.x)

            print("embed")
            print(self.embeddedlook)
            self.embedinput = tf.expand_dims(self.embeddedlook, -1)

            print("embedded")
            print(self.embedinput)


        pool_output = []

        for i,filter_size in enumerate(self.sizefilter):



            weight = tf.truncated_normal([filter_size, self.dimension, 1, self.numfilter], stddev = 0.1)

            print("weight1:")
            print(weight)
            bias = tf.zeros([self.numfilter])

            print("bias1")
            print(bias)

            convolutionlayer = tf.nn.conv2d(self.embedinput, weight, strides=[1,1,1,1], padding = 'VALID') + bias
            print("convolutional layer")
            print(convolutionlayer)


            rectified = tf.nn.relu(convolutionlayer)
            drop1 = tf.nn.dropout(rectified, self.keep_drop)

                # poolinglayer

                # continue layer -IrwanMaryam
            print("convolutional layer ")
            print(convolutionlayer)
            print("rectified layer")
            print(rectified)

            pooling = tf.nn.max_pool2d(drop1, [1, lengthsequence - filter_size +1 , 1, 1], [1,2,2,1], padding = 'VALID', name = "pool")


            print("pooling")
            print(pooling)




            pool_output.append(pooling)

            print("shape pool")
            print(pool_output)



                # full connected layer

        self.totalfilter = self.numfilter * len(self.sizefilter)
        flat1 = tf.concat(pool_output, 3)


        flat = tf.reshape(flat1, [-1, self.totalfilter])


        print("totalfilter")
        print(self.totalfilter)

        print("flat output")
        print(flat1)

        print("flat_fc")
        print(flat)
        weight_fc = tf.truncated_normal([self.totalfilter, 16], stddev = 0.01)

        bias_fc = tf.zeros([16])

        fc = tf.matmul(flat, weight_fc) + bias_fc


        drop = tf.nn.dropout(fc, self.keep_drop)



        # weight_fc2 = tf.truncated_normal([lengthsequence, lengthsequence])

        # bias_fc2 = tf.zeros([lengthsequence])

        # fc2 = tf.matmul(fc,weight_fc2) + bias_fc2

        # reshape_svm = tf.reshape(fc2, [-1, lengthsequence])
        # print("fc2 :")
        # print(fc2.shape)

        weightsvm = tf.truncated_normal([16, num_class], stddev = 0.01)

        print("weight svm")
        print(weightsvm)
        B = tf.zeros([num_class])

        raw = tf.subtract(tf.matmul(drop,weightsvm), B)

        drop2 = tf.nn.dropout(raw, self.keep_drop)



        print("output :")
        print(raw)



        # ----SVM Model-------

        with tf.name_scope('SVM_MODEL'):




            l2_norm = tf.reduce_mean(tf.square(weightsvm))

            alpha = tf.constant([0.1])
            classification_term = tf.reduce_mean(tf.maximum(0., tf.subtract(1., tf.multiply(drop2, self.y))))
            self.loss = tf.add(classification_term, tf.multiply(alpha, l2_norm))

            pred = tf.sign(raw)
            self.accuracy = tf.reduce_mean(tf.cast(tf.equal(pred, self.y), tf.float32))


            optimizer = tf.train.AdamOptimizer(learning_rate = self.learning_rate)
            gradvars = optimizer.compute_gradients(self.loss, aggregation_method = 2)
            self.trainop = optimizer.apply_gradients(gradvars, global_step = self.global_step)

---这是在train.py中进行训练的循环

    import tensorflow as tf
    import numpy as np
    import os
    import time
    import datetime
    from filtertext import *
    import sys
    from cnnsvm import cnn
    # from utils import *
    from sklearn.model_selection import train_test_split
    from tensorflow.contrib import learn

    x, y, vocabulary, vocabulary_invi = load_data()
    keep_probability = tf.placeholder(tf.float32)
    sizevocab = len(vocabulary)
    epoch = 10000
    batch_size = 512
    Xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size = 0.1)
    lengthsequence = Xtrain.shape[1]
    num_class = ytrain.shape[1]

    with tf.Session() as sess:

    model = cnn(sizevocab, lengthsequence, num_class, batch_size)




    init_op = tf.compat.v1.global_variables_initializer()
    sess.run(init_op)
    saver = tf.compat.v1.train.Saver(tf.global_variables())
    trainBatch = batch_iter(Xtrain, ytrain, batch_size, epoch)



    for batch in trainBatch:

        Xbatch,Ybatch = batch


        train_dict = {model.x: Xbatch, model.y:Ybatch, model.keep_drop: 0.5}

        _, step, loss, accuracy = sess.run([model.trainop, model.global_step, model.loss, model.accuracy], feed_dict = train_dict)

        print("step {0}: loss = {1} accuracy = {2}".format(step, loss, accuracy))

        # current_step = tf.train.global_step(sess, model.global_step)

        if step % 300 == 0:
            feed_dict = {model.x: xtest, model.y: ytest, model.keep_drop: 1.0}

            print("Evaluate : ")
            accuracy, loss= sess.run([model.accuracy, model.loss], feed_dict = feed_dict)
            # train_accuracy = sess.run(model.accuracy, feed_dict = feed_dict)


            print("step {0}: accuracy = {1} loss = {2}".format(step, accuracy, loss))

学习的结果如下:


step 11690: loss = [0.9972683] accuracy = 0.3134765625

step 11691: loss = [1.0026996] accuracy = 0.1650390625

step 11692: loss = [1.0028301] accuracy = 0.1689453125

step 11693: loss = [0.9968007] accuracy = 0.3642578125

step 11694: loss = [0.9940929] accuracy = 0.3857421875

step 11695: loss = [1.0010462] accuracy = 0.22265625

step 11696: loss = [1.0015349] accuracy = 0.201171875

step 11697: loss = [0.99722] accuracy = 0.3349609375

step 11698: loss = [0.99936765] accuracy = 0.2724609375

step 11699: loss = [1.0001634] accuracy = 0.21875

step 11700: loss = [0.9981262] accuracy = 0.2783203125

Evaluate : 
step 11700: accuracy = 0.254451721906662 loss = [0.9997652]

0 个答案:

没有答案