code: def test_tf_keras_mnist_cnn(random_seed):
      """This is the basic mnist cnn example from keras."""
  
      tf = pytest.importorskip("tensorflow")
  
      rs = np.random.RandomState(random_seed)
      tf.compat.v1.random.set_random_seed(random_seed)
  
      from tensorflow.compat.v1 import ConfigProto, InteractiveSession
      from tensorflow.keras import backend as K
      from tensorflow.keras.layers import (
          Activation,
          Conv2D,
          Dense,
          Dropout,
          Flatten,
          MaxPooling2D,
      )
      from tensorflow.keras.models import Sequential
  
      config = ConfigProto()
      config.gpu_options.allow_growth = True
      sess = InteractiveSession(config=config)
  
      tf.compat.v1.disable_eager_execution()
  
      batch_size = 128
      num_classes = 10
      epochs = 1
  
      # input image dimensions
      img_rows, img_cols = 28, 28
  
      # the data, split between train and test sets
      # (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
      x_train = rs.randn(200, 28, 28)
      y_train = rs.randint(0, 9, 200)
      x_test = rs.randn(200, 28, 28)
      y_test = rs.randint(0, 9, 200)
  
      if K.image_data_format() == "channels_first":
          x_train = x_train.reshape(
              x_train.shape[0], 1, img_rows, img_cols
          )
          x_test = x_test.reshape(
              x_test.shape[0], 1, img_rows, img_cols
          )
          input_shape = (1, img_rows, img_cols)
      else:
          x_train = x_train.reshape(
              x_train.shape[0], img_rows, img_cols, 1
          )
          x_test = x_test.reshape(
              x_test.shape[0], img_rows, img_cols, 1
          )
          input_shape = (img_rows, img_cols, 1)
  
      x_train = x_train.astype("float32")
      x_test = x_test.astype("float32")
      x_train /= 255
      x_test /= 255
  
      # convert class vectors to binary class matrices
      y_train = tf.keras.utils.to_categorical(y_train, num_classes)
      y_test = tf.keras.utils.to_categorical(y_test, num_classes)
  
      model = Sequential()
      model.add(
          Conv2D(
              32,
              kernel_size=(3, 3),
              activation="relu",
              input_shape=input_shape,
          )
      )
      model.add(Conv2D(64, (3, 3), activation="relu"))
      model.add(MaxPooling2D(pool_size=(2, 2)))
      model.add(Dropout(0.25))
      model.add(Flatten())
      model.add(Dense(32, activation="relu"))  # 128
      model.add(Dropout(0.5))
      model.add(Dense(num_classes))
      model.add(Activation("softmax"))
  
      model.compile(
          loss=tf.keras.losses.categorical_crossentropy,
          optimizer=tf.keras.optimizers.legacy.Adadelta(),
          metrics=["accuracy"],
      )
  
      model.fit(
          x_train[:1000, :],
          y_train[:1000, :],
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test[:1000, :], y_test[:1000, :]),
      )
  
      # explain by passing the tensorflow inputs and outputs
      inds = rs.choice(x_train.shape[0], 20, replace=False)
      e = shap.GradientExplainer(
          (model.layers[0].input, model.layers[-1].input),
          x_train[inds, :, :],
      )
      shap_values = e.shap_values(x_test[:1], nsamples=2000)
  
      diff = sess.run(
          model.layers[-1].input,
          feed_dict={model.layers[0].input: x_test[:1]},
      ) - sess.run(
          model.layers[-1].input,
          feed_dict={model.layers[0].input: x_train[inds, :, :]},
      ).mean(
          0
      )
  
      sums = np.array(
          [shap_values[i].sum() for i in range(len(shap_values))]
      )
      d = np.abs(sums - diff).sum()
      assert (
          d / (np.abs(diff).sum() + 0.01) < 0.1
      ), "Sum of SHAP values does not match difference! %f" % (
          d / np.abs(diff).sum()
      )
      sess.close()
  
file_path: shap/tests/explainers/test_gradient.py
imports: from urllib.error import HTTPError
  import numpy
  import pandas
  import pytest
  import shap
label_desc: Use image node_id 1 for calling a global function with example usage: test_tf_keras_mnist_cnn(random_seed) without return types