Skip to content

Instantly share code, notes, and snippets.

@shadiakiki1986
Created March 19, 2019 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadiakiki1986/173dd44fd7fec7c202605c596069e604 to your computer and use it in GitHub Desktop.
Save shadiakiki1986/173dd44fd7fec7c202605c596069e604 to your computer and use it in GitHub Desktop.
SO 51469446 bounty.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "SO 51469446 bounty.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/shadiakiki1986/173dd44fd7fec7c202605c596069e604/so-51469446-bounty.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"metadata": {
"id": "BTeyQ8XeGt3H",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# https://stackoverflow.com/questions/51469446/keras-and-error-setting-an-array-element-with-a-sequence\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense\n",
"from keras.optimizers import RMSprop\n",
"\n",
"def network():\n",
" model = Sequential()\n",
" model.add(Dense(units=50, activation='relu', input_dim=8)) # <<< input_dim is now 8\n",
" model.add(Dense(units=50, activation='relu'))\n",
" model.add(Dense(units=50, activation='relu'))\n",
" model.add(Dense(units=1, activation='softmax'))\n",
" opt = RMSprop(lr=0.00025)\n",
" model.compile(loss='mse', optimizer=opt)\n",
" return model\n",
"\n",
" \n",
" \n",
" \n",
"import pandas as pd\n",
"import random\n",
"\n",
"data = pd.DataFrame()\n",
"state = [0]*3\n",
"for i in range(3):\n",
" state[i]= random.choice([True, False])\n",
"move = random.randint(1,4)\n",
"reward = random.choice([-1, -10, 10])\n",
"future_state = [0]*3\n",
"for i in range(3):\n",
" future_state[i] = random.choice([True, False])\n",
"Q = 1\n",
"\n",
"# Flatten the arrays here\n",
"#\n",
"# vvvvvvvvvvvvvvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n",
"# vvvvvvvvvvvvvvvvvvvvvvvvvvvv vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv\n",
"array = [state[0], state[1], state[2], move, reward, future_state[0], future_state[1], future_state[2], Q]\n",
"\n",
"data = data.append([array])\n",
"\n",
"idx_target = len(array)-1 # <<< index of target is the last one\n",
"training = data.drop([idx_target], axis = 1)\n",
"target = data[idx_target]"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "cSZ_nngMHo1i",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "8de3f127-6996-4b87-8ae0-48740b03b606"
},
"cell_type": "code",
"source": [
"array"
],
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[False, False, True, 4, -10, False, True, False, 1]"
]
},
"metadata": {
"tags": []
},
"execution_count": 20
}
]
},
{
"metadata": {
"id": "WGJMyG5PHhBN",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 80
},
"outputId": "1559166e-d29f-4249-c570-86b01987a835"
},
"cell_type": "code",
"source": [
"training"
],
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" <th>2</th>\n",
" <th>3</th>\n",
" <th>4</th>\n",
" <th>5</th>\n",
" <th>6</th>\n",
" <th>7</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>False</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>4</td>\n",
" <td>-10</td>\n",
" <td>False</td>\n",
" <td>True</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 0 1 2 3 4 5 6 7\n",
"0 False False True 4 -10 False True False"
]
},
"metadata": {
"tags": []
},
"execution_count": 21
}
]
},
{
"metadata": {
"id": "vq78ey1eHjjY",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
},
"outputId": "27856fc7-70b1-4eb2-841c-6adf7f4f123e"
},
"cell_type": "code",
"source": [
"target"
],
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0 1\n",
"Name: 8, dtype: int64"
]
},
"metadata": {
"tags": []
},
"execution_count": 22
}
]
},
{
"metadata": {
"id": "80OCrqsEHhJf",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 107
},
"outputId": "d99db3f5-7b42-44b0-a056-f5b7a72813f7"
},
"cell_type": "code",
"source": [
"model = network()\n",
"model.fit(training,target,epochs=2)"
],
"execution_count": 23,
"outputs": [
{
"output_type": "stream",
"text": [
"Epoch 1/2\n",
"1/1 [==============================] - 0s 400ms/step - loss: 0.0000e+00\n",
"Epoch 2/2\n",
"1/1 [==============================] - 0s 2ms/step - loss: 0.0000e+00\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<keras.callbacks.History at 0x7fb5cea17630>"
]
},
"metadata": {
"tags": []
},
"execution_count": 23
}
]
},
{
"metadata": {
"id": "wHYj5f2fG9ZX",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment