creazy_asp_net
creazy_asp_net
采纳率0%
2020-06-27 18:21

神经网络故障分类,训练过程中训练集测试集准确率都很高,但是保存完模型后再载入预测时准确ji率很低怎么办?

这个准确率极低,基本上就算不上有准确的说法了。加载模型主要是对故障进行分类,输出故障标签,输入的时候用的是测试集。
下面是我加载模型测试的代码,求大家帮忙指点

import csv
import numpy as np
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
import pandas as pd

import random
import xlwt as w
from sklearn.preprocessing import OneHotEncoder

def generatebatch(X,Y,n_examples, batch_size):
    for batch_i in range(n_examples // batch_size):
        start = batch_i*batch_size
        end = start + batch_size
        batch_xs = X[start:end]
        batch_ys = Y[start:end]
        yield batch_xs, batch_ys       # 生成每一个batch
    # #
def weight_variable(shape):           #权重
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):           #偏执
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
     return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME")

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")

def prediction(pic):
    # 每个批次大小
    batch_size = 128
    # #重写神经网络
    tx = tf.placeholder(tf.float32, [None, 512])
    #tx=tf.reshape(tx,(-1,512))  #更改数据结构w
    ty = tf.placeholder(tf.float32, [None, 40])
    keep_prob = tf.placeholder(tf.float32)
    x_image = tf.reshape(tx, [-1, 512, 1, 1])
    # #
    # #第一层,后面类似,不多赘述
    W_conv1 = weight_variable([6, 1, 1, 32])
    b_conv1 = bias_variable([32])
    h_conv1 = tf.nn.relu(tf.nn.conv2d(x_image, W_conv1,strides=[1,4,4,1], padding="SAME") + b_conv1)
    # BN归一化层+激活层
    batch_mean, batch_var = tf.nn.moments(h_conv1, [0, 1, 2], keep_dims=True)
    shift = tf.Variable(tf.zeros([32]))
    scale = tf.Variable(tf.ones([32]))
    epsilon = 1e-3
    BN_out1 = tf.nn.batch_normalization(h_conv1, batch_mean, batch_var, shift, scale, epsilon)
    relu_BN_maps1 = tf.nn.relu(BN_out1)
    h_pool1 =  max_pool_2x2(relu_BN_maps1)

            #全连接层
    W_fc1 = weight_variable([8*64, 256])     ##  modify
    b_fc1 = bias_variable([256])
    #
    h_pool2_flat = tf.reshape(h_pool4, [-1,8*64])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
    #

    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
    # BN归一化层+激活层
    batch_mean, batch_var = tf.nn.moments(h_fc1_drop, [0, 1], keep_dims=True)
    shift = tf.Variable(tf.zeros([256]))
    scale = tf.Variable(tf.ones([256]))
    epsilon = 1e-3
    BN_out9 = tf.nn.batch_normalization(h_fc1_drop, batch_mean, batch_var, shift, scale, epsilon)
    relu_BN_maps9 = tf.nn.relu(BN_out9)
    #
    W_fc3 = weight_variable([256, 40])
    b_fc3 = bias_variable([40])
    FC3=tf.matmul(relu_BN_maps9, W_fc3) + b_fc3
    #
    prediction = tf.nn.softmax(FC3,name='prediction')
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=ty, logits=prediction))
    #
    train_step = tf.train.AdamOptimizer(1e-3).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(ty, 1))
    res = tf.argmax(prediction, 1)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    # #
    saver=tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        saver.restore(sess, './model2')#模型加载
        print(sess.run(res, feed_dict={tx: pic, keep_prob:1.0}))
        print('-----------------------------')

def img2data(path):#数据集的读取
    linest1=[]
    with open(path) as file:
        datas=csv.reader(file)
        for line in datas:
            linest1.append(line)
    linest2=np.array(linest1)
    linest3=np.delete(linest2,0,axis=0)#数据集的处理
    #linest3=np.delete(linest3,0,axis=1)
    linest4=np.delete(linest3,512,axis=1)
    linest5=linest4.astype(np.float32)
    xst1=linest5
    xst1=np.reshape(xst1,(-1,512))#数据shape的转换
    return xst1

picture = img2data('d:/bbb.csv')#数据位置
prediction(picture)



我现在不知道到底哪里出了问题,运行可以出现结果,就是结果不准确,原代码准确率挺高的, 求求大家救救孩子吧

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • shiguagnmanbu shiguagnmanbu 6月前

    我也是这个问题,训练的时候训练集和验证集上得到的错误率很低,然而模型保存后,再加载预测相同的训练集和验证集,错误率就比在训练时得到的错误率高很多,有没有大佬指点一下

    点赞 评论 复制链接分享
  • qq_45041871 Trouble.. 1月前

    我也是,请问您解决了吗

     

    点赞 评论 复制链接分享
  • qq_45531360 夏木与森秋 9月前

    我也出现了这个问题,我训练时的损失很小,验证集的损失也很小,但是当我用一张图片做测试时,出现的结果根本不是我想要的

    点赞 评论 复制链接分享
  • irischenhe 阿歆。 1月前

    我也一样,请问您解决了吗

    点赞 评论 复制链接分享

为你推荐