def sample_x_y(self, x, argmax=False, test=False):
batchsize = x.data.shape[0]
y_distribution = self.encoder_x_y(x, test=test, softmax=True).data
n_labels = y_distribution.shape[1]
if self.gpu:
y_distribution = cuda.to_cpu(y_distribution)
sampled_y = np.zeros((batchsize, n_labels), dtype=np.float32)
if argmax:
args = np.argmax(y_distribution, axis=1)
for b in xrange(batchsize):
sampled_y[b, args[b]] = 1
else:
for b in xrange(batchsize):
label_id = np.random.choice(np.arange(n_labels), p=y_distribution[b])
sampled_y[b, label_id] = 1
sampled_y = Variable(sampled_y)
if self.gpu:
sampled_y.to_gpu()
return sampled_y
python类softmax()的实例源码
def encode(self, x_input, x_query, answer):
m = self.encode_input(x_input)
u = self.encode_query(x_query)
# print "m.data.shape", m.data.shape
# print "u.data.shape", u.data.shape
mu = functions.matmul(m, u, transb=True)
# print "mu.data.shape", mu.data.shape
# print "mu.data", mu.data
p = functions.softmax(mu)
c = self.encode_output(x_input)
# print "p.data.shape:", p.data.shape
# print "c.data.shape:", c.data.shape
# print "functions.swapaxes(c ,2, 1):", functions.swapaxes(c ,2, 1).data.shape
o = functions.matmul(functions.swapaxes(c ,1, 0), p) # (2, 50, 1)
o = functions.swapaxes(o ,1, 0) # (2, 50)
# print "u.data.shape:", u.data.shape
# print "o.data.shape:", o.data.shape
# print "u.data.shape:", u.data
# print "o.data.shape:", o.data
# print (u+o).data.shape
predict = self.W(u + o)
# print predict.data.shape
loss = functions.softmax_cross_entropy(predict, answer)
return loss
def use_model(model):
data = np.loadtxt('data/human_test_1_32_32_32.txt').reshape(1, 1, 32, 32, 32).astype(np.float32)
y= model.fwd(data)
A= F.softmax(y).data
print(A.argmax(axis=1))
print(A[0,A.argmax(axis=1)])
def __call__(self, x):
"""Return a softmax probability distribution over predicted classes."""
# Convolutional layers
hs, _ = self.feature_map_activations(x)
h = hs[-1]
# Fully connected layers
h = F.dropout(F.relu(self.fc6(h)))
h = F.dropout(F.relu(self.fc7(h)))
h = self.fc8(h)
return F.softmax(h)
def predict(self, input_x):
if isinstance(input_x, chainer.Variable):
device = cuda.get_device(input_x.data)
else:
device = cuda.get_device(input_x)
xp = self.predictor.xp
with device:
output = self.predictor(input_x)
batch_size, input_channel, input_h, input_w = input_x.shape
batch_size, _, grid_h, grid_w = output.shape
x, y, w, h, conf, prob = F.split_axis(F.reshape(output, (batch_size, self.predictor.n_boxes, self.predictor.n_classes+5, grid_h, grid_w)), (1, 2, 3, 4, 5), axis=2)
x = F.sigmoid(x)
y = F.sigmoid(y)
conf = F.sigmoid(conf)
prob = F.transpose(prob, (0, 2, 1, 3, 4))
prob = F.softmax(prob)
prob = F.transpose(prob, (0, 2, 1, 3, 4))
# convert coordinates to those on the image
x_shift = xp.asarray(np.broadcast_to(np.arange(grid_w, dtype=np.float32), x.shape))
y_shift = xp.asarray(np.broadcast_to(np.arange(grid_h, dtype=np.float32).reshape(grid_h, 1), y.shape))
w_anchor = xp.asarray(np.broadcast_to(np.reshape(np.array(self.anchors, dtype=np.float32)[:, 0], (self.predictor.n_boxes, 1, 1, 1)), w.shape))
h_anchor = xp.asarray(np.broadcast_to(np.reshape(np.array(self.anchors, dtype=np.float32)[:, 1], (self.predictor.n_boxes, 1, 1, 1)), h.shape))
box_x = (x + x_shift) / grid_w
box_y = (y + y_shift) / grid_h
box_w = F.exp(w) * w_anchor / grid_w
box_h = F.exp(h) * h_anchor / grid_h
return box_x, box_y, box_w, box_h, conf, prob
def predict(self, input_x):
if isinstance(input_x, chainer.Variable):
device = cuda.get_device(input_x.data)
else:
device = cuda.get_device(input_x)
xp = self.predictor.xp
with device:
output = self.predictor(input_x)
batch_size, input_channel, input_h, input_w = input_x.shape
batch_size, _, grid_h, grid_w = output.shape
x, y, w, h, conf, prob = F.split_axis(F.reshape(output, (batch_size, self.predictor.n_boxes, self.predictor.n_classes+5, grid_h, grid_w)), (1, 2, 3, 4, 5), axis=2)
x = F.sigmoid(x)
y = F.sigmoid(y)
conf = F.sigmoid(conf)
prob = F.transpose(prob, (0, 2, 1, 3, 4))
prob = F.softmax(prob)
prob = F.transpose(prob, (0, 2, 1, 3, 4))
# convert coordinates to those on the image
x_shift = xp.asarray(np.broadcast_to(np.arange(grid_w, dtype=np.float32), x.shape))
y_shift = xp.asarray(np.broadcast_to(np.arange(grid_h, dtype=np.float32).reshape(grid_h, 1), y.shape))
w_anchor = xp.asarray(np.broadcast_to(np.reshape(np.array(self.anchors, dtype=np.float32)[:, 0], (self.predictor.n_boxes, 1, 1, 1)), w.shape))
h_anchor = xp.asarray(np.broadcast_to(np.reshape(np.array(self.anchors, dtype=np.float32)[:, 1], (self.predictor.n_boxes, 1, 1, 1)), h.shape))
box_x = (x + x_shift) / grid_w
box_y = (y + y_shift) / grid_h
box_w = F.exp(w) * w_anchor / grid_w
box_h = F.exp(h) * h_anchor / grid_h
return box_x, box_y, box_w, box_h, conf, prob
def listnet(x, t):
"""
The Top-1 approximated ListNet loss as in Cao et al (2006), Learning to
Rank: From Pairwise Approach to Listwise Approach
:param x: The activation of the previous layer
:param t: The target labels
:return: The loss
"""
# ListNet top-1 reduces to a softmax and simple cross entropy
st = F.softmax(t, axis=0)
sx = F.softmax(x, axis=0)
return -F.mean(st * F.log(sx))
def select_action(self, t, greedy_action_func, action_value=None):
assert action_value is not None
assert isinstance(action_value,
chainerrl.action_value.DiscreteActionValue)
n_actions = action_value.q_values.shape[1]
with chainer.no_backprop_mode():
probs = chainer.cuda.to_cpu(
F.softmax(action_value.q_values / self.T).data).ravel()
return np.random.choice(np.arange(n_actions), p=probs)
def all_prob(self):
with chainer.force_backprop_mode():
if self.min_prob > 0:
return (F.softmax(self.beta * self.logits)
* (1 - self.min_prob * self.n)) + self.min_prob
else:
return F.softmax(self.beta * self.logits)
def predict(self,batch,randFlag):
t = [[bi] for bi in [1] * batch]
t = self.makeEmbedBatch(t)
ys_d = self.dec(t, train=False)
ys_w = [self.h2w(y) for y in ys_d]
name_arr_arr = []
if randFlag:
t = [predictRandom(F.softmax(y_each)) for y_each in ys_w]
else:
t = [y_each.data[-1].argmax(0) for y_each in ys_w]
name_arr_arr.append(t)
t = [self.embed(xp.array([t_each], dtype=xp.int32)) for t_each in t]
count_len = 0
while count_len < 50:
ys_d = self.dec(t, train=False)
ys_w = [self.h2w(y) for y in ys_d]
if randFlag:
t = [predictRandom(F.softmax(y_each)) for y_each in ys_w]
else:
t = [y_each.data[-1].argmax(0) for y_each in ys_w]
name_arr_arr.append(t)
t = [self.embed(xp.array([t_each], dtype=xp.int32)) for t_each in t]
count_len += 1
tenti = xp.array(name_arr_arr).T
for name in tenti:
name = [self.vocab.itos(nint) for nint in name]
if "</s>" in name:
print(" Gen:{}".format("".join(name[:name.index("</s>")])))
def weighted_cross_entropy(p,t,weight_arr,sec_arr,weigh_flag=True):
print("p:{}".format(p.data.shape))
b = np.zeros(p.shape,dtype=np.float32)
b[np.arange(p.shape[0]), t] = 1
soft_arr = F.softmax(p)
log_arr = -F.log(soft_arr)
xent = b*log_arr
#
# print("sec_arr:{}".format(sec_arr))
# print("xent_shape:{}".format(xent.data.shape))
xent = F.split_axis(xent,sec_arr,axis=0)
print([xent_e.data.shape[0] for xent_e in xent])
x_sum = [F.reshape(F.sum(xent_e)/xent_e.data.shape[0],(1,1)) for xent_e in xent]
# print("x_sum:{}".format([x_e.data for x_e in x_sum]))
xent = F.concat(x_sum,axis=0)
#
# print("xent1:{}".format(xent.data))
xent = F.max(xent,axis=1)/p.shape[0]
# print("xent2:{}".format(xent.data))
if not weigh_flag:
return F.sum(xent)
# print("wei_arr:{}".format(weight_arr))
# print("wei_arr:{}".format(weight_arr.data.shape))
print("xent3:{}".format(xent.data.shape))
wxent= F.matmul(weight_arr,xent,transa=True)
wxent = F.sum(F.sum(wxent,axis=0),axis=0)
print("wxent:{}".format(wxent.data))
return wxent
def probs(self):
return F.softmax(self.logits)
def attention_sum(encoding, query):
alpha = F.softmax(F.batch_matmul(encoding, query, transb=True))
alpha, encoding = F.broadcast(alpha[:, :, :, None],
encoding[:, :, None, :])
return F.sum(alpha * encoding, axis=1)
def _attend(self, p):
weight = F.batch_matmul(self.source_hiddens, p)
weight = F.where(self.mask, weight, self.minf)
attention = F.softmax(weight)
return attention
def _attend(self, p):
p = self.xh(p)
p = F.expand_dims(p, 1)
p = F.broadcast_to(p, self.shape2)
h = F.tanh(self.h + p)
shape3 = (self.batchsize * self.src_len, self.dim_hid)
h_reshaped = F.reshape(h, shape3)
weight_reshaped = self.hw(h_reshaped)
weight = F.reshape(weight_reshaped, (self.batchsize, self.src_len, 1))
weight = F.where(self.mask, weight, self.minf)
attention = F.softmax(weight)
return attention
def __call__(self, x):
return functions.softmax(x, self.axis)
def main():
model = load_model(args.model_dir)
assert model is not None
vocab, vocab_inv = load_vocab(args.model_dir)
assert vocab is not None
assert vocab_inv is not None
vocab_size = model.vocab_size
with chainer.using_config("train", False):
for n in range(args.num_generate):
word_ids = np.arange(0, vocab_size, dtype=np.int32)
token = ID_BOS
x = np.asarray([[token]]).astype(np.int32)
model.reset_state()
while token != ID_EOS and x.shape[1] < args.max_sentence_length:
u = model.forward_one_step(x)
p = F.softmax(u).data[-1]
token = np.random.choice(word_ids, size=1, p=p)
x = np.append(x, np.asarray([token]).astype(np.int32), axis=1)
sentence = []
for token in x[0]:
word = vocab_inv[token]
sentence.append(word)
print(" ".join(sentence))
def check_forward(self, x_data, use_cudnn=True):
x = chainer.Variable(x_data)
y = functions.softmax(x, use_cudnn)
self.assertEqual(y.data.dtype, self.dtype)
y_expect = numpy.exp(self.x)
y_roll = numpy.rollaxis(y_expect, 1, y_expect.ndim)
for i in numpy.ndindex(y_roll.shape[:-1]):
y_roll[i] /= y_roll[i].sum()
gradient_check.assert_allclose(
y_expect, y.data, **self.check_forward_options)
def forward(self):
x = chainer.Variable(self.x)
return functions.softmax(x, use_cudnn=self.use_cudnn)
def __call__(self, input_blob, test_mode=False):
# explicit and very flexible DAG!
#################################
data = input_blob[0]
labels = input_blob[1]
if(len(input_blob) >= 3):
weights_classes = input_blob[2]
else:
weights_classes = chainer.Variable(cuda.cupy.ones((self.classes, 1), dtype='float32'))
# ---- CONTRACTION BLOCKS ---- #
blob_b0 = self.bnorm0(data)
(blob_b1, indices_b1, size_b1) = F.max_pooling_2dIndices(self.bnorm1(F.relu(self.conv1(blob_b0)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
(blob_b2, indices_b2, size_b2) = F.max_pooling_2dIndices(self.bnorm2(F.relu(self.conv2(blob_b1)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
(blob_b3, indices_b3, size_b3) = F.max_pooling_2dIndices(self.bnorm3(F.relu(self.conv3(blob_b2)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
(blob_b4, indices_b4, size_b4) = F.max_pooling_2dIndices(self.bnorm4(F.relu(self.conv4(blob_b3)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
# ---- EXPANSION BLOCKS ---- #
blob_b5 = self.bnorm5(F.relu(self.conv5(F.unpooling_2d(blob_b4, indices_b4, size_b4))), test=test_mode)
blob_b6 = self.bnorm6(F.relu(self.conv6(F.unpooling_2d(blob_b5, indices_b3, size_b3))), test=test_mode)
blob_b7 = self.bnorm7(F.relu(self.conv7(F.unpooling_2d(blob_b6, indices_b2, size_b2))), test=test_mode)
blob_b8 = self.bnorm8(F.relu(self.conv8(F.unpooling_2d(blob_b7, indices_b1, size_b1))), test=test_mode)
#ipdb.set_trace()
# ---- SOFTMAX CLASSIFIER ---- #
self.blob_class = self.classi(blob_b8)
self.probs = F.softmax(self.blob_class)
# ---- CROSS-ENTROPY LOSS ---- #
#ipdb.set_trace()
self.loss = F.weighted_cross_entropy(self.probs, labels, weights_classes, normalize=True)
self.output_point = self.probs
return self.loss