帮我用pytorch改写:def make_generator_model(): model = tf.keras.Sequential() model.add(layers.Input(shape=(100, 12))) model.add(layers.Bidirectional(layers.LSTM(64, return_sequences=True))) model.add(layers.Conv1D(filters=128, kernel_size=16, strides=1, padding='same')) model.add(layers.LeakyReLU()) model.add(layers.Conv1D(filters=64, kernel_size=16, strides=1, padding='same')) model.add(layers.LeakyReLU()) model.add(layers.UpSampling1D(2)) model.add(layers.Conv1D(filters=32, kernel_size=16, strides=1, padding='same')) model.add(layers.LeakyReLU()) model.add(layers.Conv1D(filters=16, kernel_size=16, strides=1, padding='same')) model.add(layers.LeakyReLU()) model.add(layers.UpSampling1D(2)) model.add(layers.Conv1D(filters=1, kernel_size=16, strides=1, padding='same', activation='tanh')) model.add(layers.Permute((2, 1))) return model def make_discriminator_model(): model = tf.keras.Sequential() model.add(layers.Input(shape=(1, 400))) model.add(layers.Permute((2, 1))) model.add(layers.Conv1D(filters=32, kernel_size=16, strides=1, padding='same')) model.add(layers.LeakyReLU()) # model.add(layers.Dropout(0.4)) model.add(layers.Conv1D(filters=64, kernel_size=16, strides=1, padding='same')) model.add(layers.LeakyReLU()) model.add(layers.MaxPool1D(pool_size=2)) model.add(layers.Conv1D(filters=128, kernel_size=16, strides=1, padding='same')) model.add(layers.LeakyReLU()) # model.add(layers.Dropout(0.4)) model.add(layers.Conv1D(filters=256, kernel_size=16, strides=1, padding='same')) model.add(layers.LeakyReLU()) model.add(layers.MaxPool1D(pool_size=2)) model.add(layers.Flatten()) model.add(layers.Dense(1)) return model
时间: 2024-02-24 13:00:15 浏览: 109
PyTorch中model.zero_grad()和optimizer.zero_grad()用法
以下是使用PyTorch重写的make_generator_model和make_discriminator_model函数:
```python
import torch.nn as nn
def make_generator_model():
model = nn.Sequential(
nn.LSTM(12, 64, bidirectional=True, batch_first=True),
nn.Conv1d(128, 128, kernel_size=16, stride=1, padding=8),
nn.LeakyReLU(),
nn.Conv1d(64, 64, kernel_size=16, stride=1, padding=8),
nn.LeakyReLU(),
nn.Upsample(scale_factor=2),
nn.Conv1d(32, 32, kernel_size=16, stride=1, padding=8),
nn.LeakyReLU(),
nn.Conv1d(16, 16, kernel_size=16, stride=1, padding=8),
nn.LeakyReLU(),
nn.Upsample(scale_factor=2),
nn.Conv1d(1, 1, kernel_size=16, stride=1, padding=8),
nn.Tanh(),
nn.Permute((0, 2, 1))
)
return model
def make_discriminator_model():
model = nn.Sequential(
nn.Conv1d(400, 32, kernel_size=16, stride=1, padding=8),
nn.LeakyReLU(),
nn.Conv1d(32, 64, kernel_size=16, stride=1, padding=8),
nn.LeakyReLU(),
nn.MaxPool1d(kernel_size=2),
nn.Conv1d(64, 128, kernel_size=16, stride=1, padding=8),
nn.LeakyReLU(),
nn.Conv1d(128, 256, kernel_size=16, stride=1, padding=8),
nn.LeakyReLU(),
nn.MaxPool1d(kernel_size=2),
nn.Flatten(),
nn.Linear(256 * 25, 1)
)
return model
```
请注意,这里使用了PyTorch中的模块替换了TensorFlow中的模块。在PyTorch中,nn.LSTM和nn.Conv1d的默认输入格式是(batch_size, seq_len, input_size)和(batch_size, input_channels, seq_len)。另外,使用nn.Linear替换了最后的全连接层。
阅读全文