def higher_order_tensors(step):
# We're not limited to passing scalar tensors to the summary
# operation. If we pass a rank-1 or rank-2 tensor, it'll be visualized
# as a table in TensorBoard. (For higher-ranked tensors, you'll see
# just a 2D slice of the data.)
#
# To demonstrate this, let's create a multiplication table.
# First, we'll create the table body, a `step`-by-`step` array of
# strings.
numbers = tf.range(step)
numbers_row = tf.expand_dims(numbers, 0) # shape: [1, step]
numbers_column = tf.expand_dims(numbers, 1) # shape: [step, 1]
products = tf.matmul(numbers_column, numbers_row) # shape: [step, step]
table_body = tf.as_string(products)
# Next, we'll create a header row and column, and a little
# multiplication sign to put in the corner.
bold_numbers = tf.string_join(['**', tf.as_string(numbers), '**'])
bold_row = tf.expand_dims(bold_numbers, 0)
bold_column = tf.expand_dims(bold_numbers, 1)
corner_cell = tf.constant(u'\u00d7'.encode('utf-8')) # MULTIPLICATION SIGN
# Now, we have to put the pieces together. Using `axis=0` stacks
# vertically; using `axis=1` juxtaposes horizontally.
table_body_and_top_row = tf.concat([bold_row, table_body], axis=0)
table_left_column = tf.concat([[[corner_cell]], bold_column], axis=0)
table_full = tf.concat([table_left_column, table_body_and_top_row], axis=1)
# The result, `table_full`, is a rank-2 string tensor of shape
# `[step + 1, step + 1]`. We can pass it directly to the summary, and
# we'll get a nicely formatted table in TensorBoard.
tf.summary.text('multiplication_table', table_full)
评论列表
文章目录