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

神经网络故障分类,训练过程中训练集测试集准确率都很高,但是保存完模型后再载入预测时准确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)



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

  • 写回答

6条回答 默认 最新

  • 夏木与森秋 2020-07-01 17:18
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型