如何準備資料以輸入到 tensorflow 模型(比如 keras Sequential 模型)?
我知道如何準備x_train、y_train和使用 numpyx_test和y_testscipy(最終是 pandassklearn風格),其中train/test資料是用于訓練神經模型的訓練和測驗資料,和x/y代表 2D 稀疏矩陣和表示整數標簽的 1D numpy 陣列大小與x資料中的原始數量相同。
到目前為止,我在資料集檔案中苦苦掙扎,但沒有太多見識……
到目前為止,我只能使用類似的東西將 scipy.sparse 矩陣轉換為tensorflow.SparseTensor
import numpy as np
import tensorflow as tf
from scipy import sparse as sp
x = sp.csr_matrix( ... )
x = tf.SparseTensor(indices=np.vstack([*x.nonzero()]).T,
values=x.data,
dense_shape=x.shape)
我可以使用類似的東西將 numpy 陣列轉換為tensorflow.Tensor
import numpy as np
import tensorflow as tf
y = np.array( ... ) # 1D array of len == x.shape[0]
y = tf.constant(y)
- 如何將
x和對齊y到單個資料集中以構建批處理、緩沖區……并從資料集實用程式中受益? - 我應該使用tensorflow.data.Dataset模塊的任何一個
zip,from_tensor_slices還是任何其他方法?
x和的例子y是
x = tf.SparseTensor(indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])
y = tf.constant(np.array(range(3)))
uj5u.com熱心網友回復:
您應該可以使用tf.data.Data.from_tensor_slices,因為您提到“y 是一個 1D numpy 陣列,表示與 x 資料中的行數相同大小的整數標簽”:
import tensorflow as tf
x = tf.SparseTensor(indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])
y = tf.constant(np.array(range(3)))
dataset = tf.data.Dataset.from_tensor_slices((x, y))
for x, y in dataset:
print(x, y)
SparseTensor(indices=tf.Tensor([[0]], shape=(1, 1), dtype=int64), values=tf.Tensor([1], shape=(1,), dtype=int32), dense_shape=tf.Tensor([4], shape=(1,), dtype=int64)) tf.Tensor(0, shape=(), dtype=int64)
SparseTensor(indices=tf.Tensor([[2]], shape=(1, 1), dtype=int64), values=tf.Tensor([2], shape=(1,), dtype=int32), dense_shape=tf.Tensor([4], shape=(1,), dtype=int64)) tf.Tensor(1, shape=(), dtype=int64)
SparseTensor(indices=tf.Tensor([], shape=(0, 1), dtype=int64), values=tf.Tensor([], shape=(0,), dtype=int32), dense_shape=tf.Tensor([4], shape=(1,), dtype=int64)) tf.Tensor(2, shape=(), dtype=int64)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/461730.html
標籤:Python 麻木的 张量流 scipy 张量流数据集
下一篇:將值添加到矩陣的下一行
