考慮下面的代碼:
import tensorflow as tf
input_slice=3
labels_slice=2
def split_window(x):
inputs = tf.slice(x,[0], [input_slice])
labels = tf.slice(x,[input_slice], [labels_slice])
return inputs, labels
dataset = tf.data.Dataset.range(1, 25 1).batch(5).map(split_window)
for i, j in dataset:
print(i.numpy(),end="->")
print(j.numpy())
這段代碼會給我輸出:
[1 2 3]->[4 5]
[6 7 8]->[ 9 10]
[11 12 13]->[14 15]
[16 17 18]->[19 20]
[21 22 23]->[24 25]
張量中的每一行j代表一個特征。我想找到所有功能的最大值。在這種情況下,它將是 25。我如何找到所有特征的最大值?
uj5u.com熱心網友回復:
您的問題的一種解決方案是使用tf.TensorArray和tf.reduce_max:
import tensorflow as tf
input_slice=3
labels_slice=2
def split_window(x):
inputs = tf.slice(x,[0], [input_slice])
labels = tf.slice(x,[input_slice], [labels_slice])
return inputs, labels
dataset = tf.data.Dataset.range(1, 25 1).batch(5).map(split_window)
ta = tf.TensorArray(tf.int64, size=0, dynamic_size=True)
for i, j in dataset:
print(i.shape, i.numpy(),end="->")
print(j.numpy())
ta.write(ta.size(), j)
max_value = tf.reduce_max(ta.stack(), axis=(0, 1).numpy()
print(max_value)
# 25
隨著tf.reduce_max您獲得跨維度 0 和 1 的最大值并減少您的張量。如果我沒有正確理解問題,請隨時提供一些反饋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/328253.html
