本文整理汇总了Python中scipy.random.seed函数的典型用法代码示例。如果您正苦于以下问题:Python seed函数的具体用法?Python seed怎么用?Python seed使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了seed函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: row_func
def row_func(i):
pyrandom.seed(initseed + int(i))
scirandom.seed(initseed + int(i))
k = scirandom.binomial(N, p, 1)[0]
cur_row[:] = 0.0
cur_row[pyrandom.sample(myrange, k)] = weight
return cur_row
示例2: test_gmres
def test_gmres(self):
# Ensure repeatability
random.seed(0)
# For these small matrices, Householder and MGS GMRES should give the same result,
# and for symmetric (but possibly indefinite) matrices CR and GMRES should give same result
for maxiter in [1,2,3]:
for case, symm_case in zip(self.cases, self.symm_cases):
A = case['A']
b = case['b']
x0 = case['x0']
A_symm = symm_case['A']
b_symm = symm_case['b']
x0_symm = symm_case['x0']
# Test agreement between Householder and GMRES
(x, flag) = gmres_householder(A,b,x0=x0,maxiter=min(A.shape[0],maxiter))
(x2, flag2) = gmres_mgs(A,b,x0=x0,maxiter=min(A.shape[0],maxiter))
assert_array_almost_equal(x/norm(x), x2/norm(x2),
err_msg='Householder GMRES and MGS GMRES gave different results for small matrix')
assert_equal(flag, flag2,
err_msg='Householder GMRES and MGS GMRES returned different convergence flags for small matrix')
# Test agreement between GMRES and CR
if A_symm.shape[0] > 1:
residuals2 = []
(x2, flag2) = gmres_mgs(A_symm, b_symm, x0=x0_symm, maxiter=min(A.shape[0],maxiter),residuals=residuals2)
residuals3 = []
(x3, flag2) = cr(A_symm, b_symm, x0=x0_symm, maxiter=min(A.shape[0],maxiter),residuals=residuals3)
residuals2 = array(residuals2)
residuals3 = array(residuals3)
assert_array_almost_equal(residuals3/norm(residuals3), residuals2/norm(residuals2),
err_msg='CR and GMRES yield different residual vectors')
assert_array_almost_equal(x2/norm(x2), x3/norm(x3), err_msg='CR and GMRES yield different answers')
示例3: reset
def reset(self, params, repetition):
# if params['encoding'] == 'basic':
# self.inputEncoder = PassThroughEncoder()
# elif params['encoding'] == 'distributed':
# self.outputEncoder = PassThroughEncoder()
# else:
# raise Exception("Encoder not found")
print params
self.inputEncoder = PassThroughEncoder()
self.outputEncoder = PassThroughEncoder()
if params['dataset'] == 'nyc_taxi':
self.dataset = NYCTaxiDataset()
else:
raise Exception("Dataset not found")
self.testCounter = 0
self.history = []
self.resets = []
self.currentSequence = self.dataset.generateSequence()
random.seed(6)
self.nDimInput = 3
self.nDimOutput = 1
self.net = buildNetwork(self.nDimInput, params['num_cells'], self.nDimOutput,
hiddenclass=LSTMLayer, bias=True, outputbias=True, recurrent=True)
示例4: reset
def reset(self, params, repetition):
print params
self.nDimInput = 3
self.inputEncoder = PassThroughEncoder()
if params['output_encoding'] == None:
self.outputEncoder = PassThroughEncoder()
self.nDimOutput = 1
elif params['output_encoding'] == 'likelihood':
self.outputEncoder = ScalarBucketEncoder()
self.nDimOutput = self.outputEncoder.encoder.n
if params['dataset'] == 'nyc_taxi' or params['dataset'] == 'nyc_taxi_perturb_baseline':
self.dataset = NYCTaxiDataset(params['dataset'])
else:
raise Exception("Dataset not found")
self.testCounter = 0
self.resets = []
self.iteration = 0
# initialize LSTM network
random.seed(6)
if params['output_encoding'] == None:
self.net = buildNetwork(self.nDimInput, params['num_cells'], self.nDimOutput,
hiddenclass=LSTMLayer, bias=True, outputbias=True, recurrent=True)
elif params['output_encoding'] == 'likelihood':
self.net = buildNetwork(self.nDimInput, params['num_cells'], self.nDimOutput,
hiddenclass=LSTMLayer, bias=True, outclass=SigmoidLayer, recurrent=True)
(self.networkInput, self.targetPrediction, self.trueData) = \
self.dataset.generateSequence(
prediction_nstep=params['prediction_nstep'],
output_encoding=params['output_encoding'])
示例5: setUp
def setUp(self):
self.numberOfSamples = 1000
pkl_file = open(os.path.join(unittest_dir, "test_data", "kdeOrigin_xyz.pck"), "r")
xp = cPickle.load(pkl_file)
yp = cPickle.load(pkl_file)
zp = cPickle.load(pkl_file)
self.sampOrg = SamplingOrigin.SamplingOrigin(zp, xp, yp)
random.seed(10)
示例6: setRandomParameters
def setRandomParameters(net,seed=None,randFunc=random.random):
"""
Sets parameters to random values given by the function randFunc (by
default, uniformly distributed on [0,1) ).
"""
random.seed(seed)
net.setOptimizables( randFunc(len(net.GetParameters())) )
return net.GetParameters()
示例7: GaussianRandomInitializer
def GaussianRandomInitializer(gridShape, sigma=0.2, seed=None, slipSystem=None, slipPlanes=None, slipDirections=None, vacancy=None, smectic=None):
oldgrid = copy.copy(gridShape)
if len(gridShape) == 1:
gridShape = (128,)
if len(gridShape) == 2:
gridShape = (128,128)
if len(gridShape) == 3:
gridShape = (128,128,128)
""" Returns a random initial set of fields of class type PlasticityState """
if slipSystem=='gamma':
state = SlipSystemState.SlipSystemState(gridShape,slipPlanes=slipPlanes,slipDirections=slipDirections)
elif slipSystem=='betaP':
state = SlipSystemBetaPState.SlipSystemState(gridShape,slipPlanes=slipPlanes,slipDirections=slipDirections)
else:
if vacancy is not None:
state = VacancyState.VacancyState(gridShape,alpha=vacancy)
elif smectic is not None:
state = SmecticState.SmecticState(gridShape)
else:
state = PlasticityState.PlasticityState(gridShape)
field = state.GetOrderParameterField()
Ksq_prime = FourierSpaceTools.FourierSpaceTools(gridShape).kSq * (-sigma**2/4.)
if seed is None:
seed = 0
n = 0
random.seed(seed)
Ksq = FourierSpaceTools.FourierSpaceTools(gridShape).kSq.numpy_array()
for component in field.components:
temp = random.normal(scale=gridShape[0],size=gridShape)
ktemp = fft.rfftn(temp)*(sqrt(pi)*sigma)**len(gridShape)*exp(-Ksq*sigma**2/4.)
field[component] = numpy.real(fft.irfftn(ktemp))
#field[component] = GenerateGaussianRandomArray(gridShape, temp ,sigma)
n += 1
"""
t, s = LoadState("2dstate32.save", 0)
for component in field.components:
for j in range(0,32):
field[component][:,:,j] = s.betaP[component].numpy_array()
"""
## To make seed consistent across grid sizes and convergence comparison
gridShape = copy.copy(oldgrid)
if gridShape[0] != 128:
state = ResizeState(state,gridShape[0],Dim=len(gridShape))
state = ReformatState(state)
state.ktools = FourierSpaceTools.FourierSpaceTools(gridShape)
return state
示例8: test_minimal_residual
def test_minimal_residual(self):
# Ensure repeatability
random.seed(0)
self.definite_cases.extend(self.spd_cases)
for case in self.definite_cases:
A = case['A']
maxiter = case['maxiter']
x0 = rand(A.shape[0],)
b = zeros_like(x0)
reduction_factor = case['reduction_factor']
if A.dtype != complex:
# This function should always decrease (assuming zero RHS)
fvals = []
def callback(x):
fvals.append(sqrt(dot(ravel(x),
ravel(A*x.reshape(-1, 1)))))
#
(x, flag) = minimal_residual(A, b, x0=x0,
tol=1e-16, maxiter=maxiter,
callback=callback)
actual_factor = (norm(ravel(b) - ravel(A*x.reshape(-1, 1))) /
norm(ravel(b) - ravel(A*x0.reshape(-1, 1))))
assert(actual_factor < reduction_factor)
if A.dtype != complex:
for i in range(len(fvals)-1):
assert(fvals[i+1] <= fvals[i])
# Test preconditioning
A = pyamg.gallery.poisson((10, 10), format='csr')
x0 = rand(A.shape[0], 1)
b = zeros_like(x0)
fvals = []
def callback(x):
fvals.append(sqrt(dot(ravel(x), ravel(A*x.reshape(-1, 1)))))
#
resvec = []
sa = pyamg.smoothed_aggregation_solver(A)
(x, flag) = minimal_residual(A, b, x0, tol=1e-8, maxiter=20,
residuals=resvec, M=sa.aspreconditioner(),
callback=callback)
assert(resvec[-1] < 1e-8)
for i in range(len(fvals)-1):
assert(fvals[i+1] <= fvals[i])
示例9: test_steepest_descent
def test_steepest_descent(self):
# Ensure repeatability
random.seed(0)
for case in self.spd_cases:
A = case['A']
b = case['b']
x0 = case['x0']
maxiter = case['maxiter']
reduction_factor = case['reduction_factor']
# This function should always decrease
fvals = []
def callback(x):
fvals.append(0.5*dot(ravel(x), ravel(A*x.reshape(-1, 1))) -
dot(ravel(b), ravel(x)))
(x, flag) = steepest_descent(A, b, x0=x0, tol=1e-16,
maxiter=maxiter, callback=callback)
actual_factor = (norm(ravel(b) - ravel(A*x.reshape(-1, 1))) /
norm(ravel(b) - ravel(A*x0.reshape(-1, 1))))
assert(actual_factor < reduction_factor)
if A.dtype != complex:
for i in range(len(fvals)-1):
assert(fvals[i+1] <= fvals[i])
# Test preconditioning
A = pyamg.gallery.poisson((10, 10), format='csr')
b = rand(A.shape[0], 1)
x0 = rand(A.shape[0], 1)
fvals = []
def callback(x):
fvals.append(0.5*dot(ravel(x), ravel(A*x.reshape(-1, 1))) -
dot(ravel(b), ravel(x)))
resvec = []
sa = pyamg.smoothed_aggregation_solver(A)
(x, flag) = steepest_descent(A, b, x0, tol=1e-8, maxiter=20,
residuals=resvec, M=sa.aspreconditioner(),
callback=callback)
assert(resvec[-1] < 1e-8)
for i in range(len(fvals)-1):
assert(fvals[i+1] <= fvals[i])
示例10: test_ishermitian
def test_ishermitian(self):
# make tests repeatable
random.seed(0)
casesT = []
casesF = []
# 1x1
casesT.append(mat(rand(1, 1)))
casesF.append(mat(1.0j*rand(1, 1)))
# 2x2
A = array([[1.0, 0.0], [2.0, 1.0]])
Ai = 1.0j*A
casesF.append(A)
casesF.append(Ai)
A = A + Ai
casesF.append(A)
casesT.append(A + A.conjugate().T)
# 3x3
A = mat(rand(3, 3))
Ai = 1.0j*rand(3, 3)
casesF.append(A)
casesF.append(Ai)
A = A + Ai
casesF.append(A)
casesT.append(A + A.H)
for A in casesT:
# dense arrays
assert_equal(ishermitian(A, fast_check=False), True)
assert_equal(ishermitian(A, fast_check=True), True)
# csr arrays
A = csr_matrix(A)
assert_equal(ishermitian(A, fast_check=False), True)
assert_equal(ishermitian(A, fast_check=True), True)
for A in casesF:
# dense arrays
assert_equal(ishermitian(A, fast_check=False), False)
assert_equal(ishermitian(A, fast_check=True), False)
# csr arrays
A = csr_matrix(A)
assert_equal(ishermitian(A, fast_check=False), False)
assert_equal(ishermitian(A, fast_check=True), False)
示例11: get_neural_net
def get_neural_net(input_number, output_number, NetworkType, HiddenLayerType, neurons_per_layer=[9], use_bias=False):
random.seed(123)
input = LinearLayer(input_number)
output = SoftmaxLayer(output_number)
neural_net = NetworkType()
neural_net.addInputModule(input)
neural_net.addOutputModule(output)
if use_bias:
bias = BiasUnit()
neural_net.addModule(bias)
prev = input
for i in range(0, len(neurons_per_layer)):
hidden = HiddenLayerType(neurons_per_layer[i])
neural_net.addModule(hidden)
neural_net.addConnection(FullConnection(prev, hidden))
if use_bias:
neural_net.addConnection(FullConnection(bias, hidden))
prev = hidden
neural_net.addConnection(FullConnection(prev, output))
if use_bias:
neural_net.addConnection(FullConnection(bias, output))
neural_net.sortModules()
fast_net = neural_net.convertToFastNetwork()
if fast_net is not None:
neural_net = fast_net
print "Use fast C++ implementation"
else:
print "Use standard Python implementation"
print "Create neural network with {} neurons ({} layers)".format(neurons_per_layer, len(neurons_per_layer))
return neural_net
示例12: SineWaveInitializer
def SineWaveInitializer(gridShape, randomPhase=False, seed=None, slipSystem=False, slipPlanes=None, slipDirections=None):
"""
Initialize a plasticity state by setting all its components in any dimension with a
single period of a sine function.
"""
if seed is not None:
random.seed(seed)
if slipSystem=='gamma':
pass
#state = SlipSystemState.SlipSystemState(gridShape,slipPlanes=slipPlanes,slipDirections=slipDirections)
elif slipSystem=='betaP':
pass
#state = SlipSystemState.SlipSystemBetaPState(gridShape,slipPlanes=slipPlanes,slipDirections=slipDirections)
else:
state = PlasticityState.PlasticityState(gridShape)
field = state.GetOrderParameterField()
for component in field.components:
field[component] = GenerateSineArray(gridShape, field.GridDimension(), randomPhase = randomPhase)
return state
示例13: train
def train(self, params, verbose=False):
if params['reset_every_training']:
if verbose:
print 'create lstm network'
random.seed(6)
if params['output_encoding'] == None:
self.net = buildNetwork(self.nDimInput, params['num_cells'], self.nDimOutput,
hiddenclass=LSTMLayer, bias=True, outputbias=True, recurrent=True)
elif params['output_encoding'] == 'likelihood':
self.net = buildNetwork(self.nDimInput, params['num_cells'], self.nDimOutput,
hiddenclass=LSTMLayer, bias=True, outclass=SigmoidLayer, recurrent=True)
self.net.reset()
ds = SequentialDataSet(self.nDimInput, self.nDimOutput)
networkInput = self.window(self.networkInput, params)
targetPrediction = self.window(self.targetPrediction, params)
# prepare a training data-set using the history
for i in xrange(len(networkInput)):
ds.addSample(self.inputEncoder.encode(networkInput[i]),
self.outputEncoder.encode(targetPrediction[i]))
if params['num_epochs'] > 1:
trainer = RPropMinusTrainer(self.net, dataset=ds, verbose=verbose)
if verbose:
print " train LSTM on ", len(ds), " records for ", params['num_epochs'], " epochs "
if len(networkInput) > 1:
trainer.trainEpochs(params['num_epochs'])
else:
self.trainer.setData(ds)
self.trainer.train()
# run through the training dataset to get the lstm network state right
self.net.reset()
for i in xrange(len(networkInput)):
self.net.activate(ds.getSample(i)[0])
示例14: _set_params_and_init
def _set_params_and_init(self, mod, **kwargs):
if not mod.converged: raise ValueError(" Error: This model has not converged, abort sampling ...")
self.seed = 199
self.verbose = False
self.N = 1000
if kwargs is not None:
for key, value in kwargs.iteritems():
setattr(self, key, value)
random.seed(self.seed)
if (mod.multi):
# N is recalculated in case of multi mass
self.Nj = numpy.array(mod.Mj/mod.mj, dtype='int')
self.N = sum(self.Nj)
if min(self.Nj)==0: raise ValueError(" Error: One of the mass components has zero stars!")
self.mod = mod
self.ani = True if min(mod.raj)/mod.rt < 3 else False
示例15: test_condest
def test_condest(self):
# make tests repeatable
random.seed(0)
# Should be exact for small matrices
cases = []
A = mat(array([2.14]))
cases.append(A)
A = mat(array([2.14j]))
cases.append(A)
A = mat(array([-1.2 + 2.14j]))
cases.append(A)
for i in range(1, 6):
A = mat(rand(i, i))
cases.append(A)
cases.append(1.0j*A)
A = mat(A + 1.0j*rand(i, i))
cases.append(A)
for A in cases:
eigs = eigvals(A)
exact = max(abs(eigs))/min(abs(eigs))
c = condest(A)
assert_almost_equal(exact, c)