薪水求解-----keras训练但是精度和损失没有任何变化

机器学习
新手专区
标签: #<Tag:0x00007fc071710910> #<Tag:0x00007fc071710668>

(goldne) #1
# 导入包
import numpy as np
from numpy.random import*
#准备数据集
data=normal(1,0.25,(60000,2))
train_data=data[:50000]
test_data=data[-5000:]
val_data=data[50000:-5000]
train_label=train_data[:,0]+train_data[:,1]>2
test_label=test_data[:,0]+test_data[:,1]>2
val_label=val_data[:,0]+val_data[:,1]>2
#定义训练
from keras import models
from keras import layers
from keras import losses 
from keras import metrics
from keras import optimizers
def build_model():
    model=models.Sequential()
    model.add(layers.Dense(16,activation='relu',input_shape=(2,)))
    model.add(layers.Dense(1,activation='softmax'))
    model.compile(optimizer=optimizers.RMSprop(lr=0.00001),loss='binary_crossentropy',metrics=['accuracy'])
    return model
model=build_model()
all_acc=[]
all_loss=[]
epochs=2
for i in range(epochs):
    history=model.fit(train_data,train_label,epochs=5,batch_size=6000,validation_data=(val_data,val_label))
#     print(model.get_weights())
    all_acc.append(history.history['val_acc'])
    all_loss.append(history.history['val_loss'])

Train on 50000 samples, validate on 5000 samples
Epoch 1/5
50000/50000 [==============================] - 0s 1us/step - loss: 7.9224 - acc: 0.5031 - val_loss: 7.9680 - val_acc: 0.5002
Epoch 2/5
50000/50000 [==============================] - 0s 1us/step - loss: 7.9224 - acc: 0.5031 - val_loss: 7.9680 - val_acc: 0.5002
Epoch 3/5
50000/50000 [==============================] - 0s 1us/step - loss: 7.9224 - acc: 0.5031 - val_loss: 7.9680 - val_acc: 0.5002
Epoch 4/5
50000/50000 [==============================] - 0s 1us/step - loss: 7.9224 - acc: 0.5031 - val_loss: 7.9680 - val_acc: 0.5002
Epoch 5/5
50000/50000 [==============================] - 0s 1us/step - loss: 7.9224 - acc: 0.5031 - val_loss: 7.9680 - val_acc: 0.5002
Train on 50000 samples, validate on 5000 samples
Epoch 1/5
50000/50000 [==============================] - 0s 1us/step - loss: 7.9224 - acc: 0.5031 - val_loss: 7.9680 - val_acc: 0.5002
Epoch 2/5
50000/50000 [==============================] - 0s 1us/step - loss: 7.9224 - acc: 0.5031 - val_loss: 7.9680 - val_acc: 0.5002
Epoch 3/5
50000/50000 [==============================] - 0s 1us/step - loss: 7.9224 - acc: 0.5031 - val_loss: 7.9680 - val_acc: 0.5002
Epoch 4/5
50000/50000 [==============================] - 0s 1us/step - loss: 7.9224 - acc: 0.5031 - val_loss: 7.9680 - val_acc: 0.5002
Epoch 5/5
50000/50000 [==============================] - 0s 1us/step - loss: 7.9224 - acc: 0.5031 - val_loss: 7.9680 - val_acc: 0.5002

如上所示,训练的参数根本没有发生变化,所以loss和acc都没有变,但我不知道是那里出错了


(fsm) #2

梯度爆炸,或者梯度消失


(goldne) #3

你能再看看吗,我现在把代码贴上去了


(iQuant) #4

可以将完整策略分享出来,我们帮您看一下。


(goldne) #5

这就是全部的代码了,完整策略是什么意思?


(lu0817) #6

试一下lr=0.001


(达达) #7

试试最后一层用sigmoid吧 softmax经常是多分类问题


(goldne) #8

我的问题解决了,是最后一层没有使用sigmoid的问题,谢谢