passed reference to network, added assignment operator function to Sequential
This commit is contained in:
@@ -90,6 +90,9 @@ class Sequential
|
||||
//! Copy constructor.
|
||||
Sequential(const Sequential&);
|
||||
|
||||
//! Copy assignment operator.
|
||||
void operator = (const Sequential& layer);
|
||||
|
||||
//! Destroy the Sequential object.
|
||||
~Sequential();
|
||||
|
||||
@@ -229,6 +232,9 @@ class Sequential
|
||||
//! Locally-stored output height visitor.
|
||||
OutputHeightVisitor outputHeightVisitor;
|
||||
|
||||
//! Locally-stored copy visitor
|
||||
CopyVisitor<CustomLayers...> copyVisitor;
|
||||
|
||||
//! The input width.
|
||||
size_t width;
|
||||
|
||||
|
||||
@@ -56,6 +56,27 @@ Sequential(const bool model, const bool ownsLayers) :
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
template <typename InputDataType, typename OutputDataType, bool Residual,
|
||||
typename... CustomLayers>
|
||||
void Sequential<InputDataType, OutputDataType, Residual, CustomLayers...>::
|
||||
operator = (const Sequential& layer)
|
||||
{
|
||||
model = layer.model;
|
||||
reset = layer.reset;
|
||||
width = layer.width;
|
||||
height = layer.height;
|
||||
ownsLayers = layer.ownsLayers;
|
||||
parameters = layer.parameters;
|
||||
network.clear();
|
||||
// Build new layers according to source network.
|
||||
for (size_t i = 0; i < layer.network.size(); ++i)
|
||||
{
|
||||
this->network.push_back(boost::apply_visitor(copyVisitor,
|
||||
layer.network[i]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename InputDataType, typename OutputDataType, bool Residual,
|
||||
typename... CustomLayers>
|
||||
Sequential<
|
||||
|
||||
@@ -78,7 +78,7 @@ class QLearning
|
||||
* @param environment Reinforcement learning task.
|
||||
*/
|
||||
QLearning(TrainingConfig config,
|
||||
NetworkType network,
|
||||
NetworkType& network,
|
||||
PolicyType policy,
|
||||
ReplayType replayMethod,
|
||||
UpdaterType updater = UpdaterType(),
|
||||
@@ -141,7 +141,7 @@ class QLearning
|
||||
TrainingConfig config;
|
||||
|
||||
//! Locally-stored learning network.
|
||||
NetworkType learningNetwork;
|
||||
NetworkType& learningNetwork;
|
||||
|
||||
//! Locally-stored target network.
|
||||
NetworkType targetNetwork;
|
||||
|
||||
@@ -31,13 +31,13 @@ QLearning<
|
||||
PolicyType,
|
||||
ReplayType
|
||||
>::QLearning(TrainingConfig config,
|
||||
NetworkType network,
|
||||
NetworkType& network,
|
||||
PolicyType policy,
|
||||
ReplayType replayMethod,
|
||||
UpdaterType updater,
|
||||
EnvironmentType environment):
|
||||
config(std::move(config)),
|
||||
learningNetwork(std::move(network)),
|
||||
learningNetwork(network),
|
||||
updater(std::move(updater)),
|
||||
#if ENS_VERSION_MAJOR >= 2
|
||||
updatePolicy(NULL),
|
||||
|
||||
@@ -39,59 +39,21 @@ template <
|
||||
class DuelingDQN
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
//! Default constructor.
|
||||
DuelingDQN()
|
||||
{ /* Nothing to do here. */ }
|
||||
|
||||
//! Copy constructor.
|
||||
DuelingDQN(const DuelingDQN& model):
|
||||
featureNetwork(model.featureNetwork),
|
||||
advantageNetwork(model.advantageNetwork),
|
||||
valueNetwork(model.valueNetwork),
|
||||
actionValues(model.actionValues)
|
||||
{
|
||||
std::cout << "copyConstructor" << '\n';
|
||||
|
||||
featureNetwork = new Sequential<>();
|
||||
valueNetwork = new Sequential<>();
|
||||
advantageNetwork = new Sequential<>();
|
||||
concat = new Concat<>(true);
|
||||
concat.Add(&valueNetwork);
|
||||
concat.Add(&advantageNetwork);
|
||||
|
||||
concat->Add(valueNetwork);
|
||||
concat->Add(advantageNetwork);
|
||||
completeNetwork.Add(new IdentityLayer<>());
|
||||
completeNetwork.Add(&featureNetwork);
|
||||
completeNetwork.Add(&concat);
|
||||
completeNetwork.Parameters() = model.completeNetwork.Parameters();
|
||||
};
|
||||
|
||||
//! Move constructor.
|
||||
DuelingDQN(DuelingDQN&& model):
|
||||
featureNetwork(std::move(model.featureNetwork)),
|
||||
advantageNetwork(std::move(model.advantageNetwork)),
|
||||
valueNetwork(std::move(model.valueNetwork)),
|
||||
actionValues(std::move(model.actionValues))
|
||||
{
|
||||
std::cout << "move Constructor" << '\n';
|
||||
|
||||
concat = new Concat<>(true);
|
||||
concat.Add(&valueNetwork);
|
||||
concat.Add(&advantageNetwork);
|
||||
|
||||
completeNetwork.Add(new IdentityLayer<>());
|
||||
completeNetwork.Add(&featureNetwork);
|
||||
completeNetwork.Add(&concat);
|
||||
completeNetwork.Parameters() = model.completeNetwork.Parameters();
|
||||
completeNetwork.Add(featureNetwork);
|
||||
completeNetwork.Add(concat);
|
||||
}
|
||||
|
||||
DuelingDQN& operator = (DuelingDQN model)
|
||||
{
|
||||
std::swap(this->completeNetwork, model.completeNetwork);
|
||||
std::swap(featureNetwork, model.featureNetwork);
|
||||
std::swap(valueNetwork, model.valueNetwork);
|
||||
std::swap(advantageNetwork, model.advantageNetwork);
|
||||
return *this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Construct an instance of DuelingDQN class.
|
||||
*
|
||||
@@ -107,26 +69,26 @@ class DuelingDQN
|
||||
completeNetwork(EmptyLoss<>(), GaussianInitialization(0, 0.001))
|
||||
{
|
||||
featureNetwork = new Sequential<>();
|
||||
featureNetwork.Add(new Linear<>(inputDim, h1));
|
||||
featureNetwork.Add(new ReLULayer<>());
|
||||
featureNetwork->Add(new Linear<>(inputDim, h1));
|
||||
featureNetwork->Add(new ReLULayer<>());
|
||||
|
||||
valueNetwork = new Sequential<>();
|
||||
valueNetwork.Add(new Linear<>(h1, h2));
|
||||
valueNetwork.Add(new ReLULayer<>());
|
||||
valueNetwork.Add(new Linear<>(h2, 1));
|
||||
valueNetwork->Add(new Linear<>(h1, h2));
|
||||
valueNetwork->Add(new ReLULayer<>());
|
||||
valueNetwork->Add(new Linear<>(h2, 1));
|
||||
|
||||
advantageNetwork = new Sequential<>();
|
||||
advantageNetwork.Add(new Linear<>(h1, h2));
|
||||
advantageNetwork.Add(new ReLULayer<>());
|
||||
advantageNetwork.Add(new Linear<>(h2, outputDim));
|
||||
advantageNetwork->Add(new Linear<>(h1, h2));
|
||||
advantageNetwork->Add(new ReLULayer<>());
|
||||
advantageNetwork->Add(new Linear<>(h2, outputDim));
|
||||
|
||||
concat = new Concat<>(true);
|
||||
concat.Add(&valueNetwork);
|
||||
concat.Add(&advantageNetwork);
|
||||
concat->Add(valueNetwork);
|
||||
concat->Add(advantageNetwork);
|
||||
|
||||
completeNetwork.Add(new IdentityLayer<>());
|
||||
completeNetwork.Add(&featureNetwork);
|
||||
completeNetwork.Add(&concat);
|
||||
completeNetwork.Add(featureNetwork);
|
||||
completeNetwork.Add(concat);
|
||||
this->ResetParameters();
|
||||
}
|
||||
|
||||
@@ -139,14 +101,22 @@ class DuelingDQN
|
||||
valueNetwork(std::move(valueNetwork))
|
||||
{
|
||||
concat = new Concat<>(true);
|
||||
concat.Add(&valueNetwork);
|
||||
concat.Add(&advantageNetwork);
|
||||
concat->Add(valueNetwork);
|
||||
concat->Add(advantageNetwork);
|
||||
completeNetwork.Add(new IdentityLayer<>());
|
||||
completeNetwork.Add(&featureNetwork);
|
||||
completeNetwork.Add(&concat);
|
||||
completeNetwork.Add(featureNetwork);
|
||||
completeNetwork.Add(concat);
|
||||
this->ResetParameters();
|
||||
}
|
||||
|
||||
//! Copy assignment operator.
|
||||
void operator = (const DuelingDQN& model)
|
||||
{
|
||||
*valueNetwork = *model.valueNetwork;
|
||||
*advantageNetwork = *model.advantageNetwork;
|
||||
*featureNetwork = *model.featureNetwork;
|
||||
}
|
||||
|
||||
/**
|
||||
* Predict the responses to a given set of predictors. The responses will
|
||||
* reflect the output of the given output layer as returned by the
|
||||
@@ -222,16 +192,16 @@ class DuelingDQN
|
||||
CompleteNetworkType completeNetwork;
|
||||
|
||||
//! Locally-stored concat network.
|
||||
Concat<> concat;
|
||||
Concat<>* concat;
|
||||
|
||||
//! Locally-stored feature network.
|
||||
FeatureNetworkType featureNetwork;
|
||||
FeatureNetworkType* featureNetwork;
|
||||
|
||||
//! Locally-stored advantage network.
|
||||
AdvantageNetworkType advantageNetwork;
|
||||
AdvantageNetworkType* advantageNetwork;
|
||||
|
||||
//! Locally-stored value network.
|
||||
ValueNetworkType valueNetwork;
|
||||
ValueNetworkType* valueNetwork;
|
||||
|
||||
//! Locally-stored actionValues of the network.
|
||||
arma::mat actionValues;
|
||||
|
||||
Reference in New Issue
Block a user