{"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":"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\n# for 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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-25T18:26:03.765418Z","iopub.execute_input":"2023-02-25T18:26:03.766894Z","iopub.status.idle":"2023-02-25T18:26:03.804226Z","shell.execute_reply.started":"2023-02-25T18:26:03.766837Z","shell.execute_reply":"2023-02-25T18:26:03.803331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:03.806955Z","iopub.execute_input":"2023-02-25T18:26:03.807765Z","iopub.status.idle":"2023-02-25T18:26:14.628701Z","shell.execute_reply.started":"2023-02-25T18:26:03.807722Z","shell.execute_reply":"2023-02-25T18:26:14.626933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### model training","metadata":{}},{"cell_type":"code","source":"# # help(tf.keras.Sequential())\n\n# Sequential groups a linear stack of layers into a tf.keras.Model.\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:14.630188Z","iopub.execute_input":"2023-02-25T18:26:14.630932Z","iopub.status.idle":"2023-02-25T18:26:14.635967Z","shell.execute_reply.started":"2023-02-25T18:26:14.630890Z","shell.execute_reply":"2023-02-25T18:26:14.634900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential()\nmodel.add(tf.keras.Input(shape=(1,)))\nmodel.add(tf.keras.layers.Dense(1))\n","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:14.638256Z","iopub.execute_input":"2023-02-25T18:26:14.638835Z","iopub.status.idle":"2023-02-25T18:26:14.857676Z","shell.execute_reply.started":"2023-02-25T18:26:14.638797Z","shell.execute_reply":"2023-02-25T18:26:14.855891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Optionally, the first layer can receive an `input_shape` argument:\nmodel2 = tf.keras.Sequential()\nmodel2.add(tf.keras.layers.Dense(8, input_shape=(1,)))\n# Afterwards, we do automatic shape inference:\nmodel2.add(tf.keras.layers.Dense(1))\n","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:14.859230Z","iopub.execute_input":"2023-02-25T18:26:14.859605Z","iopub.status.idle":"2023-02-25T18:26:14.894436Z","shell.execute_reply.started":"2023-02-25T18:26:14.859572Z","shell.execute_reply":"2023-02-25T18:26:14.892551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:14.896287Z","iopub.execute_input":"2023-02-25T18:26:14.896693Z","iopub.status.idle":"2023-02-25T18:26:14.908669Z","shell.execute_reply.started":"2023-02-25T18:26:14.896657Z","shell.execute_reply":"2023-02-25T18:26:14.906396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:14.910758Z","iopub.execute_input":"2023-02-25T18:26:14.911106Z","iopub.status.idle":"2023-02-25T18:26:14.923553Z","shell.execute_reply.started":"2023-02-25T18:26:14.911073Z","shell.execute_reply":"2023-02-25T18:26:14.921717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:14.926417Z","iopub.execute_input":"2023-02-25T18:26:14.927025Z","iopub.status.idle":"2023-02-25T18:26:14.936750Z","shell.execute_reply.started":"2023-02-25T18:26:14.926972Z","shell.execute_reply":"2023-02-25T18:26:14.934587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = np.array([-3.0, -1.0, 0.0, 3.0, 5.0, 7.0], dtype=float)","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:14.939492Z","iopub.execute_input":"2023-02-25T18:26:14.940233Z","iopub.status.idle":"2023-02-25T18:26:14.949809Z","shell.execute_reply.started":"2023-02-25T18:26:14.940086Z","shell.execute_reply":"2023-02-25T18:26:14.947797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='sgd', loss='mse')\nmodel.fit(x, y, epochs=200)","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:14.952826Z","iopub.execute_input":"2023-02-25T18:26:14.953220Z","iopub.status.idle":"2023-02-25T18:26:17.460351Z","shell.execute_reply.started":"2023-02-25T18:26:14.953182Z","shell.execute_reply":"2023-02-25T18:26:17.459296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict([2, 3, 4, 10.0])","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:17.461803Z","iopub.execute_input":"2023-02-25T18:26:17.462136Z","iopub.status.idle":"2023-02-25T18:26:17.627699Z","shell.execute_reply.started":"2023-02-25T18:26:17.462100Z","shell.execute_reply":"2023-02-25T18:26:17.625920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.compile(optimizer='sgd', loss='mse')\nmodel2.fit(x, y, epochs=200)","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:26:17.630395Z","iopub.execute_input":"2023-02-25T18:26:17.631132Z","iopub.status.idle":"2023-02-25T18:26:19.739391Z","shell.execute_reply.started":"2023-02-25T18:26:17.631083Z","shell.execute_reply":"2023-02-25T18:26:19.738036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.predict([2, 3, 4, 10.0])","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:27:06.204052Z","iopub.execute_input":"2023-02-25T18:27:06.204471Z","iopub.status.idle":"2023-02-25T18:27:06.294056Z","shell.execute_reply.started":"2023-02-25T18:27:06.204437Z","shell.execute_reply":"2023-02-25T18:27:06.293259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.models.save_model","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:28:31.093883Z","iopub.execute_input":"2023-02-25T18:28:31.094368Z","iopub.status.idle":"2023-02-25T18:28:31.102996Z","shell.execute_reply.started":"2023-02-25T18:28:31.094326Z","shell.execute_reply":"2023-02-25T18:28:31.101408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tf.keras.models.save_model(model, \"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:34:59.909395Z","iopub.execute_input":"2023-02-25T18:34:59.909820Z","iopub.status.idle":"2023-02-25T18:34:59.915908Z","shell.execute_reply.started":"2023-02-25T18:34:59.909782Z","shell.execute_reply":"2023-02-25T18:34:59.914276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# help(tf.lite.TFLiteConverter.from_saved_model)\nhelp(tf.lite.TFLiteConverter.from_keras_model)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:35:10.745428Z","iopub.execute_input":"2023-02-25T18:35:10.745887Z","iopub.status.idle":"2023-02-25T18:35:10.751755Z","shell.execute_reply.started":"2023-02-25T18:35:10.745848Z","shell.execute_reply":"2023-02-25T18:35:10.751000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model_lite = tf.lite.TFLiteConverter.from_saved_model(\"model.h5\")\nconverter = tf.lite.TFLiteConverter.from_keras_model(model)\ntflite_model = converter.convert()","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:37:00.405568Z","iopub.execute_input":"2023-02-25T18:37:00.405954Z","iopub.status.idle":"2023-02-25T18:37:01.585857Z","shell.execute_reply.started":"2023-02-25T18:37:00.405921Z","shell.execute_reply":"2023-02-25T18:37:01.583768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"converter","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:37:07.122432Z","iopub.execute_input":"2023-02-25T18:37:07.122840Z","iopub.status.idle":"2023-02-25T18:37:07.131131Z","shell.execute_reply.started":"2023-02-25T18:37:07.122806Z","shell.execute_reply":"2023-02-25T18:37:07.129687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tflite_model","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:37:14.692333Z","iopub.execute_input":"2023-02-25T18:37:14.692743Z","iopub.status.idle":"2023-02-25T18:37:14.699092Z","shell.execute_reply.started":"2023-02-25T18:37:14.692706Z","shell.execute_reply":"2023-02-25T18:37:14.696948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.io.gfile.GFile('model.tflite', 'wb') as f:\n    f.write(tflite_model)","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:37:34.482751Z","iopub.execute_input":"2023-02-25T18:37:34.483158Z","iopub.status.idle":"2023-02-25T18:37:34.488490Z","shell.execute_reply.started":"2023-02-25T18:37:34.483124Z","shell.execute_reply":"2023-02-25T18:37:34.487691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! ls -ahl","metadata":{"execution":{"iopub.status.busy":"2023-02-25T18:37:54.767339Z","iopub.execute_input":"2023-02-25T18:37:54.767863Z","iopub.status.idle":"2023-02-25T18:37:55.044263Z","shell.execute_reply.started":"2023-02-25T18:37:54.767811Z","shell.execute_reply":"2023-02-25T18:37:55.043101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}