Reading Matrix Simulink Robot Arm - matrix

I am building the trajectory of a robot arm and I have computed the position of the degrees of freedom in matrix format, i.e. the first position is row 1 of the matrix (x1,y1,z1), the 2nd position is row 2 (x2,y2,z2) etc.
I am trying to read a row at a time in Simulink and change to the following row when the first position has been reached. However it is not possible for me to solve this with a script. Any suggestions?

If I am understanding your question correctly you have a matrix of desired states and some sort of control loop inside of Simulink.
In brief, loops in Simulink are best represented by switches. And the easiest way to access individual rows of your matrix of desired states (x,y,z) is through the "Select Rows" block.
For example you can feed your matrix into the "Select Rows" simulink block "In1". Then you can take the difference (using sum block) between the output of the the "Select Rows" block and your current state (x,y,z) and feed the norm of this difference into a compare block set to "less than". Feed the result of the compare block into a switch so that the switch will be thrown on when the result of the compare block is less than some tolerance. The switch can be set to 0 when False and 1 + the previous output value of the switch when True. The output value of this switch can then be used to choose the index on the "row selector."
You might encounter a scenario in which the switch will stay true for too long in which case you can either reduce your tolerance or create your own transient switch by using an AND gate to the TRUE input to switch.

Related

How to get immediate next word probability using GPT2 model?

I was trying the hugging face gpt2 model. I have seen the run_generation.py script, which generates a sequence of tokens given a prompt. I am aware that we can use GPT2 for NLG.
In my use case, I wish to determine the probability distribution for (only) the immediate next word following the given prompt. Ideally this distribution would be over the entire vocab.
For example, given the prompt: "How are ", it should give a probability distribution where "you" or "they" have the some high floating point values and other vocab words have very low floating values.
How to do this using hugging face transformers? If it is not possible in hugging face, is there any other transformer model that does this?
You can have a look at how the generation script works with the probabilities.
GPT2LMHeadModel (as well as other "MLHead"-models) returns a tensor that contains for each input the unnormalized probability of what the next token might be. I.e., the last output of the model is the normalized probability of the next token (assuming input_ids is a tensor with token indices from the tokenizer):
outputs = model(input_ids)
next_token_logits = outputs[0][:, -1, :]
You get the distribution by normalizing the logits using softmax. The indices in the first dimension of the next_token_logits correspond to indices in the vocabulary that you get from the tokenizer object.
Selecting the last logits becomes tricky when you use a batch size bigger than 1 and sequences of different lengths. In that case, you would need to specify attention_mask in the model call to mask out padding tokens and then select the last logits using torch.index_select. It is much easier either to use batch size 1 or batch of equally long sequences.
You can use any autoregressive model in Transformers: there is distilGPT-2 (a distilled version of GPT-2), CTRL (which is basically GPT-2 trained with some additional "commands"), the original GPT (under the name openai-gpt), XLNet (designed for contextual embeddings, but can be used for generation in arbitrary order). There are probably more, you can Hugging Face Model Hub.

No division in numerical differentiaton algorithm

I am trying to understand an algorithm that evaluates the speed of a moving element. The position sensors are sampled with varying but rather big speed (from 16MSPS to 24MSPS) and the speed is calculated as a simple difference between the last two values.
The formula for the speed is then v = f(x_(n+1)) - f(x_n) , and according to all numerical approaches i was expectingv = (f(x+h) - f(x)) / h
I don't really understand why the division is omitted. Under what circumstances can the division be ignored?
This system is implemented on a FPGA.
It can be ignored when h is 1 as divide by 1 is a no-op.
Thanks to many comments I was able to understand the problem:
The unit calculating the speed doesn't need to know the time period. By subtracting the next sampled value from the previous one, it produces output values. These values represent a function, that is is linearly dependent on the speed. One way to understand this is, that this output is kind of 'speed without units'. The output can be than further manipulated (oversampled, undersampled) to achieve desired signal quality.
To able to determine the speed in some exact units (like m/s) at least the sampling frequency has to be given. In case of rotational movement also other constants are needed, such as the radius of the axis where the sensor is mounted, etc. This happens at some later point.

Is there any option to do FOR loop in excel?

