|
|
|
|
|
|
from typing import Callable, List |
|
|
|
|
|
|
|
import numpy as np |
|
|
|
import tensorflow as tf |
|
|
|
import tensorflow.contrib.layers as c_layers |
|
|
|
try: |
|
|
|
import tensorflow.compat.v1 as tf |
|
|
|
except ImportError: |
|
|
|
import tensorflow as tf |
|
|
|
|
|
|
|
if True: # TODO TF2 |
|
|
|
tf_variance_scaling = tf.initializers.variance_scaling |
|
|
|
tf_flatten = tf.layers.flatten |
|
|
|
tf_rnn = tf.nn.rnn_cell |
|
|
|
else: |
|
|
|
import tensorflow.contrib.layers as c_layers |
|
|
|
tf_variance_scaling = c_layers.variance_scaling_initializer |
|
|
|
tf_flatten = c_layers.flatten |
|
|
|
tf_rnn = tf.contrib.rnn |
|
|
|
|
|
|
|
from mlagents.trainers.trainer import UnityTrainerException |
|
|
|
from mlagents.envs.brain import CameraResolution |
|
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
def scaled_init(scale): |
|
|
|
return c_layers.variance_scaling_initializer(scale) |
|
|
|
return tf_variance_scaling(scale) |
|
|
|
|
|
|
|
@staticmethod |
|
|
|
def swish(input_activation: tf.Tensor) -> tf.Tensor: |
|
|
|
|
|
|
activation=activation, |
|
|
|
reuse=reuse, |
|
|
|
name="hidden_{}".format(i), |
|
|
|
kernel_initializer=c_layers.variance_scaling_initializer(1.0), |
|
|
|
kernel_initializer=tf_variance_scaling(1.0), |
|
|
|
) |
|
|
|
return hidden |
|
|
|
|
|
|
|
|
|
|
reuse=reuse, |
|
|
|
name="conv_2", |
|
|
|
) |
|
|
|
hidden = c_layers.flatten(conv2) |
|
|
|
hidden = tf_flatten(conv2) |
|
|
|
|
|
|
|
with tf.variable_scope(scope + "/" + "flat_encoding"): |
|
|
|
hidden_flat = LearningModel.create_vector_observation_encoder( |
|
|
|
|
|
|
reuse=reuse, |
|
|
|
name="conv_3", |
|
|
|
) |
|
|
|
hidden = c_layers.flatten(conv3) |
|
|
|
hidden = tf_flatten(conv3) |
|
|
|
|
|
|
|
with tf.variable_scope(scope + "/" + "flat_encoding"): |
|
|
|
hidden_flat = LearningModel.create_vector_observation_encoder( |
|
|
|
|
|
|
) |
|
|
|
hidden = tf.add(block_input, hidden) |
|
|
|
hidden = tf.nn.relu(hidden) |
|
|
|
hidden = c_layers.flatten(hidden) |
|
|
|
hidden = tf_flatten(hidden) |
|
|
|
|
|
|
|
with tf.variable_scope(scope + "/" + "flat_encoding"): |
|
|
|
hidden_flat = LearningModel.create_vector_observation_encoder( |
|
|
|
|
|
|
memory_in = tf.reshape(memory_in[:, :], [-1, m_size]) |
|
|
|
half_point = int(m_size / 2) |
|
|
|
with tf.variable_scope(name): |
|
|
|
rnn_cell = tf.contrib.rnn.BasicLSTMCell(half_point) |
|
|
|
lstm_vector_in = tf.contrib.rnn.LSTMStateTuple( |
|
|
|
rnn_cell = tf_rnn.BasicLSTMCell(half_point) |
|
|
|
lstm_vector_in = tf_rnn.LSTMStateTuple( |
|
|
|
memory_in[:, :half_point], memory_in[:, half_point:] |
|
|
|
) |
|
|
|
recurrent_output, lstm_state_out = tf.nn.dynamic_rnn( |
|
|
|