CNNNet_torcs_agent_torcsAgent_dqn.py 4.63 KB
Newer Older
1
2
import mxnet as mx
import numpy as np
Sebastian Nickels's avatar
Sebastian Nickels committed
3
import math
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
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


Christian Fuß's avatar
Christian Fuß committed
44
45
46
47
48
49
50
51
52
53
54
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)


class CustomRNN(gluon.HybridBlock):
Sebastian Nickels's avatar
Sebastian Nickels committed
55
    def __init__(self, hidden_size, num_layers, dropout, bidirectional, **kwargs):
Christian Fuß's avatar
Christian Fuß committed
56
57
        super(CustomRNN, self).__init__(**kwargs)
        with self.name_scope():
Sebastian Nickels's avatar
Sebastian Nickels committed
58
            self.rnn = gluon.rnn.RNN(hidden_size=hidden_size, num_layers=num_layers, dropout=dropout,
Christian Fuß's avatar
Christian Fuß committed
59
60
61
62
63
64
65
66
                                     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):
Sebastian Nickels's avatar
Sebastian Nickels committed
67
    def __init__(self, hidden_size, num_layers, dropout, bidirectional, **kwargs):
Christian Fuß's avatar
Christian Fuß committed
68
69
        super(CustomLSTM, self).__init__(**kwargs)
        with self.name_scope():
Sebastian Nickels's avatar
Sebastian Nickels committed
70
            self.lstm = gluon.rnn.LSTM(hidden_size=hidden_size, num_layers=num_layers, dropout=dropout,
Christian Fuß's avatar
Christian Fuß committed
71
72
73
74
75
76
77
78
                                       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):
Sebastian Nickels's avatar
Sebastian Nickels committed
79
    def __init__(self, hidden_size, num_layers, dropout, bidirectional, **kwargs):
Christian Fuß's avatar
Christian Fuß committed
80
81
        super(CustomGRU, self).__init__(**kwargs)
        with self.name_scope():
Sebastian Nickels's avatar
Sebastian Nickels committed
82
            self.gru = gluon.rnn.GRU(hidden_size=hidden_size, num_layers=num_layers, dropout=dropout,
Christian Fuß's avatar
Christian Fuß committed
83
84
85
86
87
88
89
                                     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
90
class Net_0(gluon.HybridBlock):
91
    def __init__(self, data_mean=None, data_std=None, **kwargs):
Nicola Gatto's avatar
Nicola Gatto committed
92
        super(Net_0, self).__init__(**kwargs)
93
        with self.name_scope():
Nicola Gatto's avatar
Nicola Gatto committed
94
95
            if data_mean:
                assert(data_std)
Sebastian Nickels's avatar
Sebastian Nickels committed
96
97
                self.input_normalization_state_ = ZScoreNormalization(data_mean=data_mean['state_'],
                                                                               data_std=data_std['state_'])
98
            else:
Sebastian Nickels's avatar
Sebastian Nickels committed
99
                self.input_normalization_state_ = NoNormalization()
100

101
            self.fc1_ = gluon.nn.Dense(units=512, use_bias=True, flatten=True)
102
103
104
            # fc1_, output shape: {[512,1,1]}

            self.tanh1_ = gluon.nn.Activation(activation='tanh')
105
            self.fc2_ = gluon.nn.Dense(units=256, use_bias=True, flatten=True)
106
107
108
            # fc2_, output shape: {[256,1,1]}

            self.tanh2_ = gluon.nn.Activation(activation='tanh')
109
            self.fc3_ = gluon.nn.Dense(units=30, use_bias=True, flatten=True)
110
111
112
            # fc3_, output shape: {[30,1,1]}


Christian Fuß's avatar
Christian Fuß committed
113
            pass
114

Sebastian Nickels's avatar
Sebastian Nickels committed
115
116
117
    def hybrid_forward(self, F, state_):
        state_ = self.input_normalization_state_(state_)
        fc1_ = self.fc1_(state_)
118
119
120
121
        tanh1_ = self.tanh1_(fc1_)
        fc2_ = self.fc2_(tanh1_)
        tanh2_ = self.tanh2_(fc2_)
        fc3_ = self.fc3_(tanh2_)
Sebastian Nickels's avatar
Sebastian Nickels committed
122
        qvalues_ = F.identity(fc3_)
Sebastian Nickels's avatar
Sebastian Nickels committed
123
124

        return qvalues_
Nicola Gatto's avatar
Nicola Gatto committed
125