实验性 Tensorflow 数据集的形状问题
Problem with shapes of experimental Tensorflow dataset
我正在尝试将 numpy 数组存储在 Tensorflow 数据集中。该模型在使用 numpy 数组作为训练和测试数据时正确拟合,但当我 将 numpy 数组存储在单个 Tensorflow 数据集 中时则不正确。问题在于数据集的维度。虽然乍一看形状还不错,但有些地方不对。
在尝试了多种方法来重塑我的 Tensorflow 数据集之后,我仍然无法让它工作。我的代码如下:
train_x.shape
Out[54]: (7200, 40)
train_y.shape
Out[55]: (7200,)
dataset = tf.data.Dataset.from_tensor_slices((x,y))
print(dataset)
Out[56]: <TensorSliceDataset shapes: ((40,), ()), types: (tf.int32, tf.int32)>
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy')
history = model.fit(dataset, epochs=EPOCHS, batch_size=256)
sparse_softmax_cross_entropy_with_logits
logits.get_shape()))
ValueError: Shape mismatch: The shape of labels (received (1,)) should equal the shape of logits except for the last dimension (received (40, 1351)).
我看到 this answer but I am sure it doesn't apply here. I must use sparse_categorical_crossentropy. I am inspiring myself from this example 我想在 Tensorflow 数据集中存储训练和测试数据。我还想将数组存储在数据集中,因为稍后我将不得不使用它。
在使用 tf.data.Dataset
时,您不能将 batch_size
与 model.fit()
一起使用。而是使用 tf.data.Dataset.batch()
。您必须按如下方式更改代码才能正常工作。
import numpy as np
import tensorflow as tf
# Some toy data
train_x = np.random.normal(size=(7200, 40))
train_y = np.random.choice([0,1,2], size=(7200))
dataset = tf.data.Dataset.from_tensor_slices((train_x,train_y))
dataset = dataset.batch(256)
#### - Define your model here - ####
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy')
history = model.fit(dataset, epochs=EPOCHS)
我正在尝试将 numpy 数组存储在 Tensorflow 数据集中。该模型在使用 numpy 数组作为训练和测试数据时正确拟合,但当我 将 numpy 数组存储在单个 Tensorflow 数据集 中时则不正确。问题在于数据集的维度。虽然乍一看形状还不错,但有些地方不对。
在尝试了多种方法来重塑我的 Tensorflow 数据集之后,我仍然无法让它工作。我的代码如下:
train_x.shape
Out[54]: (7200, 40)
train_y.shape
Out[55]: (7200,)
dataset = tf.data.Dataset.from_tensor_slices((x,y))
print(dataset)
Out[56]: <TensorSliceDataset shapes: ((40,), ()), types: (tf.int32, tf.int32)>
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy')
history = model.fit(dataset, epochs=EPOCHS, batch_size=256)
sparse_softmax_cross_entropy_with_logits
logits.get_shape()))
ValueError: Shape mismatch: The shape of labels (received (1,)) should equal the shape of logits except for the last dimension (received (40, 1351)).
我看到 this answer but I am sure it doesn't apply here. I must use sparse_categorical_crossentropy. I am inspiring myself from this example 我想在 Tensorflow 数据集中存储训练和测试数据。我还想将数组存储在数据集中,因为稍后我将不得不使用它。
在使用 tf.data.Dataset
时,您不能将 batch_size
与 model.fit()
一起使用。而是使用 tf.data.Dataset.batch()
。您必须按如下方式更改代码才能正常工作。
import numpy as np
import tensorflow as tf
# Some toy data
train_x = np.random.normal(size=(7200, 40))
train_y = np.random.choice([0,1,2], size=(7200))
dataset = tf.data.Dataset.from_tensor_slices((train_x,train_y))
dataset = dataset.batch(256)
#### - Define your model here - ####
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy')
history = model.fit(dataset, epochs=EPOCHS)