Skip to content

Instantly share code, notes, and snippets.

@chck
Created September 16, 2018 19:36
Show Gist options
  • Save chck/b26df1832e56d33e94bf8e3ad43d5f28 to your computer and use it in GitHub Desktop.
Save chck/b26df1832e56d33e94bf8e3ad43d5f28 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"https://github.com/hyoiutu/Miriabot_learning/tree/fe0f5cb7a48542c9e710853609fc0282fefeef42\n",
"\"\"\"\n",
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"from_vocab_size = 40000\n",
"to_vocab_size = 40000\n",
"\n",
"# We use a number of buckets and pad to the closest one for efficiency.\n",
"# See seq2seq_model.Seq2SeqModel for details of how they work.\n",
"_buckets = [(5, 10), (10, 15), (20, 25), (40, 50)]\n",
"\n",
"size = 1024\n",
"num_layers = 3\n",
"max_gradient_norm = 5.0\n",
"batch_size = 64\n",
"learning_rate = 0.5\n",
"learning_rate_decay_factor = 0.99\n",
"dtype = tf.float16 if True else tf.float32"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"from seq2seq_model import Seq2SeqModel\n",
"\n",
"def build_model():\n",
" return Seq2SeqModel(\n",
" from_vocab_size,\n",
" to_vocab_size,\n",
" _buckets,\n",
" size,\n",
" num_layers,\n",
" max_gradient_norm,\n",
" batch_size,\n",
" learning_rate,\n",
" learning_rate_decay_factor,\n",
" forward_only=False)\n",
"\n",
"# https://github.com/tensorflow/tensorflow/issues/11157#issuecomment-353725791\n",
"setattr(tf.contrib.rnn.GRUCell, '__deepcopy__', lambda self, _: self)\n",
"setattr(tf.contrib.rnn.BasicLSTMCell, '__deepcopy__', lambda self, _: self)\n",
"setattr(tf.contrib.rnn.MultiRNNCell, '__deepcopy__', lambda self, _: self)\n",
"\n",
"graph = tf.Graph()\n",
"with graph.as_default():\n",
" model = build_model()\n",
"\n",
"# with tf.Session(graph=tf.Graph()) as sess:\n",
"# model = build_model()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"seq2seq_model.Seq2SeqModel"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(model)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"# https://stackoverflow.com/questions/37718934/how-to-plot-the-tensorflow-neural-network-object\n",
"sess = tf.Session(graph=graph)\n",
"writer = tf.summary.FileWriter('/tmp/tensorboard_log', sess.graph)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.utils import plot_model\n",
"from tensorflow.python.keras.utils.vis_utils import model_to_dot\n",
"from IPython.display import SVG\n",
"import pydotplus as pydot"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'Seq2SeqModel' object has no attribute 'layers'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-45-52d09bb01d1a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mplot_model\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~/.anyenv/envs/pyenv/versions/3.5.5/lib/python3.5/site-packages/tensorflow/python/keras/utils/vis_utils.py\u001b[0m in \u001b[0;36mplot_model\u001b[0;34m(model, to_file, show_shapes, show_layer_names, rankdir)\u001b[0m\n\u001b[1;32m 146\u001b[0m \u001b[0;34m'LR'\u001b[0m \u001b[0mcreates\u001b[0m \u001b[0ma\u001b[0m \u001b[0mhorizontal\u001b[0m \u001b[0mplot\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 147\u001b[0m \"\"\"\n\u001b[0;32m--> 148\u001b[0;31m \u001b[0mdot\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel_to_dot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshow_shapes\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshow_layer_names\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrankdir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 149\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mextension\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplitext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mto_file\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 150\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mextension\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.anyenv/envs/pyenv/versions/3.5.5/lib/python3.5/site-packages/tensorflow/python/keras/utils/vis_utils.py\u001b[0m in \u001b[0;36mmodel_to_dot\u001b[0;34m(model, show_shapes, show_layer_names, rankdir)\u001b[0m\n\u001b[1;32m 78\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuilt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 79\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuild\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 80\u001b[0;31m \u001b[0mlayers\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlayers\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 81\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 82\u001b[0m \u001b[0;31m# Create graph nodes.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'Seq2SeqModel' object has no attribute 'layers'"
]
}
],
"source": [
"plot_model(model)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!tensorboard --logdir /tmp/tensorboard_log"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment