Files
iui-group-l-name-zensiert/2-second-project/tdt/DataViz.ipynb
2021-07-19 02:20:02 +02:00

1019 lines
49 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "51c8325a",
"metadata": {},
"source": [
"# Constants"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "1195492b",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true' # this is required\n",
"os.environ['CUDA_VISIBLE_DEVICES'] = '2' # set to '0' for GPU0, '1' for GPU1 or '2' for GPU2. Check \"gpustat\" in a terminal."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e81968e4",
"metadata": {},
"outputs": [],
"source": [
"glob_path = '/opt/iui-datarelease3-sose2021/*.csv'\n",
"\n",
"pickle_file = '../data.pickle'"
]
},
{
"cell_type": "markdown",
"id": "15c58c37",
"metadata": {},
"source": [
"# Config"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b3170558",
"metadata": {},
"outputs": [],
"source": [
"# Possibilities: 'SYY', 'SYN', 'SNY', 'SNN', \n",
"# 'JYY', 'JYN', 'JNY', 'JNN'\n",
"cenario = 'JNN'\n",
"\n",
"win_sz = 30\n",
"stride_sz = 30\n",
"\n",
"# divisor for neuron count step downs (hard to describe), e.g. dense_step = 3: layer1=900, layer2 = 300, layer3 = 100, layer4 = 33...\n",
"dense_steps = 3\n",
"# amount of dense/dropout layers\n",
"layer_count = 7\n",
"# how much to drop\n",
"drop_count = 0.2"
]
},
{
"cell_type": "markdown",
"id": "40301046",
"metadata": {},
"source": [
"# Helper Functions"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4775b67d",
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"\n",
"def pplot(dd):\n",
" x = dd.shape[0]\n",
" fix = int(x/3)+1\n",
" fiy = 3\n",
" fig, axs = plt.subplots(fix, fiy, figsize=(3*fiy, 9*fix))\n",
" \n",
" for i in range(x):\n",
" axs[int(i/3)][i%3].plot(dd[i])"
]
},
{
"cell_type": "markdown",
"id": "ed6a72de",
"metadata": {},
"source": [
"# Loading Data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "937e015f",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from glob import glob\n",
"import pandas as pd\n",
"from tqdm import tqdm\n",
"\n",
"def dl_from_blob(filename, user_filter=None):\n",
" \n",
" dic_data = []\n",
" \n",
" for p in tqdm(glob(glob_path)):\n",
" path = p\n",
" filename = path.split('/')[-1].split('.')[0]\n",
" splitname = filename.split('_')\n",
" user = int(splitname[0][1:])\n",
" if (user_filter):\n",
" if (user != user_filter):\n",
" continue\n",
" scenario = splitname[1][len('Scenario'):]\n",
" heightnorm = splitname[2][len('HeightNormalization'):] == 'True'\n",
" armnorm = splitname[3][len('ArmNormalization'):] == 'True'\n",
" rep = int(splitname[4][len('Repetition'):])\n",
" session = int(splitname[5][len('Session'):])\n",
" data = pd.read_csv(path)\n",
" dic_data.append(\n",
" {\n",
" 'filename': path,\n",
" 'user': user,\n",
" 'scenario': scenario,\n",
" 'heightnorm': heightnorm,\n",
" 'armnorm': armnorm,\n",
" 'rep': rep,\n",
" 'session': session,\n",
" 'data': data \n",
" }\n",
" )\n",
" return dic_data"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5c94a433",
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"\n",
"def save_pickle(f, structure):\n",
" _p = open(f, 'wb')\n",
" pickle.dump(structure, _p)\n",
" _p.close()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "0f21ce12",
"metadata": {},
"outputs": [],
"source": [
"def load_pickles(f) -> list:\n",
" _p = open(pickle_file, 'rb')\n",
" _d = pickle.load(_p)\n",
" _p.close()\n",
" \n",
" return _d"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6ba89630",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading data...\n",
"../data.pickle found...\n",
"768\n",
"CPU times: user 582 ms, sys: 2.35 s, total: 2.93 s\n",
"Wall time: 2.94 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"def load_data() -> list:\n",
" if os.path.isfile(pickle_file):\n",
" print(f'{pickle_file} found...')\n",
" return load_pickles(pickle_file)\n",
" print(f'Didn\\'t find {pickle_file}...')\n",
" all_data = dl_from_blob(glob_path)\n",
" print(f'Creating {pickle_file}...')\n",
" save_pickle(pickle_file, all_data)\n",
" return all_data\n",
"\n",
"print(\"Loading data...\")\n",
"dic_data = load_data()\n",
"print(len(dic_data))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "80425264",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 401 µs, sys: 0 ns, total: 401 µs\n",
"Wall time: 406 µs\n"
]
}
],
"source": [
"%%time\n",
"\n",
"# Categorized Data\n",
"cdata = dict() \n",
"# Sorting, HeightNorm, ArmNorm\n",
"cdata['SYY'] = list() \n",
"cdata['SYN'] = list() \n",
"cdata['SNY'] = list() \n",
"cdata['SNN'] = list() \n",
"\n",
"# Jenga, HeightNorm, ArmNorm\n",
"cdata['JYY'] = list() \n",
"cdata['JYN'] = list() \n",
"cdata['JNY'] = list() \n",
"cdata['JNN'] = list() \n",
"for d in dic_data:\n",
" if d['scenario'] == 'Sorting':\n",
" if d['heightnorm']:\n",
" if d['armnorm']:\n",
" cdata['SYY'].append(d)\n",
" else:\n",
" cdata['SYN'].append(d)\n",
" else:\n",
" if d['armnorm']:\n",
" cdata['SNY'].append(d)\n",
" else:\n",
" cdata['SNN'].append(d)\n",
" elif d['scenario'] == 'Jenga':\n",
" if d['heightnorm']:\n",
" if d['armnorm']:\n",
" cdata['JYY'].append(d)\n",
" else:\n",
" cdata['JYN'].append(d)\n",
" else:\n",
" if d['armnorm']:\n",
" cdata['JNY'].append(d)\n",
" else:\n",
" cdata['JNN'].append(d)"
]
},
{
"cell_type": "markdown",
"id": "cb6c7f2b",
"metadata": {},
"source": [
"# Preprocessing"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b32822b3",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def drop(entry) -> pd.DataFrame:\n",
" droptable = ['participantID', 'FrameID', 'Scenario', 'HeightNormalization', 'ArmNormalization', 'Repetition', 'Session', 'Unnamed: 0']\n",
" centry = pickle.loads(pickle.dumps(entry))\n",
" return centry['data'].drop(droptable, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5296f567",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def floatize(entry) -> pd.DataFrame:\n",
" centry = pickle.loads(pickle.dumps(entry))\n",
" centry['data']['LeftHandTrackingAccuracy'] = (entry['data']['LeftHandTrackingAccuracy'] == 'High') * 1.0\n",
" centry['data']['RightHandTrackingAccuracy'] = (entry['data']['RightHandTrackingAccuracy'] == 'High') * 1.0\n",
" return centry['data']"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "bbdeb7db",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"right_Hand_ident='right_Hand'\n",
"left_Hand_ident='left_hand'\n",
"\n",
"def rem_low_acc(entry) -> pd.DataFrame:\n",
" centry = pickle.loads(pickle.dumps(entry))\n",
" right_Hand_cols = [c for c in centry['data'] if right_Hand_ident in c]\n",
" left_Hand_cols = [c for c in centry['data'] if left_Hand_ident in c]\n",
" \n",
" centry['data'].loc[centry['data']['RightHandTrackingAccuracy'] == 0.0, right_Hand_cols] = np.nan\n",
" centry['data'].loc[centry['data']['LeftHandTrackingAccuracy'] == 0.0, left_Hand_cols] = np.nan\n",
" return centry['data']"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "626926ac",
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.preprocessing.sequence import pad_sequences\n",
"\n",
"stride = 150\n",
"def pad(entry) -> pd.DataFrame:\n",
" centry = pickle.loads(pickle.dumps(entry))\n",
" cols = centry['data'].columns\n",
" pentry = pad_sequences(centry['data'].T.to_numpy(),\n",
" maxlen=(int(centry['data'].shape[0]/stride)+1)*stride,\n",
" dtype='float64',\n",
" padding='pre', \n",
" truncating='post',\n",
" value=np.nan\n",
" ) \n",
" pdentry = pd.DataFrame(pentry.T, columns=cols)\n",
" pdentry.loc[0] = [0 for _ in cols]\n",
" return pdentry"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "66ed68ee",
"metadata": {},
"outputs": [],
"source": [
"def interpol(entry) -> pd.DataFrame:\n",
" centry = pickle.loads(pickle.dumps(entry))\n",
" return centry['data'].interpolate(method='linear', axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "0179fb93",
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.preprocessing import timeseries_dataset_from_array\n",
"\n",
"def slicing(entry):\n",
" centry = pickle.loads(pickle.dumps(entry))\n",
" return timeseries_dataset_from_array(\n",
" data=centry['data'], \n",
" targets=[centry['user'] for _ in range(centry['data'].shape[0])], \n",
" sequence_length=win_sz,\n",
" sequence_stride=stride_sz, \n",
" batch_size=8, \n",
" seed=177013\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "a174022d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 96/96 [00:17<00:00, 5.38it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 16.9 s, sys: 1.16 s, total: 18.1 s\n",
"Wall time: 17.9 s\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"%%time\n",
"\n",
"classes = 16 # dynamic\n",
"\n",
"def preproc(data):\n",
" res_list = list()\n",
" \n",
" for e in tqdm(data):\n",
" res_list.append(preproc_entry(e))\n",
" \n",
" return res_list\n",
" \n",
"def preproc_entry(entry):\n",
" entry2 = pickle.loads(pickle.dumps(entry))\n",
" entry2['data'] = drop(entry2)\n",
" \n",
" entry3 = pickle.loads(pickle.dumps(entry2))\n",
" entry3['data'] = floatize(entry3)\n",
" \n",
" entry4 = pickle.loads(pickle.dumps(entry3))\n",
" entry4['data'] = rem_low_acc(entry4)\n",
" \n",
" entry5 = pickle.loads(pickle.dumps(entry4))\n",
" entry5['data'] = pad(entry5)\n",
" \n",
" entry6 = pickle.loads(pickle.dumps(entry5))\n",
" entry6['data'] = interpol(entry6)\n",
" \n",
" entry7 = pickle.loads(pickle.dumps(entry6))\n",
" entry7['data'] = slicing(entry7)\n",
" \n",
" return entry7\n",
"\n",
"pdata = preproc(cdata[cenario])"
]
},
{
"cell_type": "markdown",
"id": "4d604e31",
"metadata": {},
"source": [
"# Building Model"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "d9079b58",
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense, Flatten, BatchNormalization, Dropout, LSTM\n",
"import tensorflow.keras as keras\n",
"\n",
"def build_model(shape, classes):\n",
" \n",
" model = Sequential()\n",
" ncount = shape[0]*shape[1]\n",
" \n",
" model.add(Flatten(input_shape=shape))\n",
" \n",
" model.add(Dropout(drop_count))\n",
" model.add(BatchNormalization())\n",
" \n",
" for i in range(1,layer_count):\n",
" neurons = int(ncount/pow(dense_steps,i))\n",
" if neurons <= classes*dense_steps:\n",
" break\n",
" model.add(Dropout(drop_count*i))\n",
" model.add(Dense(neurons, activation='relu'))\n",
" \n",
" model.add(Dense(classes, activation='softmax'))\n",
"\n",
" model.compile(\n",
" optimizer=tf.keras.optimizers.Adam(0.001),\n",
" loss=\"categorical_crossentropy\", \n",
" metrics=[\"acc\"],\n",
" )\n",
"\n",
" return model\n",
"\n",
"def build_mlp(input_shape, nb_classes):\n",
" input_layer = keras.layers.Input(input_shape)\n",
"\n",
" # flatten/reshape because when multivariate all should be on the same axis \n",
" input_layer_flattened = keras.layers.Flatten()(input_layer)\n",
" \n",
" layer_1 = keras.layers.Dropout(0.1)(input_layer_flattened)\n",
" layer_1 = keras.layers.Dense(500, activation='relu')(layer_1)\n",
"\n",
" layer_2 = keras.layers.Dropout(0.2)(layer_1)\n",
" layer_2 = keras.layers.Dense(500, activation='relu')(layer_2)\n",
"\n",
" layer_3 = keras.layers.Dropout(0.2)(layer_2)\n",
" layer_3 = keras.layers.Dense(500, activation='relu')(layer_3)\n",
"\n",
" output_layer = keras.layers.Dropout(0.3)(layer_3)\n",
" output_layer = keras.layers.Dense(nb_classes, activation='softmax')(output_layer)\n",
"\n",
" model = keras.models.Model(inputs=input_layer, outputs=output_layer)\n",
"\n",
" model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adadelta(),\n",
" metrics=['accuracy'])\n",
"\n",
" return model"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "371ca0ef",
"metadata": {},
"outputs": [],
"source": [
"checkpoint_file = './goat.weights'\n",
"\n",
"def train_model(X_train, y_train, X_test, y_test):\n",
" model = build_model(X_train[0].shape, 16)\n",
" \n",
" model.summary()\n",
"\n",
" model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n",
" filepath = checkpoint_file,\n",
" save_weights_only=True,\n",
" monitor='val_acc',\n",
" mode='max',\n",
" save_best_only=True\n",
" )\n",
" \n",
" history = model.fit(X_train, \n",
" y_train,\n",
" epochs=30,\n",
" batch_size=128,\n",
" shuffle=True,\n",
" verbose=2,\n",
" validation_data=(X_test, y_test),\n",
" callbacks=[model_checkpoint_callback]\n",
" \n",
" )\n",
" return model, history\n",
"\n",
"def train_mlp(X_train, y_train, X_test, y_test):\n",
" model = build_mlp(X_train[0].shape, 16)\n",
" model.summary()\n",
" \n",
" reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='loss', factor=0.5, patience=200, min_lr=0.1)\n",
"\n",
" model_checkpoint = keras.callbacks.ModelCheckpoint(filepath=checkpoint_file, monitor='loss', \n",
" save_best_only=True)\n",
"\n",
" callbacks = [reduce_lr,model_checkpoint]\n",
" history = model.fit(X_train, \n",
" y_train,\n",
" epochs=5000,\n",
" batch_size=16,\n",
" shuffle=True,\n",
" verbose=2,\n",
" validation_data=(X_test, y_test),\n",
" callbacks=callbacks\n",
" )\n",
" \n",
" return model, history"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "687e0410",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 377 µs, sys: 0 ns, total: 377 µs\n",
"Wall time: 400 µs\n"
]
},
{
"data": {
"text/plain": [
"(48, 48)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"train = np.array([x['data'] for x in pdata if x['session'] == 1])\n",
"test = np.array([x['data'] for x in pdata if x['session'] == 2])\n",
"\n",
"len(train), len(test)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "f54675a7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 8.11 s, sys: 3.7 s, total: 11.8 s\n",
"Wall time: 4.34 s\n"
]
}
],
"source": [
"%%time\n",
"\n",
"X_train = list()\n",
"y_train = list()\n",
"\n",
"X_test = list()\n",
"y_test = list()\n",
"\n",
"train = list()\n",
"test = list()\n",
"\n",
"for x in pdata:\n",
" if x['session'] == 1:\n",
" train.append(\n",
" {\n",
" 'label': x['user'],\n",
" 'data': list()\n",
" })\n",
" for y in x['data'].unbatch().as_numpy_iterator():\n",
" X_train.append(y[0])\n",
" y_train.append(y[1])\n",
" \n",
" train[-1]['data'].append(y[0])\n",
" if x['session'] == 2:\n",
" test.append(\n",
" {\n",
" 'label': x['user'],\n",
" 'data': list()\n",
" })\n",
" for y in x['data'].unbatch().as_numpy_iterator():\n",
" X_test.append(y[0])\n",
" y_test.append(y[1])\n",
" \n",
" test[-1]['data'].append(y[0])\n",
"\n",
"X_train = np.array(X_train)\n",
"y_train = np.array(y_train)\n",
"X_test = np.array(X_test)\n",
"y_test = np.array(y_test)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "572563d8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((5620, 30, 410), (5620,), (3675, 30, 410), (3675,))"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"X_train.shape, y_train.shape, X_test.shape, y_test.shape"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "1eab0365",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 478 ms, sys: 98.7 ms, total: 577 ms\n",
"Wall time: 576 ms\n"
]
}
],
"source": [
"%%time\n",
"\n",
"from sklearn.preprocessing import LabelBinarizer\n",
"\n",
"lb = LabelBinarizer()\n",
"yy_train = lb.fit_transform(y_train)\n",
"yy_test = lb.fit_transform(y_test)\n",
"\n",
"for e in test:\n",
" e['label'] = lb.transform([e['label']])\n",
" e['data'] = np.array(e['data'])\n",
" \n",
"for e in train:\n",
" e['label'] = lb.transform([e['label']])\n",
" e['data'] = np.array(e['data'])\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "b28ff92e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(5620, 30, 410)\n",
"(5620, 16)\n",
"(3675, 30, 410)\n",
"(3675, 16)\n"
]
}
],
"source": [
"print(X_train.shape)\n",
"print(yy_train.shape)\n",
"print(X_test.shape)\n",
"print(yy_test.shape)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "acbf1e73",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"flatten (Flatten) (None, 12300) 0 \n",
"_________________________________________________________________\n",
"dropout (Dropout) (None, 12300) 0 \n",
"_________________________________________________________________\n",
"batch_normalization (BatchNo (None, 12300) 49200 \n",
"_________________________________________________________________\n",
"dropout_1 (Dropout) (None, 12300) 0 \n",
"_________________________________________________________________\n",
"dense (Dense) (None, 4100) 50434100 \n",
"_________________________________________________________________\n",
"dropout_2 (Dropout) (None, 4100) 0 \n",
"_________________________________________________________________\n",
"dense_1 (Dense) (None, 1366) 5601966 \n",
"_________________________________________________________________\n",
"dropout_3 (Dropout) (None, 1366) 0 \n",
"_________________________________________________________________\n",
"dense_2 (Dense) (None, 455) 621985 \n",
"_________________________________________________________________\n",
"dropout_4 (Dropout) (None, 455) 0 \n",
"_________________________________________________________________\n",
"dense_3 (Dense) (None, 151) 68856 \n",
"_________________________________________________________________\n",
"dropout_5 (Dropout) (None, 151) 0 \n",
"_________________________________________________________________\n",
"dense_4 (Dense) (None, 50) 7600 \n",
"_________________________________________________________________\n",
"dense_5 (Dense) (None, 16) 816 \n",
"=================================================================\n",
"Total params: 56,784,523\n",
"Trainable params: 56,759,923\n",
"Non-trainable params: 24,600\n",
"_________________________________________________________________\n",
"Epoch 1/30\n"
]
},
{
"ename": "ValueError",
"evalue": "in user code:\n\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:855 train_function *\n return step_function(self, iterator)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:845 step_function **\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1285 run\n return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica\n return self._call_for_each_replica(fn, args, kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3608 _call_for_each_replica\n return fn(*args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:838 run_step **\n outputs = model.train_step(data)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:795 train_step\n y_pred = self(x, training=True)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1030 __call__\n outputs = call_fn(inputs, *args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py:380 call\n return super(Sequential, self).call(inputs, training=training, mask=mask)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:420 call\n return self._run_internal_graph(\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:556 _run_internal_graph\n outputs = node.layer(*args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1030 __call__\n outputs = call_fn(inputs, *args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/layers/core.py:230 call\n output = control_flow_util.smart_cond(training, dropped_inputs,\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/utils/control_flow_util.py:109 smart_cond\n return smart_module.smart_cond(\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/framework/smart_cond.py:54 smart_cond\n return true_fn()\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/layers/core.py:224 dropped_inputs\n return nn.dropout(\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:206 wrapper\n return target(*args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/util/deprecation.py:535 new_func\n return func(*args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/ops/nn_ops.py:5106 dropout\n return dropout_v2(x, rate, noise_shape=noise_shape, seed=seed, name=name)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:206 wrapper\n return target(*args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/ops/nn_ops.py:5186 dropout_v2\n raise ValueError(\"rate must be a scalar tensor or a float in the \"\n\n ValueError: rate must be a scalar tensor or a float in the range [0, 1), got 1\n",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n",
"\u001b[0;32m<ipython-input-18-c07d217cb8ba>\u001b[0m in \u001b[0;36mtrain_model\u001b[0;34m(X_train, y_train, X_test, y_test)\u001b[0m\n\u001b[1;32m 14\u001b[0m )\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m history = model.fit(X_train, \n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0my_train\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0mepochs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py\u001b[0m in \u001b[0;36mfit\u001b[0;34m(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)\u001b[0m\n\u001b[1;32m 1181\u001b[0m _r=1):\n\u001b[1;32m 1182\u001b[0m \u001b[0mcallbacks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mon_train_batch_begin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1183\u001b[0;31m \u001b[0mtmp_logs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterator\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1184\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mdata_handler\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshould_sync\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1185\u001b[0m \u001b[0mcontext\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0masync_wait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m 887\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 888\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mOptionalXlaContext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_jit_compile\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 889\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 890\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 891\u001b[0m \u001b[0mnew_tracing_count\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexperimental_get_tracing_count\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py\u001b[0m in \u001b[0;36m_call\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m 931\u001b[0m \u001b[0;31m# This is the first call of __call__, so we have to initialize.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 932\u001b[0m \u001b[0minitializers\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 933\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_initialize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwds\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0madd_initializers_to\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minitializers\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 934\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 935\u001b[0m \u001b[0;31m# At this point we know that the initialization is complete (or less\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py\u001b[0m in \u001b[0;36m_initialize\u001b[0;34m(self, args, kwds, add_initializers_to)\u001b[0m\n\u001b[1;32m 761\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_graph_deleter\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFunctionDeleter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_lifted_initializer_graph\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 762\u001b[0m self._concrete_stateful_fn = (\n\u001b[0;32m--> 763\u001b[0;31m self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access\n\u001b[0m\u001b[1;32m 764\u001b[0m *args, **kwds))\n\u001b[1;32m 765\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/eager/function.py\u001b[0m in \u001b[0;36m_get_concrete_function_internal_garbage_collected\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 3048\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3049\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_lock\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3050\u001b[0;31m \u001b[0mgraph_function\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_maybe_define_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3051\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mgraph_function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3052\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/eager/function.py\u001b[0m in \u001b[0;36m_maybe_define_function\u001b[0;34m(self, args, kwargs)\u001b[0m\n\u001b[1;32m 3442\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3443\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_function_cache\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmissed\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcall_context_key\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3444\u001b[0;31m \u001b[0mgraph_function\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_create_graph_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3445\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_function_cache\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprimary\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mcache_key\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgraph_function\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3446\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/eager/function.py\u001b[0m in \u001b[0;36m_create_graph_function\u001b[0;34m(self, args, kwargs, override_flat_arg_shapes)\u001b[0m\n\u001b[1;32m 3277\u001b[0m \u001b[0marg_names\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbase_arg_names\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mmissing_arg_names\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3278\u001b[0m graph_function = ConcreteFunction(\n\u001b[0;32m-> 3279\u001b[0;31m func_graph_module.func_graph_from_py_func(\n\u001b[0m\u001b[1;32m 3280\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_name\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3281\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_python_function\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py\u001b[0m in \u001b[0;36mfunc_graph_from_py_func\u001b[0;34m(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)\u001b[0m\n\u001b[1;32m 997\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moriginal_func\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf_decorator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munwrap\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpython_func\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 998\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 999\u001b[0;31m \u001b[0mfunc_outputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpython_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mfunc_args\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mfunc_kwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1000\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1001\u001b[0m \u001b[0;31m# invariant: `func_outputs` contains only Tensors, CompositeTensors,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py\u001b[0m in \u001b[0;36mwrapped_fn\u001b[0;34m(*args, **kwds)\u001b[0m\n\u001b[1;32m 670\u001b[0m \u001b[0;31m# the function a weak reference to itself to avoid a reference cycle.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 671\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mOptionalXlaContext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcompile_with_xla\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 672\u001b[0;31m \u001b[0mout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mweak_wrapped_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__wrapped__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 673\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 674\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 984\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;31m# pylint:disable=broad-except\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 985\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mhasattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"ag_error_metadata\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 986\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mag_error_metadata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto_exception\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 987\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 988\u001b[0m \u001b[0;32mraise\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: in user code:\n\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:855 train_function *\n return step_function(self, iterator)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:845 step_function **\n outputs = model.distribute_strategy.run(run_step, args=(data,))\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1285 run\n return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica\n return self._call_for_each_replica(fn, args, kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3608 _call_for_each_replica\n return fn(*args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:838 run_step **\n outputs = model.train_step(data)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:795 train_step\n y_pred = self(x, training=True)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1030 __call__\n outputs = call_fn(inputs, *args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py:380 call\n return super(Sequential, self).call(inputs, training=training, mask=mask)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:420 call\n return self._run_internal_graph(\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:556 _run_internal_graph\n outputs = node.layer(*args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:1030 __call__\n outputs = call_fn(inputs, *args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/layers/core.py:230 call\n output = control_flow_util.smart_cond(training, dropped_inputs,\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/utils/control_flow_util.py:109 smart_cond\n return smart_module.smart_cond(\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/framework/smart_cond.py:54 smart_cond\n return true_fn()\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/keras/layers/core.py:224 dropped_inputs\n return nn.dropout(\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:206 wrapper\n return target(*args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/util/deprecation.py:535 new_func\n return func(*args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/ops/nn_ops.py:5106 dropout\n return dropout_v2(x, rate, noise_shape=noise_shape, seed=seed, name=name)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:206 wrapper\n return target(*args, **kwargs)\n /opt/jupyterhub/lib/python3.8/site-packages/tensorflow/python/ops/nn_ops.py:5186 dropout_v2\n raise ValueError(\"rate must be a scalar tensor or a float in the \"\n\n ValueError: rate must be a scalar tensor or a float in the range [0, 1), got 1\n"
]
}
],
"source": [
"%%time\n",
"\n",
"model, history = train_model(np.array(X_train), np.array(yy_train), np.array(X_test), np.array(yy_test))"
]
},
{
"cell_type": "markdown",
"id": "558a333e",
"metadata": {},
"source": [
"# Eval"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "39b85ef8",
"metadata": {},
"outputs": [],
"source": [
"def predict(model, entry):\n",
" p_dict = dict()\n",
" predictions = np.argmax(model.predict(entry['data']), axis=-1)\n",
" for p in predictions:\n",
" if p in p_dict:\n",
" p_dict[p] += 1\n",
" else:\n",
" p_dict[p] = 1\n",
" prediction = max(p_dict, key=p_dict.get)\n",
" return prediction+1"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "326061ed",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'model' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n",
"\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'model' is not defined"
]
}
],
"source": [
"%%time\n",
"\n",
"ltest = [lb.inverse_transform(e['label'])[0] for e in test]\n",
"ptest = [predict(model, e) for e in test]\n",
"\n",
"# for e in test:\n",
"# print(f\"Label: {lb.inverse_transform(e['label'])[0]:2d}\")\n",
"# print(f\"Prediction: {predict(model, e):2d}\\n_______________\")"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "52fdcf4c",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'model' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n",
"\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'model' is not defined"
]
}
],
"source": [
"%%time\n",
"\n",
"ltrain = [lb.inverse_transform(e['label'])[0] for e in train]\n",
"ptrain = [predict(model, e) for e in train]\n",
"\n",
"# for e in train:\n",
"# print(f\"Label: {lb.inverse_transform(e['label'])[0]:2d}\")\n",
"# print(f\"Prediction: {predict(model, e):2d}\\n_______________\")"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "cb278ec9",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'ptrain' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<timed exec>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'ptrain' is not defined"
]
}
],
"source": [
"%%time\n",
"\n",
"from sklearn.metrics import confusion_matrix\n",
"import seaborn as sn\n",
"\n",
"from sklearn.metrics import classification_report\n",
"\n",
"set_digits = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }\n",
"\n",
"train_cm = confusion_matrix(ltrain, ptrain, normalize='true')\n",
"test_cm = confusion_matrix(ltest, ptest, normalize='true')\n",
"\n",
"df_cm = pd.DataFrame(test_cm, index=set_digits, columns=set_digits)\n",
"plt.figure(figsize = (10,7))\n",
"sn_plot = sn.heatmap(df_cm, annot=True, cmap=\"Greys\")\n",
"plt.ylabel(\"True Label\")\n",
"plt.xlabel(\"Predicted Label\")\n",
"plt.show()\n",
"\n",
"print(classification_report(ltest, ptest, zero_division=0))"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "c857fefa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cenario: JNN\n",
"win_sz: 30\n",
"stride_sz: 30\n",
"dense_steps: 3\n",
"layer_count: 7\n",
"drop_count: 0.2\n"
]
}
],
"source": [
"print(f'cenario: {cenario}')\n",
"print(f'win_sz: {win_sz}')\n",
"print(f'stride_sz: {stride_sz}')\n",
"print(f'dense_steps: {dense_steps}')\n",
"print(f'layer_count: {layer_count}')\n",
"print(f'drop_count: {drop_count}')\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e47f344b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}