RESET爱AI 2022-06-23 10:45 采纳率: 0%
浏览 208

训练过程中损失率和准确率几乎不变且很低

问题:训练过程中损失率和准确率几乎不变且很低
代码

tf1.disable_v2_behavior()
import pandas as pd
import time
import tensorflow as tf2

import sys
import scipy.io as sc
import numpy as np
from matplotlib import pyplot as plt
from sklearn import preprocessing

Data loading
feature = sc.loadmat('eegdata_mat.mat')#读取mat文件的数据
all = feature['data']

np.random.shuffle(all) mix eeg_all,将序列所有的元素随机排列,序列中的每个元素是个列表,列表重新排列

分割特征和标签
final=7160 #除四1790
all=all[0:final]
print ("all.shape",all.shape)
feature_all =all[:,1:41]
print(feature_all)
label=all[:,0:1]
print(label)

feature_all = preprocessing.scale(feature_all)
no_fea=feature_all.shape[-1] #feature_all.shape=40
from sklearn.preprocessing import OneHotEncoder
enc = OneHotEncoder(sparse=False)
label_all = enc.fit_transform(label.reshape(-1, 1))
print(label_all.shape)
#label_all = to_categorical(label)
print(label_all)
print ("label_all.shape",label_all.shape)

n_classes=6

CNN code,
feature_all=feature_all # the input data of CNN
print("cnn input feature shape", feature_all.shape)
n_fea=feature_all.shape[-1]
label_all=one_hot(label_all)

划分训练集和测试集
final=all.shape[0]
middle_number=final*3/4
middle_number=int(middle_number)
feature_training =feature_all[0:middle_number]
feature_testing =feature_all[middle_number:final]
label_training =label_all[0:middle_number]
label_testing =label_all[middle_number:final]

label_ww=label_all[middle_number:final] # for the confusion matrix
print ("label_testing",label_testing.shape)
a=feature_training
b=feature_testing
print("feature_training.shape",feature_training.shape)
print("feature_training.shape",feature_testing.shape)

keep=1
batch_size=final-middle_number
n_group=3
train_fea=[]
for i in range(n_group):
f =a[(0+batch_sizei):(batch_size+batch_sizei)]
train_fea.append(f)

print ("train_fea[0].shape:",train_fea[0].shape)

train_label=[]
for i in range(n_group):
f =label_training[(0+batch_sizei):(batch_size+batch_sizei), :]
train_label.append(f)
print ("train_label[0].shape:",train_label[0].shape)

the CNN code
def compute_accuracy(v_xs, v_ys):
全局变量
global prediction
生成预测值,也就是每个数字的概率
y_pre = sess3.run(prediction, feed_dict={xs: v_xs, keep_prob: keep})
对比预测数据标签和真实值是否相等
correct_prediction = tf1.equal(tf1.argmax(y_pre,1), tf1.argmax(v_ys,1))
计算正确预测的个数
accuracy = tf1.reduce_mean(tf1.cast(correct_prediction, tf1.float32))
result = sess3.run(accuracy, feed_dict={xs: v_xs, ys: v_ys, keep_prob: keep})
return result

def weight_variable(shape):
initial = tf1.truncated_normal(shape,stddev=0.1)
初始化参数矩阵w:生成截断式正态分布的随机数,(维度,mean=均值,stddev=标准差)
return tf1.Variable(initial)
tf11.Variable () 将变量标记为“可训练”,被标记的变量会在反向传播 中记录梯度信息

def bias_variable(shape):
initial = tf1.constant(0.1, shape=shape)
初始化参数矩阵b,tf11.constant(value,dtype=None,shape=None,name='Const',verify_shape=False)
指定了第三个参数,当第一个参数value是数字时,张量的所有元素都会用该数字填充:
return tf1.Variable(initial)
tf11.Variable () 将变量标记为“可训练”,被标记的变量会在反向传播 中记录梯度信息

def conv2d(x, W):
stride [1, x_movement, y_movement, 1]
Must have strides[0] = strides[3] = 1
return tf1.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') #四个元素规定前后必须为1,中间两个数表示水平滑动和垂直滑动步长值

def max_pool_1x2(x):
stride [1, x_movement, y_movement, 1]
return tf1.nn.max_pool(x, ksize=[1,1,2,1], strides=[1,1,2,1], padding='SAME')

define placeholder for inputs to network
tf11.placeholder(dtype,shape=None,name=None)
xs = tf1.placeholder(tf1.float32, [None, n_fea])#shape:(?,64);占位
ys = tf1.placeholder(tf1.float32, [None, n_classes])#shape:(?,6)
keep_prob = tf1.placeholder(tf1.float32)
x_image = tf1.reshape(xs, [-1, 1, n_fea, 1]) #shape:(?,1,64,1)
print("x_image.shape",x_image.shape) # [n_samples, 28,28,1]
print("x_image",x_image)

conv1 layer
W_conv1 = weight_variable([1,1,1,20]) #shape=(1, 1, 1, 20)
b_conv1 = bias_variable([20]) #shape=(20,)
h_conv1 = tf1.nn.leaky_relu(tf1.layers.batch_normalization((conv2d(x_image, W_conv1) + b_conv1),training=True)) #shape=(?, 1, 64, 20)
h_pool1 = max_pool_1x2(h_conv1) #shape=(?, 1, 32, 20)
h_conv1_drop = tf1.nn.dropout(h_pool1,keep_prob,noise_shape=[tf1.shape(h_pool1)[0],1,1,tf1.shape(h_pool1)[3]])

