{"cells":[{"metadata":{"_uuid":"4c3b0901-26e1-4514-9433-5b9bee58bf08","_cell_guid":"6e4a4809-477d-44db-9726-91ce766db5c2","trusted":true},"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)\nimport keras as ks\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, BatchNormalization\nfrom keras.callbacks import EarlyStopping\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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainX = pd.read_csv(\"../input/the-game-of-life-more-data/train_delta3_source.csv\", index_col=False).drop(['Unnamed: 0'], axis=1)\ntrainX.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainY = pd.read_csv(\"../input/the-game-of-life-more-data/train_delta3_target.csv\", index_col=False).drop(['Unnamed: 0'], axis=1)\ntrainY.shape[0]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"def createModel():\n    nn = Sequential()\n    nn.add(Conv2D(256, 5, padding='same', activation='relu', input_shape=(25, 25, 1)))\n    nn.add(BatchNormalization())\n    for i in range(6):\n        nn.add(Conv2D(256, 5, padding='same', activation='relu'))\n        nn.add(BatchNormalization())\n    nn.add(Conv2D(1, 5, padding='same', activation='sigmoid'))\n    nn.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n    return nn\n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Training"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = createModel()\nes = EarlyStopping(monitor='loss', patience=9, min_delta=0.001)\nmodel.fit(np.array(trainX.values.reshape(-1, 25, 25, 1)), np.array(trainY.values.reshape(-1, 25, 25, 1)), batch_size=50, epochs=50, verbose=1, callbacks=[es])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sampleSubmitData = pd.read_csv(\"../input/conways-reverse-game-of-life-2020/sample_submission.csv\", index_col=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testData = pd.read_csv(\"../input/conways-reverse-game-of-life-2020/test.csv\", index_col=0)\ntestData = testData[testData.delta == 3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = testData.iloc[:, 1:].values.reshape(-1, 25, 25, 1)\nsubmit = model.predict(test).reshape(-1, 625).round(0).astype('uint8')\nsubmitData = pd.DataFrame(data=submit, index=testData.index, columns=sampleSubmitData.columns)\nsubmitData.to_csv(\"submit3.csv\")","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}