我的神经网络模型精度始终为50%

时间:2020-04-16 15:54:25

标签: python tensorflow machine-learning keras

训练完模型后,测试准确性始终为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)

损耗曲线

enter image description here

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

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

print(acc)

0.5

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这是我的信号数据。

@IBAction func guitarSwitch(_ sender: Any) {
    if self.guitarPlay.isOn == false
    {
    print("Top: \(instrumentCounter)")
    instrumentCounter -= 1
    print("Top: \(instrumentCounter)")
    }
    else if self.guitarPlay.isOn && instrumentCounter == 1
        {
            self.guitarPlay.setOn(false, animated: true)
            showTooManyAlert()
            print("Middle: \(instrumentCounter)")
        }
            else{
                instrumentCounter += 1
                print(instrumentCounter)
            }
}

(8000,)

func showTooManyAlert(){
    let alertController = UIAlertController(title: "Alert", message:
        "You can only select one instrument. Unselect another instrument to select this one.", preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Okay", style: .default))

    self.present(alertController, animated: true, completion: nil)
}

(4000,8000)

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

(2000,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

(6000,8000)