[np.random.normal(0.0, 0.9), np.random.normal(0.0, 0.9)] 这个是什么意思
时间: 2023-07-23 07:48:02 浏览: 104
这个是用来生成符合正态分布的随机数的函数。具体来说,`np.random.normal(mean, stddev, size)`会生成一个大小为`size`的数组,其中的元素符合均值为`mean`,标准差为`stddev`的正态分布。在这个例子中,`np.random.normal(0.0, 0.9)`会生成一个均值为0,标准差为0.9的正态分布随机数,这个函数被用来生成样本数据。因为在这个例子中,我们想要生成两个不同的簇,所以我们在样本数据生成的时候,使用了两个不同的均值和标准差来产生两个不同的正态分布随机数,分别对应于两个簇的数据。
相关问题
用pytorch写一段代码:以西储大学CWRU轴承故障的振动信号数据库作为模型的训练集,故障类型标签包括:正常、内圈故障、外圈故障和滚动体故障四类。首先将一维故障信号转换为二维灰度图作为CNN模型的输...
以下是一个简单的示例代码,用于将一维信号转换为二维灰度图,并使用PyTorch中的CNN模型进行训练。
首先,需要导入必要的Python库和CWRU轴承故障的振动信号数据库。
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split
# Load the CWRU bearing dataset
data = pd.read_csv("cwru_data.csv")
labels = pd.read_csv("cwru_labels.csv")
```
然后,我们将一维信号转换为二维灰度图。我们可以使用Python的OpenCV库进行这个过程,也可以手动编写代码来完成这个过程。这里,我们演示手动编写代码的过程。
```python
# Convert 1D signals to 2D grayscale images
def signal_to_image(signal):
image = np.zeros((256, 256))
for i in range(256):
for j in range(256):
image[i][j] = signal[i*256+j]
return image
# Convert all signals to images
images = []
for i in range(len(data)):
images.append(signal_to_image(data.iloc[i]))
# Convert images to PyTorch tensors
images = torch.tensor(images).unsqueeze(1)
```
接下来,我们需要将故障类型标签转换为数字标签。
```python
# Convert labels to numerical labels
label_map = {"Normal": 0, "Inner": 1, "Outer": 2, "Rolling": 3}
labels = labels.replace({"Fault": label_map})
```
然后,我们可以将数据集拆分为训练集和测试集。
```python
# Split the dataset into train and test sets
train_images, test_images, train_labels, test_labels = train_test_split(images, labels, test_size=0.2, random_state=42)
```
最后,我们可以定义CNN模型并进行训练。
```python
# Define the CNN model
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, padding=1)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
self.fc1 = nn.Linear(64 * 64 * 64, 128)
self.fc2 = nn.Linear(128, 4)
def forward(self, x):
x = self.pool(nn.functional.relu(self.conv1(x)))
x = self.pool(nn.functional.relu(self.conv2(x)))
x = x.view(-1, 64 * 64 * 64)
x = nn.functional.relu(self.fc1(x))
x = self.fc2(x)
return x
# Initialize the model and loss function
model = CNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
# Train the model
for epoch in range(10):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 200 == 199:
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 200))
running_loss = 0.0
# Test the model
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the test images: %d %%' % (100 * correct / total))
```
这里,我们使用了一个简单的CNN模型,并在训练集上进行了10个epoch的训练。最后,我们在测试集上评估了模型的准确性。
我用的是新版gym,请修改这份代码,使之能运行
为了使您的代码能够兼容新版Gym(例如 Gymnasium),您需要对一些API调用进行调整。以下是主要的改动点:
1. **环境创建**:`gym.make` 返回的对象不再直接提供 `reset()` 方法,而是返回一个 `EnvSpec` 对象。您需要使用 `reset()` 方法时传递 `seed` 参数。
2. **步进方法**:`step()` 方法现在返回五个元素,而不是四个。具体来说,它返回 `(observation, reward, terminated, truncated, info)` 而不是 `(observation, reward, done, info)`。
3. **动作空间和观测空间**:`action_space` 和 `observation_space` 的属性和方法可能有所变化,但通常情况下不需要修改。
下面是修改后的代码:
```python
import numpy as np
from scipy.stats import truncnorm
import gymnasium as gym
import itertools
import torch
import torch.nn as nn
import torch.nn.functional as F
import collections
import matplotlib.pyplot as plt
class CEM:
def __init__(self, n_sequence, elite_ratio, fake_env, upper_bound, lower_bound):
self.n_sequence = n_sequence
self.elite_ratio = elite_ratio
self.upper_bound = upper_bound
self.lower_bound = lower_bound
self.fake_env = fake_env
def optimize(self, state, init_mean, init_var):
mean, var = init_mean, init_var
X = truncnorm(-2, 2, loc=np.zeros_like(mean), scale=np.ones_like(var))
state = np.tile(state, (self.n_sequence, 1))
for _ in range(5):
lb_dist, ub_dist = mean - self.lower_bound, self.upper_bound - mean
constrained_var = np.minimum(
np.minimum(np.square(lb_dist / 2), np.square(ub_dist / 2)),
var
)
action_sequences = [X.rvs() for _ in range(self.n_sequence)] * np.sqrt(constrained_var) + mean
returns = self.fake_env.propagate(state, action_sequences)[:, 0]
elites = action_sequences[np.argsort(returns)][-int(self.elite_ratio * self.n_sequence):]
new_mean = np.mean(elites, axis=0)
new_var = np.var(elites, axis=0)
mean = 0.1 * mean + 0.9 * new_mean
var = 0.1 * var + 0.9 * new_var
return mean
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class Swish(nn.Module):
def __init__(self):
super(Swish, self).__init__()
def forward(self, x):
return x * torch.sigmoid(x)
def init_weights(m):
def truncated_normal_init(t, mean=0.0, std=0.01):
torch.nn.init.normal_(t, mean=mean, std=std)
while True:
cond = (t < mean - 2 * std) | (t > mean + 2 * std)
if not torch.sum(cond):
break
t = torch.where(
cond,
torch.nn.init.normal_(torch.ones(t.shape, device=device), mean=mean, std=std),
t
)
return t
if type(m) == nn.Linear or isinstance(m, FCLayer):
truncated_normal_init(m.weight, std=1 / (2 * np.sqrt(m._input_dim)))
m.bias.data.fill_(0.0)
class FCLayer(nn.Module):
def __init__(self, input_dim, output_dim, ensemble_size, activation):
super(FCLayer, self).__init__()
self._input_dim, self._output_dim = input_dim, output_dim
self.weight = nn.Parameter(torch.Tensor(ensemble_size, input_dim, output_dim).to(device))
self._activation = activation
self.bias = nn.Parameter(torch.Tensor(ensemble_size, output_dim).to(device))
def forward(self, x):
return self._activation(torch.add(torch.bmm(x, self.weight), self.bias[:, None, :]))
class EnsembleModel(nn.Module):
def __init__(self, state_dim, action_dim, ensemble_size=5, learning_rate=1e-3):
super(EnsembleModel, self).__init__()
self._output_dim = (state_dim + 1) * 2
self._max_logvar = nn.Parameter((torch.ones((1, self._output_dim // 2)).float() / 2).to(device), requires_grad=False)
self._min_logvar = nn.Parameter((-torch.ones((1, self._output_dim // 2)).float() * 10).to(device), requires_grad=False)
self.layer1 = FCLayer(state_dim + action_dim, 200, ensemble_size, Swish())
self.layer2 = FCLayer(200, 200, ensemble_size, Swish())
self.layer3 = FCLayer(200, 200, ensemble_size, Swish())
self.layer4 = FCLayer(200, 200, ensemble_size, Swish())
self.layer5 = FCLayer(200, self._output_dim, ensemble_size, nn.Identity())
self.apply(init_weights)
self.optimizer = torch.optim.Adam(self.parameters(), lr=learning_rate)
def forward(self, x, return_log_var=False):
ret = self.layer5(self.layer4(self.layer3(self.layer2(self.layer1(x)))))
mean = ret[:, :, :self._output_dim // 2]
logvar = self._max_logvar - F.softplus(self._max_logvar - ret[:, :, self._output_dim // 2:])
logvar = self._min_logvar + F.softplus(logvar - self._min_logvar)
return mean, logvar if return_log_var else torch.exp(logvar)
def loss(self, mean, logvar, labels, use_var_loss=True):
inverse_var = torch.exp(-logvar)
if use_var_loss:
mse_loss = torch.mean(torch.mean(torch.pow(mean - labels, 2) * inverse_var, dim=-1), dim=-1)
var_loss = torch.mean(torch.mean(logvar, dim=-1), dim=-1)
total_loss = torch.sum(mse_loss) + torch.sum(var_loss)
else:
mse_loss = torch.mean(torch.pow(mean - labels, 2), dim=(1, 2))
total_loss = torch.sum(mse_loss)
return total_loss, mse_loss
def train(self, loss):
self.optimizer.zero_grad()
loss += 0.01 * torch.sum(self._max_logvar) - 0.01 * torch.sum(self._min_logvar)
loss.backward()
self.optimizer.step()
class EnsembleDynamicsModel:
def __init__(self, state_dim, action_dim, num_network=5):
self._num_network = num_network
self._state_dim, self._action_dim = state_dim, action_dim
self.model = EnsembleModel(state_dim, action_dim, ensemble_size=num_network)
self._epoch_since_last_update = 0
def train(self, inputs, labels, batch_size=64, holdout_ratio=0.1, max_iter=20):
permutation = np.random.permutation(inputs.shape[0])
inputs, labels = inputs[permutation], labels[permutation]
num_holdout = int(inputs.shape[0] * holdout_ratio)
train_inputs, train_labels = inputs[num_holdout:], labels[num_holdout:]
holdout_inputs, holdout_labels = inputs[:num_holdout], labels[:num_holdout]
阅读全文