Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_running_mean(self, running_mean):
"""
Set the running mean of the BatchNormalization layer.
:param running_mean: a Numpy array.
"""
callZooFunc(self.bigdl_type, "setRunningMean",
self.value, JTensor.from_ndarray(running_mean))
return self
def row_to_sample(row, column_info, model_type="wide_n_deep"):
"""
convert a row to sample given column feature information of a WideAndDeep model
:param row: Row of userId, itemId, features and label
:param column_info: ColumnFeatureInfo specify information of different features
:return: TensorSample as input for WideAndDeep model
"""
wide_tensor = get_wide_tensor(row, column_info)
deep_tensor = get_deep_tensors(row, column_info)
deep_tensors = [JTensor.from_ndarray(ele) for ele in deep_tensor]
label = row[column_info.label]
model_type = model_type.lower()
if model_type == "wide_n_deep":
feature = [wide_tensor] + deep_tensors
elif model_type == "wide":
feature = wide_tensor
elif model_type == "deep":
feature = deep_tensors
else:
raise TypeError("Unsupported model_type: %s" % model_type)
return Sample.from_jtensor(feature, label)
def set_running_std(self, running_std):
"""
Set the running variance of the BatchNormalization layer.
:param running_std: a Numpy array.
"""
callZooFunc(self.bigdl_type, "setRunningStd",
self.value, JTensor.from_ndarray(running_std))
return self
def __init__(self,
learningrate=1e-3,
learningrate_decay=0.0,
weightdecay=0.0,
momentum=0.0,
dampening=DOUBLEMAX,
nesterov=False,
leaningrate_schedule=None,
learningrates=None,
weightdecays=None,
bigdl_type="float"):
super(SGD, self).__init__(None, bigdl_type, learningrate, learningrate_decay, weightdecay,
momentum, dampening, nesterov,
leaningrate_schedule if (leaningrate_schedule) else Default(),
JTensor.from_ndarray(learningrates), JTensor.from_ndarray(weightdecays))
"""
Save a variable dictionary to a Java object file, so it can be read by BigDL
:param tensors: tensor dictionary
:param target_path: where is the Java object file store
:param bigdl_type: model variable numeric type
:return: nothing
"""
import numpy as np
jtensors = {}
for tn in tensors.keys():
if not isinstance(tensors[tn], np.ndarray):
value = np.array(tensors[tn])
else:
value = tensors[tn]
jtensors[tn] = JTensor.from_ndarray(value)
callBigDlFunc(bigdl_type, "saveTensorDictionary", jtensors, target_path)
wide_columns = column_info.wide_base_cols + column_info.wide_cross_cols
wide_dims = column_info.wide_base_dims + column_info.wide_cross_dims
wide_length = len(wide_columns)
acc = 0
indices = []
for i in range(0, wide_length):
index = row[wide_columns[i]]
if i == 0:
res = index
else:
acc += wide_dims[i - 1]
res = acc + index
indices.append(res)
values = np.array([i + 1 for i in indices])
shape = np.array([sum(wide_dims)])
return JTensor.sparse(values, np.array(indices), shape)
def set_running_mean(self, running_mean):
"""
Set the running mean of the BatchNormalization layer.
:param running_mean: a Numpy array.
"""
callBigDlFunc(self.bigdl_type, "setRunningMean",
self.value, JTensor.from_ndarray(running_mean))
return self
# hasattr(jinvoker, name) always return true here,
# so you need to invoke the method to check if it exist or not
try:
api = getattr(jinvoker, name)
java_result = api(*args)
result = _java2py(gateway, java_result)
except Exception as e:
error = e
if "does not exist" not in str(e):
raise e
else:
return result
raise error
class JTensor(BJTensor):
def __init__(self, storage, shape, bigdl_type="float", indices=None):
super(JTensor, self).__init__(storage, shape, bigdl_type, indices)
@classmethod
def from_ndarray(cls, a_ndarray, bigdl_type="float"):
"""
Convert a ndarray to a DenseTensor which would be used in Java side.
"""
if a_ndarray is None:
return None
assert isinstance(a_ndarray, np.ndarray), \
"input should be a np.ndarray, not %s" % type(a_ndarray)
return cls(a_ndarray,
a_ndarray.shape,
bigdl_type)
def convert_output(output):
if type(output) is JTensor:
return output.to_ndarray()
elif(len(output) == 1):
return output[0].to_ndarray()
else:
return [x.to_ndarray() for x in output]
n_output,
eps=1e-5,
momentum=0.1,
affine=True,
init_weight=None,
init_bias=None,
init_grad_weight=None,
init_grad_bias=None,
bigdl_type="float"):
super(SpatialBatchNormalization, self).__init__(None, bigdl_type,
n_output,
eps,
momentum,
affine,
JTensor.from_ndarray(init_weight),
JTensor.from_ndarray(init_bias),
JTensor.from_ndarray(init_grad_weight),
JTensor.from_ndarray(init_grad_bias))