Cleaning up parameter() parameters
This commit is contained in:
Родитель
e8b61f2ea2
Коммит
d91c8bc767
|
@ -109,7 +109,7 @@ def seqcla():
|
|||
|
||||
# setup embedding matrix
|
||||
embedding = C.parameter((embed_dim, vocab), learning_rate_multiplier=0.0,
|
||||
init='fromFile', init_from_file_path=embedding_file)
|
||||
init_from_file_path=embedding_file)
|
||||
|
||||
# get the vector representing the word
|
||||
sequence = C.times(embedding, features, name='sequence')
|
||||
|
@ -146,7 +146,7 @@ def seqcla():
|
|||
acc = calc_accuracy(train_file, ctx.output_filename_base)
|
||||
|
||||
# and test for the same number...
|
||||
TOLERANCE_ABSOLUTE = 1E-06
|
||||
TOLERANCE_ABSOLUTE = 1E-05
|
||||
assert np.allclose(acc, 0.6006415396952687, atol=TOLERANCE_ABSOLUTE)
|
||||
|
||||
"""
|
||||
|
|
|
@ -653,70 +653,67 @@ def input(shape, dynamic_axis='', name=None):
|
|||
return Input(shape, dynamicAxis=dynamic_axis, name=name)
|
||||
|
||||
|
||||
def parameter(shape=None, value=0, learning_rate_multiplier=1.0, init='uniform',
|
||||
init_value_scale=1, init_from_file_path='', init_from_literal=None,
|
||||
random_seed=-1, name=None):
|
||||
def parameter(shape=None, value=None, learning_rate_multiplier=1.0,
|
||||
init_from_file_path=None, name=None):
|
||||
"""
|
||||
It creates a parameter tensor.
|
||||
|
||||
Args:
|
||||
shape (tuple or int): the shape of the input tensor. If `init='fromLiteral'`, shape is not needed as it will be inferred from the literal.
|
||||
value: a scalar initial value that would be replicated for every element in the tensor
|
||||
learning_rate_multiplier (float):
|
||||
init (str): 'uniform', 'fromFile' or 'fromLiteral'
|
||||
init_value_scale (float): a scaling factor for the initial value
|
||||
init_from_file_path (str): the file that contains the initial tensor value
|
||||
init_from_literal (ndarray): the numpy array used to initialize the tensor parameter
|
||||
random_seed (float): the seed used for initialization
|
||||
shape (tuple or int, optional): the shape of the input tensor. If not provided, it will be inferred from ``value``.
|
||||
value (scalar or NumPy array, optional): a scalar initial value that would be replicated for every element in the tensor or NumPy array. If ``None``, the tensor will be initialized uniformly random.
|
||||
learning_rate_multiplier (float): set to control the learning rate on this particular node
|
||||
init_from_file_path (str): the file that contains the initial tensor value. Used only if ``value=None``.
|
||||
name (str, optional): the name of the node in the network
|
||||
|
||||
Returns:
|
||||
:class:`cntk.graph.ComputationNode`
|
||||
"""
|
||||
|
||||
from . import cntk1
|
||||
|
||||
# if the parameter is initialized from a literal value
|
||||
if (init == 'fromLiteral'):
|
||||
"""
|
||||
To be as generic as possible, we
|
||||
- flatten the data
|
||||
- initialize a ParameterTensor operator with it
|
||||
- ensure that the graph does not backprob to it.
|
||||
- Finally we to reshape it.
|
||||
"""
|
||||
if value is None:
|
||||
if shape is None:
|
||||
raise ValueError('you need to specify at least shape or value')
|
||||
|
||||
value = init_from_literal
|
||||
if init_from_file_path:
|
||||
return cntk1.ParameterTensor(shape, init='fromFile',
|
||||
initFromFilePath=init_from_file_path, name=name)
|
||||
else:
|
||||
return cntk1.ParameterTensor(shape, name=name)
|
||||
|
||||
from .. import utils
|
||||
if not (np.isscalar(value) or utils.is_tensor(value)):
|
||||
raise ValueError('value type is not supported: %s' % type(value))
|
||||
"""
|
||||
To be as generic as possible, we
|
||||
- flatten the data
|
||||
- initialize a ParameterTensor operator with it
|
||||
- ensure that the graph does not backprob to it.
|
||||
- Finally we to reshape it.
|
||||
"""
|
||||
|
||||
if isinstance(value, list) or np.isscalar(value):
|
||||
value = np.asarray(value)
|
||||
from .. import utils
|
||||
if not (np.isscalar(value) or utils.is_tensor(value)):
|
||||
raise ValueError('value type is not supported: %s' % type(value))
|
||||
|
||||
import scipy.sparse
|
||||
if scipy.sparse.issparse(value):
|
||||
raise ValueError('only dense data is supported')
|
||||
if isinstance(value, list) or np.isscalar(value):
|
||||
value = np.asarray(value)
|
||||
|
||||
param_shape = value.shape if value.shape else (1,)
|
||||
literal_shape = (param_shape[0], np.multiply.reduce(param_shape[1:]))
|
||||
import scipy.sparse
|
||||
if scipy.sparse.issparse(value):
|
||||
raise ValueError('only dense data is supported')
|
||||
|
||||
literal_array = np.reshape(value, literal_shape)
|
||||
param_shape = value.shape if value.shape else (1,)
|
||||
literal_shape = (param_shape[0], np.multiply.reduce(param_shape[1:]))
|
||||
|
||||
from io import BytesIO
|
||||
s = BytesIO()
|
||||
np.savetxt(s, literal_array, '%.4f')
|
||||
literal_array = np.reshape(value, literal_shape)
|
||||
|
||||
return cntk1.ParameterTensor(
|
||||
dims=param_shape,
|
||||
learningRateMultiplier=learning_rate_multiplier,
|
||||
init='fromLiteral',
|
||||
initFromLiteral=s.getvalue().decode())
|
||||
from io import BytesIO
|
||||
s = BytesIO()
|
||||
np.savetxt(s, literal_array, '%.4f')
|
||||
|
||||
else:
|
||||
return cntk1.ParameterTensor(shape, learning_rate_multiplier, init,
|
||||
init_value_scale, value, init_from_file_path,
|
||||
randomSeed=random_seed, name=name)
|
||||
return cntk1.ParameterTensor(
|
||||
dims=param_shape,
|
||||
learningRateMultiplier=learning_rate_multiplier,
|
||||
init='fromLiteral',
|
||||
initFromLiteral=s.getvalue().decode())
|
||||
|
||||
|
||||
def constant(value, name=None):
|
||||
|
@ -730,8 +727,7 @@ def constant(value, name=None):
|
|||
:class:`cntk.graph.ComputationNode`
|
||||
"""
|
||||
|
||||
return parameter(name=name, init='fromLiteral', init_from_literal=value,
|
||||
learning_rate_multiplier=0.0)
|
||||
return parameter(value=value, learning_rate_multiplier=0.0, name=name)
|
||||
|
||||
|
||||
def dynamic_axis(name=None):
|
||||
|
|
|
@ -19,9 +19,10 @@ def eval(node):
|
|||
# [array([[[-150., 200.], [5., 10.]]])]
|
||||
|
||||
Args:
|
||||
node (cntk.graph.ComputationNode): the node to evaluate
|
||||
node (:class:`cntk.graph.ComputationNode`): the node to evaluate
|
||||
|
||||
Returns:
|
||||
numpy array containing the result
|
||||
NumPy array containing the result
|
||||
"""
|
||||
|
||||
from cntk.context import get_context
|
||||
|
@ -31,15 +32,15 @@ def eval(node):
|
|||
ctx = get_context()
|
||||
first = True
|
||||
|
||||
# the params are passed as arryas e.g. plus([1,2], [3,4]), we need to
|
||||
# wrap them with input and parameter nodes
|
||||
# The params are passed as arryas, e.g. plus([1,2], [3,4]), and we need to
|
||||
# wrap them with input and parameter nodes.
|
||||
if node.params:
|
||||
for p in node.params:
|
||||
if p in node.inputs:
|
||||
val = getattr(node, p)
|
||||
# one param needs to be an Input() node. This is being fixed in
|
||||
#CNTK we will remove this workaround onces we can evaluate a
|
||||
#network with no inputs
|
||||
# One param needs to be an Input() node. This will being fixed in
|
||||
# CNTK soon, so that we can remove this workaround and evaluate a
|
||||
# network with no inputs.
|
||||
if first:
|
||||
if not isinstance(val, list):
|
||||
# inputs have the outmost dimension for sequences
|
||||
|
|
Загрузка…
Ссылка в новой задаче