I am trying to create a model with 'sequence.gather' operator, but getting an error "Where operation can only operate on scalar input" when calling 'train_minibatch'.
input_seq_axis = Axis('inputAxis')
input_sequence = sequence.input_variable(shape=vocab_dim, sequence_axis=input_seq_axis)
vowel_mask_sequence = sequence.input_variable(shape=2, sequence_axis=input_seq_axis)
a = Sequential([
C.layers.Recurrence(C.layers.LSTM(hidden_dim)),
])
b=C.sequence.gather(a(input_sequence),vowel_mask_sequence)
z=Dense(3)(b)
label_sequence = sequence.input_variable(3, sequence_axis=z.dynamic_axes[1])
How can I fix the error ? I even dont use 'where' operator.
For sequence.gather(x, y), y has to be a scalar, that is to say:
assert y.shape == (1,)
The values of y must be either 0 or 1, and also with the same exact dynamic axis as x.
An example on how to use sequence.gather from a library i maintain.
I am a newbie of matlab and am trying to define a pretty complex function to plot it. The content of file is following:
function [res] = distribution (input)
a = factorial(30)
b = factorial(input) * factorial(30-input)
c = power(0.05, input)
d = power(0.95, 30-input)
a/b*c*d
end
in the file named distribution with .m extension. But when I run it error returns: "Error using distribution (line 4). Not enough input arguments."
I read through the "Getting Started" and find no solution. Does anyone have suggestions on this?
The name of the single argument to your function distribution(..), namely argument input, conflicts with the existing native input command of Matlab,
input: Prompt for user input.
...
x = input(prompt)
Try choosing a different name of this argument (in example below: foo), and also remember to return your result by assigning it to the return variable res:
function res = distribution (foo)
a = factorial(30);
b = factorial(foo) * factorial(30-foo);
c = power(0.05, foo);
d = power(0.95, 30-foo);
res = a/b*c*d; % <--- note, return parameter assignment
end
Is it possible to define a function with a function handler as an argument in Matlab?
I've tried with
function x = name(#f,gh)
but I get an error message stating Invalid syntax at '#'.
You cannot use syntax involving # in function definition. The anonymous function handle would do the work:
function x = SO_Example(h,gh)
x = h(gh);
And you can call the function as follows:
SO_Example(#(a)a.^2 , 2)
ans = 4
Or like this:
h = #(a)a.^2;
SO_Example(h,2)
ans = 4
Please, see comments for additional explanations
I have a MATLAB file that contains a single top-level function, called sandbox. That function in turn contains two nested functions, mysum and myprod, which are identical in functionality and what parameters they allow except that one uses #sum internally and the other uses #prod internally. My goal is to create a wrapper function to use in both mysum and myprod that takes care of all the validation and input parsing. This function is called applyFunc.
Here's where it gets tricky. mysum and myprod come in two forms:
mysum(v) returns sum(v, 1).
mysum(v, 'imag') returns sum(v, 1) + 1i
Any other combinations of input should throw an error.
I'm having trouble using inputParser to parse these various combinations of input, specifically the optional string input. Here's the code:
function sandbox()
%% Data
v = [1 4; 3 3];
%% Calculations
s = mysum(v);
si = mysum(v, 'imag');
p = myprod(v);
pi = myprod(v, 'imag');
%% Accuracy tests
assert(isequal(s, [4 7]))
assert(isequal(si, [4+1i 7+1i]))
assert(isequal(p, [3 12]))
assert(isequal(pi, [3+1i 12+1i]))
function x = mysum(varargin)
x = applyFunc(#sum, varargin{:});
end
function x = myprod(varargin)
x = applyFunc(#prod, varargin{:});
end
end
function x = applyFunc(func, varargin)
p = inputParser();
p.addRequired('func', #(x) validateattributes(x, {'function_handle'}, {'scalar'}));
p.addRequired('v', #(x) validateattributes(x, {'double'}, {}, 'applyFunc:msg', 'v'));
p.addOptional('imag', '', #(x) validatestring(x, {'imag', ''})); % THIS LINE IS THE PROBLEM
p.parse(func, varargin{:});
f = p.Results.func;
v = p.Results.v;
strflag = p.Results.imag;
x = f(v);
if ~isempty(strflag)
validatestring(strflag, {'imag'});
x = x + 1i;
end
end
The line that's causing the problem is this one (as marked in the code above):
p.addOptional('imag', '', #(x) validatestring(x, {'imag', ''}));
The documentation for inputParser says that:
For optional string inputs, specify a validation function. Without a validation function, the input parser interprets valid string inputs as invalid parameter names and throws an error.
Unfortunately I don't have any idea how to do this. Is there something simple Im missing or what? If the 'imag' argument isn't passed at all (as in the assignment of s and p), the code works fine, but if I do pass it, I get this error:
Error using sandbox>applyFunc (line 32)
The value of 'imag' is invalid. It must satisfy the function:
#(x)validatestring(x,{'imag',''}).
Error in sandbox/mysum (line 18)
x = applyFunc(#sum, varargin{:});
Error in sandbox (line 7)
si = mysum(v, 'imag');
Any help?
The problem is that validatestring returns the matching string from the cell argument ({'imag',''}) rather than a Boolean indicating if it passes validation. Instead, use strcmp and any:
#(x) any(strcmp(x,{'imag', ''}))
Also, with validatestring, if the input string did not match either 'imag' or '' (actually just 'imag' since empty strings only match in R2014a+), it would throw an error rather than returning false so that the inputParser could return the appropriate error.
Another nice way to fix the problem is to change the syntax of applyFunc entirely so that instead of just 'imag' as an optional string input argument, use a Parameter-Value with 'imag' as the parameter and a validated boolean as the input.
The input definition suggested by Amro in the comments:
p.addParameter('imag', false, #(x)validateattributes(x, {'logical'}, {'scalar'}))
The usage:
mysum(x,'imag',true)
mysum(x) % default is equivalent to mysum(x,'imag',false)
This would simplify the rest of the code with p.Result.imag being a logical scalar. I would suggest:
x = f(v) + p.Result.imag*1i;
The problem is not inputParser, I think the issue is with validatestring.
1) First it does not match on empty strings:
>> x = ''
x =
''
>> validatestring(x, {'imag',''})
Expected input to match one of these strings:
imag,
The input did not match any of the valid strings.
Caused by:
Error using validatestring>checkString (line 85)
Expected input to be a row vector.
2) Second, if it successfully matches, it returns the resolved string (from one of the valid choice), instead of true/false. inputParser requires that the validation function either return a boolean, or nothing but throws error on failure.
I have the following lines of code
arg1 = ( x<=a ).*(log(x)) + ( x>a).*(log(2*a-x));
num = sinh(arg1);
den = const + cosh(arg1);
re = num./den + const2;
re1 = ;
But re is not defined at x=0, as log blows up at 0. But re has a limiting value at 0 which is defined as const3.
I want re1 as const3 when x=0 and as re when x>0.
I tried using piecewise as
re1:= piecewise([x = 0, const3],[ x>0, re]);
But this does not work.
I get the error "Undefined function or method re1 for input arguments of type char.
How should I get the desired result?
Just use logical indexing:
re1 = re;
re1(x == 0) = const3;
and, even though it's probably "impossible", my experience tells me it's a good idea to do this as well:
re1(x >= 2*a) = const3;
You will need Symbolic Math Toolbox.
In general, if you define f(x) as a function and want to calculate the limit at x=a, you do as follow,
sym x
const3 = limit (sym (f),x,a)
This might help: http://www.mathworks.in/help/symbolic/limit.html