{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":4120,"databundleVersionId":44256,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import timeit\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport category_encoders as ce\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split, GridSearchCV\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.metrics import roc_auc_score, ConfusionMatrixDisplay, classification_report\nfrom sklearn.linear_model import SGDClassifier\npd.set_option('display.max_columns', None)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-01-30T09:13:34.127471Z","iopub.execute_input":"2024-01-30T09:13:34.128016Z","iopub.status.idle":"2024-01-30T09:13:34.635128Z","shell.execute_reply.started":"2024-01-30T09:13:34.127973Z","shell.execute_reply":"2024-01-30T09:13:34.633908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data(nrows=100_000):\n    chunksize = 10 ** 6\n    filename=\"../input/avazu-ctr-prediction/train.gz\"\n    chunks = []\n    with pd.read_csv(filename, chunksize=chunksize, compression='gzip', nrows=nrows) as reader:\n        for chunk in reader:\n            chunks.append(chunk)\n    data = pd.concat(chunks)\n\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:34.637446Z","iopub.execute_input":"2024-01-30T09:13:34.637810Z","iopub.status.idle":"2024-01-30T09:13:34.644441Z","shell.execute_reply.started":"2024-01-30T09:13:34.637778Z","shell.execute_reply":"2024-01-30T09:13:34.643304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def find_best_estimator(params, clf, X, Y, verbose=0):\n    grid_search = GridSearchCV(clf, params, n_jobs=-1, cv=3, scoring='roc_auc')\n    if verbose:\n        grid_search.fit(X, Y, verbose=100)\n    else:\n        grid_search.fit(X, Y)\n    print(grid_search.best_params_)\n    return grid_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:34.646655Z","iopub.execute_input":"2024-01-30T09:13:34.647713Z","iopub.status.idle":"2024-01-30T09:13:34.663381Z","shell.execute_reply.started":"2024-01-30T09:13:34.647667Z","shell.execute_reply":"2024-01-30T09:13:34.662243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nrows_data = 300_000\ndata = load_data(nrows=nrows_data)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:34.666421Z","iopub.execute_input":"2024-01-30T09:13:34.666794Z","iopub.status.idle":"2024-01-30T09:13:36.407742Z","shell.execute_reply.started":"2024-01-30T09:13:34.666763Z","shell.execute_reply":"2024-01-30T09:13:36.406712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:36.409282Z","iopub.execute_input":"2024-01-30T09:13:36.409724Z","iopub.status.idle":"2024-01-30T09:13:36.447732Z","shell.execute_reply.started":"2024-01-30T09:13:36.409681Z","shell.execute_reply":"2024-01-30T09:13:36.446568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:36.449471Z","iopub.execute_input":"2024-01-30T09:13:36.449894Z","iopub.status.idle":"2024-01-30T09:13:36.764932Z","shell.execute_reply.started":"2024-01-30T09:13:36.449852Z","shell.execute_reply":"2024-01-30T09:13:36.763859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:36.766410Z","iopub.execute_input":"2024-01-30T09:13:36.766767Z","iopub.status.idle":"2024-01-30T09:13:36.968556Z","shell.execute_reply.started":"2024-01-30T09:13:36.766736Z","shell.execute_reply":"2024-01-30T09:13:36.967438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = data.drop(columns=['id', 'click', 'hour', 'device_id', 'device_ip'])\nY = data['click']","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:36.969858Z","iopub.execute_input":"2024-01-30T09:13:36.970292Z","iopub.status.idle":"2024-01-30T09:13:37.004336Z","shell.execute_reply.started":"2024-01-30T09:13:36.970262Z","shell.execute_reply":"2024-01-30T09:13:37.003141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:37.005996Z","iopub.execute_input":"2024-01-30T09:13:37.006381Z","iopub.status.idle":"2024-01-30T09:13:37.201016Z","shell.execute_reply.started":"2024-01-30T09:13:37.006349Z","shell.execute_reply":"2024-01-30T09:13:37.200021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enc = OneHotEncoder(handle_unknown='ignore')\nX_train_enc = enc.fit_transform(X_train)\nX_test_enc = enc.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:37.206463Z","iopub.execute_input":"2024-01-30T09:13:37.206880Z","iopub.status.idle":"2024-01-30T09:13:38.541851Z","shell.execute_reply.started":"2024-01-30T09:13:37.206845Z","shell.execute_reply":"2024-01-30T09:13:38.540895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgdc_clf = SGDClassifier(loss='log_loss', penalty=None, fit_intercept=True, max_iter=100, learning_rate='constant', eta0=0.01)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:38.542979Z","iopub.execute_input":"2024-01-30T09:13:38.543741Z","iopub.status.idle":"2024-01-30T09:13:38.549193Z","shell.execute_reply.started":"2024-01-30T09:13:38.543709Z","shell.execute_reply":"2024-01-30T09:13:38.548032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgdc_clf.fit(X_train_enc, Y_train)\npos_prob = sgdc_clf.predict_proba(X_test_enc)[:, 1]\nprint(f\"Area under curve ROC for testing dataset is: {roc_auc_score(Y_test, pos_prob)}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:38.550647Z","iopub.execute_input":"2024-01-30T09:13:38.551155Z","iopub.status.idle":"2024-01-30T09:13:39.705080Z","shell.execute_reply.started":"2024-01-30T09:13:38.551040Z","shell.execute_reply":"2024-01-30T09:13:39.703813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(Y_test, sgdc_clf.predict(X_test_enc), target_names=['0', '1']))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:39.706762Z","iopub.execute_input":"2024-01-30T09:13:39.707336Z","iopub.status.idle":"2024-01-30T09:13:39.766449Z","shell.execute_reply.started":"2024-01-30T09:13:39.707291Z","shell.execute_reply":"2024-01-30T09:13:39.765188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ConfusionMatrixDisplay.from_estimator(\n    sgdc_clf, X_test_enc, Y_test, display_labels=['0', '1'], xticks_rotation=\"vertical\"\n)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:39.768396Z","iopub.execute_input":"2024-01-30T09:13:39.769184Z","iopub.status.idle":"2024-01-30T09:13:40.395654Z","shell.execute_reply.started":"2024-01-30T09:13:39.769121Z","shell.execute_reply":"2024-01-30T09:13:40.394453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Penalty, eta0, alpha","metadata":{}},{"cell_type":"code","source":"sgdc_params = {'penalty': ['l2', 'l1', 'elasticnet', None],\n               'eta0': [0.001, 0.01, 1.0],\n               'alpha': [0.01, 0.1, 1.0]}","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:40.397241Z","iopub.execute_input":"2024-01-30T09:13:40.398295Z","iopub.status.idle":"2024-01-30T09:13:40.404406Z","shell.execute_reply.started":"2024-01-30T09:13:40.398248Z","shell.execute_reply":"2024-01-30T09:13:40.403211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid_search = GridSearchCV(sgdc_clf, sgdc_params, n_jobs=-1, cv=3, scoring='roc_auc')\ngrid_search.fit(X_train_enc, Y_train)\nprint(grid_search.best_params_)\nsgdc_best = grid_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:13:40.405792Z","iopub.execute_input":"2024-01-30T09:13:40.406258Z","iopub.status.idle":"2024-01-30T09:14:19.792042Z","shell.execute_reply.started":"2024-01-30T09:13:40.406215Z","shell.execute_reply":"2024-01-30T09:14:19.790508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pos_prob = sgdc_best.predict_proba(X_test_enc)[:, 1]\nprint(f\"Area under curve ROC for testing dataset is: {roc_auc_score(Y_test, pos_prob)}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:14:19.794295Z","iopub.execute_input":"2024-01-30T09:14:19.794679Z","iopub.status.idle":"2024-01-30T09:14:19.817783Z","shell.execute_reply.started":"2024-01-30T09:14:19.794642Z","shell.execute_reply":"2024-01-30T09:14:19.815983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(Y_test, sgdc_best.predict(X_test_enc), target_names=['0', '1']))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:14:19.819117Z","iopub.execute_input":"2024-01-30T09:14:19.819542Z","iopub.status.idle":"2024-01-30T09:14:19.877941Z","shell.execute_reply.started":"2024-01-30T09:14:19.819509Z","shell.execute_reply":"2024-01-30T09:14:19.876840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ConfusionMatrixDisplay.from_estimator(\n    sgdc_best, X_test_enc, Y_test, display_labels=['0', '1'], xticks_rotation=\"vertical\"\n)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:14:19.879522Z","iopub.execute_input":"2024-01-30T09:14:19.881533Z","iopub.status.idle":"2024-01-30T09:14:20.209658Z","shell.execute_reply.started":"2024-01-30T09:14:19.881486Z","shell.execute_reply":"2024-01-30T09:14:20.208678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Online learning","metadata":{}},{"cell_type":"markdown","source":"10 000 000 samples","metadata":{}},{"cell_type":"code","source":"big_data = load_data(10_000_000)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:14:20.211237Z","iopub.execute_input":"2024-01-30T09:14:20.211853Z","iopub.status.idle":"2024-01-30T09:15:13.550213Z","shell.execute_reply.started":"2024-01-30T09:14:20.211820Z","shell.execute_reply":"2024-01-30T09:15:13.548899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"big_data.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:15:13.551875Z","iopub.execute_input":"2024-01-30T09:15:13.552653Z","iopub.status.idle":"2024-01-30T09:15:13.567141Z","shell.execute_reply.started":"2024-01-30T09:15:13.552596Z","shell.execute_reply":"2024-01-30T09:15:13.566013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"big_X = big_data.drop(columns=['id', 'click', 'hour', 'device_id', 'device_ip'])\nbig_Y = big_data['click']","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:15:13.569233Z","iopub.execute_input":"2024-01-30T09:15:13.569691Z","iopub.status.idle":"2024-01-30T09:15:14.705411Z","shell.execute_reply.started":"2024-01-30T09:15:13.569642Z","shell.execute_reply":"2024-01-30T09:15:14.704231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"big_X_train, big_X_test, big_Y_train, big_Y_test = train_test_split(big_X, big_Y, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:15:14.706666Z","iopub.execute_input":"2024-01-30T09:15:14.707267Z","iopub.status.idle":"2024-01-30T09:15:27.563511Z","shell.execute_reply.started":"2024-01-30T09:15:14.707235Z","shell.execute_reply":"2024-01-30T09:15:27.562373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"big_enc = OneHotEncoder(handle_unknown='ignore')\nbig_enc.fit(big_X_train)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:15:27.565047Z","iopub.execute_input":"2024-01-30T09:15:27.565401Z","iopub.status.idle":"2024-01-30T09:15:36.121650Z","shell.execute_reply.started":"2024-01-30T09:15:27.565372Z","shell.execute_reply":"2024-01-30T09:15:36.120535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgdc_lr_online = SGDClassifier(\n    loss='log_loss',\n    fit_intercept=True,\n    max_iter=100,\n    learning_rate='constant',\n    **grid_search.best_params_)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:15:36.122931Z","iopub.execute_input":"2024-01-30T09:15:36.123328Z","iopub.status.idle":"2024-01-30T09:15:36.129775Z","shell.execute_reply.started":"2024-01-30T09:15:36.123298Z","shell.execute_reply":"2024-01-30T09:15:36.128475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_time = timeit.default_timer()\nfor i in range(9):\n    x_train = big_X_train[i*1_000_000:(i+1)*1_000_000]\n    y_train = big_Y_train[i*1_000_000:(i+1)*1_000_000]    \n    x_train_enc = big_enc.transform(x_train)\n    sgdc_lr_online.partial_fit(x_train_enc, y_train, classes=[0,1])\nprint(timeit.default_timer()-start_time)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:15:36.131419Z","iopub.execute_input":"2024-01-30T09:15:36.131776Z","iopub.status.idle":"2024-01-30T09:16:17.308711Z","shell.execute_reply.started":"2024-01-30T09:15:36.131743Z","shell.execute_reply":"2024-01-30T09:16:17.307813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"big_pos_prob = sgdc_lr_online.predict_proba(big_enc.transform(big_X_test))[:, 1]\nprint(f\"Area under curve ROC for testing dataset is: {roc_auc_score(big_Y_test, big_pos_prob)}\")","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:16:17.309816Z","iopub.execute_input":"2024-01-30T09:16:17.310694Z","iopub.status.idle":"2024-01-30T09:16:23.819319Z","shell.execute_reply.started":"2024-01-30T09:16:17.310659Z","shell.execute_reply":"2024-01-30T09:16:23.818193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If we want to work still in the same notebook, we must clear memmory. Otherwise \"Your notebook tried to allocate more memory than is available. It has restarted.\" message will occour","metadata":{}},{"cell_type":"code","source":"big_data = big_data.iloc[0:0]\nbig_X, big_Y = big_data.iloc[0:0], big_data.iloc[0:0]\nbig_X_train, big_X_test, big_Y_train, big_Y_test = big_data.iloc[0:0], big_data.iloc[0:0], big_data.iloc[0:0], big_data.iloc[0:0]\nx_train, y_train, x_train_enc = big_data.iloc[0:0], big_data.iloc[0:0], big_data.iloc[0:0]","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:16:23.825604Z","iopub.execute_input":"2024-01-30T09:16:23.825973Z","iopub.status.idle":"2024-01-30T09:16:24.556605Z","shell.execute_reply.started":"2024-01-30T09:16:23.825944Z","shell.execute_reply":"2024-01-30T09:16:24.555339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Logistic Regression with TensorFlow","metadata":{}},{"cell_type":"code","source":"X_train_enc.shape ","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:16:24.558481Z","iopub.execute_input":"2024-01-30T09:16:24.559336Z","iopub.status.idle":"2024-01-30T09:16:24.566711Z","shell.execute_reply.started":"2024-01-30T09:16:24.559300Z","shell.execute_reply":"2024-01-30T09:16:24.565782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-01-30T09:16:24.567868Z","iopub.execute_input":"2024-01-30T09:16:24.568877Z","iopub.status.idle":"2024-01-30T09:16:24.779801Z","shell.execute_reply.started":"2024-01-30T09:16:24.568842Z","shell.execute_reply":"2024-01-30T09:16:24.778388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"While shape of X_train_enc is 270_000 x 8385 we can't use .toarray() method and than .astype('float32'), because new array would take 270_000 * 8385 * 4 = 9_055_800_000 bytes (it's **8,43387 Gigabytes**). To reduce number of features we are going to use **target encoder**.","metadata":{}},{"cell_type":"markdown","source":"## Feature encoding","metadata":{}},{"cell_type":"code","source":"t_enc = ce.TargetEncoder(min_samples_leaf=20, smoothing=10)\ntf_X_train = t_enc.fit_transform(X_train, Y_train)\ntf_X_test = t_enc.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T10:33:36.733514Z","iopub.execute_input":"2024-01-30T10:33:36.733932Z","iopub.status.idle":"2024-01-30T10:33:39.505657Z","shell.execute_reply.started":"2024-01-30T10:33:36.733901Z","shell.execute_reply":"2024-01-30T10:33:39.504513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size=1000\ntrain_data = tf.data.Dataset.from_tensor_slices((tf_X_train, Y_train))\ntrain_data = train_data.repeat().shuffle(5000).batch(batch_size).prefetch(1)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T10:33:39.507529Z","iopub.execute_input":"2024-01-30T10:33:39.507888Z","iopub.status.idle":"2024-01-30T10:33:39.591439Z","shell.execute_reply.started":"2024-01-30T10:33:39.507856Z","shell.execute_reply":"2024-01-30T10:33:39.590097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract the first batch\nfor features, labels in train_data.take(1):\n    print(\"First Training Data:\")\n    print(features.numpy())\n    print(\"Labels:\")\n    print(labels.numpy())","metadata":{"execution":{"iopub.status.busy":"2024-01-30T10:33:39.592861Z","iopub.execute_input":"2024-01-30T10:33:39.593256Z","iopub.status.idle":"2024-01-30T10:33:39.637936Z","shell.execute_reply.started":"2024-01-30T10:33:39.593194Z","shell.execute_reply":"2024-01-30T10:33:39.636649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_features = X_train.shape[1]\nW = tf.Variable(tf.zeros([n_features, 1]))\nb = tf.Variable(tf.zeros([1]))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T10:33:42.245009Z","iopub.execute_input":"2024-01-30T10:33:42.245485Z","iopub.status.idle":"2024-01-30T10:33:42.254952Z","shell.execute_reply.started":"2024-01-30T10:33:42.245447Z","shell.execute_reply":"2024-01-30T10:33:42.253871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate = 0.00005\noptimizer = tf._optimizers.Adam(learning_rate)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T10:38:03.041539Z","iopub.execute_input":"2024-01-30T10:38:03.042042Z","iopub.status.idle":"2024-01-30T10:38:03.050699Z","shell.execute_reply.started":"2024-01-30T10:38:03.042002Z","shell.execute_reply":"2024-01-30T10:38:03.049427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run_optimization(x, y):\n    with tf.GradientTape() as g:\n        logits = tf.add(tf.matmul(x, W), b)[:, 0]\n        cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=logits))\n        gradients = g.gradient(cost, [W, b])\n        optimizer.apply_gradients(zip(gradients, [W, b]))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T10:33:48.134421Z","iopub.execute_input":"2024-01-30T10:33:48.134878Z","iopub.status.idle":"2024-01-30T10:33:48.141973Z","shell.execute_reply.started":"2024-01-30T10:33:48.134839Z","shell.execute_reply":"2024-01-30T10:33:48.140674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training","metadata":{}},{"cell_type":"code","source":"training_steps = 20_000\nfor step, (batch_x, batch_y) in enumerate(train_data.take(training_steps), 1):\n    batch_x = tf.cast(batch_x, dtype=tf.float32)\n    batch_y = tf.cast(batch_y, dtype=tf.float32)\n    run_optimization(batch_x, batch_y)\n    if step % 500 == 0:\n        logits = tf.add(tf.matmul(batch_x, W), b)[:, 0]\n        loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=batch_y, logits=logits))\n        print(\"Steps: %i, loss: %f\" % (step, loss))","metadata":{"execution":{"iopub.status.busy":"2024-01-30T10:38:04.857711Z","iopub.execute_input":"2024-01-30T10:38:04.858323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Testing","metadata":{}},{"cell_type":"code","source":"tf_X_test = tf.cast(tf_X_test, dtype=tf.float32)","metadata":{"execution":{"iopub.status.busy":"2024-01-30T10:37:42.114236Z","iopub.execute_input":"2024-01-30T10:37:42.114724Z","iopub.status.idle":"2024-01-30T10:37:42.126848Z","shell.execute_reply.started":"2024-01-30T10:37:42.114678Z","shell.execute_reply":"2024-01-30T10:37:42.125526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logits = tf.add(tf.matmul(tf_X_test, W), b)[:, 0]\npred = tf.nn.sigmoid(logits)\nauc_metric = tf.keras.metrics.AUC()\nauc_metric.update_state(Y_test, pred)\nprint(f'Area under curve ROC for testing dataset is: {auc_metric.result().numpy():.3f}')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}