From 64ef87879f7e793ddc07ece12527b867d197c5e7 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Mon, 17 Jan 2022 16:38:02 +0000 Subject: [PATCH] - added git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@822 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- source/RnnComponentLayer.cpp | 342 +++++++++++++++++++++++++++++++++++ source/RnnComponentLayer.hpp | 107 +++++++++++ 2 files changed, 449 insertions(+) create mode 100644 source/RnnComponentLayer.cpp create mode 100644 source/RnnComponentLayer.hpp diff --git a/source/RnnComponentLayer.cpp b/source/RnnComponentLayer.cpp new file mode 100644 index 0000000..80653c4 --- /dev/null +++ b/source/RnnComponentLayer.cpp @@ -0,0 +1,342 @@ +/* + ============================================================================== + + This is an automatically generated GUI class created by the Introjucer! + + Be careful when adding custom code to these files, as only the code within + the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded + and re-saved. + + Created with Introjucer version: 3.1.0 + + ------------------------------------------------------------------------------ + + The Introjucer is part of the JUCE library - "Jules' Utility Class Extensions" + Copyright 2004-13 by Raw Material Software Ltd. + + ============================================================================== +*/ + + +#include "RnnComponentLayer.hpp" + + + +//============================================================================== +RnnComponentLayer::RnnComponentLayer (AStack &stack, const std::string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, size_t numContext) + : Layer(name, id, numVisibleX, numVisibleY, numHidden, numContext) + , m_stack(dynamic_cast(stack)) + , m_currWeightIndexToDraw(0) + , DrawVisibleTrain(nullptr) + , DrawVisibleReconst(nullptr) + , DrawWeights(nullptr) + , DrawHidden(nullptr) + , DrawContextReconst(nullptr) + , m_sizeX(430) + , m_sizeY(190) +{ + addAndMakeVisible (DrawVisibleTrain = new DrawComponent (numVisibleX, numVisibleY)); + DrawVisibleTrain->setListener(this); + + addAndMakeVisible (DrawHidden = new DrawComponent (numHidden, 1)); + DrawHidden->setListener(this); + + addAndMakeVisible (DrawContextTrain = new DrawComponent (numContext, 1)); + DrawContextTrain->setListener(this); + + addAndMakeVisible (DrawContextReconst = new DrawComponent (numContext, 1)); + DrawContextReconst->setListener(this); + + addAndMakeVisible (DrawVisibleReconst = new DrawComponent (numVisibleX, numVisibleY)); + addAndMakeVisible (DrawWeights = new DrawComponent (numVisibleX, numVisibleY, 0.5, 0.5)); + + addAndMakeVisible (m_toggleEnable = new ToggleButton ("Enable layer")); + m_toggleEnable->setTooltip (TRANS("Enable layer for up pass")); + m_toggleEnable->setButtonText (TRANS("Enable")); + m_toggleEnable->addListener (this); + m_toggleEnable->setToggleState(true, NotificationType::dontSendNotification); + + addAndMakeVisible (m_buttonCopyH2C = new TextButton ("Copy hidden state to context")); + m_buttonCopyH2C->setTooltip (TRANS("Copy hidden state to context input")); + m_buttonCopyH2C->setButtonText (TRANS("H2C")); + m_buttonCopyH2C->addListener (this); + + redrawWeights(); + + setSize (m_sizeX, m_sizeY); + resized(); + setBounds (16, (m_sizeY + 10)*id+16, m_sizeX, m_sizeY); +} + +RnnComponentLayer::~RnnComponentLayer() +{ + DrawVisibleTrain = nullptr; + DrawVisibleReconst = nullptr; + DrawWeights = nullptr; + DrawHidden = nullptr; + DrawContextTrain = nullptr; + DrawContextReconst = nullptr; + m_buttonCopyH2C = nullptr; +} + +//============================================================================== +void RnnComponentLayer::paint (Graphics& g) +{ + g.fillAll (Colours::white); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Sigma"), + 32, 306, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Lambda"), + 120, 306, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Num. epochs"), + 32, 354, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Alpha"), + 120, 354, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Sparsity"), + 208, 306, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Sigma decay"), + 296, 306, 96, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Weight decay"), + 296, 354, 96, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Momentum"), + 208, 354, 80, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Sparsity Alpha"), + 408, 306, 96, 14, + Justification::centred, true); + + g.setColour (Colours::black); + g.setFont (Font (15.00f, Font::plain)); + g.drawText (TRANS("Weight init"), + 408, 354, 96, 14, + Justification::centred, true); + +} + +void RnnComponentLayer::resized() +{ + DrawVisibleTrain->setBoundsRelative(0, 0, 100./m_sizeX, 100./m_sizeY); + DrawVisibleReconst->setBoundsRelative(110./m_sizeX, 0, 100./m_sizeX, 100./m_sizeY); + DrawWeights->setBoundsRelative (220./m_sizeX, 0, 100./m_sizeX, 100./m_sizeY); + DrawContextTrain->setBoundsRelative (0, 110./m_sizeY, 430./m_sizeX, 20./m_sizeY); + DrawContextReconst->setBoundsRelative (0, 140./m_sizeY, 430./m_sizeX, 20./m_sizeY); + DrawHidden->setBoundsRelative (0, 170./m_sizeY, 430./m_sizeX, 20./m_sizeY); + m_toggleEnable->setBoundsRelative (330./m_sizeX, 0, 80./m_sizeX, 24./m_sizeY); + m_buttonCopyH2C->setBoundsRelative (330./m_sizeX, 30./m_sizeY, 80./m_sizeX, 24./m_sizeY); +} + +void RnnComponentLayer::mouseMove (const MouseEvent& e) +{ + //[UserCode_mouseMove] -- Add your code here... + //[/UserCode_mouseMove] +} + +void RnnComponentLayer::mouseEnter (const MouseEvent& e) +{ + //[UserCode_mouseEnter] -- Add your code here... + //[/UserCode_mouseEnter] +} + +void RnnComponentLayer::mouseExit (const MouseEvent& e) +{ + //[UserCode_mouseExit] -- Add your code here... + //[/UserCode_mouseExit] +} + +void RnnComponentLayer::mouseDown (const MouseEvent& e) +{ + //[UserCode_mouseDown] -- Add your code here... + //[/UserCode_mouseDown] +} + +void RnnComponentLayer::mouseDrag (const MouseEvent& e) +{ + //[UserCode_mouseDrag] -- Add your code here... +// e.eventComponent->setCentrePosition(e.getPosition().x, e.getPosition().y); + std::cout << "Mouse = " << e.x << "," << e.y << std::endl; + //[/UserCode_mouseDrag] +} + +void RnnComponentLayer::mouseUp (const MouseEvent& e) +{ + //[UserCode_mouseUp] -- Add your code here... + //[/UserCode_mouseUp] +} + +void RnnComponentLayer::mouseDoubleClick (const MouseEvent& e) +{ + //[UserCode_mouseDoubleClick] -- Add your code here... + //[/UserCode_mouseDoubleClick] +} + +void RnnComponentLayer::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel) +{ + //[UserCode_mouseWheelMove] -- Add your code here... + //[/UserCode_mouseWheelMove] +} + +void RnnComponentLayer::onDraw(DrawComponent &obj) +{ + if (&obj == DrawHidden) + { + m_stack.downPass(id(), obj.getData()); + } + if (&obj == DrawVisibleTrain) + { + m_stack.upDownPass(id(), getTraining()); + } + if (&obj == DrawContextTrain) + { + m_stack.upDownPass(id(), getTraining()); + } +} + +void RnnComponentLayer::buttonClicked(Button* buttonThatWasClicked) +{ + if (buttonThatWasClicked == m_toggleEnable) + { + bool state = buttonThatWasClicked->getToggleState(); + setEnable(state); + + if (state) + { + if (prev) + { + RnnComponentLayer *pComp = static_cast (prev); + pComp->m_toggleEnable->setToggleState(state, NotificationType::sendNotification); + } + } + else + { + if (next) + { + RnnComponentLayer *pComp = static_cast (next); + pComp->m_toggleEnable->setToggleState(state, NotificationType::sendNotification); + } + } + redrawReconstruction(); + } + else if (buttonThatWasClicked == m_buttonCopyH2C) + { + DrawContextTrain->getData() = DrawHidden->getData(); + m_stack.upDownPass(id(), getTraining()); + } +} + +void RnnComponentLayer::redrawReconstruction() +{ + RnnComponentLayer *pComp = static_cast (root()); + m_stack.upDownPass(pComp->id(), pComp->getTraining()); +} + +arma::mat RnnComponentLayer::getTraining() const +{ + return arma::join_rows(DrawVisibleTrain->getData(), DrawContextTrain->getData()); +} + +arma::mat RnnComponentLayer::getReconst() const +{ + return arma::join_rows(DrawVisibleReconst->getData(), DrawContextReconst->getData()); +} + +void RnnComponentLayer::trainRedraw(const arma::mat& vc) +{ + DrawVisibleTrain->getData() = vc_to_v(vc); + DrawContextTrain->getData() = vc_to_c(vc); + DrawVisibleTrain->DrawData(); + DrawContextTrain->DrawData(); +} + +void RnnComponentLayer::reconstRedraw(const arma::mat& vc) +{ + DrawVisibleReconst->getData() = vc_to_v(vc); + DrawContextReconst->getData() = vc_to_c(vc); + DrawVisibleReconst->DrawData(); + DrawContextReconst->DrawData(); +} + +void RnnComponentLayer::onUpPass(const arma::mat& v) +{ + DrawVisibleTrain->getData() = vc_to_v(v); + DrawVisibleTrain->DrawData(); + DrawContextTrain->getData() = vc_to_c(v); + DrawContextTrain->DrawData(); + trainRedraw(v); +} + +void RnnComponentLayer::onDownPass(const arma::mat& h) +{ + DrawHidden->getData() = h; + DrawHidden->DrawData(); + + arma::mat r = prob(h_to_v(h)); + reconstRedraw(r); +} + +arma::mat RnnComponentLayer::getConvolutedWeight(arma::mat const &w) +{ +// if (next) +// { +// arma::mat wc = w * next->w().t(); // * 1.0/sqrt((double)w.cols()); +// return static_cast(next)->getConvolutedWeight(wc); +// } +// else + { + return w; + } + +} +void RnnComponentLayer::redrawWeights() +{ + DrawWeights->getData() = getConvolutedWeight(whv().col(m_currWeightIndexToDraw)); + DrawWeights->DrawData(); +} + +void RnnComponentLayer::redrawWeights(size_t index) +{ + m_currWeightIndexToDraw = index; + redrawWeights(); +} + +void RnnComponentLayer::setTrainingData(arma::mat const& batch) +{ + RnnComponentLayer *pComp = static_cast (root()); + m_stack.upDownPass(id(), batch); +} +//[/MiscUserCode] + + +//============================================================================== diff --git a/source/RnnComponentLayer.hpp b/source/RnnComponentLayer.hpp new file mode 100644 index 0000000..4d5657c --- /dev/null +++ b/source/RnnComponentLayer.hpp @@ -0,0 +1,107 @@ +/* + ============================================================================== + + This is an automatically generated GUI class created by the Introjucer! + + Be careful when adding custom code to these files, as only the code within + the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded + and re-saved. + + Created with Introjucer version: 3.1.0 + + ------------------------------------------------------------------------------ + + The Introjucer is part of the JUCE library - "Jules' Utility Class Extensions" + Copyright 2004-13 by Raw Material Software Ltd. + + ============================================================================== +*/ + +#ifndef __RNN_COMPONENT_LAYER__ +#define __RNN_COMPONENT_LAYER__ + +//[Headers] -- You can add your own extra header files here -- +#include "JuceHeader.h" +#include "DrawComponent.hpp" +#include "Layer.hpp" +#include "DeepStack.hpp" + +//[/Headers] + +//============================================================================== +/** + //[Comments] + An auto-generated component, created by the Introjucer. + + Describe your class and how it works here! + //[/Comments] +*/ +class RnnComponentLayer : public Component + , public Layer + , public DrawListener + , public ButtonListener +{ +public: + //============================================================================== + RnnComponentLayer (AStack &stack, const std::string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden, size_t numContext); + ~RnnComponentLayer(); + + //============================================================================== + //[UserMethods] -- You can add your own custom methods in this section. + //[/UserMethods] + + void paint (Graphics& g); + void resized() override; + void mouseMove (const MouseEvent& e) override; + void mouseEnter (const MouseEvent& e) override; + void mouseExit (const MouseEvent& e) override; + void mouseDown (const MouseEvent& e) override; + void mouseDrag (const MouseEvent& e) override; + void mouseUp (const MouseEvent& e) override; + void mouseDoubleClick (const MouseEvent& e) override; + void mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel) override; + + void setTrainingData(arma::mat const& batch); + + void redrawWeights(); + void redrawWeights(size_t index); + void redrawReconstruction(); + + arma::mat getConvolutedWeight(arma::mat const &h); + ScopedPointer DrawVisibleTrain; + ScopedPointer DrawHidden; + ScopedPointer DrawContextTrain; + arma::mat getTraining() const; + arma::mat getReconst() const; + void trainRedraw(const arma::mat& vc); + void reconstRedraw(const arma::mat& vc); + +private: + //[UserVariables] -- You can add your own custom variables in this section. + ScopedPointer DrawVisibleReconst; + ScopedPointer DrawContextReconst; + ScopedPointer DrawWeights; + ScopedPointer m_toggleEnable; + ScopedPointer m_buttonCopyH2C; + + void onUpPass(const arma::mat& v) override; + void onDownPass(const arma::mat& h) override; + + DeepStack &m_stack; + size_t m_currWeightIndexToDraw; + void onDraw(DrawComponent &obj) override; + void buttonClicked(Button* buttonThatWasClicked) override; + int m_sizeX; + int m_sizeY; + //[/UserVariables] + + //============================================================================== + + //============================================================================== + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RnnComponentLayer) +}; + +//[EndFile] You can add extra defines here... +//[/EndFile] + +#endif // __RNN_COMPONENT_LAYER__