How do define custom activation function - activation-function

Can u explain how can I get this function f(x)=max(Relu(x),1).
By using only Relu's variations on x (without max)?

Related

Mlrose TSPOpt Genetic Algorithm own cost function

I want to program my own cost function for my own TSP problem. I do not want to use the mlrose one, because I want to optimize real coords with time.
First of all, I created a coords_List which looks like this: [(49.321,8.213),[50.321,9.124]...)
Then I created my own fitness function which accepts the guess array and returns a float.
fitnessF = mlrose.CustomFitness(coords_list)
Now I set up mlRose:
problem_fit = mlrose.TSPOpt(length = len(coords_list),fitness_fn =fitnessF, maximize=False)
best_state, best_fitness = mlrose.genetic_alg(problem_fit, random_state = 2)
This returns:
Exception: fitness_fn must have problem type 'tsp'.
Now I set up the code like this:
fitnessF = mlrose.TravellingSales(check_fitness)
Now it will return:
object of type 'function' has no len()
Thanks in advance
Ps: I would also be ready to share my notebook over Email
I could figure out where the problem was. It is right that I have to define my custom fitness function, but for TSP I have to add the problem type:
fitnessF = mlrose.CustomFitness(coords_list,"tsp")
Now it will work.

How do i pass values into a function?

In python, I can make a function that requires data to be passed in. For example, I can make a function like:
def functionName(x)
and can then use x within the function. When I try this in processing like:
void functionName(x)
it gives me the error message Error on "... VariableDeclaratorId".
Is there a way to pass in data in processing or java like in python?
You have to define the type of the parameter. For example, if the value you want to be passed in is a float, you would define your function as:
void funtionName(float x)

Inside a function, how do I construct a new function based on original function parameters, in order to pass as an argument to another function

I'm having trouble with function declarations and scope in julia. I have a main function, let's call it mainfunc which accepts some arguments. Within this function, I would ultimately like to call a different function, say callfunc, which takes a function as an argument. This function I will call passfunc.
One further complication I have is that there is a final function which I define outside of the logic which depends on the arguments but still depends on the arguments in a different way. I can call this initfunc. This must be composed with some other function, depending on the arguments, to create passfunc.
Based on the arguments given to mainfunc, I will have different definitions of passfunc. Given the answer I got to a related question here, I initially tried to define my function logic in the following way, using anonymous functions which are apparently more efficient:
function mainfunc(args)
init_func = x -> funcA(x, args)
if args[1] == "foo"
anon_func = x -> func1(x, args)
elseif args[1] == "bar"
anon_func = x -> func2(x, args)
end
function passfunc(x)
return init_func(x) + anon_func(x)
end
# ... define other args...
callfunc(passfunc, other_args)
end
Defining my function in this way leads to errors in julia - apparently passfunc is an undefined variable when I run this code. Does the scope not allow the anonymous functions to be defined in the if statements? How else could I write code that achieves this?
I feel like a better understanding of functional programming principles would make the solution here obvious. Thank you in advance for any tips you can offer on improving this.
Also, I am running this with julia v0.7

Ti Nspire: Convert solve(...) output to a callable Function

in order to calculate the inverse function of f(x) I defined following function:
inv(fx):=exp▶list(solve(fx=y,x),x)
which output is:
inv(x^(2)) {piecewise(−√(y),y≥0),piecewise(√(y),y≥0)}
So that part works already, but how can I use this result as a callable function i(y)?
Thanks for your help
Outside of your program, you can turn the result into function i(y) with:
i(y):=piecewise(-√(y),y≥0,√(y),y≥0)
I do not have a CAS, so your results may differ, but, because the function can only return one value, it would only return (and display in the graph) the first value, in this case, -√(y). If you want to display on the graph or get the values of both, you would be better off creating two separate functions (-√(y), and √(y)). Hope this helps you "use the result as a callable function."

How to use call function?

Lets say I have some g elements of class line. So I can write:
var lines = svg.selectAll('.line');
lines.exit().remove();
lines.transition().append('text')
lines.enter().append('g').attr('class', 'line');
And then append('text') is called for each g.line element. What if I want to call custom function f instead of append? I guess this should use call function, but:
lines.call(f) will call f only once (with all data, but I want to launch f separately for each element)
Luckily found that this is possible, not sure if there is smth better:
lines.transition().each(function(datum, index) {
renderDataLine(d3.select(this), data[index], ...);
});
You probably want to look at each, see here
I quote:
Invokes the specified function for each element in the current selection
I set up a fiddle (watch console) here

Resources