use of ml.shifu.shifu.core.dtrain.nn.ActivationSwish in project shifu by ShifuML.
the class ActivationSwishTest method testDerive.
@Test
public void testDerive() {
ActivationSwish swish = new ActivationSwish();
// Test activtion function
double[] swishInputValue = { -1d, 0, 1d };
double[] truth = { -1 / (1 + Math.exp(1)), 0, 1 / (1 + Math.exp(-1)) };
double error = 0;
swish.activationFunction(swishInputValue, 0, 3);
for (int i = 0; i < 3; i++) {
error += Math.abs(truth[i] - swishInputValue[i]);
}
Assert.assertTrue(error < 1E-6);
// Test derivative function
double d0 = swish.derivativeFunction(0.0, 0.0);
Assert.assertTrue(Math.abs(d0 - 0.5) < 1E-6);
double d1 = swish.derivativeFunction(1, 0.0);
double d11 = (1 + 2 * Math.exp(-1)) / Math.pow(1 + Math.exp(-1), 2);
Assert.assertTrue(Math.abs(d1 - d11) < 1E-6);
double dn1 = swish.derivativeFunction(-1, 0.0);
double dn11 = 1 / Math.pow(1 + Math.exp(1), 2);
Assert.assertTrue(Math.abs(dn1 - dn11) < 1E-6);
}
use of ml.shifu.shifu.core.dtrain.nn.ActivationSwish in project shifu by ShifuML.
the class PersistBasicFloatNetwork method readNetwork.
public BasicFloatNetwork readNetwork(final DataInput in) throws IOException {
final BasicFloatNetwork result = new BasicFloatNetwork();
final FlatNetwork flat = new FlatNetwork();
// read properties
Map<String, String> properties = new HashMap<String, String>();
int size = in.readInt();
for (int i = 0; i < size; i++) {
properties.put(ml.shifu.shifu.core.dtrain.StringUtils.readString(in), ml.shifu.shifu.core.dtrain.StringUtils.readString(in));
}
result.getProperties().putAll(properties);
// read fields
flat.setBeginTraining(in.readInt());
flat.setConnectionLimit(in.readDouble());
flat.setContextTargetOffset(readIntArray(in));
flat.setContextTargetSize(readIntArray(in));
flat.setEndTraining(in.readInt());
flat.setHasContext(in.readBoolean());
flat.setInputCount(in.readInt());
flat.setLayerCounts(readIntArray(in));
flat.setLayerFeedCounts(readIntArray(in));
flat.setLayerContextCount(readIntArray(in));
flat.setLayerIndex(readIntArray(in));
flat.setLayerOutput(readDoubleArray(in));
flat.setOutputCount(in.readInt());
flat.setLayerSums(new double[flat.getLayerOutput().length]);
flat.setWeightIndex(readIntArray(in));
flat.setWeights(readDoubleArray(in));
flat.setBiasActivation(readDoubleArray(in));
// read activations
flat.setActivationFunctions(new ActivationFunction[flat.getLayerCounts().length]);
int acSize = in.readInt();
for (int i = 0; i < acSize; i++) {
String name = ml.shifu.shifu.core.dtrain.StringUtils.readString(in);
if (name.equals("ActivationReLU")) {
name = ActivationReLU.class.getName();
} else if (name.equals("ActivationLeakyReLU")) {
name = ActivationLeakyReLU.class.getName();
} else if (name.equals("ActivationSwish")) {
name = ActivationSwish.class.getName();
} else if (name.equals("ActivationPTANH")) {
name = ActivationPTANH.class.getName();
} else {
name = "org.encog.engine.network.activation." + name;
}
ActivationFunction af = null;
try {
final Class<?> clazz = Class.forName(name);
af = (ActivationFunction) clazz.newInstance();
} catch (final ClassNotFoundException e) {
throw new PersistError(e);
} catch (final InstantiationException e) {
throw new PersistError(e);
} catch (final IllegalAccessException e) {
throw new PersistError(e);
}
double[] params = readDoubleArray(in);
for (int j = 0; j < params.length; j++) {
af.setParam(j, params[j]);
}
flat.getActivationFunctions()[i] = af;
}
// read subset
int subsetSize = in.readInt();
Set<Integer> featureList = new HashSet<Integer>();
for (int i = 0; i < subsetSize; i++) {
featureList.add(in.readInt());
}
result.setFeatureSet(featureList);
result.getStructure().setFlat(flat);
return result;
}
Aggregations