class_docstr: A Convolutional neural network on smiles strings
  
  Reimplementation of the discriminator module in ORGAN [1]_ .
  Originated from [2]_.
  
  This model applies multiple 1D convolutional filters to
  the padded strings, then max-over-time pooling is applied on
  all filters, extracting one feature per filter.  All
  features are concatenated and transformed through several
  hidden layers to form predictions.
  
  This model is initially developed for sentence-level
  classification tasks, with words represented as vectors. In
  this implementation, SMILES strings are dissected into
  characters and transformed to one-hot vectors in a similar
  way. The model can be used for general molecular-level
  classification or regression tasks. It is also used in the
  ORGAN model as discriminator.
  
  Training of the model only requires SMILES strings input,
  all featurized datasets that include SMILES in the `ids`
  attribute are accepted. PDBbind, QM7 and QM7b are not
  supported. To use the model, `build_char_dict` should be
  called first before defining the model to build character
  dict of input dataset, example can be found in
  examples/delaney/delaney_textcnn.py
  
  References
  ----------
  .. [1]  Guimaraes, Gabriel Lima, et al. "Objective-reinforced generative adversarial networks (ORGAN) for sequence generation models." arXiv preprint arXiv:1705.10843 (2017).
  .. [2] Kim, Yoon. "Convolutional neural networks for sentence classification." arXiv preprint arXiv:1408.5882 (2014).
class_docstr_tok: ["A","Convolutional","neural","network","on","smiles","strings","Reimplementation","of","the","discriminator","module","in","ORGAN","[","1","]","_",".","Originated","from","[","2","]","_",".","This","model","applies","multiple","1D","convolutional","filters","to","the","padded","strings",",","then","max-over-time","pooling","is","applied","on","all","filters",",","extracting","one","feature","per","filter",".","All","features","are","concatenated","and","transformed","through","several","hidden","layers","to","form","predictions",".","This","model","is","initially","developed","for","sentence-level","classification","tasks",",","with","words","represented","as","vectors",".","In","this","implementation",",","SMILES","strings","are","dissected","into","characters","and","transformed","to","one-hot","vectors","in","a","similar","way",".","The","model","can","be","used","for","general","molecular-level","classification","or","regression","tasks",".","It","is","also","used","in","the","ORGAN","model","as","discriminator",".","Training","of","the","model","only","requires","SMILES","strings","input",",","all","featurized","datasets","that","include","SMILES","in","the","`","ids","`","attribute","are","accepted",".","PDBbind",",","QM7","and","QM7b","are","not","supported",".","To","use","the","model",",","`","build_char_dict","`","should","be","called","first","before","defining","the","model","to","build","character","dict","of","input","dataset",",","example","can","be","found","in","examples\/delaney\/delaney_textcnn.py","References","--","--","--","--","--","..","[","1","]","Guimaraes",",","Gabriel","Lima",",","et","al",".","``","Objective-reinforced","generative","adversarial","networks","(","ORGAN",")","for","sequence","generation","models",".","''","arXiv","preprint","arXiv:1705.10843","(","2017",")",".","..","[","2","]","Kim",",","Yoon",".","``","Convolutional","neural","networks","for","sentence","classification",".","''","arXiv","preprint","arXiv:1408.5882","(","2014",")","."]
docstr: Parameters
  ----------
  n_tasks: int
      Number of tasks
  char_dict: dict
      Mapping from characters in smiles to integers
  seq_length: int
      Length of sequences(after padding)
  n_embedding: int, optional
      Length of embedding vector
  filter_sizes: list of int, optional
      Properties of filters used in the conv net
  num_filters: list of int, optional
      Properties of filters used in the conv net
  dropout: float, optional
      Dropout rate
  mode: str
      Either "classification" or "regression" for type of model.
