本文整理汇总了Python中numpy.Array方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.Array方法的具体用法?Python numpy.Array怎么用?Python numpy.Array使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.Array方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _variable_on_cpu
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def _variable_on_cpu(name, shape, initializer, trainable):
"""Helper function to get a variable stored on cpu.
Args:
name: A `str` holding the name of the variable.
shape: An `Array` defining the shape of the Variable. For example: [2,1,3].
initializer: The `tf.Initializer` to use to initialize the variable.
trainable: A `bool` stating wheter the variable is trainable or not.
Returns:
A `tf.Variable` on CPU.
"""
with tf.device('/cpu:0'): #TODO will this work?
dtype = tf.float32
var = tf.get_variable(name, shape, initializer=initializer, dtype=dtype, trainable=trainable)
#dtf.add_to_collection('CPU', var)
return var
示例2: process_hidden_tensors
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def process_hidden_tensors(t):
"""Embeddings are returned from the BERT model in a non-ideal embedding shape:
- unnecessary batch dimension
- Undesired second sentence "[SEP]".
Drop the unnecessary information and just return what we need for the first sentence
"""
# Drop unnecessary batch dim and second sent
t = t.squeeze(0)[:-1]
# Drop second sentence sep ??
t = t[1:-1]
# Convert to numpy
return t.data.numpy()
# np.Array -> np.Array
示例3: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def __init__( self, title, subdivisions, grid_centering='G', shift=np.array( [ 0., 0., 0. ] ) ):
"""
Initialise an AutoKPoints object
Args:
title (Str): The first line of the file, treated as a comment by VASP.
grid_centering (Str, optional): Specify gamma-centered (G) or the original Monkhorst-Pack scheme (MP). Default is 'G'.
subdivisions: (np.Array( Int, Int, Int )): Numbers of subdivisions along each reciprocal lattice vector.
shift: (np.Array( Float, Float, Float ), optional): Optional shift of the mesh (s_1, s_2, s_3). Default is ( [ 0., 0., 0. ] ).
Returns:
None
"""
accepted_grid_centerings = [ 'G', 'MP' ]
if grid_centering not in accepted_grid_centerings:
raise ValueError
self.title = title
self.grid_centering = grid_centering
self.subdivisions = subdivisions
self.shift = shift
示例4: update_mask
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def update_mask(record, mask_array):
"""Update mask in tensorflow example.
Args:
record (tf.train.Example): Record to update
mask_array (numpy.Array): HxW array of class values.
Returns: Updated tf.train.Example.
"""
def norm2bytes(value):
return value.encode() if isinstance(value, str) and six.PY3 else value
mask_data = get_png_string(mask_array)
feature = record.features.feature['image/segmentation/class/encoded']
feature.bytes_list.value.pop()
feature.bytes_list.value.append(norm2bytes(mask_data))
return record
示例5: split_truncate_theta
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def split_truncate_theta(theta, chi_max, eps):
"""Split and truncate a two-site wave function in mixed canonical form.
Split a two-site wave function as follows::
vL --(theta)-- vR => vL --(A)--diag(S)--(B)-- vR
| | | |
i j i j
Afterwards, truncate in the new leg (labeled ``vC``).
Parameters
----------
theta : np.Array[ndim=4]
Two-site wave function in mixed canonical form, with legs ``vL, i, j, vR``.
chi_max : int
Maximum number of singular values to keep
eps : float
Discard any singular values smaller than that.
Returns
-------
A : np.Array[ndim=3]
Left-canonical matrix on site i, with legs ``vL, i, vC``
S : np.Array[ndim=1]
Singular/Schmidt values.
B : np.Array[ndim=3]
Right-canonical matrix on site j, with legs ``vC, j, vR``
"""
chivL, dL, dR, chivR = theta.shape
theta = np.reshape(theta, [chivL * dL, dR * chivR])
X, Y, Z = svd(theta, full_matrices=False)
# truncate
chivC = min(chi_max, np.sum(Y > eps))
piv = np.argsort(Y)[::-1][:chivC] # keep the largest `chivC` singular values
X, Y, Z = X[:, piv], Y[piv], Z[piv, :]
# renormalize
S = Y / np.linalg.norm(Y) # == Y/sqrt(sum(Y**2))
# split legs of X and Z
A = np.reshape(X, [chivL, dL, chivC])
B = np.reshape(Z, [chivC, dR, chivR])
return A, S, B
示例6: reconstruct_batch
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def reconstruct_batch(self, output, batch_id, chosen_labels=None):
""" Create the song associated with the network output
Args:
output (list[np.Array]): The ouput of the network (size batch_size*output_dim)
batch_id (int): The batch that we must reconstruct
chosen_labels (list[np.Array[batch_size, int]]): the sampled class at each timestep (useful to reconstruct the generated song)
Return:
Song: The reconstructed song
"""
raise NotImplementedError('Abstract class')
示例7: update
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def update(self, sigmoid_logits, true_labels):
"""Update the ROC tracker, with the predictions on one batch made during validation.
Args:
sigmoid_logits: `np.Array` and 2D arrray holding the sigmoid logits for the validation batch.
true_labels: `np.Array` and 2D arrray holding the true labels for the validation batch.
"""
# threshold this thing
# we consider a class "predicted" if it's sigmoid activation is higher than 0.5 (predicted labels)
batch_predicted_labels = np.greater(sigmoid_logits, 0.5)
batch_predicted_labels = batch_predicted_labels.astype(float)
batch_pred_pos = np.sum(batch_predicted_labels, axis=0) #sum up along the batch dim, keep the channels
batch_actual_pos = np.sum(true_labels, axis=0) #sum up along the batch dim, keep the channels
# calculate the true positives:
batch_true_pos = np.sum(np.multiply(batch_predicted_labels, true_labels), axis=0)
# and update the counts
self.pred_positives_sum += batch_pred_pos #what the model said
self.actual_positives_sum += batch_actual_pos #what the labels say
self.true_positive_sum += batch_true_pos # where labels and model predictions>0.5 match
assert len(self.true_positive_sum) == self._opts._nclasses
# add the predictions to the roc_score tracker
self.roc_score.append(sigmoid_logits)
self.roc_labels.append(true_labels)
示例8: softmax
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def softmax(X, theta = 1.0, axis = None):
"""Compute the softmax of each element along an axis of X.
Args:
X: `ND-Array`, Probably should be floats.
theta: float parameter, used as a multiplier
prior to exponentiation. Default = 1.0 (optional).
axis: axis to compute values along. Default is the
first non-singleton axis (optional).
Returns:
An `Array` of same shape as X. The result will sum to 1 along the specified axis.
"""
# make X at least 2d
y = np.atleast_2d(X)
# find axis
if axis is None:
axis = next(j[0] for j in enumerate(y.shape) if j[1] > 1)
# multiply y against the theta parameter,
y = y * float(theta)
# subtract the max for numerical stability
y = y - np.expand_dims(np.max(y, axis = axis), axis)
# exponentiate y
y = np.exp(y)
# take the sum along the specified axis
ax_sum = np.expand_dims(np.sum(y, axis = axis), axis)
# finally: divide elementwise
p = y / ax_sum
# flatten if X was 1D
if len(X.shape) == 1: p = p.flatten()
return p
示例9: normalize
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def normalize(a):
"""Divide each head by its norm"""
norms = np.linalg.norm(a, axis=-1, keepdims=True)
return a / norms
# np.Array:<a,b,c,d> -> np.Array<a,b,c*d>
示例10: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def __init__(self, folder, pattern=FAISS_LAYER_PATTERN):
super().__init__(folder, pattern)
self.head_mask = partial(create_mask, self.head_size, self.nheads)
# Int -> [Int] -> np.Array -> Int -> (np.Array(), )
示例11: apply_griffin_lim
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import Array [as 别名]
def apply_griffin_lim(inputs, input_lens, CONFIG, ap):
'''Apply griffin-lim to each sample iterating throught the first dimension.
Args:
inputs (Tensor or np.Array): Features to be converted by GL. First dimension is the batch size.
input_lens (Tensor or np.Array): 1D array of sample lengths.
CONFIG (Dict): TTS config.
ap (AudioProcessor): TTS audio processor.
'''
wavs = []
for idx, spec in enumerate(inputs):
wav_len = (input_lens[idx] * ap.hop_length) - ap.hop_length # inverse librosa padding
wav = inv_spectrogram(spec, ap, CONFIG)
# assert len(wav) == wav_len, f" [!] wav lenght: {len(wav)} vs expected: {wav_len}"
wavs.append(wav[:wav_len])
return wavs