-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added model saving and loading with GSON
- Loading branch information
Showing
11 changed files
with
427 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,10 +1,15 @@ | ||
plugins { | ||
id 'java' | ||
id 'com.github.johnrengelman.shadow' version '8.1.1' | ||
} | ||
|
||
group = 'net.echo' | ||
version = '2.0.0' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation 'com.google.code.gson:gson:2.10.1' | ||
} |
This file contains 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,61 @@ | ||
package net.echo.brain4j.adapters; | ||
|
||
import com.google.gson.*; | ||
import net.echo.brain4j.activation.Activations; | ||
import net.echo.brain4j.layer.Layer; | ||
import net.echo.brain4j.layer.impl.DenseLayer; | ||
import net.echo.brain4j.layer.impl.DropoutLayer; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public class LayerAdapter implements JsonSerializer<Layer>, JsonDeserializer<Layer> { | ||
|
||
@Override | ||
public JsonElement serialize(Layer layer, Type type, JsonSerializationContext context) { | ||
JsonObject object = new JsonObject(); | ||
|
||
object.addProperty("type", layer.getClass().getSimpleName()); | ||
object.addProperty("activation", layer.getActivation().name()); | ||
|
||
if (layer instanceof DenseLayer) { | ||
double[] biases = new double[layer.getNeurons().size()]; | ||
|
||
for (int i = 0; i < biases.length; i++) { | ||
biases[i] = layer.getNeurons().get(i).getBias(); | ||
} | ||
|
||
object.add("biases", context.serialize(biases)); | ||
} else if (layer instanceof DropoutLayer dropoutLayer) { | ||
object.addProperty("rate", dropoutLayer.getDropout()); | ||
} | ||
|
||
return object; | ||
} | ||
|
||
@Override | ||
public Layer deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException { | ||
String layerType = element.getAsJsonObject().get("type").getAsString(); | ||
String activationType = element.getAsJsonObject().get("activation").getAsString(); | ||
|
||
Activations activations = Activations.valueOf(activationType); | ||
|
||
return switch (layerType) { | ||
case "DenseLayer" -> { | ||
double[] biases = context.deserialize(element.getAsJsonObject().get("biases"), double[].class); | ||
|
||
DenseLayer layer = new DenseLayer(biases.length, activations); | ||
|
||
for (int i = 0; i < layer.getNeurons().size(); i++) { | ||
layer.getNeuronAt(i).setBias(biases[i]); | ||
} | ||
|
||
yield layer; | ||
} | ||
case "DropoutLayer" -> { | ||
double dropout = element.getAsJsonObject().get("rate").getAsDouble(); | ||
yield new DropoutLayer(dropout); | ||
} | ||
default -> throw new IllegalArgumentException("Unknown layer type: " + layerType); | ||
}; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/net/echo/brain4j/adapters/OptimizerAdapter.java
This file contains 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,53 @@ | ||
package net.echo.brain4j.adapters; | ||
|
||
import com.google.gson.*; | ||
import net.echo.brain4j.training.optimizers.Optimizer; | ||
import net.echo.brain4j.training.optimizers.impl.Adam; | ||
import net.echo.brain4j.training.optimizers.impl.SGD; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
public class OptimizerAdapter implements JsonSerializer<Optimizer>, JsonDeserializer<Optimizer> { | ||
|
||
@Override | ||
public JsonElement serialize(Optimizer optimizer, Type type, JsonSerializationContext context) { | ||
JsonObject object = new JsonObject(); | ||
|
||
object.addProperty("type", optimizer.getClass().getSimpleName()); | ||
|
||
JsonObject data = new JsonObject(); | ||
|
||
data.addProperty("learningRate", optimizer.getLearningRate()); | ||
|
||
if (optimizer instanceof Adam adam) { | ||
data.addProperty("beta1", adam.getBeta1()); | ||
data.addProperty("beta2", adam.getBeta2()); | ||
data.addProperty("epsilon", adam.getEpsilon()); | ||
} | ||
|
||
object.add("data", data); | ||
return object; | ||
} | ||
|
||
@Override | ||
public Optimizer deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException { | ||
JsonObject object = jsonElement.getAsJsonObject(); | ||
String optimizerType = object.get("type").getAsString(); | ||
|
||
JsonObject data = object.get("data").getAsJsonObject(); | ||
double learningRate = data.get("learningRate").getAsDouble(); | ||
|
||
return switch (optimizerType) { | ||
case "SGD" -> new SGD(learningRate); | ||
case "Adam" -> { | ||
Adam adam = new Adam(learningRate); | ||
|
||
adam.setBeta1(data.get("beta1").getAsDouble()); | ||
adam.setBeta2(data.get("beta2").getAsDouble()); | ||
adam.setEpsilon(data.get("epsilon").getAsDouble()); | ||
yield adam; | ||
} | ||
default -> null; | ||
}; | ||
} | ||
} |
This file contains 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 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 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 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 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.