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.vhd] - Blame information for rev 10

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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