def compile(self, in_x, train_feed, eval_feed):
n = np.product(self.in_d)
m, param_init_fn = [dom[i] for (dom, i) in zip(self.domains, self.chosen)]
#sc = np.sqrt(6.0) / np.sqrt(m + n)
#W = tf.Variable(tf.random_uniform([n, m], -sc, sc))
W = tf.Variable( param_init_fn( [n, m] ) )
b = tf.Variable(tf.zeros([m]))
# if the number of input dimensions is larger than one, flatten the
# input and apply the affine transformation.
if len(self.in_d) > 1:
in_x_flat = tf.reshape(in_x, shape=[-1, n])
out_y = tf.add(tf.matmul(in_x_flat, W), b)
else:
out_y = tf.add(tf.matmul(in_x, W), b)
return out_y
# computes the output dimension based on the padding scheme used.
# this comes from the tensorflow documentation
python类product()的实例源码
def get_outdim(self):
#assert in_x == self.b.get_outdim()
# relaxing input dimension equal to output dimension. taking into
# account the padding scheme considered.
out_d_b = self.b.get_outdim()
in_d = self.in_d
if len(out_d_b) == len(in_d):
out_d = tuple(
[max(od_i, id_i) for (od_i, id_i) in zip(out_d_b, in_d)])
else:
# flattens both input and output.
out_d_b_flat = np.product(out_d_b)
in_d_flat = np.product(in_d)
out_d = (max(out_d_b_flat, in_d_flat) ,)
return out_d
def get_surface(self, dest_surf = None):
camera = self.camera
im = highgui.cvQueryFrame(camera)
#convert Ipl image to PIL image
#print type(im)
if im:
xx = opencv.adaptors.Ipl2NumPy(im)
#print type(xx)
#print xx.iscontiguous()
#print dir(xx)
#print xx.shape
xxx = numpy.reshape(xx, (numpy.product(xx.shape),))
if xx.shape[2] != 3:
raise ValueError("not sure what to do about this size")
pg_img = pygame.image.frombuffer(xxx, (xx.shape[1],xx.shape[0]), "RGB")
# if there is a destination surface given, we blit onto that.
if dest_surf:
dest_surf.blit(pg_img, (0,0))
return dest_surf
#return pg_img
def test_addsumprod(self):
# Tests add, sum, product.
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
assert_equal(np.add.reduce(x), add.reduce(x))
assert_equal(np.add.accumulate(x), add.accumulate(x))
assert_equal(4, sum(array(4), axis=0))
assert_equal(4, sum(array(4), axis=0))
assert_equal(np.sum(x, axis=0), sum(x, axis=0))
assert_equal(np.sum(filled(xm, 0), axis=0), sum(xm, axis=0))
assert_equal(np.sum(x, 0), sum(x, 0))
assert_equal(np.product(x, axis=0), product(x, axis=0))
assert_equal(np.product(x, 0), product(x, 0))
assert_equal(np.product(filled(xm, 1), axis=0), product(xm, axis=0))
s = (3, 4)
x.shape = y.shape = xm.shape = ym.shape = s
if len(s) > 1:
assert_equal(np.concatenate((x, y), 1), concatenate((xm, ym), 1))
assert_equal(np.add.reduce(x, 1), add.reduce(x, 1))
assert_equal(np.sum(x, 1), sum(x, 1))
assert_equal(np.product(x, 1), product(x, 1))
def test_testAddSumProd(self):
# Test add, sum, product.
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
self.assertTrue(eq(np.add.reduce(x), add.reduce(x)))
self.assertTrue(eq(np.add.accumulate(x), add.accumulate(x)))
self.assertTrue(eq(4, sum(array(4), axis=0)))
self.assertTrue(eq(4, sum(array(4), axis=0)))
self.assertTrue(eq(np.sum(x, axis=0), sum(x, axis=0)))
self.assertTrue(eq(np.sum(filled(xm, 0), axis=0), sum(xm, axis=0)))
self.assertTrue(eq(np.sum(x, 0), sum(x, 0)))
self.assertTrue(eq(np.product(x, axis=0), product(x, axis=0)))
self.assertTrue(eq(np.product(x, 0), product(x, 0)))
self.assertTrue(eq(np.product(filled(xm, 1), axis=0),
product(xm, axis=0)))
if len(s) > 1:
self.assertTrue(eq(np.concatenate((x, y), 1),
concatenate((xm, ym), 1)))
self.assertTrue(eq(np.add.reduce(x, 1), add.reduce(x, 1)))
self.assertTrue(eq(np.sum(x, 1), sum(x, 1)))
self.assertTrue(eq(np.product(x, 1), product(x, 1)))
def __init__(self, input_shape, output_shape, output_sparsity=.05):
"""
"""
self.learning_rate = 1/100
self.input_shape = tuple(input_shape)
self.output_shape = tuple(output_shape)
self.input_size = np.product(self.input_shape)
self.output_size = np.product(self.output_shape)
self.on_bits = max(1, int(round(output_sparsity * self.output_size)))
self.xp_q = NStepQueue(3, .90, self.learn)
self.expected_values = np.random.random((self.input_size, self.output_size)) * self.learning_rate
self.expected_values = np.array(self.expected_values, dtype=np.float32)
print("Supervised Controller")
print("\tExpected Values shape:", self.expected_values.shape)
print("\tFuture discount:", self.xp_q.discount)
print("\tLearning Rate:", self.learning_rate)
def predict(self, input_sdr=None):
"""
Argument inputs is ndarray of indexes into the input space.
Returns probability of each catagory in output space.
"""
self.input_sdr.assign(input_sdr)
pdf = self.stats[self.input_sdr.flat_index]
if True:
# Combine multiple probabilities into single pdf. Product, not
# summation, to combine probabilities of independant events. The
# problem with this is if a few unexpected bits turn on it
# mutliplies the result by zero, and the test dataset is going to
# have unexpected things in it.
return np.product(pdf, axis=0, keepdims=False)
else:
# Use summation B/C it works well.
return np.sum(pdf, axis=0, keepdims=False)
def add_task(self, dataset_filename, model_filename):
dataset_src = open(dataset_filename,'r').read()
model_src = open(model_filename,"r").read()
src,info = hopt.extract_hopts(model_src)
ss_size = int(np.product( map(lambda x: len(x["options"]), info.values() ) ))
print "Search space size: ", ss_size
w = []
for i in range(0,ss_size):
info_i,src_i = hopt.produce_variant(src,copy.deepcopy(info),i)
info_i["subtask"] = i
w.append( (info_i,dataset_src,src_i) )
print "submitting task..."
rv = self.socket.send_json(("submit_task", w))
print rv
def repeat_sum(u,shape,rep_axes):
"""
Computes sum of a repeated matrix
In effect, this routine computes
code:`np.sum(repeat(u,shape,rep_axes))`. However, it performs
this without having to perform the full repetition.
"""
# Must convert to np.array to perform slicing
shape_vec = np.array(shape,dtype=int)
rep_vec = np.array(rep_axes,dtype=int)
# repeat and sum
urep = repeat_axes(u,shape,rep_axes,rep=False)
usum = np.sum(urep)*np.product(shape_vec[rep_vec])
return usum
def get_final_shape(data_array, out_dims, direction_to_names):
"""
Determine the final shape that data_array must be reshaped to in order to
have one axis for each of the out_dims (for instance, combining all
axes collected by the '*' direction).
"""
final_shape = []
for direction in out_dims:
if len(direction_to_names[direction]) == 0:
final_shape.append(1)
else:
# determine shape once dimensions for direction (usually '*') are combined
final_shape.append(
np.product([len(data_array.coords[name])
for name in direction_to_names[direction]]))
return final_shape
def _init_space(self, space):
if not isinstance(space, gym.Space):
raise ValueError("Unknown space, type '%s'" % type(space))
elif isinstance(space, gym.spaces.Box):
n_dims = np.product(space.shape)
handler = BoxClipHandler(space.low, space.high)
elif isinstance(space, gym.spaces.Discrete):
n_dims = 1
handler = IntHandler(space.n)
elif isinstance(space, gym.spaces.HighLow):
n_dims = space.num_rows
handler = HighLowHandler(space.matrix)
elif isinstance(space, gym.spaces.Tuple):
raise NotImplementedError("Space of type '%s' is not supported"
% type(space))
return n_dims, handler
def train(self, sentences, iterations=1000):
# Preprocess sentences to create indices of context and next words
self.dictionary = build_dictionary(sentences, self.vocabulary_size)
indices = to_indices(sentences, self.dictionary)
self.reverse_dictionary = {index: word for word, index in self.dictionary.items()}
inputs, outputs = self.create_context(indices)
# Create cost and gradient function for gradient descent
shapes = [self.W_shape, self.U_shape, self.H_shape, self.C_shape]
flatten_nplm_cost_gradient = flatten_cost_gradient(nplm_cost_gradient, shapes)
cost_gradient = bind_cost_gradient(flatten_nplm_cost_gradient, inputs, outputs,
sampler=get_stochastic_sampler(10))
# Train neural network
parameters_size = np.sum(np.product(shape) for shape in shapes)
initial_parameters = np.random.normal(size=parameters_size)
self.parameters, cost_history = gradient_descent(cost_gradient, initial_parameters, iterations)
return cost_history
def ser(x, y):
"""Measure symbol error rate between symbols in x and y.
:param x: symbol array #1
:param y: symbol array #2
:returns: symbol error rate
>>> import arlpy
>>> arlpy.comms.ser([0,1,2,3], [0,1,2,2])
0.25
"""
x = _np.asarray(x, dtype=_np.int)
y = _np.asarray(y, dtype=_np.int)
n = _np.product(_np.shape(x))
e = _np.count_nonzero(x^y)
return float(e)/n
def ber(x, y, m=2):
"""Measure bit error rate between symbols in x and y.
:param x: symbol array #1
:param y: symbol array #2
:param m: symbol alphabet size (maximum 64)
:returns: bit error rate
>>> import arlpy
>>> arlpy.comms.ber([0,1,2,3], [0,1,2,2], m=4)
0.125
"""
x = _np.asarray(x, dtype=_np.int)
y = _np.asarray(y, dtype=_np.int)
if _np.any(x >= m) or _np.any(y >= m) or _np.any(x < 0) or _np.any(y < 0):
raise ValueError('Invalid data for specified m')
if m == 2:
return ser(x, y)
if m > _MAX_M:
raise ValueError('m > %d not supported' % (_MAX_M))
n = _np.product(_np.shape(x))*_np.log2(m)
e = x^y
e = e[_np.nonzero(e)]
e = _np.sum(_popcount[e])
return float(e)/n
def __init__(self, xshape, dtype, opt=None):
"""
Initialise an FISTADFT object with problem size and options.
Parameters
----------
xshape : tuple of ints
Shape of working variable X (the primary variable)
dtype : data-type
Data type for working variables
opt : :class:`FISTADFT.Options` object
Algorithm options
"""
if opt is None:
opt = FISTADFT.Options()
Nx = np.product(xshape)
super(FISTADFT, self).__init__(Nx, xshape, dtype, opt)
self.Xf = None
self.Yf = None
def __init__(self, xshape, dtype, opt=None):
"""
Initialise an ADMMEqual object with problem size and options.
Parameters
----------
xshape : tuple of ints
Shape of working variable X (the primary variable)
dtype : data-type
Data type for working variables
opt : :class:`ADMMEqual.Options` object
Algorithm options
"""
if opt is None:
opt = ADMMEqual.Options()
Nx = np.product(xshape)
super(ADMMEqual, self).__init__(Nx, xshape, xshape, dtype, opt)
def mpraw_as_np(shape, dtype):
"""Construct a numpy array of the specified shape and dtype for which the
underlying storage is a multiprocessing RawArray in shared memory.
Parameters
----------
shape : tuple
Shape of numpy array
dtype : data-type
Data type of array
Returns
-------
arr : ndarray
Numpy array
"""
sz = int(np.product(shape))
csz = sz * np.dtype(dtype).itemsize
raw = mp.RawArray('c', csz)
return np.frombuffer(raw, dtype=dtype, count=sz).reshape(shape)
def slinterp(X, factor, copy=True):
"""
Slow-ish linear interpolation of a 1D numpy array. There must be some
better function to do this in numpy.
Parameters
----------
X : ndarray
1D input array to interpolate
factor : int
Integer factor to interpolate by
Return
------
X_r : ndarray
"""
sz = np.product(X.shape)
X = np.array(X, copy=copy)
X_s = np.hstack((X[1:], [0]))
X_r = np.zeros((factor, sz))
for i in range(factor):
X_r[i, :] = (factor - i) / float(factor) * X + (i / float(factor)) * X_s
return X_r.T.ravel()[:(sz - 1) * factor + 1]
def slinterp(X, factor, copy=True):
"""
Slow-ish linear interpolation of a 1D numpy array. There must be some
better function to do this in numpy.
Parameters
----------
X : ndarray
1D input array to interpolate
factor : int
Integer factor to interpolate by
Return
------
X_r : ndarray
"""
sz = np.product(X.shape)
X = np.array(X, copy=copy)
X_s = np.hstack((X[1:], [0]))
X_r = np.zeros((factor, sz))
for i in range(factor):
X_r[i, :] = (factor - i) / float(factor) * X + (i / float(factor)) * X_s
return X_r.T.ravel()[:(sz - 1) * factor + 1]
def exact_expected_fscore_naive(probs, thresh):
"""NB: This algorithm is exponential in the size of probs!
Based on initial measurements, less than 15 items is
sub-second. 16 = 2s, 17=4s, 18=8s, and, well, you know
the rest...
possible relaxation to allow larger number of products:
force items with sufficiently low probs (e.g. < 1%) off
in groundtruths.
"""
probs = np.asarray(probs)
n = len(probs)
expected = 0
p_none = np.product(1-probs)
predict_none = p_none > thresh
predictions = (probs >= thresh).astype(np.int8)
for gt in itertools.product([0,1], repeat=n):
gt = np.array(gt)
fs = fscore(predictions, gt, predict_none)
p = gt_prob(gt, probs)
expected += fs * p
return expected
def eqe1(E, query, vocabulary, priors):
"""
Arguments:
E - word embedding
Q - list of query terms
vocabulary -- list of relevant words
priors - precomputed priors with same indices as vocabulary
>>> E = dict()
>>> E['a'] = np.asarray([0.5,0.5])
>>> E['b'] = np.asarray([0.2,0.8])
>>> E['c'] = np.asarray([0.9,0.1])
>>> E['d'] = np.asarray([0.8,0.2])
>>> q = "a b".split()
>>> vocabulary = "a b c".split()
>>> priors = np.asarray([0.25,0.5,0.25])
>>> posterior = eqe1(E, q, vocabulary, priors)
>>> vocabulary[np.argmax(posterior)]
'c'
"""
posterior = [priors[i] *
np.product([delta(E[qi], E[w]) / priors[i] for qi in query])
for i, w in enumerate(vocabulary)]
return np.asarray(posterior)
def weight_by_class_balance(truth, classes=None):
"""
Determines a loss weight map given the truth by balancing the classes from the classes argument.
The classes argument can be used to only include certain classes (you may for instance want to exclude the background).
"""
if classes is None:
# Include all classes
classes = np.unique(truth)
weight_map = np.zeros_like(truth, dtype=np.float32)
total_amount = np.product(truth.shape)
for c in classes:
class_mask = np.where(truth==c,1,0)
class_weight = 1/((np.sum(class_mask)+1e-8)/total_amount)
weight_map += (class_mask*class_weight)#/total_amount
return weight_map
def eval_expression(expr, values=None):
"""
Evaluate a symbolic expression and returns a numerical array.
:param expr: A symbolic expression to evaluate, in the form of a N_terms * N_Vars matrix
:param values: None, or a dictionary of variable:value pairs, to substitute in the symbolic expression.
:return: An evaled expression, in the form of an N_terms array.
"""
n_coeffs = expr.shape[0]
evaled_expr = np.zeros(n_coeffs)
for (i, term) in enumerate(expr):
if values:
evaled_term = np.array([values.get(elem, 0) if isinstance(elem, str) else elem for elem in term])
else:
evaled_term = np.array(
[0 if isinstance(elem, str) else elem for elem in term]) # All variables at 0
evaled_expr[i] = np.product(evaled_term.astype(float)) # Gradient is the product of values
return evaled_expr
def test_addsumprod(self):
# Tests add, sum, product.
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
assert_equal(np.add.reduce(x), add.reduce(x))
assert_equal(np.add.accumulate(x), add.accumulate(x))
assert_equal(4, sum(array(4), axis=0))
assert_equal(4, sum(array(4), axis=0))
assert_equal(np.sum(x, axis=0), sum(x, axis=0))
assert_equal(np.sum(filled(xm, 0), axis=0), sum(xm, axis=0))
assert_equal(np.sum(x, 0), sum(x, 0))
assert_equal(np.product(x, axis=0), product(x, axis=0))
assert_equal(np.product(x, 0), product(x, 0))
assert_equal(np.product(filled(xm, 1), axis=0), product(xm, axis=0))
s = (3, 4)
x.shape = y.shape = xm.shape = ym.shape = s
if len(s) > 1:
assert_equal(np.concatenate((x, y), 1), concatenate((xm, ym), 1))
assert_equal(np.add.reduce(x, 1), add.reduce(x, 1))
assert_equal(np.sum(x, 1), sum(x, 1))
assert_equal(np.product(x, 1), product(x, 1))
def test_testAddSumProd(self):
# Test add, sum, product.
(x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
self.assertTrue(eq(np.add.reduce(x), add.reduce(x)))
self.assertTrue(eq(np.add.accumulate(x), add.accumulate(x)))
self.assertTrue(eq(4, sum(array(4), axis=0)))
self.assertTrue(eq(4, sum(array(4), axis=0)))
self.assertTrue(eq(np.sum(x, axis=0), sum(x, axis=0)))
self.assertTrue(eq(np.sum(filled(xm, 0), axis=0), sum(xm, axis=0)))
self.assertTrue(eq(np.sum(x, 0), sum(x, 0)))
self.assertTrue(eq(np.product(x, axis=0), product(x, axis=0)))
self.assertTrue(eq(np.product(x, 0), product(x, 0)))
self.assertTrue(eq(np.product(filled(xm, 1), axis=0),
product(xm, axis=0)))
if len(s) > 1:
self.assertTrue(eq(np.concatenate((x, y), 1),
concatenate((xm, ym), 1)))
self.assertTrue(eq(np.add.reduce(x, 1), add.reduce(x, 1)))
self.assertTrue(eq(np.sum(x, 1), sum(x, 1)))
self.assertTrue(eq(np.product(x, 1), product(x, 1)))
def _read_raw_field(self, grid, field):
field_name = field[1]
base_dir = self.ds.index.raw_file
box_list = self.ds.index.raw_field_map[field_name][0]
fn_list = self.ds.index.raw_field_map[field_name][1]
offset_list = self.ds.index.raw_field_map[field_name][2]
lev = grid.Level
filename = base_dir + "Level_%d/" % lev + fn_list[grid.id]
offset = offset_list[grid.id]
box = box_list[grid.id]
lo = box[0]
hi = box[1]
shape = hi - lo + 1
with open(filename, "rb") as f:
f.seek(offset)
f.readline() # always skip the first line
arr = np.fromfile(f, 'float64', np.product(shape))
arr = arr.reshape(shape, order='F')
return arr
def __init__(self, ds, max_level=2):
self.max_level = max_level
self.cell_count = 0
self.layers = []
self.domain_dimensions = ds.domain_dimensions
self.domain_left_edge = ds.domain_left_edge
self.domain_right_edge = ds.domain_right_edge
self.grid_filename = "amr_grid.inp"
self.ds = ds
base_layer = RadMC3DLayer(0, None, 0,
self.domain_left_edge,
self.domain_right_edge,
self.domain_dimensions)
self.layers.append(base_layer)
self.cell_count += np.product(ds.domain_dimensions)
sorted_grids = sorted(ds.index.grids, key=lambda x: x.Level)
for grid in sorted_grids:
if grid.Level <= self.max_level:
self._add_grid_to_layers(grid)
def __init__(self, patchsize, source, binary_mask=None,
random_order=False, mirrored=True, max_num=None):
self.patchsize = patchsize
self.source = source.astype(np.float32)
self.mask = binary_mask
self.random_order = random_order
self.mirrored = mirrored
self.max_num = max_num
if len(self.source.shape)==2:
self.source = self.source[:,:,np.newaxis]
if self.mask is not None and len(self.mask.shape)==2:
self.mask = self.mask[:,:,np.newaxis]
if self.mask is not None:
self.num_patches = (self.mask>0).sum()
else:
self.num_patches = np.product(self.source.shape)
def apply(self, data, copy=False):
if copy:
data = np.copy(data)
data_shape = data.shape
if len(data.shape) > 2:
data = data.reshape(data.shape[0], np.product(data.shape[1:]))
assert len(data.shape) == 2, 'Contrast norm on flattened data'
# assert np.min(data) >= 0.
# assert np.max(data) <= 1.
data -= data.mean(axis=1)[:, np.newaxis]
norms = np.sqrt(np.sum(data ** 2, axis=1)) / self.scale
norms[norms < self.epsilon] = self.epsilon
data /= norms[:, np.newaxis]
if data_shape != data.shape:
data = data.reshape(data_shape)
return data
def weight_by_class_balance(truth, classes=None):
"""
Determines a loss weight map given the truth by balancing the classes from the classes argument.
The classes argument can be used to only include certain classes (you may for instance want to exclude the background).
"""
if classes is None:
# Include all classes
classes = np.unique(truth)
weight_map = np.zeros_like(truth, dtype=np.float32)
total_amount = np.product(truth.shape)
min_weight = sys.maxint
for c in classes:
class_mask = np.where(truth==c,1,0)
class_weight = 1/((np.sum(class_mask)+1e-8)/total_amount)
if class_weight < min_weight:
min_weight = class_weight
weight_map += (class_mask*class_weight)#/total_amount
weight_map /= min_weight
return weight_map