Exemplo n.º 1
0
        private void DrawReceptiveFieldColor(Graphics graphics, int xOrigin, int yOrigin, int j)
        {
            RBMlayer bottomLayer = model.layers[0];

            float bias = bottomLayer.biasHiddenLayer[j];

            for (int h = 0; h < settings.Height; h++)
            {
                for (int w = 0; w < settings.Width; w++)
                {
                    int i = h * settings.Width + w;

                    float weight = bottomLayer.Weight(j, i);

                    int        color = (int)(255.0 * Sigmoid(bias + weight));
                    SolidBrush brush;

                    if (weight < 0)
                    {
                        color = 255 - color;
                        brush = new SolidBrush(Color.FromArgb(0, color, color));
                    }
                    else
                    {
                        brush = new SolidBrush(Color.FromArgb(color, 0, 0));
                    }

                    int y = yOrigin + h * settings.Zoom;
                    int x = xOrigin + w * settings.Zoom;

                    graphics.FillRectangle(brush, x, y, settings.Zoom, settings.Zoom);
                }
            }
        }
Exemplo n.º 2
0
        private void btSaveOutputs_Click(object sender, EventArgs e)
        {
            StreamWriter writer = null;

            int layersToProcess = model.layers.Length;

            if (settings.LayersToProcess < layersToProcess)
            {
                layersToProcess = settings.LayersToProcess;
            }

            try {
                writer = new StreamWriter(string.Format("layer{0}-output.txt", layersToProcess - 1));

                for (int s = 0; s < dstrain.Samples; s++)
                {
                    float[] prevLayerValues = dstrain[s];

                    for (int l = 0; l < layersToProcess; l++)
                    {
                        RBMlayer layer = model.layers[l];

                        float[] layerValues = new float[layer.J];

                        for (int j = 0; j < layer.J; j++)
                        {
                            float sum = layer.biasHiddenLayer[j];
                            for (int i = 0; i < layer.I; i++)
                            {
                                sum += prevLayerValues[i] * layer.Weight(j, i);
                            }
                            layerValues[j] = (Sigmoid(sum) >= 0.5) ? 1.0f : 0.0f;
                        }

                        prevLayerValues = layerValues;
                    }

                    foreach (float v in prevLayerValues)
                    {
                        writer.Write("{0} ", v);
                    }
                    writer.WriteLine();
                }
            } catch (System.Exception exception) {
                throw exception;
            } finally {
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
Exemplo n.º 3
0
        float[] Reconstruction(float[] originalValues)
        {
            float[] prevLayerValues = originalValues;

            int currentLayer = 0;

            foreach (RBMlayer layer in model.layers)
            {
                float[] layerValues = new float[layer.J];

                for (int j = 0; j < layer.J; j++)
                {
                    float sum = layer.biasHiddenLayer[j];
                    for (int i = 0; i < layer.I; i++)
                    {
                        sum += prevLayerValues[i] * layer.Weight(j, i);
                    }
                    layerValues[j] = Binarize(Sigmoid(sum));
                }

                prevLayerValues = layerValues;

                if (++currentLayer >= settings.LayersToProcess)
                {
                    break;
                }
            }

            for (int l = currentLayer - 1; l >= 0; l--)
            {
                RBMlayer layer       = model.layers[l];
                float[]  layerValues = new float[layer.I];

                for (int i = 0; i < layer.I; i++)
                {
                    float sum = layer.biasVisibleLayer[i];
                    for (int j = 0; j < layer.J; j++)
                    {
                        sum += prevLayerValues[j] * layer.Weight(j, i);
                    }
                    layerValues[i] = (float)Sigmoid(sum);
                }

                prevLayerValues = layerValues;
            }

            return(prevLayerValues);
        }
Exemplo n.º 4
0
        private void DrawReceptiveField(Graphics graphics, int xOrigin, int yOrigin, int j)
        {
            RBMlayer bottomLayer = model.layers[0];

            for (int h = 0; h < settings.Height; h++)
            {
                for (int w = 0; w < settings.Width; w++)
                {
                    int i = h * settings.Width + w;

                    int color = (int)(255.0f * (bottomLayer.Weight(j, i) - minWeight) / (maxWeight - minWeight));

                    int y = yOrigin + h * settings.Zoom;
                    int x = xOrigin + w * settings.Zoom;

                    graphics.FillRectangle(new SolidBrush(Color.FromArgb(color, color, color)), x, y, settings.Zoom, settings.Zoom);
                }
            }
        }
Exemplo n.º 5
0
        private void btLoadModel_Click(object sender, EventArgs e)
        {
            try {
                Cursor.Current = Cursors.WaitCursor;
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(DBNmodel));
                XmlReader     xmlReader     = XmlReader.Create(settings.ModelFilename);

                model = (DBNmodel)xmlSerializer.Deserialize(xmlReader);
            } catch {
                model = null;
            } finally {
                Cursor.Current = Cursors.Arrow;
            }

            if (model == null || model.layers.Length == 0)
            {
                MessageBox.Show("Invalid model.");
                return;
            }

            RBMlayer bottomLayer = model.layers[0];

            int visibleUnits = bottomLayer.I;

            minWeight     = maxWeight = bottomLayer.weights[0];
            maxWeightBias = 0.0f;

            for (int j = 0; j < bottomLayer.J; j++)
            {
                float bias = bottomLayer.biasHiddenLayer[j];

                for (int i = 0; i < visibleUnits; i++)
                {
                    float w = bottomLayer.Weight(j, i);

                    if (minWeight > w)
                    {
                        minWeight = w;
                    }
                    else if (maxWeight < w)
                    {
                        maxWeight = w;
                    }

                    if (bias + w > maxWeightBias)
                    {
                        maxWeightBias = bias + w;
                    }
                }
            }

            if (settings.Width == 0 && settings.Height == 0)
            {
                int pixels = (int)Math.Sqrt(visibleUnits);
                if (visibleUnits == pixels * pixels)
                {
                    settings.Width             = settings.Height = pixels;
                    proprieties.SelectedObject = settings;
                }
            }

            dstrain             = new DataSet(model.TrainFilename, Path.GetDirectoryName(settings.ModelFilename));
            imageScroll.Maximum = dstrain.Samples - 1;

            splitContainer.Panel2.Invalidate();
        }
Exemplo n.º 6
0
        private void SaveDatasetOutputs(DataSet ds, string aditionalInfo)
        {
            Cursor.Current = Cursors.WaitCursor;

            int numberLayers = model.layers.Length;

            labelOperation.Text = "Saving " + aditionalInfo + " datasets";
            progress.Maximum    = numberLayers;
            statusBar.Visible   = true;

            StreamWriter writer    = null;
            StreamWriter writerAll = null;

            for (int layersToProcess = 1; layersToProcess <= numberLayers; layersToProcess++)
            {
                try {
                    writer = new StreamWriter(string.Format("{0}-{1}-layer{2}-output.csv", Path.GetFileNameWithoutExtension(settings.ModelFilename), aditionalInfo, layersToProcess));

                    if (layersToProcess == numberLayers)
                    {
                        writerAll = new StreamWriter(string.Format("{0}-{1}-all-output.csv", Path.GetFileNameWithoutExtension(settings.ModelFilename), aditionalInfo));
                    }

                    labelDone.Text = string.Format("{0}/{1}", layersToProcess, numberLayers);
                    progress.Value = layersToProcess;
                    this.Update();

                    for (int s = 0; s < ds.Samples; s++)
                    {
                        float[] prevLayerValues = ds[s];

                        for (int l = 0; l < layersToProcess; l++)
                        {
                            RBMlayer layer = model.layers[l];

                            float[] layerValues = new float[layer.J];

                            for (int j = 0; j < layer.J; j++)
                            {
                                float sum = layer.biasHiddenLayer[j];
                                for (int i = 0; i < layer.I; i++)
                                {
                                    sum += prevLayerValues[i] * layer.Weight(j, i);
                                }
                                layerValues[j] = (float)Sigmoid(sum);
                            }

                            prevLayerValues = layerValues;

                            if (writerAll != null && layersToProcess == numberLayers)
                            {
                                if (l > 0)
                                {
                                    writerAll.Write(',');
                                }
                                writerAll.Write("{0}", prevLayerValues[0]);
                                for (int i = 1; i < prevLayerValues.Length; i++)
                                {
                                    writerAll.Write(",{0}", prevLayerValues[i]);
                                }
                            }
                        }
                        if (writerAll != null)
                        {
                            writerAll.WriteLine();
                        }

                        writer.Write("{0}", prevLayerValues[0]);
                        for (int i = 1; i < prevLayerValues.Length; i++)
                        {
                            writer.Write(",{0}", prevLayerValues[i]);
                        }
                        writer.WriteLine();
                    }
                } catch (System.Exception exception) {
                    throw exception;
                } finally {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                    if (writerAll != null)
                    {
                        writerAll.Close();
                    }
                }
            }

            statusBar.Visible = false;
            Cursor.Current    = Cursors.Arrow;
        }