This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
executable file
·146 lines (138 loc) · 4.59 KB
/
model.py
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
50
51
52
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import tensorflow as tf
import numpy as np
class Model():
def __init__(self, input_shape=[None, 84, 84, 4], num_outputs=4, learning_rate=0.0001):
self._input_shape = input_shape
self._num_outputs = num_outputs
self._learning_rate = learning_rate
self._define_model()
def _define_model(self):
self._input_layer = tf.placeholder(
shape=self._input_shape,
dtype=tf.float32,
name="input"
)
self._scaled_inputs = self._input_layer / 255
self._conv1 = tf.layers.conv2d(
inputs=self._scaled_inputs,
filters=32,
kernel_size=[8, 8],
strides=4,
kernel_initializer=tf.variance_scaling_initializer(scale=2),
padding="valid",
activation=tf.nn.relu,
use_bias=False,
name="conv1"
)
self._conv2 = tf.layers.conv2d(
inputs=self._conv1,
filters=64,
kernel_size=[4, 4],
strides=2,
kernel_initializer=tf.variance_scaling_initializer(scale=2),
padding="valid",
activation=tf.nn.relu,
use_bias=False,
name="conv2"
)
self._conv3 = tf.layers.conv2d(
inputs=self._conv2,
filters=64,
kernel_size=[3, 3],
strides=1,
kernel_initializer=tf.variance_scaling_initializer(scale=2),
padding="valid",
activation=tf.nn.relu,
use_bias=False,
name="conv3"
)
self._conv4 = tf.layers.conv2d(
inputs=self._conv3,
filters=1024,
kernel_size=[7, 7],
strides=1,
kernel_initializer=tf.variance_scaling_initializer(scale=2),
padding="valid",
activation=tf.nn.relu,
use_bias=False,
name="conv4"
)
self._value_stream, self._advantage_stream = tf.split(self._conv4, 2, 3)
self._value_stream = tf.layers.flatten(self._value_stream)
self._advantage_stream = tf.layers.flatten(self._advantage_stream)
self._advantage = tf.layers.dense(
inputs=self._advantage_stream,
units=self._num_outputs,
kernel_initializer=tf.variance_scaling_initializer(scale=2),
name="advantage"
)
self._value = tf.layers.dense(
inputs=self._value_stream,
units=1,
kernel_initializer=tf.variance_scaling_initializer(scale=2),
name="value"
)
self._q_values = self._value + tf.subtract(
self._advantage,
tf.reduce_mean(
self._advantage,
axis=1,
keep_dims=True
)
)
self._best_action = tf.arg_max(self._q_values, 1)
self._target_q = tf.placeholder(
shape=[None],
dtype=tf.float32
)
self._action = tf.placeholder(
shape=[None],
dtype=tf.int32
)
self._q = tf.reduce_sum(
tf.multiply(
self._q_values,
tf.one_hot(
self._action,
self._num_outputs,
dtype=tf.float32
)
),
axis=1
)
self._loss = tf.reduce_mean(
tf.losses.huber_loss(
labels=self._target_q,
predictions=self._q
)
)
self._optimizer = tf.train.AdamOptimizer(learning_rate=self._learning_rate)
self._update = self._optimizer.minimize(self._loss)
def _reshape_states(self, states):
return np.reshape(
states,
(
-1,
self._input_shape[1],
self._input_shape[2],
self._input_shape[3]
)
)
def get_best_action(self, session, state):
state = self._reshape_states(state)
return session.run(self._best_action, feed_dict={self._input_layer: state})
def get_q_values(self, session, states):
states = self._reshape_states(states)
return session.run(self._q_values, feed_dict={self._input_layer: states})
def train(self, session, states, target_q, actions):
states = self._reshape_states(states)
return session.run(
[
self._loss,
self._update
],
feed_dict={
self._input_layer: states,
self._target_q: target_q,
self._action: actions
})