-
-
Notifications
You must be signed in to change notification settings - Fork 196
400 refactor network class to interface #402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c80d278
ML-396 Converted MLPRegressor to work with NumPower/NDArray related c…
apphp 13acae6
ML-396 removed unneeded export function
apphp 3b65a47
ML-396 added test for NumPower
apphp d7404f8
ML-396 added USE_NUMPOWER_TRANSPOSE option to Network
apphp d538799
ML-396 added USE_NUMPOWER_TRANSPOSE option to Network
apphp f333c67
ML-396 fixed issue with samples normalization
apphp 1583ee3
ML-396 removed unneeded packages from composer
apphp 57037c6
ML-396 removed unneeded packages from composer
apphp 22df3e0
Merge branch '3.0' into 396-convert-mlp-classifier-to-NumPower
apphp b79ddb6
Merge branch '3.0' into 396-convert-mlp-classifier-to-NumPower
apphp 66ce8ec
ML-400 convert Network to interface
apphp 0db4266
ML-400 added normalizeSamples to FeedForward
apphp 7454f25
ML-400 extended Network interface
apphp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| <?php | ||
|
|
||
| namespace Rubix\ML\Datasets\Generators\SwissRoll; | ||
|
|
||
| use NDArray; | ||
| use NumPower; | ||
| use Rubix\ML\Datasets\Generators\Generator; | ||
| use Rubix\ML\Datasets\Labeled; | ||
| use Rubix\ML\Exceptions\InvalidArgumentException; | ||
|
|
||
| use function cos; | ||
| use function sin; | ||
| use function log; | ||
| use function sqrt; | ||
| use function mt_rand; | ||
|
|
||
| use const Rubix\ML\HALF_PI; | ||
|
|
||
| /** | ||
| * Swiss Roll | ||
| * | ||
| * Generate a 3-dimensional swiss roll dataset with continuous valued labels. | ||
| * The labels are the inputs to the swiss roll transformation and are suitable | ||
| * for non-linear regression problems. | ||
| * | ||
| * References: | ||
| * [1] S. Marsland. (2009). Machine Learning: An Algorithmic Perspective, | ||
| * Chapter 10. | ||
| * | ||
| * @category Machine Learning | ||
| * @package Rubix/ML | ||
| * @author Andrew DalPino | ||
| * @author Samuel Akopyan <leumas.a@gmail.com> | ||
| */ | ||
| class SwissRoll implements Generator | ||
| { | ||
| /** | ||
| * The center vector of the swiss roll. | ||
| * | ||
| * @var list<float> | ||
| */ | ||
| protected array $center; | ||
|
|
||
| /** | ||
| * The scaling factor of the swiss roll. | ||
| * | ||
| * @var float | ||
| */ | ||
| protected float $scale; | ||
|
|
||
| /** | ||
| * The depth of the swiss roll i.e the scale of the y dimension. | ||
| * | ||
| * @var float | ||
| */ | ||
| protected float $depth; | ||
|
|
||
| /** | ||
| * The standard deviation of the gaussian noise. | ||
| * | ||
| * @var float | ||
| */ | ||
| protected float $noise; | ||
|
|
||
| /** | ||
| * @param float $x | ||
| * @param float $y | ||
| * @param float $z | ||
| * @param float $scale | ||
| * @param float $depth | ||
| * @param float $noise | ||
| * @throws InvalidArgumentException | ||
| */ | ||
| public function __construct( | ||
| float $x = 0.0, | ||
| float $y = 0.0, | ||
| float $z = 0.0, | ||
| float $scale = 1.0, | ||
| float $depth = 21.0, | ||
| float $noise = 0.1 | ||
| ) { | ||
| if ($scale < 0.0) { | ||
| throw new InvalidArgumentException('Scale must be' | ||
| . " greater than 0, $scale given."); | ||
| } | ||
|
|
||
| if ($depth < 0) { | ||
| throw new InvalidArgumentException('Depth must be' | ||
| . " greater than 0, $depth given."); | ||
| } | ||
|
|
||
| if ($noise < 0.0) { | ||
| throw new InvalidArgumentException('Noise factor cannot be less' | ||
| . " than 0, $noise given."); | ||
| } | ||
|
|
||
| $this->center = [$x, $y, $z]; | ||
| $this->scale = $scale; | ||
| $this->depth = $depth; | ||
| $this->noise = $noise; | ||
| } | ||
|
|
||
| /** | ||
| * Return the dimensionality of the data this generates. | ||
| * | ||
| * @internal | ||
| * | ||
| * @return int<0,max> | ||
| */ | ||
| public function dimensions() : int | ||
| { | ||
| return 3; | ||
| } | ||
|
|
||
| /** | ||
| * Generate n data points. | ||
| * | ||
| * @param int<0,max> $n | ||
| * @return Labeled | ||
| */ | ||
| public function generate(int $n) : Labeled | ||
| { | ||
| $range = M_PI + HALF_PI; | ||
|
|
||
| $t = []; | ||
| $y = []; | ||
| $coords = []; | ||
|
|
||
| for ($i = 0; $i < $n; ++$i) { | ||
| $u = mt_rand() / mt_getrandmax(); | ||
| $ti = (($u * 2.0) + 1.0) * $range; | ||
| $t[] = $ti; | ||
|
|
||
| $uy = mt_rand() / mt_getrandmax(); | ||
| $y[] = $uy * $this->depth; | ||
|
|
||
| $coords[] = [ | ||
| $ti * cos($ti), | ||
| $y[$i], | ||
| $ti * sin($ti), | ||
| ]; | ||
| } | ||
|
|
||
| $noise = []; | ||
|
|
||
| if ($this->noise > 0.0) { | ||
| for ($i = 0; $i < $n; ++$i) { | ||
| $row = []; | ||
|
|
||
| for ($j = 0; $j < 3; ++$j) { | ||
| $u1 = mt_rand() / mt_getrandmax(); | ||
| $u2 = mt_rand() / mt_getrandmax(); | ||
| $u1 = $u1 > 0.0 ? $u1 : 1e-12; | ||
|
|
||
| $z0 = sqrt(-2.0 * log($u1)) * cos(2.0 * M_PI * $u2); | ||
|
|
||
| $row[] = $z0 * $this->noise; | ||
| } | ||
|
|
||
| $noise[] = $row; | ||
| } | ||
| } else { | ||
| for ($i = 0; $i < $n; ++$i) { | ||
| $noise[] = [0.0, 0.0, 0.0]; | ||
| } | ||
| } | ||
|
|
||
| $center = []; | ||
|
|
||
| for ($i = 0; $i < $n; ++$i) { | ||
| $center[] = $this->center; | ||
| } | ||
|
|
||
| $coords = NumPower::array($coords); | ||
| $noise = NumPower::array($noise); | ||
| $center = NumPower::array($center); | ||
|
|
||
| $samples = NumPower::add( | ||
| NumPower::add( | ||
| NumPower::multiply($coords, $this->scale), | ||
| $center | ||
| ), | ||
| $noise | ||
| ); | ||
|
|
||
| return Labeled::quick($samples->toArray(), $t); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changelog entry "Turn back Network interface" is grammatically unclear. Consider rephrasing to something like "Restore Network interface" / "Bring back Network interface" so readers understand what changed.