{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "import random\n", "from math import pi" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['🟥', '🟦', '🟧', '🟨', '🟩', '🟪', '🟫']\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares = [chr(i) for i in range(0x1F7E5, 0x1F7EC)]\n", "tfsquares = tf.constant(squares)\n", "print(squares)\n", "colors = tf.constant([10, 219, 38, 48, 80, 282, 20] * tf.constant(pi/180.0), dtype=tf.float32)\n", "color_vectors = tf.transpose(\n", " tf.stack([\n", " tf.math.cos(colors),\n", " tf.math.sin(colors)]\n", " )\n", ")\n", "color_vectors" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def one_hot_to_string(tensor):\n", " square_select = tf.math.argmax(tensor, 2)\n", "\n", " tfstring = tf.strings.join(\n", " tf.map_fn(\n", " lambda v:\n", " tf.strings.join(\n", " tf.gather(tfsquares, tf.cast(v, tf.int64))), square_select, fn_output_signature=tf.string), \"\\n\")\n", "\n", " return tfstring.numpy().decode()\n", "\n", "def index_to_string(tensor):\n", " return tf.argmax((tf.einsum(\"ijk,lk->ijl\", tensor, color_vectors)), axis=2)\n" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vi = tf.map_fn(lambda v: tf.map_fn(lambda s: color_vectors[s],v, fn_output_signature=tf.float32),tf.constant(e, shape=(8, 8)),fn_output_signature=tf.float32)\n", "\n", "index_to_string(vi)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# def colors_to_one_hot(tensor):\n", "vi = tf.map_fn(lambda v: tf.map_fn(lambda s: color_vectors[s],v, fn_output_signature=tf.float32),tf.constant(e, shape=(8, 8)),fn_output_signature=tf.float32)\n", "ns = tf.image.extract_patches(tf.constant(vi, shape=(1,8,8,2)), (1,3,3,1), (1,1,1,1), (1,1,1,1), padding=\"SAME\")\n", "virs = tf.reshape(vi, (8*8,2))\n", "# tf.reduce_sum(tf.multiply(tf.repeat(vi, 9, -1), ns))\n", "tf.abs(tf.einsum(\"...k,...k->...\", tf.repeat(vi, 9, -1), ns))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor([0. 0. 0. 1. 0. 0. 0.], shape=(7,), dtype=float32)\n", "tf.Tensor([[3]], shape=(1, 1), dtype=int64)\n", "🟨🟥🟨🟨🟩🟫🟪🟫\n", "🟩🟪🟧🟧🟦🟧🟦🟫\n", "🟩🟦🟫🟨🟦🟨🟩🟫\n", "🟪🟫🟩🟨🟧🟪🟥🟩\n", "🟥🟫🟨🟫🟧🟦🟦🟧\n", "🟩🟫🟫🟧🟨🟨🟪🟫\n", "🟩🟪🟦🟥🟧🟫🟧🟨\n", "🟨🟪🟨🟧🟪🟦🟨🟧\n", "--------------\n", "🟧🟨🟦🟪🟧🟨🟪🟨\n", "🟨🟧🟫🟧🟥🟦🟪🟩\n", "🟫🟪🟨🟨🟧🟫🟫🟩\n", "🟧🟦🟦🟧🟫🟨🟫🟥\n", "🟩🟥🟪🟧🟨🟩🟫🟪\n", "🟫🟩🟨🟦🟨🟫🟦🟩\n", "🟫🟦🟧🟦🟧🟧🟪🟩\n", "🟫🟪🟫🟩🟨🟨🟥🟨\n", "tf.Tensor(\n", "[[0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 1. 0. 0. 1. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0.]\n", " [0. 0. 0. 0. 0. 0. 0. 0.]], shape=(8, 8), dtype=float32)\n" ] } ], "source": [ "oh = tf.one_hot(3, 7)\n", "print(oh)\n", "print(tf.where(oh))\n", "\n", "# a = tf.constant([[list(tf.one_hot(random.randrange(7), 7)) for i in range(8) ] for i in range(8)], shape=(8,8,len(squares)))\n", "\n", "# print([[list(tf.one_hot(random.randrange(7), 7)) for i in range(8) ] for i in range(8)])\n", "e = [[random.randrange(7) for i in range(8)] for i in range(8)]\n", "# print(e)\n", "\n", "a = tf.one_hot(tf.constant(e, shape=(8, 8)), len(squares))\n", "\n", "print(one_hot_to_string(a))\n", "# print(\"--------------\")\n", "# print(one_hot_to_string(tf.reverse(a, axis=(-2,))))\n", "# print(\"--------------\")\n", "# print(one_hot_to_string(tf.reverse(a, axis=(-3,))))\n", "# print(\"--------------\")\n", "# print(one_hot_to_string(tf.transpose(a, perm=(1,0,2))))\n", "print(\"--------------\")\n", "# print(one_hot_to_string(tf.reverse(tf.transpose(tf.reverse(a, axis=(-2,)), perm=(1,0,2)), axis=(-2,))))\n", "print(one_hot_to_string(tf.reverse(a, axis=(-2,-3,))))\n", "# print(tensor_to_string(tf.transpose(a, [1, 0, 2])))\n", "# print(\"--------------\")\n", "# print(tensor_to_string(tf.linalg.matmul( a , tf.constant(\n", "# [\n", "# [0, 0, 0, 0, 0, 0, 0, 1],\n", "# [0, 0, 0, 0, 0, 0, 1, 0],\n", "# [0, 0, 0, 0, 0, 1, 0, 0],\n", "# [0, 0, 0, 0, 1, 0, 0, 0],\n", "# [0, 0, 0, 1, 0, 0, 0, 0],\n", "# [0, 0, 1, 0, 0, 0, 0, 0],\n", "# [0, 1, 0, 0, 0, 0, 0, 0],\n", "# [1, 0, 0, 0, 0, 0, 0, 0]\n", "# ], dtype=tf.float32, shape=(8,8,1)\n", "# ), transpose_a=True)))\n", "# print(\"--------------\")\n", "# print(tensor_to_string(tf.reverse(a, (0,))))\n", "# print(\"--------------\")\n", "# print(tensor_to_string(tf.reverse(a, (1,))))\n", "# print(tensor_to_string(\n", "# tf.linalg.matmul(\n", "# tf.transpose(a, [0, 2, 1]),\n", "# tf.reverse(a, (1,)))\n", "# ))\n", "print(\n", " tf.einsum(\n", " \"ijk,ijk->ij\",\n", " a,\n", " tf.reverse(a, (1,)))\n", ")\n", "\n", "\n", "# print(tf.constant(range(6), shape=(2,3)))\n", "# tf.map_fn(tf.math.reduce_sum, tf.constant(range(6), shape=(2,3)))\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import aesthetic_loss\n", "\n", "es = [[[random.randrange(7) for i in range(8)]\n", " for i in range(8)] for j in range(16)]\n", "\n", "vis = tf.stack(\n", " [tf.map_fn(lambda v: tf.map_fn(lambda s: color_vectors[s], v, fn_output_signature=tf.float32), tf.constant(\n", " e, shape=(8, 8)), fn_output_signature=tf.float32) for e in es]\n", ")\n", "\n", "aesthetic_loss.aesthetic_loss(vis)\n", "# aesthetic_loss.compute_score(vis, vis)\n", "# tf.linalg.l2_normalize(vis, axis=-1)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = tf.Variable(2)\n", "y = tf.Variable(3)\n", "\n", "xp = tf.pow(x, [0,1,2,3])\n", "yp = tf.pow(y, [0,1,2,3])\n", "\n", "A = tf.Variable([[i+j for i in range(4)] for j in range(4)], shape=(4,4))\n", "tf.einsum(\"i,ij,j\", xp, A, yp)\n" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = tf.random.normal((16,2))\n", "rp = tf.map_fn(lambda v: tf.pow(tf.repeat(tf.reshape(v, shape=(2,1)), repeats=4, axis=1), tf.constant([[0,1,2,3], [0,1,2,3]], dtype=tf.float32)), r)\n", "A = tf.Variable(tf.reshape([float(i) for i in range(8*4*4)], shape=(8,4,4)), shape=(8,4,4), dtype=tf.float32)\n", "tf.einsum(\"ik,ij,lkj->il\", rp[:,0], rp[:,1], A)\n", "# rp[:,0]\n" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tf.Tensor(\n", "[[ 4.15302128e-01 -1.17927468e+00 2.49658436e-01 6.19551420e-01\n", " -1.39033377e-01 2.79209971e-01 3.97601783e-01 -2.17277408e-01]\n", " [ 1.74276757e+00 5.69359064e-02 5.38259745e-04 1.49045396e+00\n", " -7.87080288e-01 -3.98119450e-01 1.18716180e-01 1.86502957e+00]\n", " [ 1.78081155e+00 -1.29199183e+00 -5.72377980e-01 1.73489356e+00\n", " -1.81523621e+00 -7.56731749e-01 5.50791979e-01 2.23066378e+00]\n", " [-2.86107287e-02 2.38285348e-01 -2.90394664e-01 2.20198780e-02\n", " 3.43291536e-02 2.77290910e-01 -3.25144343e-02 3.50418538e-01]\n", " [-2.32135087e-01 1.95894271e-01 5.75335659e-02 -1.76543556e-03\n", " -4.01449911e-02 2.00845733e-01 -9.64274257e-02 2.31855541e-01]\n", " [ 4.60646629e-01 7.75275290e-01 -9.96064782e-01 -5.15016496e-01\n", " 2.03099363e-02 7.87942708e-02 -3.07008505e-01 7.16598153e-01]\n", " [ 3.02803397e-01 -8.83126408e-02 3.72119397e-01 1.14753373e-01\n", " -8.30153376e-03 6.69163764e-02 -3.60930443e-01 4.32033747e-01]\n", " [-1.55036688e-01 2.96032988e-02 5.69820963e-02 -1.04342289e-01\n", " 2.18345195e-01 1.60513148e-01 -2.30654161e-02 2.92460948e-01]\n", " [-9.37448144e-02 4.92064208e-02 1.25066668e-01 -1.08998209e-01\n", " 1.47912696e-01 7.13704750e-02 -9.69988406e-02 3.16246897e-01]\n", " [ 3.76237392e-01 -8.38992000e-02 3.31230164e-01 2.86825836e-01\n", " 3.67110878e-01 -3.61552715e-01 6.06793404e-01 -1.22976625e+00]\n", " [-1.00743435e-01 -1.46258920e-01 -1.82411104e-01 -3.14753801e-01\n", " 7.92229354e-01 2.98170269e-01 -1.82591528e-01 2.78764069e-01]\n", " [ 3.58289123e-01 1.97965109e+00 -1.18626821e+00 -1.47391105e+00\n", " 6.11245155e-01 -4.61193591e-01 -7.64733970e-01 1.45492101e+00]\n", " [-2.00078160e-01 4.32360053e-01 1.63023725e-01 -3.31694707e-02\n", " -4.53773856e-01 4.14216101e-01 1.76714227e-01 2.12158859e-01]\n", " [ 4.91659880e-01 1.56133473e+00 -8.19299459e-01 9.75016415e-01\n", " -2.52336192e+00 1.69403172e+00 2.05944443e+00 1.03490460e+00]\n", " [-2.33240795e+00 6.64168453e+00 -4.04779243e+00 -5.99489832e+00\n", " 1.60179734e+00 2.19676828e+00 4.11457443e+00 2.81916380e-01]\n", " [ 1.73429586e-02 4.88152355e-02 3.41429591e-01 -2.39478126e-02\n", " -2.88104061e-02 1.31737068e-01 -2.55863100e-01 2.94781595e-01]], shape=(16, 8), dtype=float32)\n" ] } ], "source": [ "import polymap\n", "r = tf.random.normal((16,2))\n", "\n", "pm = polymap.Polymap(8,4)\n", "# pm.build([16,2])\n", "print(pm(r))\n" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r = tf.random.normal(shape = (16, 8, 8, 2))\n", "tf.linalg.l2_normalize(r,axis=-1)" ] } ], "metadata": { "kernelspec": { "display_name": ".patch_gen_venv", "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.10.9" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }