Home > Software design >  keras - Difference between model predict function and model API function
keras - Difference between model predict function and model API function

Time:02-12

I am trying to obtain the outputs and the final hidden states from a SimpleRNN layer in my model. The model has structure:

inp= Input(shape= (input_length,)) 
emb_out= Embedding(input_dim, output_dim, input_length= input_length, 
                         weights= [Emat], trainable= False)(inp)
rnn_out, rnn_state= SimpleRNN(200,  return_state= True)(emb_out)
#out= Dense(1, activation= 'sigmoid')(rnn_out)
model = Model(inputs=inp, outputs=[rnn_out, rnn_state])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
model.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_7 (InputLayer)         [(None, 1403)]            0         
_________________________________________________________________
embedding_10 (Embedding)     (None, 1403, 100)         4348900   
_________________________________________________________________
simple_rnn_9 (SimpleRNN)     [(None, 200), (None, 200) 60200     
=================================================================


I notice that the shape of outputs are different when I use the model to predict test data model.predict(xte), and model(xte) when I fit the test data to the model API. I would like some help interpreting the difference of the 2 operations and what the returned objects refer to.

# 1.
all_outs, fin_states= model.predict(xte_pad)
print('Test data shape:', xte_pad.shape)
print(all_outs.shape, fin_states.shape)
>> Test data shape: (2400, 1403)
(2400, 200) (2400, 200)

# 2.
outs= model(xte_pad)
print('Test data shape:', xte_pad.shape)
print(outs.shape)
>> Test data shape: (2400, 1403)
(2400, 1403, 200)

CodePudding user response:

When doing outs= model(xte_pad), you will get a list of outputs and a list does not have the property shape. The outputs are actually identical:

import tensorflow as tf

input_length = 1403
inp= tf.keras.layers.Input(shape= (input_length,)) 
emb_out= tf.keras.layers.Embedding(500, 100, input_length= input_length, trainable= False)(inp)
rnn_out, rnn_state= tf.keras.layers.SimpleRNN(200,  return_state= True)(emb_out)

model = tf.keras.Model(inputs=inp, outputs=[rnn_out, rnn_state])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
model.summary()

xte_pad = tf.random.uniform((10, 1403), maxval=500, dtype=tf.int32)
all_outs, fin_states= model.predict(xte_pad)
print('Test data shape:', xte_pad.shape)
print(all_outs.shape, fin_states.shape)


all_outs, fin_states = model(xte_pad)
print('Test data shape:', xte_pad.shape)
print(all_outs.shape, fin_states.shape)
Test data shape: (10, 1403)
(10, 200) (10, 200)
Test data shape: (10, 1403)
(10, 200) (10, 200)
  • Related