简体   繁体   中英

Errors related to data type and input shape when building a sequential model

I'm doing a little experiment to understand how a sequential model is built.

I have a numpy array with the shape of (10, 10, 5), call it feature_0 . And I created my sequential model as below:

model = tf.keras.models.Sequential([
    layers.Dense(units=16, input_shape=(10, 5)),
    layers.Dense(units=8),
    layers.Dense(units=1)
])
model(features_0)
model.summary()

This returns a model summary to me as shown below:

Model: "sequential_16"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_48 (Dense)             (None, 10, 16)            96        
_________________________________________________________________
dense_49 (Dense)             (None, 10, 8)             136       
_________________________________________________________________
dense_50 (Dense)             (None, 10, 1)             9         
=================================================================
Total params: 241
Trainable params: 241
Non-trainable params: 0
_________________________________________________________________

This is what I expected. I know I don't need to pass features_0 to my model to see the summary because the input shape has been specified. However, when I tried this, it gave me an error:

model = tf.keras.models.Sequential([
    layers.Dense(units=16),
    layers.Dense(units=8),
    layers.Dense(units=1)
])
model(features_0)
model.summary()

I simply removed the input shape in the first hidden layer. What I am expecting to see is that it will return a model summary to me and the output shape will just be multiple since the input shape is not given. Instead, I got the error below:

InvalidArgumentError: cannot compute MatMul as input #1(zero-based) was expected to be a int64 tensor 
but is a float tensor [Op:MatMul]

What's the difference between the first and second ways I build the model? Should I always specify input shape? It seems have something to do with the data type. As the error suggested, the model is expecting integer inputs, but my feature_0 is a numpy array of integers with the shape of (10, 10, 5). I created the numpy array like this:

features_0 = np.random.randint(100, size=(10, 10, 5))

Thanks for the help.

I have resolved this by converting 'int64' to 'float64' as follows:

features_1 = features_0.astype('float32')

model = tf.keras.models.Sequential([
    layers.Dense(units=16),
    layers.Dense(units=8),
    layers.Dense(units=1)
])
model(features_1)
model.summary()

It returned a model summary as anticipated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM