HeNine 2 years ago
parent f701bff0a8
commit 4091c0e79e

@ -0,0 +1,5 @@
import tensorflow as tf
def my_loss_fn(y_true, y_pred):
return tf.reduce_mean(squared_difference, axis=-1) # Note the `axis=-1`

@ -0,0 +1,4 @@
from tensorflow import keras
input = keras.Input((2,))

@ -0,0 +1,155 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['🟥', '🟦', '🟧', '🟨', '🟩', '🟪', '🟫']\n"
]
}
],
"source": [
"squares = [chr(i) for i in range(0x1F7E5, 0x1F7EC)]\n",
"tfsquares = tf.constant(squares)\n",
"print(squares)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"def tensor_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"
]
},
{
"cell_type": "code",
"execution_count": 95,
"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",
"tf.Tensor(\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. 1. 0. 0. 1. 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. 1. 0. 0. 0. 0. 1. 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(tensor_to_string(a))\n",
"print(\"--------------\")\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"
]
}
],
"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.0"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading…
Cancel
Save