【问题标题】:my neural network model accuracy is always 50%我的神经网络模型准确率始终为 50%
【发布时间】:2020-07-29 22:45:20
【问题描述】:

训练我的模型后,测试准确率始终为 50%。我下面的代码有什么问题?

0~4000个正常信号数据,4001~6000个异常信号数据进行二分类。 数据维度为(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, acc = model.evaluate(test_data, test_label)

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

print(acc)

0.5

【问题讨论】:

  • 你有 8000 个特征?
  • 是的,数据形状是 (6000, 8000)

标签: python tensorflow machine-learning keras


【解决方案1】:

您的模型很可能只为您的测试数据预测一个类别。

这可能是由您的特征缩放方法引起的。您应该根据从训练集中提取的统计数据对测试数据进行标准化。

【讨论】:

  • mean = train_data.mean(axis=0) std = train_data.std(axis=0) train_data -= mean train_data /= std test_data -= mean test_data /= std 我像这样改变我的代码,但注意到更好.. 准确率始终为 50%
  • 您能否检查以确保所有测试样本的输入特征都不同?
  • 是的,所有测试样本的输入特征都不同。
【解决方案2】:

对于这么多功能,您的模型太弱/太小。就在您的第一层,您通过将 8000 个特征转换为 8 个特征来破坏所有信息!使用更多的单元,远不止于此,让它学习一些东西,而不是破坏你的数据集。您的模型目前无法比随机预测更好。

【讨论】:

  • 我已经更改了我的代码 little blt。但模型仍然只预测一个值。我猜这个模型的数据缩放有问题......
  • 那你最好检查一下你的数据,也许这是一个没有意义的数据,8000个特征的数据是什么?!
  • 数据是人工生成的信号。你的意思是我需要使用特征提取技术吗?
  • 您的数据没有意义,如果您的信号中有一些独特的模式,那么您一开始所做的任意标记是错误且毫无意义的。这就像有猫和狗的照片,但是为猫图像和狗图像分配 1 的标签,并为猫和狗图像分配 0 的标签。模型到底应该如何学习它们之间的区别?所以问题是你的数据,或者更具体地说是你的标签。您不能只是将标签随机分配给不同的实体并期望产生有意义的东西。
  • 我认为我的信号数据有不同的模式。见下面的代码。
【解决方案3】:

这是我的信号数据。

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,)

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)

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)

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

(6000, 8000)

【讨论】:

    猜你喜欢
    • 2020-08-22
    • 2019-06-13
    • 1970-01-01
    • 2019-08-07
    • 2023-03-31
    • 2020-04-13
    • 2018-02-21
    • 2021-12-12
    • 1970-01-01
    相关资源
    最近更新 更多