在tensorflow/keras中处理Conv1D层中的维度的正确方法是什么?

问题描述 投票:0回答:1

我一直在尝试用 Keras 中的 Conv1D 解决这个问题。基本上,我有一个 100 x 1 的向量 BathyZ。我想在将其与 2 个标量输入 Tperiod 和 AMP_WK 合并之前对其进行一些卷积以预测另一个 100 x 1 向量。 (bathyZ 具有我希望发现的空间变异性)。这是我第一次使用张量流数据集和解析/反序列化。我总是遇到一些我似乎无法理解的形状错误。

主要是,第一个密集层似乎没有在其输入中接收到正确的尺寸,我不明白为什么,因为所有东西的输入尺寸对我来说似乎都是有意义的。为什么它期待 6402 并得到 (None, 165)?我正在对单个记录进行调试,因此批量大小较小,如果这是一个问题的话。无论如何它应该有效,对吧?我的理解是我的输入应该是 (None,100,1) 以允许 100 x 1 序列使用不同的批量大小。我尝试过几种重塑方法,但似乎都不起作用,所以也许我错过了一些更基本的东西

### ERROR MESSAGE

ValueError: Exception encountered when calling Functional.call().

Input 0 of layer "dense_110" is incompatible with the layer: expected axis -1 of input shape to have value 6402, but received input with shape (None, 165)

Arguments received by Functional.call():
  • inputs={'bathyZ': 'tf.Tensor(shape=(None, 100, 1), dtype=float32)', 'AMP_WK': 'tf.Tensor(shape=(None, 1), dtype=float32)', 'Tperiod': 'tf.Tensor(shape=(None, 1), dtype=float32)'}
  • training=True
  • mask={'bathyZ': 'None', 'AMP_WK': 'None', 'Tperiod': 'None'}
### CODE

feature_description = {
        'bathyZ': tf.io.FixedLenFeature([], tf.string),
        'bathyZ_shape': tf.io.FixedLenFeature([3], tf.int64),
        'AMP_WK': tf.io.FixedLenFeature([], tf.float32),
        'Tperiod': tf.io.FixedLenFeature([], tf.float32),
        'skew': tf.io.FixedLenFeature([], tf.string),
        'skew_shape': tf.io.FixedLenFeature([3], tf.int64),
    }
    
    def _parse_function(proto):
        # Parse
        parsed_features = tf.io.parse_single_example(proto, feature_description)
    
        # Decode/reshape the serialized tensors
        bathyZ = parsed_features['bathyZ']
        bathyZ = tf.io.parse_tensor(bathyZ, out_type=tf.float32)
        bathyZ = tf.reshape(bathyZ, [100, 1])
    
        skew = parsed_features['skew']
        skew = tf.io.parse_tensor(skew, out_type=tf.float32)
        skew = tf.reshape(skew, [100, 1])
    
        # Get other inputs, reshape
        AMP_WK = parsed_features['AMP_WK']
        Tperiod = parsed_features['Tperiod']
    
        AMP_WK = tf.reshape(AMP_WK, [1])
        Tperiod = tf.reshape(Tperiod, [1])
        
        # Create tuple
        inputs = {'bathyZ': bathyZ, 'AMP_WK': AMP_WK, 'Tperiod': Tperiod}
        outputs = {'skew': skew}
        
        return inputs, outputs
    
    # Create a TFRecordDataset and map the parsing function
    tfrecord_path = 'ML_0004.tfrecord'
    dataset = tf.data.TFRecordDataset(tfrecord_path)
    dataset = dataset.map(_parse_function)
        
        
    # Model
    
    
    def create_model():
        # Tensor input branch (shape: 100 timesteps, 1 feature)
        bathyZ = Input(shape=(100, 1), name='bathyZ')
        x = layers.Conv1D(32, 3, activation='relu', padding='same')(bathyZ)
        x = layers.Conv1D(64, 3, activation='relu', padding='same')(x)
        x = layers.Flatten()(x)
    
        # Scalar inputs
        AMP_WK = Input(shape=(1,), name='AMP_WK')
        Tperiod = Input(shape=(1,), name='Tperiod')
        
        # Combine all branches
        combined = layers.concatenate([x, AMP_WK, Tperiod])
    
        # Fully connected layer
        z = layers.Dense(64, activation='relu')(combined)
        z = layers.Dense(128, activation='relu')(z)
    
        # Output layer (tensor output, same shape as input tensor)
        skew = layers.Dense(100, activation='linear', name='skew')(z)
    
        # Create the model
        model = models.Model(inputs=[bathyZ, AMP_WK, Tperiod], outputs=skew)
        return model
    
    # Example usage:
    model = create_model()
    model.compile(optimizer='adam', loss='mse')
    model.summary()
    dataset = dataset.batch(1)  
    model.fit(dataset)

我尝试将解析和预处理步骤中的各种输入张量重塑为不同版本的 [1,100]、[1,100,1] 等,并跟踪形状如何演变。但我总是遇到某种尺寸错误

python tensorflow machine-learning keras conv-neural-network
1个回答
0
投票

出于某种原因,尽管输入被存储为键值,但它们赋予

model = models.Model
的顺序很重要。如果你颠倒顺序,使其
model = models.Model(inputs=[Tperiod, AMP_WK, bathyZ], outputs=skew)
,它就有效。

© www.soinside.com 2019 - 2024. All rights reserved.