Aufgrund einer Wartung wird GitLab am 26.10. zwischen 8:00 und 9:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 26.10. between 8:00 and 9:00 am.

CNNNet_CriticNetwork.py 5.35 KB
Newer Older
Nicola Gatto's avatar
Nicola Gatto committed
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
import mxnet as mx
import numpy as np
from mxnet import gluon


class ZScoreNormalization(gluon.HybridBlock):
    def __init__(self, data_mean, data_std, **kwargs):
        super(ZScoreNormalization, self).__init__(**kwargs)
        with self.name_scope():
            self.data_mean = self.params.get('data_mean', shape=data_mean.shape,
                init=mx.init.Constant(data_mean.asnumpy().tolist()), differentiable=False)
            self.data_std = self.params.get('data_std', shape=data_mean.shape,
                init=mx.init.Constant(data_std.asnumpy().tolist()), differentiable=False)

    def hybrid_forward(self, F, x, data_mean, data_std):
        x = F.broadcast_sub(x, data_mean)
        x = F.broadcast_div(x, data_std)
        return x


class Padding(gluon.HybridBlock):
    def __init__(self, padding, **kwargs):
        super(Padding, self).__init__(**kwargs)
        with self.name_scope():
            self.pad_width = padding

    def hybrid_forward(self, F, x):
        x = F.pad(data=x,
            mode='constant',
            pad_width=self.pad_width,
            constant_value=0)
        return x


class NoNormalization(gluon.HybridBlock):
    def __init__(self, **kwargs):
        super(NoNormalization, self).__init__(**kwargs)

    def hybrid_forward(self, F, x):
        return x


Sebastian N.'s avatar
Sebastian N. committed
43
44
45
46
47
48
49
50
51
52
class Reshape(gluon.HybridBlock):
    def __init__(self, shape, **kwargs):
        super(Reshape, self).__init__(**kwargs)
        with self.name_scope():
            self.shape = shape

    def hybrid_forward(self, F, x):
        return F.reshape(data=x, shape=self.shape)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
class CustomRNN(gluon.HybridBlock):
    def __init__(self, hidden_size, num_layers, bidirectional, **kwargs):
        super(CustomRNN, self).__init__(**kwargs)
        with self.name_scope():
            self.rnn = gluon.rnn.RNN(hidden_size=hidden_size, num_layers=num_layers,
                                     bidirectional=bidirectional, activation='tanh', layout='NTC')

    def hybrid_forward(self, F, data, state0):
        output, [state0] = self.rnn(data, [F.swapaxes(state0, 0, 1)])
        return output, F.swapaxes(state0, 0, 1)


class CustomLSTM(gluon.HybridBlock):
    def __init__(self, hidden_size, num_layers, bidirectional, **kwargs):
        super(CustomLSTM, self).__init__(**kwargs)
        with self.name_scope():
            self.lstm = gluon.rnn.LSTM(hidden_size=hidden_size, num_layers=num_layers,
                                       bidirectional=bidirectional, layout='NTC')

    def hybrid_forward(self, F, data, state0, state1):
        output, [state0, state1] = self.lstm(data, [F.swapaxes(state0, 0, 1), F.swapaxes(state1, 0, 1)])
        return output, F.swapaxes(state0, 0, 1), F.swapaxes(state1, 0, 1)


class CustomGRU(gluon.HybridBlock):
    def __init__(self, hidden_size, num_layers, bidirectional, **kwargs):
        super(CustomGRU, self).__init__(**kwargs)
        with self.name_scope():
            self.gru = gluon.rnn.GRU(hidden_size=hidden_size, num_layers=num_layers,
                                     bidirectional=bidirectional, layout='NTC')

    def hybrid_forward(self, F, data, state0):
        output, [state0] = self.gru(data, [F.swapaxes(state0, 0, 1)])
        return output, F.swapaxes(state0, 0, 1)


Nicola Gatto's avatar
Nicola Gatto committed
89
90
91
92
93
94
95
class Net_0(gluon.HybridBlock):
    def __init__(self, data_mean=None, data_std=None, **kwargs):
        super(Net_0, self).__init__(**kwargs)
        self.last_layers = {}
        with self.name_scope():
            if data_mean:
                assert(data_std)
Sebastian N.'s avatar
Merge    
Sebastian N. committed
96
97
                self.input_normalization_state_ = ZScoreNormalization(data_mean=data_mean['state_'],
                                                                               data_std=data_std['state_'])
Nicola Gatto's avatar
Nicola Gatto committed
98
            else:
Sebastian N.'s avatar
Merge    
Sebastian N. committed
99
                self.input_normalization_state_ = NoNormalization()
Nicola Gatto's avatar
Nicola Gatto committed
100

101
            self.fc2_1_ = gluon.nn.Dense(units=300, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
102
103
104
            # fc2_1_, output shape: {[300,1,1]}

            self.relu2_1_ = gluon.nn.Activation(activation='relu')
105
            self.fc3_1_ = gluon.nn.Dense(units=600, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
106
107
108
109
            # fc3_1_, output shape: {[600,1,1]}

            if data_mean:
                assert(data_std)
Sebastian N.'s avatar
Merge    
Sebastian N. committed
110
111
                self.input_normalization_action_ = ZScoreNormalization(data_mean=data_mean['action_'],
                                                                               data_std=data_std['action_'])
Nicola Gatto's avatar
Nicola Gatto committed
112
            else:
Sebastian N.'s avatar
Merge    
Sebastian N. committed
113
                self.input_normalization_action_ = NoNormalization()
Nicola Gatto's avatar
Nicola Gatto committed
114

115
            self.fc2_2_ = gluon.nn.Dense(units=600, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
116
117
            # fc2_2_, output shape: {[600,1,1]}

118
            self.fc4_ = gluon.nn.Dense(units=600, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
119
120
121
            # fc4_, output shape: {[600,1,1]}

            self.relu4_ = gluon.nn.Activation(activation='relu')
122
            self.fc5_ = gluon.nn.Dense(units=1, use_bias=True, flatten=True)
Nicola Gatto's avatar
Nicola Gatto committed
123
124
125
            # fc5_, output shape: {[1,1,1]}


Sebastian N.'s avatar
Sebastian N. committed
126
            pass
Nicola Gatto's avatar
Nicola Gatto committed
127

Sebastian N.'s avatar
Merge    
Sebastian N. committed
128
129
130
    def hybrid_forward(self, F, state_, action_):
        state_ = self.input_normalization_state_(state_)
        fc2_1_ = self.fc2_1_(state_)
Nicola Gatto's avatar
Nicola Gatto committed
131
132
        relu2_1_ = self.relu2_1_(fc2_1_)
        fc3_1_ = self.fc3_1_(relu2_1_)
Sebastian N.'s avatar
Merge    
Sebastian N. committed
133
134
        action_ = self.input_normalization_action_(action_)
        fc2_2_ = self.fc2_2_(action_)
Nicola Gatto's avatar
Nicola Gatto committed
135
136
137
138
        add4_ = fc3_1_ + fc2_2_
        fc4_ = self.fc4_(add4_)
        relu4_ = self.relu4_(fc4_)
        fc5_ = self.fc5_(relu4_)
Sebastian N.'s avatar
Merge    
Sebastian N. committed
139
140
141
        qvalues_ = fc5_

        return qvalues_
Nicola Gatto's avatar
Nicola Gatto committed
142