Skip to content

Instantly share code, notes, and snippets.

@DrSleep
Created March 19, 2017 13:53
Show Gist options
  • Save DrSleep/78c4e648343ba11ff32ceb2236b9f806 to your computer and use it in GitHub Desktop.
Save DrSleep/78c4e648343ba11ff32ceb2236b9f806 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"## given a variable v0 of shape (h, w, c0),\n",
"## we would like to create a new variable v1 of shape (h, w, c1)\n",
"## such that (c1 > c0), and v1[:, :, :c0] == v0\n",
"\n",
"## here h = 5, w = 4, c0 = 3, c1 = 4\n",
"graph = tf.Graph()\n",
"\n",
"with graph.as_default():\n",
" v0 = tf.Variable(np.ones(shape=(5, 4, 3)))\n",
" v1 = tf.Variable(np.zeros(shape=(5, 4, 4)))\n",
" ## split v1 through the last axie\n",
" v1_splits = tf.split(value=v1, num_or_size_splits=4, axis=2)\n",
" ## replace first v1[:,:, :3] values with the copy of v0 values\n",
" assign_op = v1.assign(tf.concat([v0] + v1_splits[3:], axis=2))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"v0: [[[ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]]\n",
"\n",
" [[ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]]\n",
"\n",
" [[ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]]\n",
"\n",
" [[ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]]\n",
"\n",
" [[ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]\n",
" [ 1. 1. 1.]]]\n",
"\n",
"v1 (before assignment): [[[ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]]\n",
"\n",
" [[ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]]\n",
"\n",
" [[ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]]\n",
"\n",
" [[ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]]\n",
"\n",
" [[ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0.]]]\n",
"\n",
"v1 (after assignment): [[[ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]]\n",
"\n",
" [[ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]]\n",
"\n",
" [[ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]]\n",
"\n",
" [[ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]]\n",
"\n",
" [[ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]\n",
" [ 1. 1. 1. 0.]]]\n",
"\n"
]
}
],
"source": [
"with tf.Session(graph=graph) as sess:\n",
" sess.run(tf.global_variables_initializer())\n",
" graph.finalize()\n",
" print(\"v0: %s\\n\" % sess.run(v0))\n",
" print(\"v1 (before assignment): %s\\n\" % sess.run(v1))\n",
" sess.run(assign_op)\n",
" print(\"v1 (after assignment): %s\\n\" % sess.run(v1))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment