Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_dummy_encoder_output(encoder_out_shape=(100, 80, 5)):
"""
This only provides an example to generate dummy encoder output
"""
(T, B, D) = encoder_out_shape
encoder_out = {}
encoder_out["encoder_out"] = torch.from_numpy(
np.random.randn(*encoder_out_shape).astype(np.float32)
)
seq_lengths = torch.from_numpy(np.random.randint(low=1, high=T, size=B))
# some dummy mask
encoder_out["encoder_padding_mask"] = torch.arange(T).view(1, T).expand(
B, -1
) >= seq_lengths.view(B, 1).expand(-1, T)
encoder_out["encoder_padding_mask"].t_()
# encoer_padding_mask is (T, B) tensor, with (t, b)-th element indicate
# whether encoder_out[t, b] is valid (=0) or not (=1)
return encoder_out
boxes: numpy array of shape [num_boxes, 4]. Each row is in form
[y_min, x_min, y_max, x_max].
"""
y_1 = np.random.uniform(size=(1, num_boxes)) * max_height
y_2 = np.random.uniform(size=(1, num_boxes)) * max_height
x_1 = np.random.uniform(size=(1, num_boxes)) * max_width
x_2 = np.random.uniform(size=(1, num_boxes)) * max_width
boxes = np.zeros(shape=(num_boxes, 4))
boxes[:, 0] = np.minimum(y_1, y_2)
boxes[:, 1] = np.minimum(x_1, x_2)
boxes[:, 2] = np.maximum(y_1, y_2)
boxes[:, 3] = np.maximum(x_1, x_2)
return boxes.astype(np.float32)
def test_obj_oriented_wavefront_focusing_reverses():
x = y = np.arange(128, dtype=np.float32)
z = np.random.rand(128, 128)
wf = propagation.Wavefront(x=x, y=y, fcn=z, wavelength=HeNe)
wf2 = wf.focus(1, 1).unfocus(1, 1) # first is efl, meaningless. second is Q, we neglect padding at the moment
assert np.allclose(wf.fcn, wf2.fcn)
ex_g = OCLArray.empty((Nz, Ny, Nx), np.complex64)
ey_g = OCLArray.empty((Nz, Ny, Nx), np.complex64)
ez_g = OCLArray.empty((Nz, Ny, Nx), np.complex64)
kxs_g = OCLArray.from_array(kxs.astype(np.float32))
kys_g = OCLArray.from_array(kys.astype(np.float32))
t = time.time()
p.run_kernel("debye_wolf_lattice", (Nx, Ny, Nz),
None,
ex_g.data,
ey_g.data,
ez_g.data,
u_g.data,
np.float32(1.), np.float32(0.),
# np.float32(-dx*(Nx-1)//2.),np.float32(dx*(Nx-1)//2.),
# np.float32(-dy*(Ny-1)//2.),np.float32(dy*(Ny-1)//2.),
# np.float32(-dz*(Nz-1)//2.),np.float32(dz*(Nz-1)//2.),
np.float32(dx*(-Nx//2)), np.float32(dx*(Nx//2-1)),
np.float32(dy*(-Ny//2)), np.float32(dy*(Ny//2-1)),
np.float32(dz*(-Nz//2)), np.float32(dz*(Nz//2-1)),
np.float32(1.*lam/n0),
np.float32(alpha1),
np.float32(alpha2),
kxs_g.data,
kys_g.data,
np.int32(len(kxs)),
np.float32(sigma)
)
u = u_g.get()
def half_mask(half_type, img_size):
mask = np.ones((img_size, img_size), dtype=np.float32)
half = int(img_size / 2.)
if half_type == 0: # top mask
mask[:half, :] = 0.
elif half_type == 1: # bottom mask
mask[half:, :] = 0.
elif half_type == 2: # left mask
mask[:, :half] = 0.
elif half_type == 3: # right mask
mask[:, half:] = 0.
else:
raise NotImplementedError
return mask
hidden = tf.concat([self.elu, self.prev_rewards, self.prev_actions_onehot,
self.timestep], 1, name="Concatenated_input")
else:
hidden = self.elu
summary_hidden_act = tf.contrib.layers.summarize_activation(hidden)
rnn_in = tf.expand_dims(hidden, [0], name="RNN_input")
step_size = tf.shape(self.inputs)[:1]
if FLAGS.fw:
rnn_cell = LayerNormFastWeightsBasicRNNCell(48)
# self.initial_state = rnn_cell.zero_state(tf.shape(self.inputs)[0], tf.float32)
# self.initial_fast_weights = rnn_cell.zero_fast_weights(tf.shape(self.inputs)[0], tf.float32)
h_init = np.zeros((1, 48), np.float32)
fw_init = np.zeros((1, 48, 48), np.float32)
self.state_init = [h_init, fw_init]
h_in = tf.placeholder(tf.float32, [1, 48], name="hidden_state")
fw_in = tf.placeholder(tf.float32, [1, 48, 48], name="fast_weights")
self.state_in = (h_in, fw_in)
rnn_outputs, rnn_state = tf.nn.dynamic_rnn(
rnn_cell, rnn_in, initial_state=self.state_in, sequence_length=step_size,
time_major=False)
rnn_h, rnn_fw = rnn_state
self.state_out = (rnn_h[:1, :], rnn_fw[:1, :])
rnn_out = tf.reshape(rnn_outputs, [-1, 48], name="RNN_out")
else:
lstm_cell = tf.contrib.rnn.LayerNormBasicLSTMCell(48)
c_init = np.zeros((1, lstm_cell.state_size.c), np.float32)
h_init = np.zeros((1, lstm_cell.state_size.h), np.float32)
self.state_init = [c_init, h_init]
def _smooth_l1_loss(x, t, in_weight, sigma):
sigma2 = sigma ** 2
diff = in_weight * (x - t)
abs_diff = F.absolute(diff)
flag = (abs_diff.array < (1. / sigma2)).astype(np.float32)
y = (flag * (sigma2 / 2.) * F.square(diff) +
(1 - flag) * (abs_diff - 0.5 / sigma2))
return F.sum(y)
lons = np.genfromtxt(os.path.join(BASEPATH, 'LON.TXT'))
lats = np.genfromtxt(os.path.join(BASEPATH, 'LAT.TXT'))
dn50 = np.genfromtxt(os.path.join(BASEPATH, 'DN50.TXT'))
n050 = np.genfromtxt(os.path.join(BASEPATH, 'N050.TXT'))
# import matplotlib.pyplot as plt
# plt.contourf(lons, lats, dn50, 128)
# plt.show()
# plt.contourf(lons, lats, n050, 128)
# plt.show()
save_kwargs = {
'lons': lons.astype(np.float32),
'lats': lats.astype(np.float32),
'dn50': dn50.astype(np.float32),
'n050': n050.astype(np.float32),
}
np.savez('refract_map', **save_kwargs)
----------
t : data type
The data type for which the FITS type is requested
Returns
-------
fits_type : str or None
The FITS string code describing the data type, or None if unknown type.
"""
conv = {
np.dtype(np.bool): "L",
np.dtype(np.uint8): "B",
np.dtype(np.int16): "I",
np.dtype(np.int32): "J",
np.dtype(np.int64): "K",
np.dtype(np.float32): "E",
np.dtype(np.float64): "D",
np.dtype(np.complex64): "C",
np.dtype(np.complex128): "M",
}
try:
if t in conv:
return conv[t]
except:
pass
try:
if np.dtype(t) in conv:
return conv[np.dtype(t)]
except:
pass
try:
if np.dtype(type(t)) in conv:
def position_encoding(sentence_size, embedding_size):
"""
Position Encoding described in section 4.1 [1]
"""
encoding = np.ones((embedding_size, sentence_size), dtype=np.float32)
ls = sentence_size+1
le = embedding_size+1
for i in range(1, le):
for j in range(1, ls):
encoding[i-1, j-1] = (i - (le-1)/2) * (j - (ls-1)/2)
encoding = 1 + 4 * encoding / embedding_size / sentence_size
return np.transpose(encoding)