Skip to content

Instantly share code, notes, and snippets.

@sizhky
Created September 2, 2018 05:46
Show Gist options
  • Save sizhky/8706f2c84f858d6203c275ad6d2d06c4 to your computer and use it in GitHub Desktop.
Save sizhky/8706f2c84f858d6203c275ad6d2d06c4 to your computer and use it in GitHub Desktop.
keras activations in different place give different results
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"end_time": "2018-09-02T05:41:35.125566Z",
"start_time": "2018-09-02T05:41:35.118945Z"
},
"trusted": true
},
"cell_type": "code",
"source": "inpdim,outdim,batch,timesteps = 2, 3, 8, 4\n\nx = np.random.rand(batch, timesteps, inpdim)\n\ny = np.random.randint(0, outdim, size=batch*timesteps)\ny = y.reshape(batch, timesteps, 1)",
"execution_count": 8,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2018-09-02T05:41:35.809413Z",
"start_time": "2018-09-02T05:41:35.655003Z"
},
"trusted": true
},
"cell_type": "code",
"source": "model1 = Sequential([\n SimpleRNN(outdim, input_shape=(timesteps,inpdim), return_sequences=True, activation='softmax')\n])\n\nmodel1.compile(loss='categorical_crossentropy',optimizer=sgd())",
"execution_count": 9,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2018-09-02T05:41:36.982000Z",
"start_time": "2018-09-02T05:41:36.694834Z"
},
"cell_style": "center",
"trusted": true
},
"cell_type": "code",
"source": "model2 = Sequential([\n SimpleRNN(outdim, input_shape=(timesteps,inpdim), return_sequences=True),\n Activation('softmax')\n])\n\nmodel2.compile(loss='categorical_crossentropy',optimizer=sgd())\nmodel2.set_weights(model1.get_weights())",
"execution_count": 10,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2018-09-02T05:41:37.705218Z",
"start_time": "2018-09-02T05:41:37.493801Z"
},
"trusted": true
},
"cell_type": "code",
"source": "assert (model1.predict(x) == model2.predict(x)).all()",
"execution_count": 11,
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-11-e4885fc5d345>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mmodel1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mmodel2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"gist": {
"id": "6142c21a1466eeec98e85eb4ba5dd3c9",
"data": {
"description": "keras activations in different place give different results",
"public": true
}
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.6",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"_draft": {
"nbviewer_url": "https://gist.github.com/6142c21a1466eeec98e85eb4ba5dd3c9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment