考虑下面的代码:
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。如何找到所有特征的最大值?
发布于 2021-10-18 08:03:00
问题的一种解决方案是使用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://stackoverflow.com/questions/69608765
复制相似问题