我的 tf.data.Dataset 物件的結構如下。((3, 400, 1), (3, 400, 1))
我想將每個元素的第 3 行中的元素除以 10。我的代碼如下。但它抱怨 NumPy 陣列是不可變的(我想使用map)
def alternate_row (dataset):
xx, yy = [], []
for x, y in dataset.as_numpy_iterator():
x[2] /= 10
y[2] /= 10
xx.append(x)
yy.append(y)
return xx, yy
uj5u.com熱心網友回復:
嘗試使用tf.data.Dataset.map和tf.concat:
import tensorflow as tf
samples = 5
x1 = tf.random.normal((samples, 3, 400, 1))
x2 = tf.random.normal((samples, 3, 400, 1))
dataset = tf.data.Dataset.from_tensor_slices((x1, x2))
def divide(x1, x2):
x1 = tf.concat([x1[:2], x1[2:] / 10], axis=0)
x2 = tf.concat([x2[:2], x2[2:] / 10], axis=0)
return x1, x2
dataset = dataset.map(divide)
請注意,我假設您想要更改張量的第二維中的值,但您可以更改切片的符號以滿足您的需要。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/487608.html
標籤:Python 麻木的 张量流 张量流数据集 tf.data.dataset
下一篇:在兩個數字之間生成亂數