fc1 layer
展平
h_pool2_flat = tf1.reshape(h_conv1_drop, [-1, int(1*(n_fea/2)20)])
W_fc1 = weight_variable([int(1
(n_fea/2)*20), 120])
b_fc1 = bias_variable([120])
h_fc1 = tf1.nn.sigmoid(tf1.layers.batch_normalization(tf1.matmul(h_pool2_flat, W_fc1) + b_fc1,training=True))
h_fc1_drop = tf1.nn.dropout(h_fc1, rate = 1-keep_prob)

fc2 layer
W_fc2 = weight_variable([120, n_classes])
b_fc2 = bias_variable([n_classes])
prediction = tf1.matmul(h_fc1_drop, W_fc2) + b_fc2

the error between prediction and real data
l2 = 0.0001 * sum(tf1.nn.l2_loss(tf1_var) for tf1_var in tf1.trainable_variables())
交叉熵损失函数+L2正则化
cross_entropy = tf1.reduce_mean(tf1.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=ys))+l2
Adam优化器
train_step = tf1.train.AdamOptimizer(0.004).minimize(cross_entropy)

sess3 = tf1.Session()
init = tf1.global_variables_initializer()
sess3.run(init)

np.set_printoptions(threshold=sys.maxsize)
step = 1

画图
figure = plt.figure(1)
axes1 = figure.add_subplot(2,1,1)
step_list = [i for i in range(5,1500,5)]
axes1.set_ylabel("accuracy")
axes2 = figure.add_subplot(2,1,2)
axes2.set_xlabel("step")
axes2.set_ylabel("cost")

acc_cnn_t_list = []
cost_list1 = []
while step < 1500:
for i in range(n_group):
sess3.run(train_step, feed_dict={xs: train_fea[i], ys: train_label[i], keep_prob:keep})
if step % 5 == 0:
cost=sess3.run(cross_entropy, feed_dict={xs: b, ys: label_testing, keep_prob: keep})
acc_cnn_t = compute_accuracy(b, label_testing)
cost_list1.append(cost)
acc_cnn_t_list.append(acc_cnn_t)
print('the step is:',step,',the acc is',acc_cnn_t,', the cost is', cost)
step+=1
time1=time.clock()
axes1.plot(step_list,acc_cnn_t_list)
axes2.plot(step_list,cost_list1)
figure.show()

打印出最大的准确率
max_value = None
for num in acc_cnn_t_list:
if (max_value is None or num > max_value):
max_value = num
print('Maximum cnn_accuracy:', max_value)

acc_cnn=compute_accuracy(b, label_testing)
time2=time.perf_counter()
feature_all_cnn;得到cnn提取出来的特征
feature_all_cnn=sess3.run(h_fc1_drop, feed_dict={xs: feature_all, keep_prob: keep})
the shape of cnn output features: (28000, 64) (28000, 6)
print ("the shape of cnn output features:",feature_all.shape,label_all.shape)
time3=time.perf_counter()

运行结果及报错内容 :

the step is: 5 ,the acc is 0.49162012 , the cost is 1.4526145
the step is: 10 ,the acc is 0.49162012 , the cost is 1.4177663
the step is: 15 ,the acc is 0.49162012 , the cost is 1.4081576
the step is: 20 ,the acc is 0.49162012 , the cost is 1.4059197
the step is: 25 ,the acc is 0.49162012 , the cost is 1.4049208
the step is: 30 ,the acc is 0.49162012 , the cost is 1.4036717
the step is: 35 ,the acc is 0.49162012 , the cost is 1.4030477
the step is: 40 ,the acc is 0.49162012 , the cost is 1.402426
the step is: 45 ,the acc is 0.49162012 , the cost is 1.4018252
the step is: 50 ,the acc is 0.49162012 , the cost is 1.401349
the step is: 55 ,the acc is 0.49162012 , the cost is 1.4006782
the step is: 60 ,the acc is 0.49162012 , the cost is 1.4002885
the step is: 65 ,the acc is 0.49162012 , the cost is 1.3998444
the step is: 70 ,the acc is 0.49162012 , the cost is 1.3998264
the step is: 75 ,the acc is 0.49162012 , the cost is 1.3994607
the step is: 80 ,the acc is 0.49162012 , the cost is 1.3989166
the step is: 85 ,the acc is 0.49162012 , the cost is 1.3991143
the step is: 90 ,the acc is 0.49162012 , the cost is 1.3986534
the step is: 95 ,the acc is 0.49162012 , the cost is 1.3984616

Process finished with exit code -1

我的解答思路和尝试过的方法 :

调节学习率以及batchsize都没有太大改变,还是这样

我想要达到的结果:不知道是不是我的特征提取的数据有问题吗?(数据集是自己造的)还是深度学习的问题,这个代码是一篇ccf b的复现代码复现论文的准确率是很高的,95%差不多,但是用在我的数据上就是问题很大。
  • 写回答

1条回答 默认 最新

  • 大米粥哥哥 2022-06-23 11:17
    关注

    可以再增大学习率试试 看到loss在降了

    评论

报告相同问题?

问题事件

  • 创建了问题 6月23日

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题