I have an excel that I'm calculating my Scrum Task's completed average. I have Story point item also in the excel. My calculation is:
Result= SP * percentage of completion --> This calculation is for each row and after that I sum up all result and taking the summary.
But sometimes I am adding new task and for each task I am adding the calculation to the average result.
Is there any way to use for loop in the excel?
for(int i=0;i<50;i++){ if(SP!=null && task!=null)(B+i)*(L+i)}
My calculation is like below:
AVERAGE((B4*L4+B5*L5+B6*L6+B7*L7+B8*L8+B9*L9+B10*L10)/SUM(B4:B10))
First of all, AVERAGE is not doing anything in your formula, since the argument you pass to it is just one single value. You already do an average calculation by dividing by the sum. That average is in fact a weighted average, and so you could not even achieve that with a plain AVERAGE function.
I see several ways to make this formula more generic, so it keeps working when you add rows:
1. Use SUMPRODUCT
=SUMPRODUCT(B4:B100,L4:L100)/SUM(B4:B100)
The row number 100 is chosen arbitrarily, but should evidently encompass all data rows. If you have no data occurring below your table, then it is safe to add a large margin. You'll want to avoid the situation where you think you add a line to the table, but actually get outside of the range of the formula. Using proper Excel tables can help to avoid this situation.
2. Use an array formula
This would be a second resort for when the formula becomes more complicated and cannot be executed with a "simple" SUMPRODUCT. But the above would translate to this array formula:
=SUM(B4:B100*L4:L100)/SUM(B4:B100)
Once you have typed this in the formula bar, make sure to press Ctrl+Shift+Enter to enter it. Only then will it act as an array formula.
Again, the same remark about row number 100.
3. Use an extra column
Things get easy when you use an extra column for storing the product of B & L values for each row. So you would put in cell N4 the following formula:
=B4*L4
...and then copy that relative formula to the other rows. You can hide that column if you want.
Then the overal formula can be:
=SUM(N4:N100)/SUM(B4:B100)
With this solution you must take care to always copy a row when inserting a new row, as you need the N column to have the intermediate product formula also for any new row.

Outcome difference: using list & for-loop vs. single parameter input

This is my first question, so please let me know if I'm not giving enough details or asking a question that is not relevant on this platform!
I want to compute the same formula over a grid running from 0 to 4.0209, therefore I'm using a for-loop with an defined array using numpy.
To be certain that the for-loop is right, I've computed a selection of values by just using specific values for the radius an input in the formula.
Now, the outcomes with the same input of the radius is just slightly different. Do I interpret my grid wrongly? Or is there an error in my script?
It probably is something pretty straightforward, but maybe some of you can find a minute to help me out.
Here I use a selection of values for my radius parameter.
Here I use a for-loop to compute over a distance
Here are the differences in the outcomes:
Outcomes computed with for-loop:
9.443,086753902220000000
1.935,510475232510000000
57,174050755727700000
1,688894026484580000
0,020682674424032700
Outcomes computed with selected radii:
9.444,748178731630000000
1.938,918526458330000000
57,476599453309800000
1,703815523775800000
0,020957378277984600

How does one calculate the integral image from original?

I tried separating the individual channels of the image and then calculate using the recursive function. At the end, I joined the three channels:
function [ii] = computeIntegralImage(image)
%function to compute integral from original image
iip=zeros(size(image,1)+1,size(image,2)+1);
jjp=zeros(size(image,1)+1,size(image,2)+1);
kkp=zeros(size(image,1)+1,size(image,2)+1);
for i=2:size(iip,1)
for j=2:size(iip,2)
iip(i,j)=image(i-1,j-1,1)+iip(i,j-1)+iip(i-1,j)-iip(i-1,j-1);
end
end
for i=2:size(jjp,1)
for j=2:size(jjp,2)
jjp(i,j)=image(i-1,j-1,2)+jjp(i,j-1)+jjp(i-1,j)-jjp(i-1,j-1);
end
end
for i=2:size(kkp,1)
for j=2:size(kkp,2)
kkp(i,j)=image(i-1,j-1,3)+kkp(i,j-1)+kkp(i-1,j)-kkp(i-1,j-1);
end
end
ii= cat(3,iip,jjp,kkp);
The matlab output for function integralImage is completely white:
My output is a colorful image:
The integral image can be easily computed by first integrating over one axis, then integrating the result over the other axis. This 1D integral is computed with cumsum:
out = cumsum(image,1);
out = cumsum(out,2);
Note that if image is of an integer type, this is likely going to lead to overflow. You should convert such an array to double first.
Finally, to display the result you need to use
imshow(out,[])
otherwise you don’t see the full range of the data, and anything above 1 becomes white, as you saw with MATLAB’s result.
Regarding your code:
The problem is overflow. Convert the value taken from input to double first. In MATLAB, uint8(150)+150 == uint8(255). This leads to alternating rows and columns like you see: one step you subtract some large value from the partial sums, leading to a small value, the next step you subtract a small value leading to a large value, etc.
At first I was confused by your first row and column in the output, which remain at 0. But then I noticed that the output is one larger than the input, and you use this first column to avoid special cases.
Consider cropping the first row and column from your output.
Regarding loop order: It is faster when the inner loop is over the first dimension, as then the data is accessed in storage order and therefore uses the cache better. This should not affect the result, just the timing.

Resources