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

Go to most recent revision | 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
-- Module Name: af_sigmoid2 - Behavioral
8
-- 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
entity af_sigmoid2 is
29
   generic
30
   (
31
      Nbit : natural := 8
32
   );
33
   port
34
   (
35
      reset   : in  std_logic;
36
      clk     : in  std_logic;
37
      run_in  : in  std_logic; -- Start and input data validation
38
      inputs  : in  std_logic_vector(Nbit-1 downto 0); -- Input data
39
      run_out : out std_logic; -- Output data validation, run_in for the next layer
40
      outputs : out std_logic_vector(Nbit-1 downto 0) -- Output data
41
   );
42
end af_sigmoid2;
43
 
44
 
45
architecture Behavioral of af_sigmoid2 is
46
 
47
   -- Definition of internal modules, constants, signals, etc...
48
 
49
   -- Sigmoid parameters:
50
   constant f0 : real := 0.5; -- Slope at the origin
51
   constant fr : real := 2.0; -- fr = fmax - fmin
52
 
53
   signal dataIn: integer range (2**Nbit-1) downto 0; -- To convert std_logic_vector input to integer index for the LUT
54
   type table_t is array(0 to (2**Nbit)-1) of std_logic_vector(Nbit-1 downto 0); -- LUT type
55
 
56
-- Function Sigmoidal: generates the Look-Up-Table for the sigmoid activation function:
57
-- margin: maximun value of x.
58
   function Sigmoidal(margin:real;Nbit:natural) return table_t is
59
         variable scale,x,y,w,t: real;
60
         variable u: integer;
61
         variable fbits: std_logic_vector(Nbit-1 downto 0);
62
         variable table: table_t;
63
      begin
64
         scale := (2.0*margin)/(2.0**Nbit);   -- Calculates gap between to points
65
         x := -margin;
66
         for idx in -(2**(Nbit-1)) to (2**(Nbit-1))-1 loop
67
            y := ( fr / (1.0+exp(((-4.0*f0)/fr)*x)) ) - (fr/2.0);
68
            w := y*(2.0**(Nbit-1));           -- Shifts bits to the left
69
            t := round(w);
70
            u := integer(t);
71
            fbits := std_logic_vector(to_signed(u,Nbit));
72
            table(to_integer(to_unsigned(idx+(2**Nbit),Nbit))):= fbits;
73
            x := x+scale;
74
         end loop;
75
         return table;
76
   end Sigmoidal;
77
   signal Table: table_t := Sigmoidal(1.0,Nbit); -- Generation of the LUT (at synthesis time)
78
 
79
begin
80
 
81
   -- Description of the activation function
82
   dataIn <= to_integer(signed(inputs));
83
 
84
   Activacion: process(clk,reset)
85
      begin
86
         if clk'event and clk = '1' then
87
            if reset = '1' then
88
               run_out <= '0';
89
               outputs <= (others => '0');
90
            else
91
               if run_in = '1' then
92
                  run_out<='1';
93
                  outputs<=Table(dataIn); -- Assigns output value from the LUT
94
               else
95
                  run_out<='0';
96
               end if;
97
            end if;
98
         end if;
99
    end process;
100
end Behavioral;

powered by: WebSVN 2.1.0

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