OpenCores
URL https://opencores.org/ocsvn/artificial_neural_network/artificial_neural_network/trunk

Subversion Repositories artificial_neural_network

[/] [artificial_neural_network/] [trunk/] [ANN_kernel/] [RTL_VHDL_files/] [af_sigmoid_mat.vhd] - Blame information for rev 10

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 ojosynariz
----------------------------------------------------------------------------------
2
-- Company: CEI
3
-- Engineer: Enrique Herrero
4
--
5
-- Create Date:
6
-- Design Name: Configurable ANN
7 10 jstefanowi
-- Module Name: af_sigmoid_mat - Behavioral
8 3 ojosynariz
-- Project Name:
9
-- Target Devices:
10
-- Tool versions:
11
-- Description: Sigmoid activation function implemented as a Look-Up-Table (LUT).
12
--             Alternative set of parameters.
13
--
14
-- Dependencies:
15
--
16
-- Revision:
17
-- Revision 0.01 - File Created
18
-- Revision 1 - David Aledo
19
-- Additional Comments:
20
--
21
----------------------------------------------------------------------------------
22
library IEEE;
23
use IEEE.STD_LOGIC_1164.ALL;
24
use ieee.numeric_std.ALL;
25
use ieee.math_real.all;
26
 
27
 
28 10 jstefanowi
entity af_sigmoid_mat is
29 3 ojosynariz
   generic
30
   (
31 8 jstefanowi
      Nbit : natural := 8;
32
      lsbit : natural := 10
33 3 ojosynariz
   );
34
   port
35
   (
36
      reset   : in  std_logic;
37
      clk     : in  std_logic;
38
      run_in  : in  std_logic; -- Start and input data validation
39
      inputs  : in  std_logic_vector(Nbit-1 downto 0); -- Input data
40
      run_out : out std_logic; -- Output data validation, run_in for the next layer
41
      outputs : out std_logic_vector(Nbit-1 downto 0) -- Output data
42
   );
43 10 jstefanowi
end af_sigmoid_mat;
44 3 ojosynariz
 
45
 
46 10 jstefanowi
architecture Behavioral of af_sigmoid_mat is
47 3 ojosynariz
 
48
   -- Definition of internal modules, constants, signals, etc...
49
 
50
   -- Sigmoid parameters:
51 8 jstefanowi
   constant f0 : real := 1.0; -- Slope at the origin
52 3 ojosynariz
   constant fr : real := 2.0; -- fr = fmax - fmin
53
 
54
   signal dataIn: integer range (2**Nbit-1) downto 0; -- To convert std_logic_vector input to integer index for the LUT
55
   type table_t is array(0 to (2**Nbit)-1) of std_logic_vector(Nbit-1 downto 0); -- LUT type
56
 
57
-- Function Sigmoidal: generates the Look-Up-Table for the sigmoid activation function:
58 8 jstefanowi
-- margin: maximum value of input
59
   function Sigmoidal(margin:real;Nbit:natural;lsbit:natural) return table_t is
60 3 ojosynariz
         variable scale,x,y,w,t: real;
61
         variable u: integer;
62
         variable fbits: std_logic_vector(Nbit-1 downto 0);
63
         variable table: table_t;
64
      begin
65
         scale := (2.0*margin)/(2.0**Nbit);   -- Calculates gap between to points
66 8 jstefanowi
         x := -margin;
67 3 ojosynariz
         for idx in -(2**(Nbit-1)) to (2**(Nbit-1))-1 loop
68
            y := ( fr / (1.0+exp(((-4.0*f0)/fr)*x)) ) - (fr/2.0);
69 8 jstefanowi
            w := y*(2.0**(lsbit));           -- Shifts bits to the left
70 3 ojosynariz
            t := round(w);
71
            u := integer(t);
72
            fbits := std_logic_vector(to_signed(u,Nbit));
73
            table(to_integer(to_unsigned(idx+(2**Nbit),Nbit))):= fbits;
74
            x := x+scale;
75
         end loop;
76
         return table;
77
   end Sigmoidal;
78 8 jstefanowi
   signal Table: table_t := Sigmoidal(2.0**(Nbit-lsbit-1),Nbit,lsbit); -- Generation of the LUT (at synthesis time)
79 3 ojosynariz
 
80
begin
81
 
82
   -- Description of the activation function
83 8 jstefanowi
   dataIn <= to_integer(unsigned(inputs));
84 3 ojosynariz
 
85
   Activacion: process(clk,reset)
86
      begin
87
         if clk'event and clk = '1' then
88
            if reset = '1' then
89
               run_out <= '0';
90
               outputs <= (others => '0');
91
            else
92
               if run_in = '1' then
93
                  run_out<='1';
94
                  outputs<=Table(dataIn); -- Assigns output value from the LUT
95
               else
96
                  run_out<='0';
97
               end if;
98
            end if;
99
         end if;
100
    end process;
101
end Behavioral;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.