docstr_tok: ["Parameters","--","--","--","--","--","n_tasks",":","int","Number","of","tasks","char_dict",":","dict","Mapping","from","characters","in","smiles","to","integers","seq_length",":","int","Length","of","sequences","(","after","padding",")","n_embedding",":","int",",","optional","Length","of","embedding","vector","filter_sizes",":","list","of","int",",","optional","Properties","of","filters","used","in","the","conv","net","num_filters",":","list","of","int",",","optional","Properties","of","filters","used","in","the","conv","net","dropout",":","float",",","optional","Dropout","rate","mode",":","str","Either","``","classification","''","or","``","regression","''","for","type","of","model","."]
code: def __init__(
      self,
      n_tasks,
      char_dict,
      seq_length,
      n_embedding=75,
      kernel_sizes=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20],
      num_filters=[
          100,
          200,
          200,
          200,
          200,
          100,
          100,
          100,
          100,
          100,
          160,
          160,
      ],
      dropout=0.25,
      mode="classification",
      **kwargs,
  ):
      """
      Parameters
      ----------
      n_tasks: int
          Number of tasks
      char_dict: dict
          Mapping from characters in smiles to integers
      seq_length: int
          Length of sequences(after padding)
      n_embedding: int, optional
          Length of embedding vector
      filter_sizes: list of int, optional
          Properties of filters used in the conv net
      num_filters: list of int, optional
          Properties of filters used in the conv net
      dropout: float, optional
          Dropout rate
      mode: str
          Either "classification" or "regression" for type of model.
      """
      self.n_tasks = n_tasks
      self.char_dict = char_dict
      self.seq_length = max(seq_length, max(kernel_sizes))
      self.n_embedding = n_embedding
      self.kernel_sizes = kernel_sizes
      self.num_filters = num_filters
      self.dropout = dropout
      self.mode = mode
  
      # Build the model.
  
      smiles_seqs = Input(shape=(self.seq_length,), dtype=tf.int32)
      # Character embedding
      embedding = layers.DTNNEmbedding(
          n_embedding=self.n_embedding,
          periodic_table_length=len(self.char_dict.keys()) + 1,
      )(smiles_seqs)
      pooled_outputs = []
      conv_layers = []
      for filter_size, num_filter in zip(
          self.kernel_sizes, self.num_filters
      ):
          # Multiple convolutional layers with different filter widths
          conv_layers.append(
              Conv1D(
                  kernel_size=filter_size,
                  filters=num_filter,
                  padding="valid",
              )(embedding)
          )
          # Max-over-time pooling
          reduced = Lambda(lambda x: tf.reduce_max(x, axis=1))(
              conv_layers[-1]
          )
          pooled_outputs.append(reduced)
      # Concat features from all filters(one feature per filter)
      concat_outputs = Concatenate(axis=1)(pooled_outputs)
      dropout = Dropout(rate=self.dropout)(concat_outputs)
      dense = Dense(200, activation=tf.nn.relu)(dropout)
      # Highway layer from https://arxiv.org/pdf/1505.00387.pdf
      gather = layers.Highway()(dense)
  
      if self.mode == "classification":
          logits = Dense(self.n_tasks * 2)(gather)
          logits = Reshape((self.n_tasks, 2))(logits)
          output = Softmax()(logits)
          outputs = [output, logits]
          output_types = ["prediction", "loss"]
          loss = SoftmaxCrossEntropy()
  
      else:
          output = Dense(self.n_tasks * 1)(gather)
          output = Reshape((self.n_tasks, 1))(output)
          outputs = [output]
          output_types = ["prediction"]
          loss = L2Loss()
  
      model = tf.keras.Model(inputs=[smiles_seqs], outputs=outputs)
      super(TextCNNModel, self).__init__(
          model, loss, output_types=output_types, **kwargs
      )
  
file_path: deepchem/deepchem/models/text_cnn.py
imports: import numpy
  import tensorflow
  import copy
  import sys
  from deepchem.metrics import to_one_hot
  from deepchem.models import KerasModel, layers
  from deepchem.models.losses import L2Loss, SoftmaxCrossEntropy
  from tensorflow.keras.layers import Input, Dense, Reshape, Softmax, Dropout, Conv1D, Concatenate, Lambda
  import warnings
label_desc: Use image node_id 1 to create a new TextCNNModel object from inherited base classes: KerasModel with example: obj = TextCNNModel(n_tasks, char_dict, seq_length, n_embedding, kernel_sizes, num_filters, dropout, mode)