Keras模型摘要不正确 [英] Keras model summary incorrect

查看:132
本文介绍了Keras模型摘要不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

data_gen=image.ImageDataGenerator(rotation_range=20,width_shift_range=0.2,height_shift_range=0.2,
                                  zoom_range=0.15,horizontal_flip=False)

iter=data_gen.flow(X_train,Y_train,batch_size=64)

data_gen.flow()需要4级数据矩阵,因此X_train的形状为(60000, 28, 28, 1).在定义模型的体系结构时,我们需要传递相同的形状,即(60000, 28, 28, 1)

data_gen.flow() needs a rank 4 data matrix, so the shape of X_train is(60000, 28, 28, 1). We need to pass the same shape i.e (60000, 28, 28, 1)while defining the architecture of the model as follows;

model=Sequential()
model.add(Dense(units=64,activation='relu',kernel_initializer='he_normal',input_shape=(28,28,1)))
model.add(Flatten())    
model.add(Dense(units=10,activation='relu',kernel_initializer='he_normal'))
model.summary()

model.add(Flatten())用于处理等级2问题.现在问题出在model.summary()上.给出了错误的输出,如下所示;

model.add(Flatten()) was used to handle the rank-2 problem. Now the problem is with model.summary(). It is giving incorrect output as shown below;

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 28, 28, 64)        128       
_________________________________________________________________
flatten_1 (Flatten)          (None, 50176)             0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                501770    
=================================================================
Total params: 501,898
Trainable params: 501,898
Non-trainable params: 0

dense_1 (Dense)Output Shape应该是(None,64)Param #应该是(28*28*64)+64,即50240. dense_2 (Dense)Output Shape是正确的,但Param #应该是(64*10)+10,即650.

The Output Shape for dense_1 (Dense) should be (None,64) and Param # should be (28*28*64)+64 i.e 50240. The Output Shape for dense_2 (Dense) is correct but the Param # should be (64*10)+10i.e 650.

为什么会这样?该如何解决?

Why is this happening and how can this problem be addressed?

推荐答案

摘要不正确. keras Dense层始终在输入的最后一个维度上起作用.

The summary is not incorrect. The keras Dense layer always works on the last dimension of the input.

ref: https://www.tensorflow.org/api_docs /python/tf/keras/layers/Dense

输入形状:

Input shape:

N-D张量,形状为:(batch_size,...,input_dim).最常见的情况是>具有形状(batch_size,input_dim)的2D输入. 输出形状:

N-D tensor with shape: (batch_size, ..., input_dim). The most common situation would > be a 2D input with shape (batch_size, input_dim). Output shape:

具有以下形状的N-D张量:(batch_size,...,单位).例如,对于2D输入 形状(batch_size,input_dim),输出将具有形状(batch_size,单位).

N-D tensor with shape: (batch_size, ..., units). For instance, for a 2D input with shape (batch_size, input_dim), the output would have shape (batch_size, units).

在每个密集层之前,您需要手动应用Flatten()以确保您要传递二维数据.

Before each Dense layer, you need to manually apply Flatten() to make sure you're passing 2-d data.

您所需的output_shape的一种解决方法是:

One work-around for your desired output_shape is:

model=Sequential()
model.add(Dense(units=1,activation='linear', use_bias = False, trainable = False, kernel_initializer=tf.keras.initializers.Ones(),input_shape=(28,28,1)))
model.add(Flatten())
model.add(Dense(units=64,activation='relu'))    
model.add(Dense(units=10,activation='relu',kernel_initializer='he_normal'))
model.summary()

第一层只是一层,使用一层进行了初始化,没有任何偏差,因此它仅将输入乘以一层,然后传递到下一层进行平整.这将从模型中删除不必要的参数.

The first layer is just one layer, initialized with ones, with no bias, so it just multiplies the input with one and passes to the next layer to be flattened. This removes unnecessary parameters from the model.

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 28, 28, 1)         2         
_________________________________________________________________
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 64)                50240     
_________________________________________________________________
dense_2 (Dense)              (None, 10)                650       
=================================================================
Total params: 50,892
Trainable params: 50,892
Non-trainable params: 0

这篇关于Keras模型摘要不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