建立一个简单的KERAS网络进行分类

时间:2018-08-17 20:36:58

标签: python tensorflow neural-network keras

我正在尝试做一个简单的Keras神经网络,但该模型不合适:

Train on 562 samples, validate on 188 samples
Epoch 1/20
562/562 [==============================] - 1s 1ms/step - loss: 8.1130 - acc: 0.4911 - val_loss: 7.6320 - val_acc: 0.5213
Epoch 2/20
562/562 [==============================] - 0s 298us/step - loss: 8.1130 - acc: 0.4911 - val_loss: 7.6320 - val_acc: 0.5213
Epoch 3/20
562/562 [==============================] - 0s 295us/step - loss: 8.1130 - acc: 0.4911 - val_loss: 7.6320 - val_acc: 0.5213
Epoch 4/20
562/562 [==============================] - 0s 282us/step - loss: 8.1130 - acc: 0.4911 - val_loss: 7.6320 - val_acc: 0.5213
Epoch 5/20
562/562 [==============================] - 0s 289us/step - loss: 8.1130 - acc: 0.4911 - val_loss: 7.6320 - val_acc: 0.5213
Epoch 6/20
562/562 [==============================] - 0s 265us/step - loss: 8.1130 - acc: 0.4911 - val_loss: 7.6320 - val_acc: 0.5213

数据库的结构是这样的CSV文件:

doc venda   img1    img2    v1                  v2                  gt
RG  venda1  img123  img12   [3399, 162675, ...] [3399, 162675, ...] 1

如果img1和im2来自同一类,我打算使用v1和v2向量之间的差异来回答我。

代码:

from sklearn.model_selection import train_test_split
(X_train, X_test, Y_train, Y_test) = train_test_split(train, train_labels, test_size=0.25, random_state=42)
# create the model
model = Sequential()
model.add(Dense(10, activation="relu", input_dim=10, kernel_initializer="uniform"))
model.add(Dense(6, activation="relu", kernel_initializer="uniform"))
model.add(Dense(1, activation='sigmoid'))
print(model.summary())

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
model.fit(
        np.array(X_train), 
        np.array(Y_train), 
        shuffle=True,
        epochs=20, 
        verbose=1,
        batch_size=5,
        validation_data=(np.array(X_test), np.array(Y_test)),
)

我做错了什么?

2 个答案:

答案 0 :(得分:2)

将差向量除以某个常数,以使特征向量在0到1或-1到1的范围内。现在,值太大了,损耗也很高。如果数据正确归一化,网络学习速度会更快。

答案 1 :(得分:0)

我已经成功使用该功能对功能进行了标准化。我完全忘记了为什么我在测试和Val上从训练中使用相同的mu和sigma,但是我很确定我是在Coursera的deep.ai课程中学到的

def normalize_features(dataset):
    mu = np.mean(dataset, axis = 0) # columns
    sigma = np.std(dataset, axis = 0)
    norm_parameters = {'mu': mu,
                'sigma': sigma}
    return (dataset-mu)/(sigma+1e-10), norm_parameters

# Normal X data; using same mu and sigma from test set;

x_train, norm_parameters = normalize_features(x_train)

x_val = (x_val-norm_parameters['mu'])/(norm_parameters['sigma']+1e-10)

x_test = (x_test-norm_parameters['mu'])/(norm_parameters['sigma']+1e-10)