我也是这个问题,训练的时候训练集和验证集上得到的错误率很低,然而模型保存后,再加载预测相同的训练集和验证集,错误率就比在训练时得到的错误率高很多,有没有大佬指点一下
神经网络故障分类,训练过程中训练集测试集准确率都很高,但是保存完模型后再载入预测时准确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条回答
为你推荐
- Darknet yolov3训练数据,为何载入迭代200次的数据开始训练控制台却显示从100次开始运行?
- 深度学习
- 1个回答
- 新手提问:VS2017下,不同字符集树控件载入图标时异常
- 新手提问:MFC项目载入图标时关于fxGetApp()->LoadIcon遇到的问题
- html页面在IDEA中可以得到图片,但是在浏览器中打开html就显示不出来
- 运行mixmatch源码CIFAR10数据集时报错AttributeError: 'CIFAR10' object has no attribute 'targets',是怎么回事?
- 机器学习
- 深度学习
- tensorflow
- 1个回答
- Codeigniter - 在我的库中加载模型
- php
- 3个回答
- 找到在浏览器中出现“Not Found”错误的图像URL,但是它们实际存在
- php
- 2个回答
- 浏览器在PHP中重新加载时自动插入以前的数据
- vb.net webbrowser 载入高德地图后,无法使用3D,如何处理呢?
- visual studio
- 1个回答
- jsp载入完成后如何自动调用后台?
- 0个回答
- bp神经网络怎么实际应用啊?Python
- c# Bitmap载入图片后图片不能载入问题
- c#
- 3个回答
- chrome的tab都载入完毕,status却是loading(附代码)
- chrome
- javascript
- 2个回答
- chrome,为何明明tab都载入完整了,它的status却永远是loading?
- chrome
- javascript
- 3个回答
- tensorflow模型载入失败
- 机器学习
- 神经网络
- tensorflow
- 3个回答
- Android动态载入布局中使用适配器,不显示
- android
- 4个回答
- C# 加载窗体F时显示load 窗体F设置的大小,按钮A后显示原来的大小
- c#
- 1个回答
- vb6.0 在窗体载入时for语句没有被执行
- 1个回答
- vs2008中使用c+_+中载入system.core.dll失败?
- 1个回答
- WebForm 加入 Routing 後,.js 文件都无法载入
- 2个回答