1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
"""
# -*- coding: utf-8 -*-
#
# Copyright 2021 Michael Büsch <m@bues.ch>
#
# Licensed under the Apache License version 2.0
# or the MIT license, at your option.
# SPDX-License-Identifier: Apache-2.0 OR MIT
#
"""
__all__ = [
"init_biases",
"init_layers_biases",
"init_weights",
"init_layers_weights",
"seed",
"random",
"standard_normal",
]
import numpy as np
def init_biases(nr_neurons, initial=0.0):
return np.full((1, nr_neurons), initial)
def init_layers_biases(layout, initial=0.0):
return [ init_biases(nr_neurons, initial)
for nr_neurons in layout ]
def init_weights(nr_inputs, nr_neurons):
return (standard_normal((nr_inputs, nr_neurons)) *
np.sqrt(2.0 / nr_inputs))
def init_layers_weights(nr_inputs, layout):
layer_inputs = [nr_inputs] + list(layout[:-1])
return [ init_weights(nr_inputs=nr_inputs, nr_neurons=nr_neurons)
for nr_inputs, nr_neurons in zip(layer_inputs, layout) ]
def seed(s):
np.random.seed(s)
def random(shape):
return np.random.random_sample(shape)
def standard_normal(shape):
return np.random.standard_normal(shape)
# vim: ts=4 sw=4 expandtab
|