How to implement given Matlab code in Keras? (CNN) - matlab

I can't implement this neural network layer in Keras. I do not quite understand the purpose of "biases" zeros(1, 333) and filters sparce_init(). Help build this layer.
lr = [ 0 . 0 0 1 0 . 0 0 1 ] ;
net.layers = {} ;
Lb=333 ;%s e t d e s i r e d number o f p a t t e r n s
CL=32ˆ2 ;%s e t d e s i r e d r e s o l u t i o n f o r t h e DMD
%EncodingFullyConnectedBlock
%La y e r 1
net.layers{end+1} = struct(’biases’, zeros(1,Lb,’single’), . . .
’biasesLearningRate’,0, . . .
’biasesWeightDecay’,0, . . .
’filters’, sparseinitialization ( [ 1 1 CL Lb ]), . . .
’filtersLearningRate’,1, . . .
’filtersWeightDecay’,1,. . .
’name’, ’binary1’, . . .
’pad’ , [ 0 0 0 0 ] , . . .
’stride’, [ 1 1 ] , . . .
’type’, ’conv’) ;
As I understand it, we encode the input image with 32x32 filters in the amount of 333. But I don’t understand how to simply implement this piece.

Related

In Perl , How to generate All Possible Patterns ,

may i know how to ( in Perl ) ,
generate below All Possible Patterns in a file and on screen output , and each slot in the pattern can be accessed , ?!
many thanks for all ,
input value ,
1 , no. of slots ,
2 , no. of objects ,
for example ,
no. of object = 2 , { a , b } ,
no. of slots = 4 ,
then , output ,
no. of all possible patterns = 2^4 = 16 ,
then ,
row is 16 ,
column is 8 ,
eachSlot[i][j] = allow assign or change its value ,
then , output format look like ,
a a a a
a a a b
a a b a
a a b b
a b a a
a b a b
a b b a
a b b b
b a a a
b a a b
b a b a
b a b b
b b a a
b b a b
b b b a
b b b b
and ,
if see 'a' , then do sth actionX ,
if see 'b' , then do sth actionY ,
many thanks for all the advices and helps ,
use Algorithm::Loops qw( NestedLoops );
my #syms = qw( a b );
my $num_slots = 4;
my $iter = NestedLoops([ ( \#syms ) x $num_slots ]);
while ( my #items = $iter->() ) {
say "#items";
}
I made Set::CrossProduct:
use v5.10;
use Set::CrossProduct;
my $set = Set::CrossProduct->new( [ [ qw(a b) ] x 4 ] );
while( my $next = $set->get ) {
say "#$next";
}
ikegami showed the Algorithm::Loops module, which is also fine to get all the combinations.

q/KDB - nprev function to get all the previous n elements

I am struggling to write a nprev function in KDB; xprev function returns the nth element but I need all the prev n elements relative to the current element.
q)t:([] i:1+til 26; s:.Q.a)
q)update xp:xprev[3;]s,p:prev s from t
Any help is greatly appreciated.
You can achieve the desired result by applying prev repeatedly and flipping the result
q)n:3
q)select flip 1_prev\[n;s] from t
s
-----
" "
"a "
"ba "
"cba"
"dcb"
"edc"
..
If n is much smaller than the rows count, this will be faster than some of the more straightforward solutions.
The xprev function basically looks like this :
xprev1:{y til[count y]-x} //readable xprev
We can tweak it to get all n elements
nprev:{y til[count y]-\:1+til x}
using nprev in the query
q)update np: nprev[3;s] , xp1:xprev1[3;s] , xp: xprev[3;s], p:prev[s] from t
i s np xp1 xp p
-------------------
1 a " "
2 b "a " a
3 c "ba " b
4 d "cba" a a c
5 e "dcb" b b d
6 f "edc" c c e
k equivalent of nprev
k)nprev:{$[0h>#y;'`rank;y(!#y)-\:1+!x]}
and similarly nnext would look like
k)nnext:{$[0h>#y;'`rank;y(!#y)+\:1+!x]}

Scilab Storing Into Array [Channel Coding]

This is part of my codes
e=0;
c=0;
n=10000;
for t=zeros(1:n)
//state1
x=rand();
if(x<=0.95) then disp(t);
c=c+1;
elseif(x>0.95)
//state2
x=rand();
if(x<=0.99) then disp(t)
c=c+1;
//state3
elseif(x>0.99) then disp(t=1)
e=e+1;
arr(e)=t; //store error bits only
end
end
end
disp(c);
disp(e);
for z=1:e //loop the earlier arr(s)
disp(arr(z)) //display all arr of s
end
clear();
What I was trying to do is to generate 10000 of zeros.
Out of these 10000 zeros, there will be few with errors meaning to say for example I might get 9990 of zeros and 10 of ones.
Currently, I have made an array storing only the ones. Now I'm abit lost on how do I store both zeros and ones into the same array.
Let say, current running..I will end up with 10 of ones (Those zeros that contains error bit). Then at this part of the code, all the zeros that has turned into ones will be stored into arr(e). Therefore the output would be
0
0
0
0
0
0
0
0
0
0
But what i wanted is something like this.
arr[1] = 0
.
.
.
arr[250] = 1
.
.
.
arr[749] = 1
.
.
.
arr[1234] = 1
.
.
.
arr[5463] = 1
.
.
.
arr[6678] = 1
.
.
.
arr[8890] = 1
.
.
.
arr[9987] = 1
.
.
.
arr[10000] = 0
Which shows the error bit occur at 250,749,1234,5463,6678,8890,9987
Thank you
All you have to do is:
e = [250 759 1234 5463 6678 8890 9987];
arr = zeros(10000,1);
arr(e) = 1;
e defines where you want the values in arr to be changed to 1. You simply just use e to index into arr and set the corresponding positions to 1. That's it... nothing really to it!

Easy68K IF-ELSE branching

writing my first assembly language program for class using Easy68K.
I'm using an if-else branching to replicate the code:
IF (P > 12)
P = P * 8 + 3
ELSE
P = P - Q
PRINT P
But I think I have my branches wrong because without the first halt in my code the program runs through the IF branch anyway even after the CMP finds a case that P < 12. Am I missing something here or would this be a generally accepted way of doing this?
Here is my assembly code:
START: ORG $1000 ; Program starts at loc $1000
MOVE P, D1 ; [D1] <- P
MOVE Q, D2 ; [D2] <- Q
* Program code here
CMP #12, D1 ; is P > 12?
BGT IF ;
SUB D2, D1 ; P = P - Q
MOVE #3, D0 ; assign read command
TRAP #15 ;
SIMHALT ; halt simulator
IF ASL #3, D1 ; P = P * 8
ADD #3, D1 ; P = P + 3
ENDIF
MOVE #3, D0 ; assign read command
TRAP #15 ;
SIMHALT ; halt simulator
* Data and Variables
ORG $2000 ; Data starts at loc $2000
P DC.W 5 ;
Q DC.W 7 ;
END START ; last line of source
To do if..else, you need two jumps; one at the start, and one at the end of the first block.
While it doesn't affect correctness, it is also conventional to retain source order, which means negating the condition.
MOVE P, D1 ; [D1] <- P
MOVE Q, D2 ; [D2] <- Q
* Program code here
CMP #12, D1 ; is P > 12?
BLE ELSE ; P is <= 12
IF
ASL #3, D1 ; P = P * 8
ADD #3, D1 ; P = P + 3
BRA ENDIF
ELSE
SUB D2, D1 ; P = P - Q
ENDIF
MOVE #3, D0 ; assign read command
TRAP #15 ;
SIMHALT ; halt simulator
EASy68K supports structured assembly.
OPT SEX
IF.L P <GT> #12 THEN
ELSE
ENDI
Add the option SEX to expand the structured code during assembly if you wish to view the compare and branch instructions used to implement the structured code.

Input argument "b" is undefined

i am new in matlab and search everything. I am writing a the function. i could not able to understand why this error is comning :"Input argument "b" is undefined." . shall i intialise b =0 ? whereas it is the parameter coming from input console. my code:
function f = evenorodd( b )
%UNTITLED2 Summary of this function goes here
%zohaib
% Detailed explanation goes here
%f = b;%2;
f = [0 0];
f = rem(b,2);
if f == 0
disp(b+ 'is even')
else
disp(b+ 'is odd')
end
console:
??? Input argument "b" is undefined.
Error in ==> evenorodd at 6
f = rem(b,2);
From what I see, this is what you are trying to do:
function f = evenorodd( b )
f = rem(b,2);
if f == 0
fprintf('%i is even\n', b)
else
fprintf('%i is odd\n', b)
end
=======================
>> evenorodd(2);
2 is even
No need to initialize f as [0,0].
In MATLAB, you cant concatenate a number and string with + operator. Use fprintf.
The above function evenorodd takes one argument (integer) and returns 0 or 1.