You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
838 B
Python
32 lines
838 B
Python
1 year ago
|
from math import pi
|
||
|
import tensorflow as tf
|
||
|
|
||
|
squares = [chr(i) for i in range(0x1F7E5, 0x1F7EC)]
|
||
|
tfsquares = tf.constant(squares)
|
||
|
colors = tf.constant([10, 219, 38, 48, 80, 282, 20] *
|
||
|
tf.constant(pi/180.0), dtype=tf.float32)
|
||
|
color_vectors = tf.transpose(
|
||
|
tf.stack([
|
||
|
tf.math.cos(colors),
|
||
|
tf.math.sin(colors)]
|
||
|
)
|
||
|
)
|
||
|
|
||
|
|
||
|
def vector_to_index(tensor):
|
||
|
return tf.argmax((tf.einsum("...ijk,lk->...ijl", tensor, color_vectors)), axis=-1)
|
||
|
|
||
|
|
||
|
def index_to_string(tensor):
|
||
|
tfstring = tf.strings.join(
|
||
|
tf.map_fn(
|
||
|
lambda v:
|
||
|
tf.strings.join(
|
||
|
tf.gather(tfsquares, tf.cast(v, tf.int64))), tensor, fn_output_signature=tf.string), "\n")
|
||
|
|
||
|
return tfstring.numpy().decode()
|
||
|
|
||
|
|
||
|
def vector_to_string(tensor):
|
||
|
return index_to_string(vector_to_index(tensor))
|