def mellowmax(values, omega=1., axis=1):
"""Mellowmax function.
This is a kind of softmax function that is, unlike the Boltzmann softmax,
non-expansion.
See: http://arxiv.org/abs/1612.05628
Args:
values (Variable or ndarray):
Input values. Mellowmax is taken along the second axis.
omega (float):
Parameter of mellowmax.
axis (int):
Axis along which mellowmax is taken.
Returns:
outputs (Variable)
"""
n = values.shape[axis]
return (F.logsumexp(omega * values, axis=axis) - np.log(n)) / omega
python类softmax()的实例源码
def get_index_a(_model):
_model.predictor.reset_state()
_sentence_index_a = []
index = BOS_INDEX
while index != EOS_INDEX:
y = _model.predictor(xp.array([index], dtype=xp.int32))
probability = F.softmax(y)
probability.data[0] /= sum(probability.data[0])
try:
#???????????????????
#index = np.argmax(probability.data[0])
index = xp.random.choice(range(len(probability.data[0])), p=probability.data[0])
if index!=EOS_INDEX:
#??<EOS>???????
_sentence_index_a.append(index)
except Exception as e:
print('probability error')
break
return _sentence_index_a
def get_next_word_prob(_model, word, next_word, needModelStateReset=False):
if needModelStateReset:
_model.predictor.reset_state()
_sentence_index_a = []
index = vocab[word]
while index != EOS_INDEX:
y = _model.predictor(xp.array([index], dtype=xp.int32))
probability = F.softmax(y)
next_probs = probability.data[0]
m = np.argsort(probability.data[0])
break
# In this case, the input could be an unknow word.
if next_word not in vocab:
return (0.0, 0.0)
next_index = vocab[next_word]
k, = np.where(m == next_index)
order_prob = k[0] / len(m)
next_prob = next_probs[k[0]]
return (order_prob, next_prob, k[0])
def get_index_a(_model):
_model.predictor.reset_state()
_sentence_index_a = []
index = BOS_INDEX
while index != EOS_INDEX:
y = _model.predictor(xp.array([index], dtype=xp.int32))
probability = F.softmax(y)
probability.data[0] /= sum(probability.data[0])
try:
#???????????????????
#index = np.argmax(probability.data[0])
index = xp.random.choice(range(len(probability.data[0])), p=probability.data[0])
if index!=EOS_INDEX:
#??<EOS>???????
_sentence_index_a.append(index)
except Exception as e:
print('probability error')
break
return _sentence_index_a
def get_next_word_prob(_model, word, next_word, needModelStateReset=False):
if needModelStateReset:
_model.predictor.reset_state()
_sentence_index_a = []
index = vocab[word]
while index != EOS_INDEX:
y = _model.predictor(xp.array([index], dtype=xp.int32))
probability = F.softmax(y)
next_probs = probability.data[0]
m = np.argsort(probability.data[0])
break
# In this case, the input could be an unknow word.
if next_word not in vocab:
return (0.0, 0.0,-1)
next_index = vocab[next_word]
k, = np.where(m == next_index)
order_prob = k[0] / len(m)
next_prob = next_probs[k[0]]
return (order_prob, next_prob, k[0])
def _context(self, p, fb_mat, fbe_mat):
batch_size, source_length, _ = fb_mat.data.shape
# {pe,e}_mat: shape = [batch * srclen, atten]
pe_mat = F.reshape(
F.broadcast_to(
F.expand_dims(self.p_e(p), 1),
[batch_size, source_length, self.atten_size]),
[batch_size * source_length, self.atten_size])
e_mat = F.tanh(fbe_mat + pe_mat)
# a_mat: shape = [batch, srclen]
a_mat = F.softmax(F.reshape(self.e_a(e_mat), [batch_size, source_length]))
# q: shape = [batch, 2 * hidden]
q = F.reshape(
F.batch_matmul(a_mat, fb_mat, transa=True),
[batch_size, 2 * self.hidden_size])
return q
def attend(self, query, key, value, mask, minfs=None):
"""
Input shapes:
q=(b, units, dec_l), k=(b, units, enc_l),
v=(b, units, dec_l, enc_l), m=(b, dec_l, enc_l)
"""
# Calculate Attention Scores with Mask for Zero-padded Areas
pre_a = F.batch_matmul(query, key, transa=True) # (b, dec_l, enc_l)
minfs = self.xp.full(pre_a.shape, -np.inf, pre_a.dtype) \
if minfs is None else minfs
pre_a = F.where(mask, pre_a, minfs)
a = F.softmax(pre_a, axis=2)
# if values in axis=2 are all -inf, they become nan. thus do re-mask.
a = F.where(self.xp.isnan(a.data),
self.xp.zeros(a.shape, dtype=a.dtype), a)
reshaped_a = a[:, None] # (b, 1, dec_xl, enc_l)
# Calculate Weighted Sum
pre_c = F.broadcast_to(reshaped_a, value.shape) * value
c = F.sum(pre_c, axis=3, keepdims=True) # (b, units, dec_xl, 1)
return c
def test_decode(self, start, eos, limit):
output = []
y = chainer.Variable(np.array([[start]], dtype=np.int32))
for i in range(limit):
decode0 = self.output_embed(y)
decode1 = self.decode1(decode0)
decode2 = self.decode2(decode1)
z = self.output(decode2)
prob = F.softmax(z)
index = np.argmax(cuda.to_cpu(prob.data))
if index == eos:
break
output.append(index)
y = chainer.Variable(np.array([index], dtype=np.int32))
return output
def test(model, xs, ts, uss=None):
model.reset_state()
tags = model([Variable(
np.array([x], dtype=np.int32)
) for x in xs])
zss = []
y_mat = np.zeros((2, 2))
zs_mat = tuple(
np.zeros((clf.n_output, clf.n_output))
for clf in model.tagger.classifiers
)
for t, (y, zs) in zip(ts, tags):
y_mat[t, int(cf.sigmoid(y).data[0, 0] > 0.5)] += 1.0
if t:
zss.append(zs)
if uss:
assert len(uss) == len(zss)
for us, zs in zip(uss, zss):
for m, u, z in zip(zs_mat, us, zs):
m[u, cf.softmax(z).data.argmax(1)[0]] += 1
return y_mat, zs_mat
def generate(model, xs):
model.reset_state()
tags = model([Variable(
np.array([x], dtype=np.int32)
) for x in xs])
buf = bytearray()
for x, (y, zs) in zip(xs, tags):
buf.append(x)
if cf.sigmoid(y).data[0, 0] > 0.5:
yield (
buf.decode('utf-8', 'replace'),
tuple(
cf.softmax(z).data.argmax(1)[0]
for z in zs
)
)
buf = bytearray()
def test(model, xs, ts, uss=None):
model.reset_state()
tags = model([Variable(
np.array([x], dtype=np.int32)
) for x in xs])
zss = []
y_mat = np.zeros((2, 2))
zs_mat = tuple(
np.zeros((clf.n_output, clf.n_output))
for clf in model.tagger.classifiers
)
for t, (y, zs) in zip(ts, tags):
y_mat[t, int(cf.sigmoid(y).data[0, 0] > 0.5)] += 1.0
if t:
zss.append(zs)
if uss:
assert len(uss) == len(zss)
for us, zs in zip(uss, zss):
for m, u, z in zip(zs_mat, us, zs):
m[u, cf.softmax(z).data.argmax(1)[0]] += 1
return y_mat, zs_mat
def generate(model, xs):
model.reset_state()
tags = model([Variable(
np.array([x], dtype=np.int32)
) for x in xs])
buf = bytearray()
for x, (y, zs) in zip(xs, tags):
buf.append(x)
if cf.sigmoid(y).data[0, 0] > 0.5:
yield (
buf.decode('utf-8', 'replace'),
tuple(
cf.softmax(z).data.argmax(1)[0]
for z in zs
)
)
buf = bytearray()
def entropy_filter(self, x, b, ent_T):
xp = cuda.get_array_module(b)
eb = entropy(F.softmax(b))/np.log(b.shape[1])
eb.to_cpu()
if hasattr(eb.data,'get'):
with cuda.get_device(eb.data):
exited = eb.data < ent_T
exited = exited.get()
else:
exited = eb.data < ent_T
y_exit = []
y_cont = []
for i,idx in enumerate(exited):
if idx:
y_exit.append(b[i:i+1])
else:
y_cont.append(x[i:i+1])
if len(y_exit) > 0:
y_exit = F.vstack(y_exit)
if len(y_cont) > 0:
y_cont = F.vstack(y_cont)
return y_exit,y_cont,exited
def to_function(self):
if self.nonlinearity.lower() == "clipped_relu":
return clipped_relu()
if self.nonlinearity.lower() == "crelu":
return crelu()
if self.nonlinearity.lower() == "elu":
return elu()
if self.nonlinearity.lower() == "hard_sigmoid":
return hard_sigmoid()
if self.nonlinearity.lower() == "leaky_relu":
return leaky_relu()
if self.nonlinearity.lower() == "relu":
return relu()
if self.nonlinearity.lower() == "sigmoid":
return sigmoid()
if self.nonlinearity.lower() == "softmax":
return softmax()
if self.nonlinearity.lower() == "softplus":
return softplus()
if self.nonlinearity.lower() == "tanh":
return tanh()
if self.nonlinearity.lower() == "bst":
return bst()
raise NotImplementedError()
def __call__(self, y, a, ht, y_lex):
y_dict = F.squeeze(F.batch_matmul(y_lex, a, transa=True), axis=2)
return (y + F.log(y_dict + self.alpha))
#class LinearInterpolationLexicon(chainer.Chain):
# def __init__(self, hidden_size):
# super(LinearInterpolationLexicon, self).__init__(
# perceptron = chainer.links.Linear(hidden_size, 1)
# )
#
# def __call__(self, y, a, ht, y_lex):
# y = F.softmax(y)
# y_dict = F.squeeze(F.batch_matmul(y_lex, a, transa=True), axis=2)
# gamma = F.broadcast_to(F.sigmoid(self.perceptron(ht)), y_dict.data.shape)
# return (gamma * y_dict + (1-gamma) * y)
#
def classify(self,x=None,train=False):
if x is None:
x=Tensor.context
image = x.value
self.x_batch = image
xp = Deel.xp
x_data = xp.asarray(self.x_batch,dtype=Deel.xp.float32)
x = chainer.Variable(x_data, volatile='off' if train else 'on')
score = self.forward(x)
score = F.softmax(score)
score = Variable(score.data) #Unchain
t = ChainerTensor(score)
t.owner=self
t.use()
return t
def classify(self,x=None):
if x is None:
x=Tensor.context
if not isinstance(x,ImageTensor):
x=Input(x)
image = x.value
self.x_batch = image
xp = Deel.xp
x_data = xp.asarray(self.x_batch)
x = chainer.Variable(x_data, volatile=True)
score = self.predict(x)
score = F.softmax(score)
score = Variable(score.data) #Unchain
t = ChainerTensor(score)
t.owner=self
t.use()
return t
def classify(self,x=None):
if x is None:
x=Tensor.context
image = x.value
self.x_batch = image
xp = Deel.xp
x_data = xp.asarray(self.x_batch)
x = chainer.Variable(x_data, volatile=True)
score = self.forward(x)
score = F.softmax(score)
score = Variable(score.data) #Unchain
t = ChainerTensor(score)
t.owner=self
t.use()
return t
def __call__(self, X):
# generate random values
R = np.random.randn(X.data.shape[0], self.rand_sz)
R = Variable(R.astype("float32"))
# attach random to the inputs
h = F.concat([R, X])
#h = R
h = self.ipt(h)
#h = F.dropout(h)
y = self.out(h)
# prior knowledge: environment observation is one - hot vector
obs = F.softmax(y[:, :-2])
# prior knowledge: reward is in [0,1]
rew = F.sigmoid(y[:,[-2]])
fin = F.sigmoid(y[:, [-1]])
y = F.concat([obs, rew, fin])
return y
updater.py 文件源码
项目:Semantic-Segmentation-using-Adversarial-Networks
作者: oyam
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def forward(self, batch):
label_onehot_batch = [self._onehot_encode(pair[1]) for pair in batch]
input_img, ground_truth = self.converter(batch, self.device)
ground_truth_onehot = self.converter(label_onehot_batch, self.device)
input_img = Variable(input_img, volatile=not self.gen.train)
ground_truth = Variable(ground_truth, volatile=not self.gen.train)
ground_truth_onehot = Variable(ground_truth_onehot, volatile=not self.gen.train)
x_real = self._make_dis_input(input_img, ground_truth_onehot)
y_real = self.dis(x_real)
pred_label_map = self.gen(input_img)
x_fake = self._make_dis_input(input_img, F.softmax(pred_label_map))
y_fake = self.dis(x_fake)
self.y_fake = y_fake
self.y_real = y_real
self.pred_label_map = pred_label_map
self.ground_truth = ground_truth
def get_boxes(ans, block_x, block_y, bb_num,class_num,th, im_w,im_h,biases):
sorted_boxes = []
for by in range(block_y):
for bx in range(block_x):
for j in range(bb_num):
box = ans[by,bx,j,0:4]
conf = sigmoid(ans[by,bx,j,4])
probs = softmax(ans[by,bx,j,5:(5+class_num)])[0]
p_class = probs*conf
if np.max(p_class)<th:
continue
class_id = np.argmax(p_class)
x = (bx+sigmoid(box[0]))*(im_w/float(block_x))
y = (by+sigmoid(box[1]))*(im_h/float(block_y))
w = np.exp(box[2])*biases[j][0]*(im_w/float(block_x))
h = np.exp(box[3])*biases[j][1]*(im_h/float(block_y))
b = Box(x,y,w,h)
sorted_boxes.append([b,j,class_id, max(p_class)])
return sorted_boxes
def to_function(self):
if self.nonlinearity.lower() == "clipped_relu":
return clipped_relu()
if self.nonlinearity.lower() == "crelu":
return crelu()
if self.nonlinearity.lower() == "elu":
return elu()
if self.nonlinearity.lower() == "hard_sigmoid":
return hard_sigmoid()
if self.nonlinearity.lower() == "leaky_relu":
return leaky_relu()
if self.nonlinearity.lower() == "relu":
return relu()
if self.nonlinearity.lower() == "sigmoid":
return sigmoid()
if self.nonlinearity.lower() == "softmax":
return softmax()
if self.nonlinearity.lower() == "softplus":
return softplus()
if self.nonlinearity.lower() == "tanh":
return tanh()
raise NotImplementedError()
def generate_and_save_samples(sample_fn, length, count, dir, rate, levels):
def save_samples(data):
data = (data * np.reshape(np.arange(levels) / (levels-1), [levels, 1, 1])).sum(
axis=1, keepdims=True)
value = np.iinfo(np.int16).max
audio = (utils.inverse_mulaw(data * 2 - 1) * value).astype(np.int16)
for idx, sample in enumerate(audio):
filename = os.path.join(dir, 'sample_{}.wav'.format(idx))
wavfile.write(filename, rate, np.squeeze(sample))
samples = chainer.Variable(
chainer.cuda.cupy.zeros([count, levels, 1, length], dtype='float32'))
one_hot_ref = chainer.cuda.cupy.eye(levels).astype('float32')
with tqdm.tqdm(total=length) as bar:
for i in range(length):
probs = F.softmax(sample_fn(samples))[:, :, 0, 0, i]
samples.data[:, :, 0, i] = one_hot_ref[utils.sample_from(probs.data.get())]
bar.update()
samples.to_cpu()
save_samples(samples.data)
def generate_and_save_samples(sample_fn, height, width, channels, count, filename):
def save_images(images):
images = images.reshape((count, count, channels, height, width))
images = images.transpose(1, 3, 0, 4, 2)
images = images.reshape((height * count, width * count, channels))
scipy.misc.toimage(images, cmin=0.0, cmax=255.0).save(filename)
samples = chainer.Variable(
chainer.cuda.cupy.zeros((count ** 2, channels, height, width), dtype='float32'))
with tqdm.tqdm(total=height*width*channels) as bar:
for i in range(height):
for j in range(width):
for k in range(channels):
probs = F.softmax(sample_fn(samples))[:, :, k, i, j]
_, level_count = probs.shape
samples.data[:, k, i, j] = chainer.cuda.to_gpu(utils.sample_from(probs.data.get()) / (level_count - 1))
bar.update()
samples.to_cpu()
save_images(samples.data * 255.0)
def to_function(self):
if self.nonlinearity.lower() == "clipped_relu":
return clipped_relu()
if self.nonlinearity.lower() == "crelu":
return crelu()
if self.nonlinearity.lower() == "elu":
return elu()
if self.nonlinearity.lower() == "hard_sigmoid":
return hard_sigmoid()
if self.nonlinearity.lower() == "leaky_relu":
return leaky_relu()
if self.nonlinearity.lower() == "relu":
return relu()
if self.nonlinearity.lower() == "sigmoid":
return sigmoid()
if self.nonlinearity.lower() == "softmax":
return softmax()
if self.nonlinearity.lower() == "softplus":
return softplus()
if self.nonlinearity.lower() == "tanh":
return tanh()
raise NotImplementedError()
def compute_loss(self, dist_pos, dist_neg, margin_factor=1.0):
"""
Use Softmax on the distances as a ratio measure and compare it to a
vector of [[0, 0, ...] [1, 1, ...]] (Mean Squared Error).
This function also computes the accuracy and the 'max_distance'.
"""
# apply margin factor and take square root
dist = sqrt(F.concat((dist_pos * margin_factor, dist_neg)))
sm = F.softmax(dist)
self.loss = mse_zero_one(sm)
self.accuracy = self._accuracy(dist_pos, dist_neg)
self.mean_diff = self._mean_difference(dist_pos, dist_neg)
self.max_diff = self._max_difference(dist_pos, dist_neg)
return self.loss
def generate(net, image_model, image_path):
feature = image_model.feature(image_path)
net.initialize(feature)
candidates = [(net, [bos], 0)]
for i in range(max_length):
next_candidates = []
for prev_net, tokens, likelihood in candidates:
if tokens[-1] == eos:
next_candidates.append((None, tokens, likelihood))
continue
net = prev_net.copy()
x = xp.asarray([tokens[-1]]).astype(np.int32)
y = F.softmax(net(x))
token_likelihood = np.log(cuda.to_cpu(y.data[0]))
order = token_likelihood.argsort()[-beam_width:][::-1]
next_candidates.extend([(net, tokens + [i], likelihood + token_likelihood[i]) for i in order])
candidates = sorted(next_candidates, key=lambda x: -x[2])[:beam_width]
if all([candidate[1][-1] == eos for candidate in candidates]):
break
return [candidate[1] for candidate in candidates]
def compute_fisher(self, dataset):
fisher_accum_list = [
np.zeros(var[1].shape) for var in self.variable_list]
for _ in range(self.num_samples):
x, _ = dataset[np.random.randint(len(dataset))]
y = self.predictor(np.array([x]))
prob_list = F.softmax(y)[0].data
class_index = np.random.choice(len(prob_list), p=prob_list)
loss = F.log_softmax(y)[0, class_index]
self.cleargrads()
loss.backward()
for i in range(len(self.variable_list)):
fisher_accum_list[i] += np.square(
self.variable_list[i][1].grad)
self.fisher_list = [
F_accum / self.num_samples for F_accum in fisher_accum_list]
return self.fisher_list
def to_function(self):
if self.nonlinearity.lower() == "clipped_relu":
return clipped_relu()
if self.nonlinearity.lower() == "crelu":
return crelu()
if self.nonlinearity.lower() == "elu":
return elu()
if self.nonlinearity.lower() == "hard_sigmoid":
return hard_sigmoid()
if self.nonlinearity.lower() == "leaky_relu":
return leaky_relu()
if self.nonlinearity.lower() == "relu":
return relu()
if self.nonlinearity.lower() == "sigmoid":
return sigmoid()
if self.nonlinearity.lower() == "softmax":
return softmax()
if self.nonlinearity.lower() == "softplus":
return softplus()
if self.nonlinearity.lower() == "tanh":
return tanh()
raise NotImplementedError()
def sample_ax_label(self, a, x, argmax=True, test=False):
a = self.to_variable(a)
x = self.to_variable(x)
batchsize = x.data.shape[0]
y_distribution = self.q_y_ax(a, x, test=test, softmax=True).data
n_labels = y_distribution.shape[1]
if self.gpu_enabled:
y_distribution = cuda.to_cpu(y_distribution)
if argmax:
sampled_label = np.argmax(y_distribution, axis=1)
else:
sampled_label = np.zeros((batchsize,), dtype=np.int32)
labels = np.arange(n_labels)
for b in xrange(batchsize):
label_id = np.random.choice(labels, p=y_distribution[b])
sampled_label[b] = 1
return sampled_label