public void MLP_Shape() { TensorCachingAllocator tca = new TensorCachingAllocator(); var shape = new MultiLayerPerception.Shape { inputSize = 2, outputSize = 3, hiddenSize = 5 }; MultiLayerPerception mlp = new MultiLayerPerception(shape); IWorker worker = WorkerFactory.CreateWorker(mlp.model, WorkerFactory.Device.GPU); Tensor input = tca.Alloc(new TensorShape(1, 1, 1, shape.inputSize)); for (int i = 0; i < shape.inputSize; i++) { input[i] = i; } IWorker ex = worker.Execute(input); ex.FlushSchedule(true); Tensor output = ex.PeekOutput(); for (int i = 0; i < shape.outputSize; i++) { Debug.Log($"output[{i}] = {output[i]}"); } tca.Dispose(); ex.Dispose(); worker.Dispose(); Debug.Assert(true); }
public void TestWeightSections() { var shp = new MultiLayerPerception.Shape { hiddenSize = 2, inputSize = 2, outputSize = 2 }; MultiLayerPerception mlp = new MultiLayerPerception(shp); Debug.Log(string.Join(",", mlp.GetWeightSections())); Debug.Assert(true); }
public void MLP_Calc() { TensorCachingAllocator tca = new TensorCachingAllocator(); var shape = new MultiLayerPerception.Shape { inputSize = 2, outputSize = 3, hiddenSize = 2 }; MultiLayerPerception mlp = new MultiLayerPerception(shape); int layerCnt = 0; foreach (Layer layer in mlp.model.layers) { layerCnt++; for (int iWB = 0; iWB < layer.weights.Length; iWB++) { layer.weights[iWB] = iWB * layerCnt; } if (layer.datasets.Length == 2) { Debug.Log($"" + $"{layer.name} " + $"({layer.weights.Length}: W{layer.datasets[0].length} + B{layer.datasets[1].length}): " + $"<{string.Join(", ", layer.weights)}>"); } } string HiddenLayer = MultiLayerPerception.LayerNames.Hidden; IWorker worker = WorkerFactory.CreateWorker(mlp.model, new string[] { HiddenLayer }, WorkerFactory.Device.GPU); Tensor inTensor = tca.Alloc(new TensorShape(1, 1, 1, shape.inputSize)); for (int i = 0; i < shape.inputSize; i++) { inTensor[i] = i; Debug.Log($"input[{i}] = {inTensor[i]}"); } IWorker ex = worker.Execute(inTensor); ex.FlushSchedule(true); Tensor hTensor = ex.PeekOutput(HiddenLayer); Debug.Assert(hTensor.length == shape.hiddenSize); for (int i = 0; i < hTensor.length; i++) { Debug.Log($"hidden1[{i}] = {hTensor[i]}"); } Tensor output = ex.PeekOutput(); Debug.Assert(output.length == shape.outputSize); for (int i = 0; i < output.length; i++) { Debug.Log($"output[{i}] = {output[i]}"); } for (int iHNode = 0; iHNode < shape.hiddenSize; iHNode++) { string str = ""; float sum = 0; for (int iINode = 0; iINode < shape.inputSize; iINode++) { float w = mlp.GetWeight(HiddenLayer, iINode, iHNode); str += $"{w} * {inTensor[iINode]} + "; sum += w * inTensor[iINode]; } float b = mlp.GetBias(HiddenLayer, iHNode); str += $"{b}"; sum += b; str += $"= {hTensor[iHNode]} ({sum})"; Debug.Assert(Mathf.Approximately(sum, hTensor[iHNode])); Debug.Log(str); } tca.Dispose(); ex.Dispose(); worker.Dispose(); Debug.Assert(true); }