Forum Discussion
LUT help!!!
I have wrote this code for implementing a neural network.What it does is to take some values(inputs) and some weights and computes the SUM(values*weigths).What i want to do is to check if the total sum(values*weights)< threshold and correct the weights in order to sum(values*weights)>=threshold.I think that i have to store the weights on a look up table and change the values in order to sum(values*weights)>=threshold.How i can do this?.My code is :
LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOGIC_ARITH.ALL; USE IEEE.STD_LOGIC_UNSIGNED.ALL; USE IEEE.STD_LOGIC_SIGNED.ALL; ENTITY ANN IS GENERIC ( m : INTEGER := 4; -- Number of inputs or weights b : INTEGER := 8); --Number of bits per input or weight PORT ( x1,x2,x3,x4: IN UNSIGNED(b-1 DOWNTO 0); w : IN UNSIGNED(b-1 DOWNTO 0); clk: IN STD_LOGIC; y : buffer UNSIGNED(2*b-1 DOWNTO 0); id : buffer bit); END ANN; ARCHITECTURE NEURAL OF ANN IS TYPE weights IS ARRAY (1 TO m) OF UNSIGNED(b-1 DOWNTO 0); TYPE inputs IS ARRAY (1 TO m) OF UNSIGNED(b-1 DOWNTO 0); BEGIN PROCESS(clk,w,x1,x2,x3,x4) VARIABLE weight : weights; VARIABLE input : inputs; VARIABLE prod,acc : UNSIGNED(2*b-1 DOWNTO 0); VARIABLE sub : UNSIGNED(2*b-1 DOWNTO 0); BEGIN IF (clk'EVENT AND CLK='1') THEN weight:=w&weight(1 TO m-1); END IF; input(1):=x1; input(2):=x2; input(3):=x3; input(4):=x4; acc:=(OTHERS=>'0'); FOR j IN 1 TO m LOOP prod:=input(j)*weight(j); acc:=acc+prod; END LOOP; y<=acc; END IF; END PROCESS; END NEURAL;70 Replies
- Altera_Forum
Honored Contributor
I wrote this code.What i need to do is to insert 4 values at each clock which i store on a 2-dimensional array and i want to compute the sum of these values.
LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOGIC_ARITH.ALL; ENTITY test IS PORT( x1 : IN INTEGER RANGE 0 TO 255; clk,load : IN STD_LOGIC; output1 : out INTEGER RANGE 0 TO 1020); END test; ARCHITECTURE behavioral OF test IS TYPE windows IS ARRAY(0 TO 1,0 to 1) OF INTEGER RANGE 0 TO 255; BEGIN PROCESS(clk,x1) VARIABLE window : windows; VARIABLE sum : INTEGER RANGE 0 TO 1020; BEGIN IF(clk'EVENT AND clk='1') THEN IF(load='1') THEN FOR i IN 0 TO 1 LOOP FOR j IN 0 TO 1 LOOP window(i,j):=x1; sum:=sum+window(i,j); END LOOP; END LOOP; END IF; END IF; output1<=sum; END PROCESS; END behavioral; The code has no problem while compiling but in the simulation the results are wrong!!!.Please help.I set in the simulation for the x1 to take 2 values per clock but the result shows that something is wrong. - Altera_Forum
Honored Contributor
To be honest the purpose of my project is to write some code that implements the thing i described in earlier posts.So i suppose it's ok.I will post the code that wrote for the weights update method.Thanks again
- Altera_Forum
Honored Contributor
Yes it does behave correctly.
I just dont think it will work on a real FPGA, because it is not pipelined. - Altera_Forum
Honored Contributor
At least this code does what i want to do?Is it correct from the view of behavior?
- Altera_Forum
Honored Contributor
for a start - use signals instead of variables.
- Altera_Forum
Honored Contributor
Thanks for the response.So what do you suggest?
- Altera_Forum
Honored Contributor
I still think you need to read up on logic design. And stop using variables like you are - you're making it do a lot of work in 1 clock cycle!
- Altera_Forum
Honored Contributor
Any help?Thanks
- Altera_Forum
Honored Contributor
Ok i manage to do the first part of the project.What it does is :i insert some values for inputs(that will remain constants through all the process) and then when a clock EVENT i do this computation : input1*weight1+input2*weight2+input3*weight3.This is the multiply-accumulator unit.The second thing that i want to do(for which i need your help) is to compare this result(from the previous step) with a threshold and then fix the weight values in order to :
input1*weight1(new)+input2*weight2(new)+input3*weight3(new)>threshold.Here is my code library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; use ieee.std_logic_arith.all; entity neuron is port ( in1,in2,in3,w1,w2,w3 : in unsigned (7 downto 0); clk : in std_logic; id : out bit; output : out unsigned (15 downto 0)); end neuron; architecture behavioral of neuron is type inputs is array (1 to 3) of unsigned (7 downto 0); type weights is array (1 to 3) of unsigned (7 downto 0); constant threshold : unsigned(15 downto 0):="0000011011100111"; constant sum : unsigned:="1"; begin process(clk,w1,w2,w3) variable weight : weights; variable input : inputs; variable prod,acc : unsigned (15 downto 0); begin if(clk'EVENT AND clk='1') then input(1):=in1; input(2):=in2; input(3):=in3; weight(1):=w1; weight(2):=w2; weight(3):=w3; acc:=(others=>'0'); end if; for j in 1 to 3 loop prod:=input(j)*weight(j); acc:=acc+prod; end loop; if(acc>threshold) then id<='1'; else id<='0'; end if; output<=acc; end process; end behavioral; I attached and a photo with the simulation results - Altera_Forum
Honored Contributor
Ok i will try to implement it and i will write my feedback tommorow.I hope will find you online to help me.Thank you very much