Tensorflow 2.0: flat_map() to flatten Dataset of Dataset返回基数



我正在尝试运行以下代码(如Tensorflow文档中给出的)来创建我的数据窗口,然后平坦化数据集的数据集。

window_size = 5
windows = range_ds.window(window_size, shift=1)
for sub_ds in windows.take(5):
print(sub_ds)
flat_windows = windows.flat_map(lambda x: x)

问题是flat_windows.cardinality().numpy()返回基数为-2,这在训练期间给我带来了问题。我试着寻找方法set_cardinality的数据集,但找不到任何东西。我还尝试了其他方法将数据集的数据集扁平化,但还是没有成功。

Edit-1:训练的问题是,当我训练一个子类模型(如下所示)时,形状是未知的(在线性和密集层)。当我急切地(通过tf.config.run_functions_eagerly(True))训练模型时,模型训练得很好,但速度很慢。因此,我希望输入数据在模型训练中是已知的。

<标题>神经网络
class NeuralNetworkModel(tf.keras.Model): 
def __init__(self):
super(NeuralNetworkModel, self).__init__()
self.encoder = Encoder()        

def train_step(self, inputs):       
X        = inputs[0]
Y        = inputs[1] 

with tf.GradientTape() as tape:
enc_X    = self.encoder(X)
enc_Y    = self.encoder(Y)    
# loss:        
loss   = tf.norm(enc_Y - enc_X, axis = [0, 1], ord = 'fro')

# Compute gradients
trainable_vars = self.encoder.trainable_variables
gradients = tape.gradient(loss, trainable_vars)
# Update weights
self.optimizer.apply_gradients(zip(gradients, trainable_vars))
# Compute our own metrics
loss_tracker.update_state(loss)

# Return a dict mapping metric names to current value.
# Note that it will include the loss (tracked in self.metrics).
return {"loss": loss_tracker.result()}

@property
def metrics(self):
# We list our `Metric` objects here so that `reset_states()` can be
# called automatically at the start of each epoch
# or at the start of `evaluate()`.
# If you don't implement this property, you have to call
# `reset_states()` yourself at the time of your choosing.
return [loss_tracker]

def test_step(self, inputs):       
X = inputs[0]
Y = inputs[1] 
Psi_X    = self.encoder(X)
Psi_Y    = self.encoder(Y)    
# loss:        
loss   = tf.norm(Psi_Y - Psi_X, axis = [0, 1], ord = 'fro')
# Compute our own metrics
loss_tracker.update_state(loss)

# Return a dict mapping metric names to current value.
# Note that it will include the loss (tracked in self.metrics).
return {"loss": loss_tracker.result()}

class Encoder(tf.keras.Model):
def __init__(self):
super(Encoder, self).__init__(dtype = 'float64', name = 'Encoder')
self.input_layer   = DenseLayer(128)
self.hidden_layer1 = DenseLayer(128)
self.hidden_layer2 = DenseLayer(64)        
self.hidden_layer3 = DenseLayer(64)
self.output_layer  = LinearLayer(64)

def call(self, input_data, training):
fx = self.input_layer(input_data)        
fx = self.hidden_layer1(fx)
fx = self.hidden_layer2(fx)
fx = self.hidden_layer3(fx)
return self.output_layer(fx)    
class LinearLayer(tf.keras.layers.Layer):
def __init__(self, units):
super(LinearLayer, self).__init__(dtype = 'float64')
self.units = units
def build(self, input_shape):
input_dim = input_shape[-1]
self.w = self.add_weight(shape = (input_dim, self.units), 
initializer = "random_normal", 
trainable = True)
self.b = self.add_weight(shape = (self.units,),    
initializer = tf.zeros_initializer(),
trainable = True)
def call(self, inputs):
return tf.matmul(inputs, self.w) + self.b
class DenseLayer(tf.keras.layers.Layer):
def __init__(self, units):
super(DenseLayer, self).__init__(dtype = 'float64')
self.units = units

def build(self, input_shape):
input_dim = input_shape[-1]
self.w = self.add_weight(shape = (input_dim, self.units), 
initializer = "random_normal", 
trainable = True)
self.b = self.add_weight(shape = (self.units,),    
initializer = tf.zeros_initializer(),
trainable = True)
def call(self, inputs):
x = tf.matmul(inputs, self.w) + self.b
return tf.nn.elu(x)

我也在想这个问题。结果-2为tf.data.UNKNOWN_CARDINALITY(https://www.tensorflow.org/api_docs/python/tf/data#UNKNOWN_CARDINALITY),表示TF不知道flat_map每项返回多少元素。

我刚刚问窗口一个TensorFlow数据集不丢失基数信息?看看有没有人知道一种不丢失基数的窗口数据集的方法。

相关内容

  • 没有找到相关文章

最新更新