Skip to content

Instantly share code, notes, and snippets.

@yuzeh
Created July 24, 2018 00:30
Show Gist options
  • Save yuzeh/0d4437f5557489987b7be3038bd5c58e to your computer and use it in GitHub Desktop.
Save yuzeh/0d4437f5557489987b7be3038bd5c58e to your computer and use it in GitHub Desktop.
2018-07-23-Keras-sample_weight.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "2018-07-23-Keras-sample_weight.ipynb",
"version": "0.3.2",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"[View in Colaboratory](https://colab.research.google.com/gist/yuzeh/0d4437f5557489987b7be3038bd5c58e/2018-07-23-keras-sample_weight.ipynb)"
]
},
{
"metadata": {
"id": "rj86XI8Rpd9v",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"import keras\n",
"import numpy as np"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "EhEsaQjCpgUM",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"outputId": "85ae3cc2-0791-41c3-f2bb-b2c3bb024a57"
},
"cell_type": "code",
"source": [
"# I'm trying to figure out how sample_weight works.\n",
"\n",
"# - learn model with two heads, y1 := x, y2 := -x\n",
"\n",
"input = keras.models.Input((1,))\n",
"\n",
"out1 = keras.layers.Dense(1, name = 'y1')(input)\n",
"out2 = keras.layers.Dense(1, name = 'y2')(input)\n",
"\n",
"model = keras.models.Model(inputs = [input], outputs = [out1, out2])\n",
"\n",
"xs = np.array([0, 1, 2, 3, 4, 5]).astype(float)\n",
"y1 = xs\n",
"y2 = -xs\n",
"\n",
"kwargs = dict(\n",
" x = xs,\n",
" y = dict(y1 = y1, y2 = y2),\n",
" sample_weight = None,\n",
" batch_size = 3,\n",
" epochs = 200,\n",
" verbose = 0)\n",
"\n",
"model.compile('sgd', 'mse')\n",
"model.fit(**kwargs)\n",
"\n",
"model.get_weights()"
],
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[array([[0.9956291]], dtype=float32),\n",
" array([0.01543758], dtype=float32),\n",
" array([[-0.99854046]], dtype=float32),\n",
" array([-0.00515498], dtype=float32)]"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
},
{
"metadata": {
"id": "LfD-OE6vpy9O",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"outputId": "b63b34a3-c828-4fd4-8987-fe8c4f9f33a8"
},
"cell_type": "code",
"source": [
"# Okay this looks about right.\n",
"\n",
"# - learn model with two heads, y1 := x, y2 := -x\n",
"# - add sample_weight to model.fit\n",
"\n",
"input = keras.models.Input((1,))\n",
"\n",
"out1 = keras.layers.Dense(1, name = 'y1')(input)\n",
"out2 = keras.layers.Dense(1, name = 'y2')(input)\n",
"\n",
"model = keras.models.Model(inputs = [input], outputs = [out1, out2])\n",
"\n",
"xs = np.array([0, 1, 2, 3, 4, 5]).astype(float)\n",
"y1 = xs\n",
"y2 = -xs\n",
"\n",
"kwargs = dict(\n",
" x = xs,\n",
" y = dict(y1 = y1, y2 = y2),\n",
" sample_weight = None,\n",
" batch_size = 3,\n",
" epochs = 200,\n",
" verbose = 0)\n",
"\n",
"kwargs['sample_weight'] = dict(\n",
" y1 = np.ones(6),\n",
" y2 = np.ones(6),\n",
")\n",
"\n",
"model.compile('sgd', 'mse')\n",
"model.fit(**kwargs)\n",
"\n",
"model.get_weights()"
],
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[array([[1.0023069]], dtype=float32),\n",
" array([-0.00831777], dtype=float32),\n",
" array([[-0.99394155]], dtype=float32),\n",
" array([-0.02184387], dtype=float32)]"
]
},
"metadata": {
"tags": []
},
"execution_count": 8
}
]
},
{
"metadata": {
"id": "FDQSVzqOqSyX",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"outputId": "972e1b08-3a22-410f-e93d-645f68076182"
},
"cell_type": "code",
"source": [
"# So far so good. This is where things get weird:\n",
"\n",
"# - learn model with two heads, y1 := x, y2 := -x\n",
"# - add sample_weight to model.fit\n",
"# - drop half of the training examples by setting sample_weight = 0 for each head.\n",
"\n",
"input = keras.models.Input((1,))\n",
"\n",
"out1 = keras.layers.Dense(1, name = 'y1')(input)\n",
"out2 = keras.layers.Dense(1, name = 'y2')(input)\n",
"\n",
"model = keras.models.Model(inputs = [input], outputs = [out1, out2])\n",
"\n",
"xs = np.array([0, 1, 2, 3, 4, 5]).astype(float)\n",
"y1 = xs\n",
"y2 = -xs\n",
"\n",
"kwargs = dict(\n",
" x = xs,\n",
" y = dict(y1 = y1, y2 = y2),\n",
" sample_weight = None,\n",
" batch_size = 3,\n",
" epochs = 200,\n",
" verbose = 0)\n",
"\n",
"kwargs['sample_weight'] = dict(\n",
" y1 = np.ones(6),\n",
" y2 = np.ones(6),\n",
")\n",
"\n",
"kwargs['sample_weight']['y1'][[0, 2, 4]] = 0.\n",
"kwargs['sample_weight']['y2'][[1, 3, 5]] = 0.\n",
"\n",
"model.compile('sgd', 'mse')\n",
"model.fit(**kwargs)\n",
"\n",
"model.get_weights()"
],
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[array([[nan]], dtype=float32),\n",
" array([nan], dtype=float32),\n",
" array([[nan]], dtype=float32),\n",
" array([nan], dtype=float32)]"
]
},
"metadata": {
"tags": []
},
"execution_count": 9
}
]
},
{
"metadata": {
"id": "wZhjjJ0ArdNI",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "34c2f974-68d5-491b-d5b5-57ced93cd96f"
},
"cell_type": "code",
"source": [
"# WTF???\n",
"\n",
"# Okay, how about just one head instead of two??\n",
"\n",
"input = keras.models.Input((1,))\n",
"\n",
"out1 = keras.layers.Dense(1, name = 'y1')(input)\n",
"\n",
"model = keras.models.Model(inputs = [input], outputs = [out1])\n",
"\n",
"xs = np.array([0, 1, 2, 3, 4, 5]).astype(float)\n",
"y1 = xs\n",
"\n",
"kwargs = dict(\n",
" x = xs,\n",
" y = dict(y1 = y1),\n",
" sample_weight = None,\n",
" batch_size = 3,\n",
" epochs = 200,\n",
" verbose = 0)\n",
"\n",
"kwargs['sample_weight'] = dict(\n",
" y1 = np.ones(6),\n",
")\n",
"\n",
"kwargs['sample_weight']['y1'][[0, 2, 4]] = 0.\n",
"\n",
"model.compile('sgd', 'mse')\n",
"model.fit(**kwargs)\n",
"\n",
"model.get_weights()"
],
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[array([[nan]], dtype=float32), array([nan], dtype=float32)]"
]
},
"metadata": {
"tags": []
},
"execution_count": 10
}
]
},
{
"metadata": {
"id": "1QW2RXWgrtX7",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# At this point I'm pretty sure I don't understand how sample_weight is supposed to be used.\n",
"# Someone please help me!"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "z3tW0Fl9r2SX",
"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