简体   繁体   English

我的神经网络 model 准确率始终为 50%

[英]my neural network model accuracy is always 50%

After training my model, the test accuracy is always 50%.训练我的 model 后,测试准确率始终为 50%。 What is wrong in my code below?我下面的代码有什么问题?

0~4000 normal signal data, 4001~6000 abnormal signal data for binary classification. 0~4000个正常信号数据,4001~6000个异常信号数据进行二分类。 data dimension is (6000, 8000)数据维度为 (6000, 8000)

data = np.load('data.npy') 
label = []
for i in range(len(data)): ## labeling
    if i < 4000:
        label.append(1)
    else:
        label.append(0)

label = np.array(label)

## each 100 data was extracted for test
test_data =  np.concatenate((data[:100], data[4001:4101]), axis=0)  
test_label = np.concatenate((label[:100], label[4001:4101]), axis=0)
train_data = np.concatenate((data[100:4001], data[4101:]))
train_label = np.concatenate((label[100:4001], label[4101:]))

## data shuffleing
tmp = [[x,y]for x, y in zip(train_data, train_label)]
tmp1 = [[x,y]for x, y in zip(test_data, test_label)]
random.shuffle(tmp)
random.shuffle(tmp1) 
train_data = [n[0] for n in tmp]
train_label = [n[1] for n in tmp]
train_data = np.array(train_data)
train_label = np.array(train_label)
teet_data = [n[0] for n in tmp1]
test_label = [n[1] for n in tmp1]
test_data = np.array(test_data)
test_label = np.array(test_label)

## scaling
mean = train_data.mean(axis=0)
std = train_data.std(axis=0)

train_data -= mean
train_data /= std
test_data -= mean
test_data /= std

model = models.Sequential()
model.add(layers.Dense(128, activation='relu', input_shape=(8000,)))
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

model.compile(optimizer='Adam',
             loss='binary_crossentropy',
             metrics=['acc'])

history = model.fit(train_data,
                    train_label,
                    epochs=60,
                    batch_size=128,
                    shuffle=True,
                    validation_split=0.2)

loss curve损失曲线

在此处输入图像描述

loss, acc = model.evaluate(test_data, test_label)

200/200 [==============================] - 0s 140us/step 200/200 [===============================] - 0s 140us/步

print(acc)

0.5 0.5

It seems very likely that your model is predicting only one class for your test data.您的 model 似乎很可能只为您的测试数据预测一个 class。

It may be caused by your feature scaling approach.这可能是由您的特征缩放方法引起的。 You should standardize your test data from the statistics extracted from the training set.您应该根据从训练集中提取的统计数据对测试数据进行标准化。

your model is too weak/small for that amount of features.你的 model 对于这么多的功能来说太弱/太小了。 just in your first layer, you are destroying all the information by converting 8000 features into 8,.就在您的第一层,您通过将 8000 个特征转换为 8 个特征来破坏所有信息。 use more units.使用更多的单位。 a lot more than that and let it learn something instead of destroying your dataset.不仅如此,让它学习一些东西,而不是破坏你的数据集。 your model is not able to predict any better than random right now.您的 model 现在无法比随机预测更好。

Here is my signal data.这是我的信号数据。

import sounddevice as sd
import numpy as np
from math import pi

fs = 4000

n = np.arange(0, 2, 1/fs)

f = 13000 # x 
f1 = 1310 # x1
f2 = 175  # x 2
f3 = 45 # x3
'''
(8000,)
'''

x = np.sin(2*pi*f*n)
x1 = np.sin(2*pi*f1*n)
x2 = np.sin(2*pi*f2*n)
y = np.random.rand(len(x))
x3 = np.sin(2*pi*f3*n)
y = np.random.rand(len(x))

fault =  y*0.2 + (x1+x2 + x3) + 0.15
normal =  y*0.2 +(x1 + x2) +2

y = np.random.rand(len(x))
normal = normal
normal.shape
fault.shape

(8000,) (8000,)

normal_data=[]
for i in range (4000):
    y = np.random.rand(len(x))
    normal = 2*y*(x1 + x2)
    normal_data.append(normal)

normal_data = np.array(normal_data)
normal_data.shape

(4000, 8000) (4000, 8000)

fault_data=[]
for i in range (2000):
    y = np.random.rand(len(x))
    fault = 2*y*(x1 + x2)
    fault_data.append(fault)

fault_data = np.array(fault_data)
fault_data.shape

(2000, 8000) (2000, 8000)

## Final signal data
data = np.concatenate((normal_data, fault_data))
data.shape

(6000, 8000) (6000, 8000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM