parameters: (model_type,
                  vocabulary_size,
                  embedding_size,
                  x_train,
                  y_train,
                  x_test,
                  y_test,
                  epochs,
                  batch_size)
return_statement: return model
docstring: Train a model for IMDB sentiment classification.
  
    Args:
      model_type: Type of the model to train, as a `str`.
      vocabulary_size: Vocabulary size.
      embedding_size: Embedding dimensions.
      x_train: An int array of shape `(num_examples, max_len)`: index-encoded
        sentences.
      y_train: An int array of shape `(num_examples,)`: labels for the sentences.
      x_test: Same as `x_train`, but for test.
      y_test: Same as `y_train`, but for test.
      epochs: Number of epochs to train the model for.
      batch_size: Batch size to use during trainng.
  
    Returns:
      The trained model instance.
  
    Raises:
      ValueError: on invalid model type.
docstring_summary: Train a model for IMDB sentiment classification.
docstring_tokens: Train a model for IMDB sentiment classification .
function: def train_model(model_type,
                  vocabulary_size,
                  embedding_size,
                  x_train,
                  y_train,
                  x_test,
                  y_test,
                  epochs,
                  batch_size):
    """Train a model for IMDB sentiment classification.
  
    Args:
      model_type: Type of the model to train, as a `str`.
      vocabulary_size: Vocabulary size.
      embedding_size: Embedding dimensions.
      x_train: An int array of shape `(num_examples, max_len)`: index-encoded
        sentences.
      y_train: An int array of shape `(num_examples,)`: labels for the sentences.
      x_test: Same as `x_train`, but for test.
      y_test: Same as `y_train`, but for test.
      epochs: Number of epochs to train the model for.
      batch_size: Batch size to use during trainng.
  
    Returns:
      The trained model instance.
  
    Raises:
      ValueError: on invalid model type.
    """
  
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Embedding(vocabulary_size, embedding_size))
    if model_type == 'bidirectional_lstm':
      # TODO(cais): Uncomment the following once bug b/74429960 is fixed.
      # model.add(tf.keras.layers.Embedding(
      #     vocabulary_size, 128, input_length=maxlen))
      # model.add(tf.keras.layers.Bidirectional(
      #     tf.keras.layers.LSTM(64))
      # model.add(tf.keras.layers.Dropout(0.5))
      raise NotImplementedError()
    elif model_type == 'cnn':
      model.add(tf.keras.layers.Dropout(0.2))
      model.add(tf.keras.layers.Conv1D(250,
                                    3,
                                    padding='valid',
                                    activation='relu',
                                    strides=1))
      model.add(tf.keras.layers.GlobalMaxPooling1D())
      model.add(tf.keras.layers.Dense(250, activation='relu'))
    elif model_type == 'lstm':
      model.add(tf.keras.layers.LSTM(128))
    else:
      raise ValueError("Invalid model type: '%s'" % model_type)
    model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
  
    model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
    model.fit(x_train, y_train,
              batch_size=batch_size,
              epochs=epochs,
              validation_data=[x_test, y_test])
    return model
function_tokens: def train_model ( model_type , vocabulary_size , embedding_size , x_train , y_train , x_test , y_test , epochs , batch_size ) : model = tf . keras . Sequential ( ) model . add ( tf . keras . layers . Embedding ( vocabulary_size , embedding_size ) ) if model_type == 'bidirectional_lstm' : # TODO(cais): Uncomment the following once bug b/74429960 is fixed. # model.add(tf.keras.layers.Embedding( #     vocabulary_size, 128, input_length=maxlen)) # model.add(tf.keras.layers.Bidirectional( #     tf.keras.layers.LSTM(64)) # model.add(tf.keras.layers.Dropout(0.5)) raise NotImplementedError ( ) elif model_type == 'cnn' : model . add ( tf . keras . layers . Dropout ( 0.2 ) ) model . add ( tf . keras . layers . Conv1D ( 250 , 3 , padding = 'valid' , activation = 'relu' , strides = 1 ) ) model . add ( tf . keras . layers . GlobalMaxPooling1D ( ) ) model . add ( tf . keras . layers . Dense ( 250 , activation = 'relu' ) ) elif model_type == 'lstm' : model . add ( tf . keras . layers . LSTM ( 128 ) ) else : raise ValueError ( "Invalid model type: '%s'" % model_type ) model . add ( tf . keras . layers . Dense ( 1 , activation = 'sigmoid' ) ) model . compile ( 'adam' , 'binary_crossentropy' , metrics = [ 'accuracy' ] ) model . fit ( x_train , y_train , batch_size = batch_size , epochs = epochs , validation_data = [ x_test , y_test ] ) return model