def _gaussian(self, enc_output):
def latent_loss(mu, sigma):
pow_mu = mu * mu
pow_sigma = sigma * sigma
return 0.5 * torch.mean(pow_mu + pow_sigma - torch.log(pow_sigma) - 1)
mu = self._enc_mu(enc_output)
sigma = torch.exp(.5 * self._enc_log_sigma(enc_output))
self.latent_loss = latent_loss(mu, sigma)
weight = next(self.parameters()).data
std_z = Variable(weight.new(*sigma.size()), requires_grad=False)
std_z.data.copy_(torch.from_numpy(
np.random.normal(size=sigma.size())))
return mu + sigma * std_z
python类from_numpy()的实例源码
def get_batch(self, i, evaluation=False):
def pad_to_longest(insts, max_len):
inst_data = np.array([inst + [const.PAD] * (max_len - len(inst)) for inst in insts])
inst_data_tensor = Variable(torch.from_numpy(inst_data), volatile=evaluation)
if self.cuda:
inst_data_tensor = inst_data_tensor.cuda()
return inst_data_tensor
bsz = min(self._batch_size, self._sents_size-1-i)
src = pad_to_longest(self._src_sents[i:i+bsz], self._max_src)
tgt = pad_to_longest(self._tgt_sents[i:i+bsz], self._max_tgt)
label = Variable(torch.from_numpy(self._label[i:i+bsz]), volatile=evaluation)
if self.cuda:
label = label.cuda()
return src, tgt, label
def make_batch(batch_size):
batch_idx = np.random.choice(len(data),batch_size)
batch_sequences = [data[idx] for idx in batch_idx]
strokes = []
lengths = []
indice = 0
for seq in batch_sequences:
len_seq = len(seq[:,0])
new_seq = np.zeros((Nmax,5))
new_seq[:len_seq,:2] = seq[:,:2]
new_seq[:len_seq-1,2] = 1-seq[:-1,2]
new_seq[:len_seq,3] = seq[:,2]
new_seq[(len_seq-1):,4] = 1
new_seq[len_seq-1,2:4] = 0
lengths.append(len(seq[:,0]))
strokes.append(new_seq)
indice += 1
if use_cuda:
batch = Variable(torch.from_numpy(np.stack(strokes,1)).cuda().float())
else:
batch = Variable(torch.from_numpy(np.stack(strokes,1)).float())
return batch, lengths
################################ adaptive lr
def setUp(self):
self.x0 = torch.from_numpy(
# np.array(
# [[0.39834601, 0.6656751], [-0.44211167, -0.95197892],
# [0.52718359, 0.69099563], [-0.36314946, -0.07625845],
# [-0.53021497, -0.67317766]],
# dtype=np.float32)
np.random.uniform(-1, 1, (5, 2)).astype(np.float32)
)
self.x1 = torch.from_numpy(
# np.array(
# [[0.73587674, 0.98970324], [-0.9245277, 0.93210953],
# [-0.32989913, 0.36705822], [0.25636896, 0.10106555],
# [-0.11412049, 0.80171216]],
# dtype=np.float32)
np.random.uniform(-1, 1, (5, 2)).astype(np.float32)
)
self.t = torch.from_numpy(
# np.array(
# [1, 0, 1, 1, 0], dtype=np.float32)
np.random.randint(0, 2, (5,)).astype(np.float32)
)
self.margin = 1
def __call__(self, tensor):
"""
Args:
tensor (Tensor): Tensor of audio of size (samples x channels)
Returns:
tensor (Tensor): n_mels x hops x channels (BxLxC), where n_mels is
the number of mel bins, hops is the number of hops, and channels
is unchanged.
"""
if librosa is None:
print("librosa not installed, cannot create spectrograms")
return tensor
L = []
for i in range(tensor.size(1)):
nparr = tensor[:, i].numpy() # (samples, )
sgram = librosa.feature.melspectrogram(
nparr, **self.kwargs) # (n_mels, hops)
L.append(sgram)
L = np.stack(L, 2) # (n_mels, hops, channels)
tensor = torch.from_numpy(L).type_as(tensor)
return tensor
def test_img(im, net, base_image_size, means):
"""
Calls Caffe to get output for this image
"""
batch_size = 1
# Resize image
im_orig = im.astype(np.float32, copy=True)
im_orig -= means
im, gr, grr = upsample_image(im_orig, base_image_size)
im = np.transpose(im, axes=(2, 0, 1))
im = im[np.newaxis, :, :, :]
# Pass into model
mil_prob = net(Variable(torch.from_numpy(im), requires_grad=False).cuda())
return mil_prob
def setUpDatasets(self):
# Build training dataset
inputs, targets = self.generate_random_data(self.NUM_SAMPLES, (3, 32, 32),
num_classes=self.NUM_CLASSES,
dtype='float32')
# Split to train and split
train_inputs, train_targets = inputs[:self.NUM_TRAINING_SAMPLES], \
targets[:self.NUM_TRAINING_SAMPLES]
validate_inputs, validate_targets = inputs[self.NUM_TRAINING_SAMPLES:], \
targets[self.NUM_TRAINING_SAMPLES:]
# Convert to tensor and build dataset
train_dataset = TensorDataset(torch.from_numpy(train_inputs),
torch.from_numpy(train_targets))
validate_dataset = TensorDataset(torch.from_numpy(validate_inputs),
torch.from_numpy(validate_targets))
# Build dataloaders from dataset
self.train_loader = DataLoader(train_dataset, batch_size=16,
shuffle=True, num_workers=2, pin_memory=False)
self.validate_loader = DataLoader(validate_dataset, batch_size=16,
shuffle=True, num_workers=2, pin_memory=False)
def create_torch_variable(self, value, gpu=False):
"""Convenience method that produces a tensor given the value of the defined type.
Returns: a torch tensor of same type.
"""
if isinstance(value, torch.autograd.Variable):
if gpu:
value = value.cuda()
return value
if not torch.is_tensor(value):
if not isinstance(value, np.ndarray):
value = np.array(value, dtype=self.dtype.as_numpy_dtype)
else:
value = value.astype(self.dtype.as_numpy_dtype)
if value.size == 0:
return value
allowed = [tf.int16, tf.int32, tf.int64, tf.float16, tf.float32, tf.float64, tf.int8]
if self.dtype in allowed:
value = torch.autograd.Variable(torch.from_numpy(value))
else:
value = torch.autograd.Variable(value)
if gpu and isinstance(value, torch.autograd.Variable):
value = value.cuda()
return value
def batch_generator(batch_size, nb_batches):
batch_count = 0
while True:
pos = batch_count * batch_size
batch = dataset[pos:pos+batch_size]
X = np.zeros((batch_size, 1, img_size, img_size), dtype=np.float32)
for k, path in enumerate(batch):
im = io.imread(path)
im = color.rgb2gray(im)
X[k] = im[np.newaxis, ...]
X = torch.from_numpy(X)
X = Variable(X)
yield X, batch
batch_count += 1
if batch_count > nb_batches:
batch_count = 0
def preprocess_image(img, cuda=False):
means=[0.485, 0.456, 0.406]
stds=[0.229, 0.224, 0.225]
preprocessed_img = img.copy()[: , :, ::-1]
for i in range(3):
preprocessed_img[:, :, i] = preprocessed_img[:, :, i] - means[i]
preprocessed_img[:, :, i] = preprocessed_img[:, :, i] / stds[i]
preprocessed_img = \
np.ascontiguousarray(np.transpose(preprocessed_img, (2, 0, 1)))
preprocessed_img = torch.from_numpy(preprocessed_img)
preprocessed_img.unsqueeze_(0)
if cuda:
preprocessed_img = Variable(preprocessed_img.cuda(), requires_grad=True)
else:
preprocessed_img = Variable(preprocessed_img, requires_grad=True)
return preprocessed_img
def __call__(self, x, index=None):
output = self.pretrained_model(x)
if index is None:
index = np.argmax(output.data.cpu().numpy())
one_hot = np.zeros((1, output.size()[-1]), dtype=np.float32)
one_hot[0][index] = 1
if self.cuda:
one_hot = Variable(torch.from_numpy(one_hot).cuda(), requires_grad=True)
else:
one_hot = Variable(torch.from_numpy(one_hot), requires_grad=True)
one_hot = torch.sum(one_hot * output)
one_hot.backward(retain_variables=True)
grad = x.grad.data.cpu().numpy()
grad = grad[0, :, :, :]
return grad
def _layer_BatchNorm(self):
self.add_body(0, """
@staticmethod
def __batch_normalization(dim, name, **kwargs):
if dim == 1: layer = nn.BatchNorm1d(**kwargs)
elif dim == 2: layer = nn.BatchNorm2d(**kwargs)
elif dim == 3: layer = nn.BatchNorm3d(**kwargs)
else: raise NotImplementedError()
if 'scale' in __weights_dict[name]:
layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['scale']))
else:
layer.weight.data.fill_(1)
if 'bias' in __weights_dict[name]:
layer.state_dict()['bias'].copy_(torch.from_numpy(__weights_dict[name]['bias']))
else:
layer.bias.data.fill_(0)
layer.state_dict()['running_mean'].copy_(torch.from_numpy(__weights_dict[name]['mean']))
layer.state_dict()['running_var'].copy_(torch.from_numpy(__weights_dict[name]['var']))
return layer""")
def pull_item(self, index):
img_id = self.ids[index]
target = ET.parse(self._annopath % img_id).getroot()
img = cv2.imread(self._imgpath % img_id)
height, width, channels = img.shape
if self.target_transform is not None:
target = self.target_transform(target, width, height)
if self.transform is not None:
target = np.array(target)
img, boxes, labels = self.transform(img, target[:, :4], target[:, 4])
# to rgb
img = img[:, :, (2, 1, 0)]
# img = img.transpose(2, 0, 1)
target = np.hstack((boxes, np.expand_dims(labels, axis=1)))
return torch.from_numpy(img).permute(2, 0, 1), target, height, width
# return torch.from_numpy(img), target, height, width
def perturb_model(args, model, random_seed, env):
"""
Modifies the given model with a pertubation of its parameters,
as well as the negative perturbation, and returns both perturbed
models.
"""
new_model = ES(env.observation_space.shape[0],
env.action_space, args.small_net)
anti_model = ES(env.observation_space.shape[0],
env.action_space, args.small_net)
new_model.load_state_dict(model.state_dict())
anti_model.load_state_dict(model.state_dict())
np.random.seed(random_seed)
for (k, v), (anti_k, anti_v) in zip(new_model.es_params(),
anti_model.es_params()):
eps = np.random.normal(0, 1, v.size())
v += torch.from_numpy(args.sigma*eps).float()
anti_v += torch.from_numpy(args.sigma*-eps).float()
return [new_model, anti_model]
model_GRU.py 文件源码
项目:cnn-lstm-bilstm-deepcnn-clstm-in-pytorch
作者: bamtercelboo
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def __init__(self, args):
super(GRU, self).__init__()
self.args = args
# print(args)
self.hidden_dim = args.lstm_hidden_dim
self.num_layers = args.lstm_num_layers
V = args.embed_num
D = args.embed_dim
C = args.class_num
# self.embed = nn.Embedding(V, D, max_norm=args.max_norm)
self.embed = nn.Embedding(V, D)
# word embedding
if args.word_Embedding:
pretrained_weight = np.array(args.pretrained_weight)
self.embed.weight.data.copy_(torch.from_numpy(pretrained_weight))
# gru
self.gru = nn.GRU(D, self.hidden_dim, dropout=args.dropout, num_layers=self.num_layers)
# linear
self.hidden2label = nn.Linear(self.hidden_dim, C)
# hidden
self.hidden = self.init_hidden(self.num_layers, args.batch_size)
# dropout
self.dropout = nn.Dropout(args.dropout)
model.py 文件源码
项目:cnn-lstm-bilstm-deepcnn-clstm-in-pytorch
作者: bamtercelboo
项目源码
文件源码
阅读 45
收藏 0
点赞 0
评论 0
def __init__(self, args):
super(CNN_Text,self).__init__()
self.args = args
V = args.embed_num
D = args.embed_dim
C = args.class_num
Ci = 1
Co = args.kernel_num
Ks = args.kernel_sizes
self.embed = nn.Embedding(V, D)
# print("aaaaaaaa", self.embed.weight)
pretrained_weight = np.array(args.pretrained_weight)
self.embed.weight.data.copy_(torch.from_numpy(pretrained_weight))
# print("bbbbbbbb", self.embed.weight)
self.convs1 = [nn.Conv2d(Ci, Co, (K, D)) for K in Ks]
'''
self.conv13 = nn.Conv2d(Ci, Co, (3, D))
self.conv14 = nn.Conv2d(Ci, Co, (4, D))
self.conv15 = nn.Conv2d(Ci, Co, (5, D))
'''
self.dropout = nn.Dropout(args.dropout)
self.fc1 = nn.Linear(len(Ks)*Co, C)
model_BiGRU.py 文件源码
项目:cnn-lstm-bilstm-deepcnn-clstm-in-pytorch
作者: bamtercelboo
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def __init__(self, args):
super(BiGRU, self).__init__()
self.args = args
# print(args)
self.hidden_dim = args.lstm_hidden_dim
self.num_layers = args.lstm_num_layers
V = args.embed_num
D = args.embed_dim
C = args.class_num
# self.embed = nn.Embedding(V, D, max_norm=args.max_norm)
self.embed = nn.Embedding(V, D)
# word embedding
if args.word_Embedding:
pretrained_weight = np.array(args.pretrained_weight)
self.embed.weight.data.copy_(torch.from_numpy(pretrained_weight))
# gru
self.bigru = nn.GRU(D, self.hidden_dim, dropout=args.dropout, num_layers=self.num_layers, bidirectional=True)
# linear
self.hidden2label = nn.Linear(self.hidden_dim * 2, C)
# hidden
self.hidden = self.init_hidden(self.num_layers, args.batch_size)
# dropout
self.dropout = nn.Dropout(args.dropout)
def fetch_batch(self, part, batch_size: int = None):
if batch_size is None:
batch_size = self.batch_size
X, Y = self._fetch_batch(part, batch_size)
X = Variable(torch.from_numpy(X)).view(2*batch_size, self.image_size, self.image_size)
X1 = X[:batch_size] # (B, h, w)
X2 = X[batch_size:] # (B, h, w)
X = torch.stack([X1, X2], dim=1) # (B, 2, h, w)
Y = Variable(torch.from_numpy(Y))
if use_cuda:
X, Y = X.cuda(), Y.cuda()
return X, Y
def orthogonal_weights_init(m):
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
if hasattr(m, 'weight_v'):
w_ortho = svd_orthonormal(m.weight_v.data.cpu().numpy())
m.weight_v.data = torch.from_numpy(w_ortho)
try:
nn.init.constant(m.bias, 0)
except:
pass
else:
#nn.init.orthogonal(m.weight)
w_ortho = svd_orthonormal(m.weight.data.cpu().numpy())
#print w_ortho
#m.weight.data.copy_(torch.from_numpy(w_ortho))
m.weight.data = torch.from_numpy(w_ortho)
try:
nn.init.constant(m.bias, 0)
except:
pass
return
def _variable(self, state):
state = th.from_numpy(state).float()
if len(state.size()) < 2:
state = state.unsqueeze(0)
return V(state)