Keras:model.evaluate()的结果在所有权重和偏差均为0的情况下保持较高

时间:2019-03-01 13:25:12

标签: tensorflow keras evaluate

我使用Keras应用程序(TensorFlow后端)创建了VGG16模型。然后,我想更改这些权重的一部分,然后测试此修改后模型的准确性。为直观起见,我将所有图层的所有权重和偏差更改为0,如下所示:

model = VGG16(weights='imagenet', include_top=True)

# here is the test data and label containing 10 pictures I created.
data = np.load('./10_random_samples_array.npz')
data, label = data["X"], data["Y"]

# Modify the weights to zero
for z in [1, 2, 4, 5, 7, 8, 9, 11, 12, 13, 15, 16, 17]:     # Conv layers
    weight_bias = model.layers[z].get_weights()
    shape_weight = np.shape(weight_bias[0])
    shape_bias = np.shape(weight_bias[1])
    weight_bias[0] = np.zeros(shape=(shape_weight[0],shape_weight[1],shape_weight[2],shape_weight[3]))
    weight_bias[1] = np.zeros(shape=(shape_bias[0],))
    model.layers[z].set_weights(weight_bias)

for z in [20,21,22]:    # FC layers
    weight_bias = model.layers[z].get_weights()
    shape_weight = np.shape(weight_bias[0])
    print(z, shape_weight)
    shape_bias = np.shape(weight_bias[1])
    weight_bias[0] = np.zeros(shape=(shape_weight[0],shape_weight[1],))
    weight_bias[1] = np.zeros(shape=(shape_bias[0],))
    model.layers[z].set_weights(weight_bias)

model.compile(loss='binary_crossentropy',
              optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
              metrics=['accuracy'])

# To check if the weights have been modified.
print(model.layers[1].get_weights())

loss, acc = model.evaluate(data, label, verbose=1)
print(acc)

然后我得到这样的结果:

[array([[[[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]],

        ...(All zero, I omit them)

        [[0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.],
         [0., 0., 0., ..., 0., 0., 0.]]]], dtype=float32), 
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)]

10/10 [==============================] - 2s 196ms/step
0.9989999532699585

首先,您可以确定所有权重和偏差已更改为0,但精度仍然保持很高。这是不合理的。(model.evaluate()返回的原始结果是0.9993000030517578)

第二,我仅使用10张图片作为测试数据集。结果必须是十进制数,该点后只能有一位数字。但是我得到了0.9989999532699585。

我还尝试仅将Conv1-1中的所有权重修改为零,结果也为0.9989999532699585。看来这是最低要求。我的模型出问题了吗?还是不能以这种方式修改权重?还是model.evaluate()不能正常工作?

0 个答案:

没有答案