Created
February 16, 2018 04:47
-
-
Save graph226/eefcaa673835916e30f338fc5a003c09 to your computer and use it in GitHub Desktop.
えんべっでぃんぐるっくあっぷ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import tensorflow as tf\n", | |
| "from tensorflow import contrib\n", | |
| "import numpy as np\n", | |
| "import pickle" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "keys = tf.Variable([\"1\", \"2\", \"3\"], dtype=tf.string)\n", | |
| "values = tf.Variable([1, 2, 3], dtype=tf.float32)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "table = contrib.lookup.HashTable(\n", | |
| " contrib.lookup.KeyValueTensorInitializer(keys, values), -1)\n", | |
| "out = table.lookup(names_tf)\n", | |
| "with tf.Session() as sess:\n", | |
| " table.init.run()\n", | |
| " print(out.eval())" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "source": [ | |
| "## これはめも\n", | |
| "\n", | |
| "https://qiita.com/kzmssk/items/ddf2c0f956a5d26e992a" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 93, | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([28918, 3294, 28918, 28918, 19501, 4847])" | |
| ] | |
| }, | |
| "execution_count": 93, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "sess = tf.InteractiveSession()\n", | |
| "mapping_strings = tf.constant(all_words)\n", | |
| "table = tf.contrib.lookup.index_table_from_tensor(\n", | |
| " mapping=mapping_strings, num_oov_buckets=1, default_value=-1)\n", | |
| "features = tf.constant([\"emerson\", \"feel\", \"and\", \"palmer\", \"deep\", \"popcorn\"])\n", | |
| "ids = table.lookup(features)\n", | |
| "tf.tables_initializer().run()\n", | |
| "\n", | |
| "ids.eval()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "with open('positive_example.pickle', 'rb') as f:\n", | |
| " positive_example = pickle.load(f)\n", | |
| "with open('all_words.pickle', 'rb') as f:\n", | |
| " all_words = pickle.load(f)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "TOTAL_SIZE = len(all_words)\n", | |
| "DIM = 46\n", | |
| "BATCH_SIZE = 2\n", | |
| "np_x = np.random.rand(TOTAL_SIZE, DIM).astype(np.float32)\n", | |
| "x = tf.Variable(np_x)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "[[ 0.33407766 0.66344464 0.13722664 0.57478184 0.48289245 0.72210312\n", | |
| " 0.47872588 0.71832132 0.22690746 0.9794398 0.23006117 0.77183402\n", | |
| " 0.76896125 0.72980267 0.32537961 0.40896854 0.60130352 0.78982878\n", | |
| " 0.03451943 0.72029352 0.63087696 0.63158554 0.25121796 0.94101858\n", | |
| " 0.46409729 0.34709042 0.22485785 0.78404438 0.33605623 0.79317814\n", | |
| " 0.04439238 0.22641681 0.4303872 0.34324634 0.06531932 0.80964732\n", | |
| " 0.97386193 0.42601633 0.73470759 0.18472442 0.78394419 0.3850196\n", | |
| " 0.20360945 0.81814951 0.34891433 0.97032487]\n", | |
| " [ 0.36110488 0.25897187 0.23618111 0.97066313 0.38464648 0.70196772\n", | |
| " 0.99959099 0.47679022 0.05685244 0.98868525 0.33194125 0.50208384\n", | |
| " 0.75492752 0.96577746 0.40557855 0.46122807 0.76450753 0.0459351\n", | |
| " 0.275621 0.24587847 0.67910224 0.63183731 0.61231363 0.17298323\n", | |
| " 0.58105111 0.70024359 0.74448323 0.70820373 0.75346893 0.48200104\n", | |
| " 0.85238212 0.60653716 0.52646261 0.26115027 0.15900189 0.70963442\n", | |
| " 0.04609635 0.70944047 0.79803944 0.46148825 0.61928189 0.45201811\n", | |
| " 0.88260871 0.56540591 0.14678472 0.64773971]]\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "np_inds = np.array([0, 3])\n", | |
| "inds = tf.placeholder(tf.int32, [BATCH_SIZE])\n", | |
| "\n", | |
| "h = tf.nn.embedding_lookup(x, inds)\n", | |
| "with tf.Session() as sess:\n", | |
| " sess.run(tf.global_variables_initializer())\n", | |
| " print(sess.run(h, feed_dict={inds: np_inds}))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 90, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[ 0.61467636, 0.40071121, 0.99041861, ..., 0.77075809,\n", | |
| " 0.20176312, 0.79317933],\n", | |
| " [ 0.76744157, 0.52253145, 0.53163719, ..., 0.15996242,\n", | |
| " 0.84362042, 0.72988248],\n", | |
| " [ 0.43286699, 0.63465619, 0.28215456, ..., 0.30870688,\n", | |
| " 0.45839456, 0.45931903],\n", | |
| " ..., \n", | |
| " [ 0.60267776, 0.55912673, 0.7641086 , ..., 0.53222561,\n", | |
| " 0.79974645, 0.65310097],\n", | |
| " [ 0.87345588, 0.31659752, 0.38947991, ..., 0.60199815,\n", | |
| " 0.89751315, 0.31559569],\n", | |
| " [ 0.56062859, 0.37648842, 0.68439686, ..., 0.83632123,\n", | |
| " 0.73119318, 0.5668627 ]], dtype=float32)" | |
| ] | |
| }, | |
| "execution_count": 90, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "np_x" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "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.6.1" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment