{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Project mnist hard writing\n\ndata souce : Kaggle\nLink : https://www.kaggle.com/c/digit-recognizer\n\n2023/01/20\n2238i\n\nlets get init","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2023-07-05T08:17:24.662730Z","iopub.execute_input":"2023-07-05T08:17:24.663215Z","iopub.status.idle":"2023-07-05T08:17:24.707658Z","shell.execute_reply.started":"2023-07-05T08:17:24.663179Z","shell.execute_reply":"2023-07-05T08:17:24.706759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##IDK what is wrong with that up cell\nimport torch\nimport subprocess\n\ndef GPU():\n    if torch.cuda.is_available() == True:\n        device = 'cuda'\n        templist = [1, 2, 3]\n        templist = torch.FloatTensor(templist).to(device)\n        print(\"Cuda torch working : \", end=\"\")\n        print(templist.is_cuda)\n        print(\"current device no. : \", end=\"\")\n        print(torch.cuda.current_device())\n        print(\"GPU device count : \", end=\"\")\n        print(torch.cuda.device_count())\n        print(\"GPU name : \", end=\"\")\n        print(torch.cuda.get_device_name(0))\n        print(\"device : \", device)\n        # Execute the nvidia-smi command using subprocess\n        try:\n            output = subprocess.check_output(['nvidia-smi']).decode('utf-8')\n            print(\"nvidia-smi output:\")\n            print(output)\n        except (subprocess.CalledProcessError, FileNotFoundError) as e:\n            print(\"Error executing nvidia-smi command:\", str(e))\n    elif torch.backends.mps.is_available() == True:\n        print(\"Apple device detected\\nActivating Apple Silicon GPU\")\n        device = torch.device(\"mps\")\n    else:\n        print(\"cant use gpu , activating cpu\")\n        device = 'cpu'\n\n    return device\ndevice = GPU()\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2023-07-05T08:17:24.709883Z","iopub.execute_input":"2023-07-05T08:17:24.710523Z","iopub.status.idle":"2023-07-05T08:17:29.178932Z","shell.execute_reply.started":"2023-07-05T08:17:24.710487Z","shell.execute_reply":"2023-07-05T08:17:29.177307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nimport numpy as np\nimport torch\n\ntry:\n  f =  open(\"train.csv\")\nexcept:\n  f = open(\"/kaggle/input/digit-recognizer/train.csv\")\nread = csv.reader(f)\n\n#data to list\ndata = []\nfor row in read:\n  data.append(row)\n\n#delete legend data\ndel data[0]\n\n#get num data\ndata = np.float_(data)\ndata = np.array(data)\ny_train_numbers_origianl = data[:,0:1]\ny_train_numbers  = [[0 for col in range(10)] for row in range(len(y_train_numbers_origianl))]\n#print(y_train_numbers)\nfor row in range(len(y_train_numbers_origianl)):\n  y_train_numbers[row][int(y_train_numbers_origianl[row][0])] = 1\n#print((y_train_numbers))\n\n#get pixel data\nx_train_data = data[:,1:]\n#print(x_train_data)\n\nf.close()\n\n##TENSOR\nx_train_data = torch.Tensor(x_train_data).to(device)\ny_train_numbers = torch.Tensor(y_train_numbers).to(device)\nprint(x_train_data.type())\nprint(x_train_data.size())\nprint(y_train_numbers.size())","metadata":{"execution":{"iopub.status.busy":"2023-07-05T08:17:29.181019Z","iopub.execute_input":"2023-07-05T08:17:29.182578Z","iopub.status.idle":"2023-07-05T08:17:41.676510Z","shell.execute_reply.started":"2023-07-05T08:17:29.182525Z","shell.execute_reply":"2023-07-05T08:17:41.675320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ML","metadata":{}},{"cell_type":"code","source":"import torch\n\n\n\n#lr_list = [1,1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8]\nlr_list = [1e-4]\nnb_epoch = 100000\n\n\n\nfor LR in lr_list:\n  \n  W = torch.zeros( [784,10] , requires_grad=True,device=device)\n  b = torch.zeros( 10 , requires_grad=True,device=device)\n  optimizer = torch.optim.SGD([W,b],lr=LR)\n  loss = torch.nn.BCELoss()\n  for epoch in range(nb_epoch+1):\n    \n    hypothesis = torch.nn.functional.softmax(torch.matmul(x_train_data,W) + b,dim=1)\n    cost = loss(hypothesis,y_train_numbers)\n    optimizer.zero_grad()\n    cost.backward()\n    optimizer.step()\n    \n  print(\"LR : %.E cost : %.5f\"%(LR,cost.item()))\n","metadata":{"execution":{"iopub.status.busy":"2023-07-05T08:17:41.679024Z","iopub.execute_input":"2023-07-05T08:17:41.679402Z","iopub.status.idle":"2023-07-05T08:18:47.302790Z","shell.execute_reply.started":"2023-07-05T08:17:41.679371Z","shell.execute_reply":"2023-07-05T08:18:47.301581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  Learning rate\nLR : 1E+00 cost : 18.02737\nLR : 1E-01 cost : 18.06524\nLR : 1E-02 cost : 18.06333\nLR : 1E-03 cost : 17.90429\n### LR : 1E-04 cost : 0.04401\nLR : 1E-05 cost : 0.05749\nLR : 1E-06 cost : 0.10377\nLR : 1E-07 cost : 0.25261\nLR : 1E-08 cost : 0.31652","metadata":{}},{"cell_type":"markdown","source":"### Accuray : 94","metadata":{}},{"cell_type":"code","source":"#Test\nhypothesis_num = torch.argmax(hypothesis,dim=1) #argmax returns the INDEX #dim = 0 means max in column // dim = 1 means max in row\naccurcay = hypothesis_num.unsqueeze(1).tolist() == y_train_numbers_origianl\nacc_in_percent = accurcay.sum().item()/len(y_train_numbers_origianl)*100\nprint(accurcay)\nprint(acc_in_percent)","metadata":{"execution":{"iopub.status.busy":"2023-07-05T08:18:47.304222Z","iopub.execute_input":"2023-07-05T08:18:47.304603Z","iopub.status.idle":"2023-07-05T08:18:47.357179Z","shell.execute_reply.started":"2023-07-05T08:18:47.304573Z","shell.execute_reply":"2023-07-05T08:18:47.355629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Show Wrong Images","metadata":{}},{"cell_type":"code","source":"import PIL.Image\nimport IPython.display\nimport numpy as np\n\n#make temp img list\nimglist = [[0 for col in range(28)] for row in range(28)]\n\nfor num_row in range(len(x_train_data)):\n  if num_row > 200: #Limit the output images. there are too many \n    break\n  if accurcay[num_row][0] == False: #show me the wrong guesses I have to see for my self\n    cnt = 0\n    for row in range(28):\n      for col in range(28):\n        imglist[row][col] = x_train_data[num_row][cnt].item()\n        cnt = cnt + 1\n    img = PIL.Image.fromarray(np.uint8(imglist))#make array to size\n    img  = img.resize([150,150])#set the size\n    display(img)\n    print(\"Row num : %d // Num : %.0f Hypothesis : %.0f\"%(num_row,y_train_numbers_origianl[num_row][0],hypothesis_num[num_row]))\n","metadata":{"execution":{"iopub.status.busy":"2023-07-05T08:18:47.358708Z","iopub.execute_input":"2023-07-05T08:18:47.359047Z","iopub.status.idle":"2023-07-05T08:18:47.602089Z","shell.execute_reply.started":"2023-07-05T08:18:47.359020Z","shell.execute_reply":"2023-07-05T08:18:47.600652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# END\n\nWell I thought this would take a long time, but it only took 1 hour and 30 minutes.\n\nSome of the wrong guessed numbers were due to bad handwriting, but most of them were difficult to read due to factors such as tilting or being too bold. \n \nAnyhow, it was fun and amazing to see the images(it was hard to display image). I feel like a real AI developer","metadata":{}},{"cell_type":"markdown","source":"## update\nwow.... this ipynb was made 5 months ago, and now I see how much I improved.  \nRight now, its  2023.07.05 1642i in my english academy looking at kids talking an enlgish test.  \nANYWAY! I have came back to update this ipynb to upload at kaggle.  \nLooks like I need to see the test.csv and make a label for sample submisstion. ","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport torch\n\ntry:\n    test = pd.read_csv(\"test.csv\")\nexcept:\n    test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntry:\n    sub = pd.read_csv(\"sample_submission.csv\")\nexcept:\n    sub = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nprint(test.shape)\nprint(sub.shape)\n\ntest_tensor = torch.tensor(test.values).to(device).float()\nhypothesis = torch.nn.functional.softmax(torch.matmul(test_tensor, W) + b, dim=1)\n\npredictions = torch.argmax(hypothesis, dim=1)\n\n# Assign predictions to the second column of the second row onwards in \"sample_submission.csv\"\nsub.iloc[1:, 1] = predictions[1:].tolist()\n\n# Save the updated \"sample_submission.csv\" file\ntry:\n    sub.to_csv(\"/kaggle/working/sample_submission.csv\", index=False)\nexcept:\n    print(\"no\")\ntry:\n    sub.to_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\", index=False)\nexcept:\n    print(\"nono\")","metadata":{"execution":{"iopub.status.busy":"2023-07-05T08:18:47.603635Z","iopub.execute_input":"2023-07-05T08:18:47.604011Z","iopub.status.idle":"2023-07-05T08:18:50.559212Z","shell.execute_reply.started":"2023-07-05T08:18:47.603982Z","shell.execute_reply":"2023-07-05T08:18:50.557856Z"},"trusted":true},"execution_count":null,"outputs":[]}]}